概念:多元回归分析预测法,是指通过对两个或两个以上的自变量与一个因变量的相关分析,建立预测模型进行预测的方法。当自变量与因变量之间存在线性关系时,称为多元线性回归分析。
下面我就举几个例子来说明一下
例一:谋杀率与哪些因素有关
变量选择
states<-as.data.frame(state.x77[,c('Murder','Population','Illiteracy','Income','Frost')])
cor(states)#查看变量相关系数
Murder Population Illiteracy Income Frost
Murder 1.0000000 0.3436428 0.7029752 -0.2300776 -0.5388834
Population 0.3436428 1.0000000 0.1076224 0.2082276 -0.3321525
Illiteracy 0.7029752 0.1076224 1.0000000 -0.4370752 -0.6719470
Income -0.2300776 0.2082276 -0.4370752 1.0000000 0.2262822
Frost -0.5388834 -0.3321525 -0.6719470 0.2262822 1.0000000
我们可以明显的看出谋杀率与人口,文盲率相关性较大
将它们的关系可视化
library(car)
scatterplotMatrix(states,spread=FALSE)
还可以这么看
fit<-lm(Murder~Population+Illiteracy+Income+Frost,data = states)
summary(fit)
Call:
lm(formula = Murder ~ Population + Illiteracy + Income + Frost,
data = states)
Residuals:
Min 1Q Median 3Q Max
-4.7960 -1.6495 -0.0811 1.4815 7.6210
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.235e+00 3.866e+00 0.319 0.7510
Population 2.237e-04 9.052e-05 2.471 0.0173 *
Illiteracy 4.143e+00 8.744e-01 4.738 2.19e-05 ***
Income 6.442e-05 6.837e-04 0.094 0.9253
Frost 5.813e-04 1.005e-02 0.058 0.9541
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.535 on 45 degrees of freedom
Multiple R-squared: 0.567, Adjusted R-squared: 0.5285
F-statistic: 14.73 on 4 and 45 DF, p-value: 9.133e-08
还可以这么看
#install.packages('leaps')
library(leaps)
leaps<-regsubsets(Murder~Population+Illiteracy+Income+Frost,data = states,nbest = 4)
plot(leaps,scale = 'adjr2')
最大值0.55是只包含人口,文盲率这两个变量和截距的。
还可以这样,比较标准回归系数的大小
zstates<-as.data.frame(scale(states))#scale()标准化
zfit<-lm(Murder~Population+Illiteracy+Income+Frost,data = zstates)
coef(zfit)
(Intercept) Population Illiteracy Income Frost
-2.054026e-16 2.705095e-01 6.840496e-01 1.072372e-02 8.185407e-03
通过这几种方法,我们都可以明显的看出谋杀率与人口,文盲率相关性较大,与其它因素相关性较小。
回归诊断
> confint(fit)
2.5 % 97.5 %
(Intercept) -6.552191e+00 9.0213182149
Population 4.136397e-05 0.0004059867
Illiteracy 2.381799e+00 5.9038743192
Income -1.312611e-03 0.0014414600
Frost -1.966781e-02 0.0208304170
标记异常值
qqPlot(fit,labels = row.names(states),id.method = 'identify',simulate = T)
图如下,点一下异常值然后点finish就可以了
查看它的实际值11.5与拟合值3.878958,这条数据显然是异常的,可以抛弃
> states['Nevada',]
Murder Population Illiteracy Income Frost
Nevada 11.5 590 0.5 5149 188
> fitted(fit)['Nevada']
Nevada
3.878958
> outlierTest(fit)#或直接这么检测离群点
rstudent unadjusted p-value Bonferonni p
Nevada 3.542929 0.00095088 0.047544
car包有多个函数,可以判断误差的独立性,线性,同方差性
library(car)
durbinWatsonTest(fit)
crPlots(fit)
ncvTest(fit)
spreadLevelPlot(fit)
综合检验#install.packages('gvlma')
library(gvlma)
gvmodel<-gvlma(fit);summary(gvmodel)
检验多重共线性
根号下vif>2则表明有多重共线性
> sqrt(vif(fit))
Population Illiteracy Income Frost
1.115922 1.471682 1.160096 1.443103
都小于2所以不存在多重共线性
例二:女性身高与体重的关系
attach(women)
plot(height,weight)
通过图我们可以发现,用曲线拟合要比直线效果更好
那就试试呗
fit<-lm(weight~height+I(height^2))#含平方项
summary(fit)
Call:
lm(formula = weight ~ height + I(height^2))
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
效果是很不错的,可以得出模型为
把拟合曲线加上看看
lines(height,fitted(fit))
还可以用car包的scatterplot()函数
library(car)
scatterplot(weight~height,spread=FALSE,pch=19)#19实心圆,spread=FALSE删除了残差正负均方根在平滑曲线上
展开的非对称信息,听着就不像人话,你可以改成TRUE看看到底是什么,我反正不明白。
例三:含交互项
attach(mtcars)
fit<-lm(mpg~hp+wt+hp:wt)
summary(fit)
Call:
lm(formula = mpg ~ hp + wt + hp:wt)
Residuals:
Min 1Q Median 3Q Max
-3.0632 -1.6491 -0.7362 1.4211 4.5513
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 49.80842 3.60516 13.816 5.01e-14 ***
hp -0.12010 0.02470 -4.863 4.04e-05 ***
wt -8.21662 1.26971 -6.471 5.20e-07 ***
hp:wt 0.02785 0.00742 3.753 0.000811 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.153 on 28 degrees of freedom
Multiple R-squared: 0.8848, Adjusted R-squared: 0.8724
F-statistic: 71.66 on 3 and 28 DF, p-value: 2.981e-13
其中的hp:wt就是交互项,表示我们假设hp马力与wt重量有相关关系,通过全部的三个星可以看出响应/因变量mpg(每加仑英里)与预测/自变量都相关,也就是说mpg(每加仑英里)与汽车马力/重量都相关,且mpg与马力的关系会根据车重的不同而不同。