# R语言回归分析之影响分析

## 1. 回归分析
21个儿童测试值，x为月份，y为智力
intellect<-data.frame(
x=c(15, 26, 10,  9, 15, 20, 18, 11,  8, 20, 7,
9, 10, 11, 11, 10, 12, 42, 17, 11, 10),
y=c(95, 71, 83,  91, 102,  87, 93, 100, 104, 94, 113,
96, 83, 84, 102, 100, 105, 57, 121,  86, 100)
)
lm.sol<-lm(y~1+x, data=intellect)
summary(lm.sol)
Call:
lm(formula = y ~ 1 + x, data = intellect)

Residuals:
Min      1Q  Median      3Q     Max
-15.604  -8.731   1.396   4.523  30.285

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 109.8738     5.0678  21.681 7.31e-15 ***
x            -1.1270     0.3102  -3.633  0.00177 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 11.02 on 19 degrees of freedom
Multiple R-squared:   0.41, Adjusted R-squared:  0.3789
F-statistic:  13.2 on 1 and 19 DF,  p-value: 0.001769

#回归诊断，调用influence.measures()并做回归诊断图
influence.measures(lm.sol)
Influence measures of
lm(formula = y ~ 1 + x, data = intellect) :

dfb.1_    dfb.x    dffit cov.r   cook.d    hat inf
1   0.01664  0.00328  0.04127 1.166 8.97e-04 0.0479
2   0.18862 -0.33480 -0.40252 1.197 8.15e-02 0.1545
3  -0.33098  0.19239 -0.39114 0.936 7.17e-02 0.0628
4  -0.20004  0.12788 -0.22433 1.115 2.56e-02 0.0705
5   0.07532  0.01487  0.18686 1.085 1.77e-02 0.0479
6   0.00113 -0.00503 -0.00857 1.201 3.88e-05 0.0726
7   0.00447  0.03266  0.07722 1.170 3.13e-03 0.0580
8   0.04430 -0.02250  0.05630 1.174 1.67e-03 0.0567
9   0.07907 -0.05427  0.08541 1.200 3.83e-03 0.0799
10 -0.02283  0.10141  0.17284 1.152 1.54e-02 0.0726
11  0.31560 -0.22889  0.33200 1.088 5.48e-02 0.0908
12 -0.08422  0.05384 -0.09445 1.183 4.68e-03 0.0705
13 -0.33098  0.19239 -0.39114 0.936 7.17e-02 0.0628
14 -0.24681  0.12536 -0.31367 0.992 4.76e-02 0.0567
15  0.07968 -0.04047  0.10126 1.159 5.36e-03 0.0567
16  0.02791 -0.01622  0.03298 1.187 5.74e-04 0.0628
17  0.13328 -0.05493  0.18717 1.096 1.79e-02 0.0521
18  0.83112 -1.11275 -1.15578 2.959 6.78e-01 0.6516   *
19  0.14348  0.27317  0.85374 0.396 2.23e-01 0.0531   *
20 -0.20761  0.10544 -0.26385 1.043 3.45e-02 0.0567
21  0.02791 -0.01622  0.03298 1.187 5.74e-04 0.0628
influence.measures(lm.sol)
op <- par(mfrow=c(2,2), mar=0.4+c(4,4,1,1),
oma= c(0,0,2,0))
plot(lm.sol, 1:4)
par(op)

influence.measures(lm.sol)函数得到的回归诊断共有7列，

inf表明18，19号是强影响点。

n<-length(intellect\$x)
weights<-rep(1, n); weights[18]<-0.5
lm.correct<-lm(y~1+x, data=intellect, subset=-19,
weights=weights)
summary(lm.correct)
Call:
lm(formula = y ~ 1 + x, data = intellect, subset = -19, weights = weights)

Weighted Residuals:
Min      1Q  Median      3Q     Max
-14.300  -7.539   2.700   5.183  12.229

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 108.8716     4.4290   24.58 2.67e-15 ***
x            -1.1572     0.2937   -3.94 0.000959 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 8.617 on 18 degrees of freedom
Multiple R-squared:  0.4631,    Adjusted R-squared:  0.4333
F-statistic: 15.53 on 1 and 18 DF,  p-value: 0.0009594

attach(intellect)
par(mai=c(0.8, 0.8, 0.2, 0.2))
plot(x, y, cex=1.2, pch=21, col="red", bg="orange")
abline(lm.sol, col="blue", lwd=2)
text(x[c(19, 18)], y[c(19, 18)],
detach()
abline(lm.correct, col="red", lwd=2, lty=5)
legend(30, 120, c("Points", "Regression", "Correct Reg"),
pch=c(19, NA, NA), lty=c(NA, 1,5),
col=c("orange", "blue", "red"))

op <- par(mfrow=c(2,2), mar=0.4+c(4,4,1,1), oma= c(0,0,2,0))
plot(lm.correct, 1:4)
par(op)

#### 手把手教线性回归分析（附R语言实例）

2018-03-09 00:00:00

#### 【回归分析】[6]--残差分析

2016-10-30 17:34:56

#### R语言-回归分析及实现

2016-06-29 00:18:53

#### R语言之回归分析篇

2014-01-28 14:06:52

#### R语言回归分析中的异常值点的介绍

2016-02-23 14:30:25

#### R语言简单回归分析总结

2016-09-28 22:40:13

#### 用R语言进行回归分析

2016-07-28 08:54:11

#### R语言回归篇

2014-06-09 21:48:32

#### R语言回归分析实例

2017-11-08 09:33:50

#### R 语言与简单的回归分析

2012-11-10 12:12:10