(1)初次回归
fit = lm(Murder~Population+Illiteracy+Income+Frost,data=states)
summary(fit)
(2)逐步回归
step(fit) 逐步回归
查共线
(3)重新拟合
fit = lm(Murder~Population+Illiteracy,data=states)
(4)残差分析
r_fit = residuals(fit)
rs_fit = rstandard(fit)
fit_=fitted(fit)
(4.1)残差直方图与正态曲线差异
hist(r_fit,freq=F, ylim=c(0.00,0.20))
x=seq(-8,8,by=.5)
lines(x,dnorm(x,mean(r_fit),sd(r_fit)))
(4.2)模型是否合理,同方差,离群点
par(mfrow=c(2,2))
plot(fit)
(5)预测
pre = data.frame(Population=c(2110,2541,2348,2999),Illiteracy=c(1.9,0.7,3.3,0.2))
predict(fit,pre,interval="prediction",level=0.95)
其中states是R自带的数据,请大家拍砖提出修改意见,谢谢,形成一个处理该类问题的一个一般化的步骤方法。