回归分析就是利用样本,产生拟合方程,从而进行预测。简而言之,就是你用你手头上的数据进行模型的训练,然后用你得到的模型对于新数据进行预测。
一元线性回归:
例子:
y<- c(61,57,58,40,90,35,68)#weight
x<-c(170,168,175,153,185,135,172) #height
plot(x,y)
z<- lm(y~x+1)#假设y=ax+b
lines(x,fitted(z))#添加拟合值对x的散点图并连线
coef(z)#求模型系数
formula(z)#提取模型公式
这是模型的概括
Call:
lm(formula = y ~ x + 1)
Residuals:
1 2 3 4 5 6 7
-2.003 -4.002 -10.006 -5.992 11.988 7.019 2.996
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -107.1018 34.8642 -3.072 0.02772 *
x 1.0006 0.2099 4.768 0.00502 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 8.462 on 5 degrees of freedom
Multiple R-squared: 0.8197, Adjusted R-squared: 0.7837
F-statistic: 22.73 on 1 and 5 DF, p-value: 0.005024
解读上述:
Residuals:
1 2 3 4 5 6 7
-2.003 -4.002 -10.006 -5.992 11.988 7.019 2.996
这是表示残差
1 和 -2.003表示在第一个样本点的时候,残差为-2.003,也就是y1-y1’为-2.003
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -107.1018 34.8642 -3.072 0.02772 *
x 1.0006 0.2099 4.768 0.00502 **
第一列代表-107.1018代表的是截距,1.0006代表斜率
第二列:推算的系数的标