OLS模型:最小二乘法模型
8.1 回归的多面性
略
8.2 OLS回归
8.1简单线性回归
#8.2.2简单线性回归
dt<-women
fit<-lm(weight~height,data=dt)#先y后x
summary(fit)
plot(dt$height,dt$weight)#先x后y
abline(fit)#回归线
8.2多项式回归
#8.2.3多项式回归
##y可能不止和x相关呢?还可能和x^2相关呀
fit2<-lm(weight~height+I(height^2),data=dt)
#先y后x;I(height^2)之中的I()是为了防止出现将^识别错误
summary(fit2)
plot(dt$height,dt$weight)#先x后y
lines(dt$height,fitted(fit2))#预测值:fitted(fit2)
8.3多元回归
#8.2.4多元线性回归
states<-as.data.frame(state.x77[,c('Murder','Population','Illiteracy','Income','Frost')])
#计算相关系数
cor(states)
install.packages("car")
library(car)
scatterplotMatrix(states,spread=FALSE,smoother.args=list(lty=2))#回归前的参考
fit3<-lm(Murder~Populati