《R语言入门经典》第16章R模型和面向对象

这一章看得云里雾里,统计没有学好,感觉啥也不会

补充练习

#用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)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。