Anda di halaman 1dari 6

EJEMPLO MODELO LINEAL CON VARIABLES INDICADORAS

Se tiene un estudio sobre un nuevo suplemento de alimento (X) para tres diferentes
razas de pollos (de seis semanas) para determinar el efecto sobre la dureza (Y) de la
cscara de huevo. Ajuste un modelo de regresin lineal para las tres razas de pollo.

Dureza (Y) Raza Suplem (X)


8,42 1 1
14,68 1 3
21,42 1 5
25,45 1 6
27,14 1 7
30,53 1 8
34,51 1 9
34,52 1 9
33,24 1 10
39,63 1 11
43,98 1 12
47,77 1 14
9,86 2 3
9,54 2 3
11,96 2 4
12,46 2 5
11,38 2 6
14,69 2 8
16,48 2 9
20,11 2 12
6,52 3 2
5,11 3 5
7,75 3 7
6,84 3 8
7,65 3 10
9,49 3 15
7,03 3 16
9,41 3 18
12,01 3 20

Considerando variables indicadoras para la variable cualitativa de la forma siguiente:


1 raza 1 1 raza 2
X1 X2
0 otro caso 0 otro caso

a) Ajuste un modelo de regresin con pendientes e interceptos diferentes.


(Modelo con interaccin: Yi 0 1 X i 2 X 1i 3 X 2i 4 X i X 1i 5 X i X 2 i i )
b) Ajuste un modelo de regresin con interceptos diferentes, pero pendientes iguales.
(Modelo sin interaccin: Yi 0 1 X i 2 X 1i 3 X 2 i i )
c) Ajuste un modelo de regresin con intercepto y pendientes iguales. (Modelo recta
comn: Yi 0 1 X i i

##Leyendo los datos


dat<-read.table("d:dat.txt",h=T)
y<-dat[,1]
x<-dat[,3]
r<-dat[,2]

##Sentencias para realizar el grfico


plot(x, y , pch = c(21,3,24)[r])
legend("topleft", legend = c("Raza 1", "Raza 2","Raza
3"),pch = c(21,3,24))

##creando las variables dummy


x1<-
c(1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
)
x2<-
c(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0
)

Dureza (Y) Suplemento (X) X1 X2 Raza


8,42 1 1 0 1
14,68 3 1 0 1
21,42 5 1 0 1
25,45 6 1 0 1
27,14 7 1 0 1
30,53 8 1 0 1
34,51 9 1 0 1
34,52 9 1 0 1
33,24 10 1 0 1
39,63 11 1 0 1
43,98 12 1 0 1
47,77 14 1 0 1
9,86 3 0 1 2
9,54 3 0 1 2
11,96 4 0 1 2
12,46 5 0 1 2
11,38 6 0 1 2
14,69 8 0 1 2
16,48 9 0 1 2
20,11 12 0 1 2
6,52 2 0 0 3
5,11 5 0 0 3
7,75 7 0 0 3
6,84 8 0 0 3
7,65 10 0 0 3
9,49 15 0 0 3
7,03 16 0 0 3
9,41 18 0 0 3
12,01 20 0 0 3

##Estimando el modelo con pendientes e interceptos


diferentes utilizando matrices:
l<-rep(1,29)
X<-cbind(l,x,x1,x2,x*x1,x*x2)
b<-solve((t(X)%*%X))%*%(t(X)%*%y)
SCRComp<-t(b)%*%(t(X)%*%y)
SCRComp
[,1]
[1,] 14415.79

SCT=t(y)%*%y
SCT
[,1]
[1,] 14449.89

b
[,1]
l 5.0225372
x 0.2634373
x1 0.9436435
x2 1.4448054
2.7859925
0.8313879

sig2<-(SCT-SCRComp)/(29-6)
sig2
[,1]
[1,] 1.482512

##Estimando el modelo con pendientes e interceptos


diferentes utilizando lm
modelcompleto<-lm(y~x+x1+x2+x*x1+x*x2)
summary(modelcompleto)
Call:
lm(formula = y ~ x + x1 + x2 + x * x1 + x * x2)
Residuals:
Min 1Q Median 3Q Max
-3.2205 -0.4345 0.1201 0.8834 1.7187

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.02254 0.87188 5.761 7.22e-06 ***
x 0.26344 0.06876 3.831 0.000855 ***
x1 0.94364 1.21798 0.775 0.446374
x2 1.44481 1.32492 1.090 0.286794
x:x1 2.78599 0.11957 23.299 < 2e-16 ***
x:x2 0.83139 0.15957 5.210 2.77e-05 ***
---
Residual standard error: 1.218 on 23 degrees of freedom
Multiple R-squared: 0.9923, Adjusted R-squared: 0.9906
F-statistic: 590.4 on 5 and 23 DF, p-value: < 2.2e-16

anova(modelcompleto)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x 1 230.86 230.86 155.721 1.008e-11 ***
x1 1 2898.07 2898.07 1954.836 < 2.2e-16 ***
x2 1 442.02 442.02 298.157 1.158e-14 ***
x:x1 1 765.06 765.06 516.055 < 2.2e-16 ***
x:x2 1 40.24 40.24 27.146 2.774e-05 ***
Residuals 23 34.10 1.48

##Estimando el modelo con pendiente comn, pero diferentes


interceptos, utilizando matrices:

Xp<-X[,c(1,2,3,4)]
b<-(solve(t(Xp)%*%Xp))%*%(t(Xp)%*%y)
SCRp<-t(b)%*%(t(Xp)%*%y)
b
[,1]
l -5.182747
x 1.172819
x1 26.005430
x2 11.162628

sig2p<-(SCT-SCRp)/(29-4)
sig2p
[,1]
[1,] 33.57602
##Estimando el modelo con pendiente comn, pero diferentes
interceptos utilizando lm:

model<-lm(y~x+x1+x2)
summary(model)
Call:
lm(formula = y ~ x + x1 + x2)
Residuals:
Min 1Q Median 3Q Max
-13.5755 -2.4096 0.3248 3.1319 10.5279

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -5.1827 3.4002 -1.524 0.14000
x 1.1728 0.2494 4.703 8.04e-05 ***
x1 26.0054 2.6848 9.686 6.08e-10 ***
x2 11.1626 3.0765 3.628 0.00128 **
---
Residual standard error: 5.794 on 25 degrees of freedom
Multiple R-squared: 0.8097, Adjusted R-squared: 0.7868
F-statistic: 35.45 on 3 and 25 DF, p-value: 3.674e-09

anova(model)
Analysis of Variance Table

Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x 1 230.86 230.86 6.8757 0.014664 *
x1 1 2898.07 2898.07 86.3137 1.386e-09 ***
x2 1 442.02 442.02 13.1648 0.001278 **
Residuals 25 839.40 33.58

##Estimando el modelo con pendiente e intercepto comn,


utilizando matrices:
Xi<-X[,c(1,2)]
b<-(solve(t(Xi)%*%Xi))%*%(t(Xi)%*%y)
SCRi<-t(b)%*%(t(Xi)%*%y)
b
[,1]
l 13.5556507
x 0.5953908
SCRi<-t(b)%*%(t(Xi)%*%y)
sig2i<-(SCT-SCRi)/(29-2)
sig2i
[,1]
[1,] 154.7959

##Estimando el modelo con pendiente e intercepto comn,


utilizando matrices:

modelunarecta<-lm(y~x)
summary(modelunarecta)
Residuals:
Min 1Q Median 3Q Max
-16.052 -9.973 -3.977 9.417 25.879

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 13.5557 4.7373 2.862 0.00805 **
x 0.5954 0.4875 1.221 0.23256
---
Residual standard error: 12.44 on 27 degrees of freedom
Multiple R-squared: 0.05234, Adjusted R-squared: 0.01725
F-statistic: 1.491 on 1 and 27 DF, p-value: 0.2326

Anda mungkin juga menyukai