这一章看得云里雾里,统计没有学好,感觉啥也不会
补充练习
#用airquality数据库,拟合一个Ozone和Wind的线性模型
model1 <- lm(Ozone~Wind, data = airquality)
#创建详细的文本和诊断图
summary(model1)
par(mfrow = c(2,2))
plot(model1)
#双变量
model2 <- update(model1,. ~ . +Temp)
anova(model2)
#添加交互项并评估模型
model3 <- update(model2,. ~ . +Wind:Temp)
summary(model13)
res1 <- resid(model1)
fit1 <- fitted(model1)
res2 <- resid(model2)
fit2 <- fitted(model2)
res3 <- resid(model3)
fit3 <- fitted(model3)
resRange <- c(-1,1)*max((max(abs(res1),abs(res2))),abs(res3))
fitRange <- range((range(fit1,fit2)),fit3)
plot(fit1,res1, xlim = fitRange, ylim = resRange,col = "red", pch = 16,)
lines(loess.smooth(fit3,res3),col = "black")
points(fit2, res2,col = "blue", pch = 16)
points(fit3, res3,col = "black", pch = 16)
lines(loess.smooth(fit1,res1),col = "red")
lines(loess.smooth(fit2,res2),col = "blue")
lines(loess.smooth(fit3,res3),col = "black")
refFun <- function(res,col) abline(h = quantile(res,c(.05,.95),col = col ,lty =3))
refFun(res3,"black")
refFun(res2,"blue")
refFun(res1,"red")
anova(model1,model2,model3)
#在模型中添加Month作为分类自变量
model4 <- update(model3,. ~ . +factor(Month))
summary(model13)