简单线性回归: 当回归模型中包含一个因变量和自变量时,dom
多项式回归:当只有一个与预测变量,但同时包含变量的幂(如,X^2,X^3)函数
多元线性回归:当有不止一个预测变量时spa
例code
#欲利用身高的预测体重,如图1 > par(ask=TRUE) > opar <- par(no.readonly = TRUE) #复制一份当前的图形参数设置 > fit <- lm(weight ~ height, data = women) > summary(fit) #单词为概要、摘要的意思, 拟合模型的详细结果 Call: lm(formula = weight ~ height, data = women) Residuals: Min 1Q Median 3Q Max -1.7333 -1.1333 -0.3833 0.7417 3.1167 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -87.51667 5.93694 -14.74 1.71e-09 *** #截距(Intercept) height 3.45000 0.09114 37.85 1.09e-14 *** #回归系数 为3.45 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.525 on 13 degrees of freedom Multiple R-squared: 0.991, Adjusted R-squared: 0.9903 #Multiple R-squared:断定系数 ,Adjusted R-squared:调整断定系数 F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14 > women$weight [1] 115 117 120 123 126 129 132 135 139 142 146 150 154 159 164 > fitted(fit) #列出拟合模型的预测值 1 2 3 4 5 6 7 8 9 10 112.5833 116.0333 119.4833 122.9333 126.3833 129.8333 133.2833 136.7333 140.1833 143.6333 11 12 13 14 15 147.0833 150.5333 153.9833 157.4333 160.8833 > residuals(fit) #列出拟合模型的残差值 1 2 3 4 5 6 7 2.41666667 0.96666667 0.51666667 0.06666667 -0.38333333 -0.83333333 -1.28333333 8 9 10 11 12 13 14 -1.73333333 -1.18333333 -1.63333333 -1.08333333 -0.53333333 0.01666667 1.56666667 15 3.11666667 > plot(women$height,women$weight, #画出散点图 + xlab = "heiht", + ylab = "weight") Hit <Return> to see next plot: abline(fit) > abline(fit) #画出参考线
图1orm
经过输出结果,能够获得预测等式:ip
Weight = -87.52 + 3.45*Heightci
由于身高不可能为0,因此不必给截距项一个物理解释,它仅仅是一个常量调整项,it
在Pr(>| t |)栏,能够看到回归系数(3.45)显著不为0(p<0.0.1),代表身高每增高1英寸,体重将预期增长3.45磅。form
R平方项(0.991)代表模型能够解释体重99.1%的方差,它也是实际和预测值之间相关系数的平方。变量
残差标准误(1.53lbs)则可认为是模型用身高预测提供的平均偏差
F 统计量检验全部的预测变量与此相应变量是否都在某各概率水平之上,因为是简单回只有一个预测变量,此处F检验等同于身高回归系数的 t 检验
由图1,能够看到能够用一个弯曲的曲线来提升预测的精度,拟合一个多项式回归试试
#接着上述的例子,添加一个二项式(X^2)来提升回归的预测精度,如图2 > fit2 <- lm(weight ~ height + I(height^2),data=women) #I(height^2)表示身高的平方项 > summary(fit2) Call: lm(formula = weight ~ height + I(height^2), data = women) Residuals: Min 1Q Median 3Q Max -0.50941 -0.29611 -0.00941 0.28615 0.59706 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 261 .87818 25.19677 10.393 2.36e-07 *** height -7.34832 0.77769 -9.449 6.58e-07 *** I(height^2) 0.08306 0.00598 13.891 9.32e-09 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.3841 on 12 degrees of freedom Multiple R-squared: 0.9995, Adjusted R-squared: 0.9994 F-statistic: 1.139e+04 on 2 and 12 DF, p-value: < 2.2e-16 > plot(women$height,women$weight, #画图的时候注意加上women$ + xlab = "height", + ylab = "weight") > lines(women$height,fitted(fit2)) #注意:经过lines(x,y)添加参考线 x表示身高,fitted(fit)回归的预测值
图2
新的预测等式为:
Weight = 261.88- 7.35*Height + 0.083*Height^2
在 p < 0.001水平下,回归系数都很是的显著,模型的方差解释率已经增长到99.9%,二次项的显著性(t = 13.89,p <0.001)代表包含了二次项提升了模型的拟合度
多项式等式仍可认为是线性回归模型,由于等式还是预测变量的加权和形式,即便这样的模型
仍可认为是线性模型(参数项是线性的),能用这样的表达式进行拟合
Y ~log(X1) + sin(X2)
相反,下面的例子才能算是真正的非线性模型:
使用car包中的scatterplot()函数,它能够很容易、方便地绘制二元关系图
#以下图3 >library(car) > scatterplot(weight~height,data = women, + spread= F, #选项删除残差正负均方根在平滑曲线上的展开和非对称信息 + smoother.args =list(lty =2), #选择设置loess拟合曲线为虚线 + pch =19, #符号设置为实心圆 + xlab = "Height(inches)", + ylab = "Weight(bs.)"
图3
功能增强的图形,既提供了身高和体重的散点图、线性拟合曲线和平滑拟合(loess)曲线,还在相应边界展现了每一个变量的箱线图