SLIDE 6 Multiple linear regression in R III
#plot data and two regression lines col=rep("blue",length(Sex)) col[Sex=="Female"]="red" plot(distance~age,col=col) abline(parm[1:2],col="blue") abline(c(parm[1]+parm[3],parm[2]),col="red")
8 9 10 11 12 13 14 20 25 30 age distance
21 / 38
Multiple linear regression in R IV
res=residuals(ort2) hist(res)
Histogram of res res Frequency −6 −4 −2 2 4 6 10 20 30
qqnorm(res) qqline(res)
−2 −1 1 2 −6 −4 −2 2 4 Normal Q−Q Plot Theoretical Quantiles Sample Quantiles
fittedval=fitted(ort plot(res~fittedval)
21 22 23 24 25 26 27 −6 −4 −2 2 4 fittedval res
22 / 38
Multiple linear regression in R V
> library(lattice) > xyplot(res~Subject,groups=Subject)
Subject res
−6 −4 −2 2 4 M16 M05 M02 M11 M07 M08 M03 M12 M13 M14 M09 M15 M06 M04 M01 M10 F10F09F06F01F05F07F02F08F03F04F11
Oups - residuals not independent and identically distributed ! Hence computed F-tests not valid. Problem: subject specific intercepts (and possibly subject specific slopes too)
23 / 38
Model with subject specific intercepts
> ortss=lm(distance~Subject+age+age:factor(Sex)+factor(Sex)) > summary(ortss) Coefficients: (1 not defined because of singularities) Estimate Std. Error t value Pr(>|t|) (Intercept) 16.7611 0.6697 25.028 < 2e-16 *** Subject.L 6.8893 2.9857 2.307 0.02365 * Subject.Q 0.1675 0.9825 0.170 0.86507 Subject.C 2.7670 1.1527 2.400 0.01873 * Subject^4 2.8589 0.9497 3.010 0.00350 ** Subject^5
0.7896
0.74930 Subject^6
0.8988
0.04865 * Subject^7 0.4857 0.6986 0.695 0.48893 Subject^8 2.4339 0.8380 2.904 0.00477 ** ... Subject^20
0.7276
0.07653 . Subject^21 0.3881 0.6934 0.560 0.57725 Subject^22 2.0115 0.7296 2.757 0.00724 ** Subject^23 1.7772 0.7366 2.413 0.01816 * Subject^24
0.7025
0.27306 Subject^25 1.4231 0.7133 1.995 0.04948 * Subject^26
0.7292
0.00498 ** age 0.7844 0.0775 10.121 6.44e-16 *** factor(Sex)Female NA NA NA NA age:factor(Sex)Female
0.1214
0.01410 *
0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1 Residual standard error: 1.386 on 79 degrees of freedom Multiple R-squared: 0.8345,Adjusted R-squared: 0.7759 F-statistic: 14.23 on 28 and 79 DF, p-value: < 2.2e-16 24 / 38