您的位置:首页 > 其它

回归分析绘图,预测,残差分析

2016-07-26 11:48 411 查看

线性回归

lm以及一些很好用的函数

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 ***
height        3.45000    0.09114   37.85 1.09e-14 ***
---
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
F-statistic:  1433 on 1 and 13 DF,  p-value: 1.091e-14
#拟合出的结果
> fitted(fit)
1        2        3        4        5        6
112.5833 116.0333 119.4833 122.9333 126.3833 129.8333
7        8        9       10       11       12
133.2833 136.7333 140.1833 143.6333 147.0833 150.5333
13       14       15
153.9833 157.4333 160.8833
#每一个点的残差值
> residuals(fit)
1           2           3           4           5
2.41666667  0.96666667  0.51666667  0.06666667 -0.38333333
6           7           8           9          10
-0.83333333 -1.28333333 -1.73333333 -1.18333333 -1.63333333
11          12          13          14          15
-1.08333333 -0.53333333  0.01666667  1.56666667  3.11666667
#置信区间97.5%
> confint(fit)
2.5 %     97.5 %
(Intercept) -100.342655 -74.690679
height         3.253112   3.646888
> fit

Call:
lm(formula = weight ~ height, data = women)

Coefficients:
(Intercept)       height
-87.52         3.45


scatterplot()可以很容易方便的绘制二元关系图

> scatterplot(weight~height,data = women,spread = FALSE,lty.smoother = 3,pch = 19,xlab = "Height(inches)",ylab="Weights(lbs.)",main = "Women Age 30-39")




scatterplotMatrix()函数默认在非对角线区域绘制变量间的散点图,并添加平滑(loess)

和线性拟合曲线。对角线区域绘制每个变量的密度图和轴须图。

> states <- as.data.frame(state.x77[,c("Murder","Population","Illiteracy","Income","Frost")])
> ?state.x77
#不同变量之间的相关系数
> cor(states)
Murder Population Illiteracy     Income
Murder      1.0000000  0.3436428  0.7029752 -0.2300776
Population  0.3436428  1.0000000  0.1076224  0.2082276
Illiteracy  0.7029752  0.1076224  1.0000000 -0.4370752
Income     -0.2300776  0.2082276 -0.4370752  1.0000000
Frost      -0.5388834 -0.3321525 -0.6719470  0.2262822
Frost
Murder     -0.5388834
Population -0.3321525
Illiteracy -0.6719470
Income      0.2262822
Frost       1.0000000
> library(car)
> scatterplotMatrix(states,spread = FALSE,lty = 2,main = "Scatter Plot Matrix")




解释:

位置(1,1)图片,绘制了murder谋杀率趋势

位置(1 , 2)图片,横坐标为谋杀率,纵坐标为人口,两条曲线一个为平滑曲线(红色),一个为拟合曲线(绿色)

其余位置类似,图片位置根据states列和行分布
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息