################################################### ### chunk number 1: ################################################### options("width"=70) load("students.RData") sel=students$Length<210 #$ sx = students[sel,] attach(sx) ################################################### ### chunk number 2: ################################################### plot(Weight~Gender) ################################################### ### chunk number 3: ################################################### plot(Weight~I(as.numeric(Gender)-1)) abline(lm(Weight~Gender)) ################################################### ### chunk number 4: ################################################### summary(lm(Weight ~ Gender)) ################################################### ### chunk number 5: ################################################### summary(lm(Weight ~ Length)) ################################################### ### chunk number 6: ################################################### plot(Weight ~ Length) abline(lm(Weight ~ Length)) ################################################### ### chunk number 7: ################################################### plot(Weight~Length, col=Gender) abline(lm(Weight~Length)) ################################################### ### chunk number 8: ################################################### plot(Weight~Length, col=Gender) x = coef(lm(Weight~Length+Gender)) abline(x[1], x[2], col = 'black') abline(x[1]+x[3], x[2], col = 'red') ################################################### ### chunk number 9: ################################################### plot(Weight~Length, col=Gender) x = coef(lm(Weight~Length+Gender)) abline(x[1], x[2], col = 'black') abline(x[1]+x[3], x[2], col = 'red') abline(lm(Weight~Length), lty=2) ################################################### ### chunk number 10: ################################################### summary(lm(Weight ~ Length + Gender)) ################################################### ### chunk number 11: ################################################### summary(lm(Weight~Length*Gender)) ################################################### ### chunk number 12: ################################################### x = rep(1:10,3) y = rep(1:3,each=10) z = 0.5 * x + y + rnorm(30, sd=.5) plot(z~x,col=y) abline(1, 0.5, col=1) abline(2, 0.5, col=2) abline(3, 0.5, col=4) text(8,4.5,"y = 1", col=1) text(8,5.5,"y = 2", col=2) text(8,6.5,"y = 3", col=4) ################################################### ### chunk number 13: ################################################### summary(lm(z~x)) ################################################### ### chunk number 14: ################################################### summary(lm(z~x+y)) ################################################### ### chunk number 15: ################################################### plot(Weight ~ Length) l1 = lm(Weight ~ Length) abline(l1) xr = 145:200 w.pr = predict(l1, data.frame(Length=xr), interval = "confidence") lines(xr, w.pr[,2], col = 'blue') lines(xr, w.pr[,3], col = 'blue') w.pr = predict(l1, data.frame(Length=xr), interval = "prediction") lines(xr, w.pr[,2], col = 'red') lines(xr, w.pr[,3], col = 'red') legend(151,143,c("mean value", "single observation"), lty=c(1,1),col=c('blue', 'red')) title("two types of 95% prediction intervals") ################################################### ### chunk number 16: ################################################### cor(Length, Weight) cor(Weight, Length)