线性回归
一元线性回归
fix(women)
A data.frame: 6 × 2
| height | weight |
---|
| | |
---|
1 | 58 | 115 |
---|
2 | 59 | 117 |
---|
3 | 60 | 120 |
---|
4 | 61 | 123 |
---|
5 | 62 | 126 |
---|
6 | 63 | 129 |
---|
?women
# 查看数据的列明
names(women)
- 'height'
- 'weight'
``` # 查看数据类型 class(women) ``` 'data.frame'
# 线性拟合
fit.lm<-lm(weight~height,data=women)
fit.lm
Call:
lm(formula = weight ~ height, data = women)
Coefficients:
(Intercept) height
-87.52 3.45
summary(fit.lm)
# Residuals—残差统计量、intercept-表示截距、Estimate-包含由普通最小二乘法计算出来的估计回归系数、Std.error-估计的回归系数的标准误差、
# Multiple R-squared-拟合优度越大越好、F-statistic-判断方程的显著性检验
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
names(fit.lm)
- 'coefficients'
- 'residuals'
- 'effects'
- 'rank'
- 'fitted.values'
- 'assign'
- 'qr'
- 'df.residual'
- 'xlevels'
- 'call'
- 'terms'
- 'model'
#回归诊断图的理解
#残差与拟合图,本质上残差服从正态分布与估计值无关的假设,与估计值无关,残差应该在y=0上下随机波动
#QQ图用来检测残差是否服从正态分布
#方差相同,红线应该是水平波动不可以存在上下波动
#检查是否存在特别极端的点cook的内部即可
#使用par()函数在同一窗口中创建多个图,mfrow: 决定了网格的行值和列
par(mfrow=c(2,2))
plot(fit.lm)

#绘制散点图
plot(women$height,women$weight)
#添加拟合直线abline(a,b,h,v)a,b指定线的截距和斜率、h为水平线指定y、v为垂直线指定x
abline(fit.lm)

# 预测
#构造要预测数据,newdata的类型必须是dataframe结构,
#而且必须是与原来的名称相同
newdata <- data.frame(height= seq(50, 60, 0.5))
#predict.lm函数进行预测:predict(object,newdata,interval)object代表的是模型对象、interval代表的是置信区间的类型
#confidence是对均值做区间估计、prediction是对随机变量做区间预测
pred=predict(fit.lm, newdata, interval = "prediction")
pred
A matrix: 21 × 3 of type dbl
| fit | lwr | upr |
---|
1 | 84.98333 | 80.47778 | 89.48888 |
---|
2 | 86.70833 | 82.26669 | 91.14998 |
---|
3 | 88.43333 | 84.05432 | 92.81235 |
---|
4 | 90.15833 | 85.84061 | 94.47606 |
---|
5 | 91.88333 | 87.62550 | 96.14116 |
---|
6 | 93.60833 | 89.40894 | 97.80772 |
---|
7 | 95.33333 | 91.19087 | 99.47580 |
---|
8 | 97.05833 | 92.97122 | 101.14545 |
---|
9 | 98.78333 | 94.74992 | 102.81674 |
---|
10 | 100.50833 | 96.52692 | 104.48975 |
---|
11 | 102.23333 | 98.30213 | 106.16453 |
---|
12 | 103.95833 | 100.07550 | 107.84116 |
---|
13 | 105.68333 | 101.84696 | 109.51971 |
---|
14 | 107.40833 | 103.61642 | 111.20025 |
---|
15 | 109.13333 | 105.38383 | 112.88284 |
---|
16 | 110.85833 | 107.14911 | 114.56756 |
---|
17 | 112.58333 | 108.91219 | 116.25448 |
---|
18 | 114.30833 | 110.67300 | 117.94367 |
---|
19 | 116.03333 | 112.43148 | 119.63519 |
---|
20 | 117.75833 | 114.18755 | 121.32911 |
---|
21 | 119.48333 | 115.94117 | 123.02550 |
---|
多项式回归
#对于多项式拟合存在两种方式
fit2.lm <- lm(weight~height+I(height^2),data=women)
fit3.lm <- lm(weight~poly(height,2,raw=TRUE),data=women)
summary(fit2.lm)
summary(fit3.lm)
#对于训练集的数据进行拟合预测用fitted、对于新的样本用predict
fitted(fit2.lm)
fitted.values(fit2.lm)
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
Call:
lm(formula = weight ~ poly(height, 2, raw = TRUE), 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 ***
poly(height, 2, raw = TRUE)1 -7.34832 0.77769 -9.449 6.58e-07 ***
poly(height, 2, raw = TRUE)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
1
-
115.102941176471
2
-
117.473109243697
3
-
120.009405300582
4
-
122.711829347123
5
-
125.580381383323
6
-
128.615061409179
7
-
131.815869424693
8
-
135.182805429864
9
-
138.715869424693
10
-
142.415061409179
11
-
146.280381383323
12
-
150.311829347124
13
-
154.509405300582
14
-
158.873109243697
15
-
163.402941176471
1
-
115.102941176471
2
-
117.473109243697
3
-
120.009405300582
4
-
122.711829347123
5
-
125.580381383323
6
-
128.615061409179
7
-
131.815869424693
8
-
135.182805429864
9
-
138.715869424693
10
-
142.415061409179
11
-
146.280381383323
12
-
150.311829347124
13
-
154.509405300582
14
-
158.873109243697
15
-
163.402941176471
coef(fit2.lm)#查看回归系数
(Intercept)
-
261.878183581103
height
-
-7.34831932773043
I(height^2)
-
0.0830639948286958
plot(women$height,women$weight)
#lines与abline的区别是前者做的是一般连线图,其输入的是点向量
#后者输入的是回归模型对象,之可以添加直线绘图,可以达到相同的目的
lines(women$height,fitted(fit2.lm))
#plot(women$height,women$weight)
#fit4.lm <- lm(height~weight+I(weight^2),data=women)
#abline(fit2.lm)
par(mfrow=c(2,2))
plot(fit2.lm) #残差

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wRSClouu-1679993060920)((https://upload-images.jianshu.io/upload_images/28957475-605f2a872101ad04.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)])
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ETLh3NUP-1679993060920)(https://upload-images.jianshu.io/upload_images/28957475-76b29532f9042095.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]
# 尝试通过添加高阶项,比如(x^3)来提高回归的预测精度。
fit22.lm<-lm(weight~poly(height,3,raw=T),data=women) #多项式的方法,包含所有指定高阶项以下的项,而不仅仅只是给出指定的阶数
summary(fit22.lm)
par(mfrow=c(2,2))
plot(fit22.lm) #残差
plot(women$height,women$weight)
lines(women$height,fitted(fit22.lm))
Call:
lm(formula = weight ~ poly(height, 3, raw = T), data = women)
Residuals:
Min 1Q Median 3Q Max
-0.40677 -0.17391 0.03091 0.12051 0.42191
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -8.967e+02 2.946e+02 -3.044 0.01116 *
poly(height, 3, raw = T)1 4.641e+01 1.366e+01 3.399 0.00594 **
poly(height, 3, raw = T)2 -7.462e-01 2.105e-01 -3.544 0.00460 **
poly(height, 3, raw = T)3 4.253e-03 1.079e-03 3.940 0.00231 **
-------------------------------------------------------------------
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.2583 on 11 degrees of freedom
Multiple R-squared: 0.9998, Adjusted R-squared: 0.9997
F-statistic: 1.679e+04 on 3 and 11 DF, p-value: < 2.2e-16
](https://upload-images.jianshu.io/upload_images/28957475-d875e23e671762ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240))

多元线性回归:高阶的、交互项的
library(MASS)
#data()#因为前面有加载报的操作,所以后边data()中什么命令也没有,本质上是data(,package="MASS")
#看Boston数据集
data(Boston)
head(Boston)
tail(Boston,3)
# fix(Boston)#在R中修改数据
names(Boston) #住宅结构与居住环境信息
?Boston
A data.frame: 6 × 14
| crim | zn | indus | chas | nox | rm | age | dis | rad | tax | ptratio | black | lstat | medv |
---|
| | | | | | | | | | | | | | |
---|
1 | 0.00632 | 18 | 2.31 | 0 | 0.538 | 6.575 | 65.2 | 4.0900 | 1 | 296 | 15.3 | 396.90 | 4.98 | 24.0 |
---|
2 | 0.02731 | 0 | 7.07 | 0 | 0.469 | 6.421 | 78.9 | 4.9671 | 2 | 242 | 17.8 | 396.90 | 9.14 | 21.6 |
---|
3 | 0.02729 | 0 | 7.07 | 0 | 0.469 | 7.185 | 61.1 | 4.9671 | 2 | 242 | 17.8 | 392.83 | 4.03 | 34.7 |
---|
4 | 0.03237 | 0 | 2.18 | 0 | 0.458 | 6.998 | 45.8 | 6.0622 | 3 | 222 | 18.7 | 394.63 | 2.94 | 33.4 |
---|
5 | 0.06905 | 0 | 2.18 | 0 | 0.458 | 7.147 | 54.2 | 6.0622 | 3 | 222 | 18.7 | 396.90 | 5.33 | 36.2 |
---|
6 | 0.02985 | 0 | 2.18 | 0 | 0.458 | 6.430 | 58.7 | 6.0622 | 3 | 222 | 18.7 | 394.12 | 5.21 | 28.7 |
---|
A data.frame: 3 × 14
| crim | zn | indus | chas | nox | rm | age | dis | rad | tax | ptratio | black | lstat | medv |
---|
| | | | | | | | | | | | | | |
---|
504 | 0.06076 | 0 | 11.93 | 0 | 0.573 | 6.976 | 91.0 | 2.1675 | 1 | 273 | 21 | 396.90 | 5.64 | 23.9 |
---|
505 | 0.10959 | 0 | 11.93 | 0 | 0.573 | 6.794 | 89.3 | 2.3889 | 1 | 273 | 21 | 393.45 | 6.48 | 22.0 |
---|
506 | 0.04741 | 0 | 11.93 | 0 | 0.573 | 6.030 | 80.8 | 2.5050 | 1 | 273 | 21 | 396.90 | 7.88 | 11.9 |
---|
- 'crim'
- 'zn'
- 'indus'
- 'chas'
- 'nox'
- 'rm'
- 'age'
- 'dis'
- 'rad'
- 'tax'
- 'ptratio'
- 'black'
- 'lstat'
- 'medv'
#拟合多元线性回归
lm.fit=lm(medv~lstat+rm,data=Boston)
lm.fit
summary(lm.fit)
names(lm.fit)
Call:
lm(formula = medv ~ lstat + rm, data = Boston)
Coefficients:
(Intercept) lstat rm
-1.3583 -0.6424 5.0948
Call:
lm(formula = medv ~ lstat + rm, data = Boston)
Residuals:
Min 1Q Median 3Q Max
-18.076 -3.516 -1.010 1.909 28.131
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.35827 3.17283 -0.428 0.669
lstat -0.64236 0.04373 -14.689 <2e-16 ***
rm 5.09479 0.44447 11.463 <2e-16 ***
----------------------------------------------------
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.54 on 503 degrees of freedom
Multiple R-squared: 0.6386, Adjusted R-squared: 0.6371
F-statistic: 444.3 on 2 and 503 DF, p-value: < 2.2e-16
- 'coefficients'
- 'residuals'
- 'effects'
- 'rank'
- 'fitted.values'
- 'assign'
- 'qr'
- 'df.residual'
- 'xlevels'
- 'call'
- 'terms'
- 'model'
#提取回归系数
coef(lm.fit)
#拟合样本数据
fitted(lm.fit)
(Intercept)
-
-1.35827281187449
lstat
-
-0.642358334244129
rm
-
5.09478798433654
1
-
28.941013680603
2
-
25.4842056605593
3
-
32.6590747685798
4
-
32.4065199998349
5
-
31.6304069906576
6
-
28.0545270059976
7
-
21.2870784553023
8
-
17.7855965266756
9
-
8.1046933839978
10
-
18.2465067305075
11
-
17.9949622289472
12
-
20.7322130905842
13
-
18.5534841969081
14
-
23.6447410660871
15
-
23.1089582312963
16
-
22.9239451976971
17
-
24.6525760358365
18
-
19.73611045094
19
-
18.9297215033518
20
-
20.5737759641471
21
-
13.5173240750685
22
-
20.1483217520967
23
-
17.9089669708705
24
-
15.4876460563005
25
-
18.3528103591559
26
-
16.5621090140552
27
-
18.7444028109183
28
-
18.34995811367
29
-
23.5101884680665
30
-
24.9488893513429
31
-
13.2309525887229
32
-
21.2009271504736
33
-
11.1559662530231
34
-
15.8998380534484
35
-
16.6339862221155
36
-
22.6510756237111
37
-
21.0710752109097
38
-
22.8127543051733
39
-
22.5301423767843
40
-
29.4668659408904
41
-
33.155648488302
42
-
30.0244275043423
43
-
26.3393723415393
44
-
25.5063093520635
45
-
23.4274733730326
46
-
21.0318339224932
47
-
19.0308000359423
48
-
17.2869620498852
49
-
6.35742723749689
50
-
16.7765244616239
51
-
20.3822283431408
52
-
23.7389166204213
53
-
28.4222397493317
54
-
23.7851847604981
55
-
19.132935493086
56
-
32.4841016988669
57
-
27.455351303557
58
-
30.8304866690991
59
-
25.5426211789588
60
-
22.9159917295573
61
-
19.4438929108913
62
-
19.7615779561921
63
-
27.2106068255392
64
-
26.99027936289
65
-
29.6641164381871
66
-
27.6881301886823
67
-
21.5475159108212
68
-
23.3857884526783
69
-
18.7335005772485
70
-
22.9782247179402
71
-
27.0183336774926
72
-
22.6652580204237
73
-
25.9957983080991
74
-
25.6152963101065
75
-
26.2461427076934
76
-
24.9248809495225
77
-
22.9428716808724
78
-
23.3267053192647
79
-
22.4657440619383
80
-
22.7230509664968
81
-
29.516290370606
82
-
27.7263016832065
83
-
26.4324330592939
84
-
25.2371735973556
85
-
25.0128404446232
86
-
28.2255716016626
87
-
21.0261487355303
88
-
24.405420099229
89
-
30.807935756029
90
-
31.0462888240524
91
-
25.6758047589223
92
-
26.0065058869992
93
-
26.2207073757894
94
-
26.2964101031837
95
-
23.6764825425992
96
-
28.1230146616317
97
-
22.7565620252632
98
-
37.0472428465693
99
-
36.1897499723857
100
-
32.4484767909942
101
-
26.8633504501777
102
-
28.2625960862591
103
-
24.445575134786
104
-
21.2751450357578
105
-
22.1410064262989
106
-
17.8716899194778
107
-
16.3885033477181
108
-
20.8066642386557
109
-
23.7436478375448
110
-
20.3884894450616
111
-
21.8532804059167
112
-
26.326867827025
113
-
18.3545799414102
114
-
18.7012716564716
115
-
23.7918866493151
116
-
18.7200630115851
117
-
22.3731434350887
118
-
22.7011547991013
119
-
18.6852746348174
120
-
19.0974601972956
121
-
19.3174433930929
122
-
20.0643808164184
123
-
17.4942734297584
124
-
12.154480351257
125
-
17.3013262320283
126
-
19.6258011322085
127
-
9.72808395271157
128
-
16.6042154172969
129
-
21.5204139513767
130
-
15.5801952057933
131
-
23.4501529794949
132
-
22.9960427992055
133
-
23.9626915475232
134
-
18.6489370692436
135
-
16.8531988481851
136
-
20.0228117201171
137
-
18.0591015423275
138
-
22.1514807424117
139
-
14.7868207262998
140
-
18.121833229633
141
-
14.5775708480812
142
-
2.10891780017014
143
-
8.94081614306831
144
-
9.52892069574784
145
-
4.80679706531704
146
-
12.0152158401217
147
-
16.6199276988068
148
-
4.76981118873819
149
-
6.87170964910109
150
-
13.3786692669206
151
-
20.7747667153916
152
-
17.6434427767182
153
-
16.3914215545814
154
-
17.585033692988
155
-
20.1552247303529
156
-
20.3366406874171
157
-
15.1337859268475
158
-
31.0664154091936
159
-
25.4163470119212
160
-
27.0617688760923
161
-
26.9511812518862
162
-
35.6853144845795
163
-
37.1579350401705
164
-
39.1779468872535
165
-
20.98956503783
166
-
23.4234934216279
167
-
36.6615752792267
168
-
20.7855659943476
169
-
23.7055149510383
170
-
23.9870635202045
171
-
19.3043758329599
172
-
20.8715097750675
173
-
17.5936419068025
174
-
25.5229675540618
175
-
22.2997556462399
176
-
28.5684394120713
177
-
22.8181080946234
178
-
26.7748793868152
179
-
29.1468530877048
180
-
30.9658613142042
181
-
33.3465268796132
182
-
23.8738183052822
183
-
31.9987680449968
184
-
28.4302253908196
185
-
18.2127495396146
186
-
21.542945560438
187
-
35.6805173060786
188
-
28.9036256251452
189
-
29.1140032092827
190
-
31.7854674340077
191
-
30.7795709626038
192
-
29.9628428269645
193
-
33.3685469204126
194
-
30.055223060366
195
-
29.4741775326948
196
-
36.8553783120707
197
-
33.1466252262699
198
-
29.3196801349634
199
-
31.4488028134934
200
-
31.2487193747197
201
-
⋯
202
-
32.2889956093431
203
-
28.6989718359882
204
-
29.5293386267301
205
-
22.6634884381694
206
-
15.8586984893853
207
-
25.9907163894539
208
-
21.7991955404433
209
-
25.4910378574497
210
-
26.1381145394781
211
-
20.320371794958
212
-
16.9978750607969
213
-
17.860599465708
214
-
24.5018317613922
215
-
21.608944541447
216
-
26.7558547689144
217
-
26.7130936199981
218
-
24.4731822278228
219
-
20.1814901586924
220
-
27.3935591020704
221
-
28.1431283774336
222
-
26.8495251896564
223
-
21.4175594018623
224
-
22.1336304877984
225
-
26.1921093195769
226
-
24.1050673056101
227
-
19.7280733320953
228
-
24.3387277645277
229
-
27.1718127586885
230
-
26.4539206131412
231
-
24.2536719922697
232
-
22.2479261926042
233
-
21.8921983461714
234
-
24.0445781608031
235
-
22.8774630988419
236
-
23.079912953518
237
-
32.0065397277061
238
-
26.4052410144748
239
-
28.1442946913702
240
-
30.7020278715895
241
-
22.5177488663348
242
-
20.5521066248693
243
-
27.7539666882698
244
-
28.5979190420761
245
-
30.2109704227389
246
-
27.8655983676898
247
-
28.6337900820754
248
-
23.6154882642
249
-
30.0288482426432
250
-
22.3225269527581
251
-
25.3064527414074
252
-
18.9850434641274
253
-
22.6988012525381
254
-
22.4832194910329
255
-
21.6423912535173
256
-
26.2342092881489
257
-
21.3741821152891
258
-
19.4143489341904
259
-
18.8026558478964
260
-
39.9758901024489
261
-
12.2106758605297
262
-
14.93414327497
263
-
9.76025657614333
264
-
21.8687353006423
265
-
30.294198700716
266
-
32.4853790168681
267
-
24.1892543734149
268
-
22.8694645880148
269
-
1.30195775761222
270
-
-4.66638608393946
271
-
27.2666157053376
272
-
17.5885648081456
273
-
19.6120257343627
274
-
15.9290055899492
275
-
16.3560282948147
276
-
23.0872229306411
277
-
18.4462008597419
278
-
11.6868167812512
279
-
10.9886361726274
280
-
1.22032532580986
281
-
5.73586310340784
282
-
4.17678719883514
283
-
3.56662399733854
284
-
3.83528035713261
285
-
12.7094631547094
286
-
16.7574998437231
287
-
17.4196469309261
288
-
7.80331745385511
289
-
20.4491732446383
290
-
18.1321852870232
291
-
20.6129255525078
292
-
18.8313632934923
293
-
15.1256957159646
294
-
6.77386462218477
295
-
9.20494719516641
296
-
11.9482902426331
297
-
17.9052460209315
298
-
18.2224516713185
299
-
13.1943238483352
300
-
9.23322833788668
301
-
12.83401278218
302
-
4.73131634605215
303
-
19.4214916099036
304
-
10.3008912720989
305
-
20.8453666054506
306
-
21.4781788547428
307
-
18.9268869471935
308
-
0.142550031664321
309
-
12.0068038957583
310
-
-2.08933711100554
311
-
12.7610834695549
312
-
16.6281578577399
313
-
8.55205663278294
314
-
15.7459503587044
315
-
18.8013318725266
316
-
21.6561907753603
317
-
19.155997974418
318
-
18.3598372108161
319
-
14.7746926519857
320
-
15.9713533053291
321
-
13.0134773715595
322
-
18.3014023884075
323
-
20.912559253756
324
-
16.3701978221881
325
-
15.678485839594
326
-
19.6522402966274
327
-
20.8063778838302
328
-
23.6481692265309
329
-
21.0127304738757
330
-
20.5255950644033
331
-
17.4673982984316
332
-
19.9645844218174
333
-
12.9944849270067
334
-
7.02626334417762
335
-
12.6129404884816
336
-
14.0806609108578
337
-
18.74010433134
338
-
19.6694888976044
339
-
19.5729726660462
340
-
13.1849112767638
341
-
16.145209923781
342
-
19.5202230306005
343
-
19.9288758632398
344
-
18.507034271401
345
-
18.931042249359
346
-
21.8237219451952
347
-
21.1585279577368
348
-
19.6300786930966
349
-
25.5544162410789
350
-
20.9009363130341
351
-
20.2392621860754
352
-
16.8769482485398
353
-
18.0028384810517
354
-
20.3185105126479
355
-
20.1804654074873
356
-
22.2343776228761
357
-
21.7155457795788
358
-
21.8389097891041
359
-
25.2142140707373
360
-
21.7832886041637
361
-
18.9060879270501
362
-
17.9494598386098
363
-
15.5306597720129
364
-
17.1874841834578
365
-
18.2670419291968
366
-
19.5972674226921
367
-
22.1100097808359
368
-
22.2126117635541
369
-
26.713449141508
370
-
14.6387613959314
371
-
14.5549748923238
372
-
19.6770787948833
373
-
9.66333655102528
374
-
18.5712701048254
375
-
21.9558437806173
376
-
23.5444652765723
377
-
28.0596925753476
378
-
30.1130932224745
379
-
21.3045217110488
380
-
19.9841672653487
381
-
24.0038777689549
382
-
20.1687330773821
383
-
21.3714473085375
384
-
14.827709338248
385
-
10.8275800634918
386
-
5.52428703198578
387
-
17.5164285986196
388
-
20.5483599362519
389
-
20.0029586204622
390
-
20.1037910209262
391
-
16.223668376617
392
-
12.5231792377786
393
-
19.1036762565292
394
-
21.007986387413
395
-
17.3149906258094
396
-
20.1430194400035
397
-
26.0200592767156
398
-
23.9892159773285
399
-
30.5600671617203
400
-
29.093234747806
401
-
24.3015150598311
#提取系数的置信区间,就是置信区间
confint(lm.fit,level = 0.95)
A matrix: 3 × 2 of type dbl
| 2.5 % | 97.5 % |
---|
(Intercept) | -7.5919003 | 4.8753547 |
---|
lstat | -0.7282772 | -0.5564395 |
---|
rm | 4.2215504 | 5.9680255 |
---|
#回归诊断
par(mfrow=c(2,2))
plot(lm.fit)

带有交互项
#带交互项的回归
#注意:变量x和y在lm拟合中,x*y和x:y的意思是不一样的。
summary(lm(medv~lstat*age,data=Boston)) #单变量+交互
summary(lm(medv~age*lstat,data=Boston)) #单变量+交互
Call:
lm(formula = medv ~ lstat * age, data = Boston)
Residuals:
Min 1Q Median 3Q Max
-15.806 -4.045 -1.333 2.085 27.552
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 36.0885359 1.4698355 24.553 < 2e-16 ***
lstat -1.3921168 0.1674555 -8.313 8.78e-16 ***
age -0.0007209 0.0198792 -0.036 0.9711
lstat:age 0.0041560 0.0018518 2.244 0.0252 *
------------------------------------------------------------------------------------------------------
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 6.149 on 502 degrees of freedom
Multiple R-squared: 0.5557, Adjusted R-squared: 0.5531
F-statistic: 209.3 on 3 and 502 DF, p-value: < 2.2e-16
Call:
lm(formula = medv ~ age * lstat, data = Boston)
Residuals:
Min 1Q Median 3Q Max
-15.806 -4.045 -1.333 2.085 27.552
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 36.0885359 1.4698355 24.553 < 2e-16 ***
age -0.0007209 0.0198792 -0.036 0.9711
lstat -1.3921168 0.1674555 -8.313 8.78e-16 ***
age:lstat 0.0041560 0.0018518 2.244 0.0252 *
----------------------------------------------------
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 6.149 on 502 degrees of freedom
Multiple R-squared: 0.5557, Adjusted R-squared: 0.5531
F-statistic: 209.3 on 3 and 502 DF, p-value: < 2.2e-16
多元线性回归高阶
## 多元线性回归高阶情况
summary(lm(medv~lstat+age+poly(rm,3,raw=T),data=Boston))
summary(lm(medv~lstat+I(rm^3),data=Boston))#这种高阶的情况也是可以的只不过只能输出指定阶数的项
Call:
lm(formula = medv ~ lstat + age + poly(rm, 3, raw = T), data = Boston)
Residuals:
Min 1Q Median 3Q Max
-31.1634 -2.6180 -0.4345 1.9570 27.7091
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 160.858226 37.336812 4.308 1.98e-05 ***
lstat -0.682214 0.047720 -14.296 < 2e-16 ***
age -0.004674 0.009731 -0.480 0.6313
poly(rm, 3, raw = T)1 -52.259779 18.264834 -2.861 0.0044 **
poly(rm, 3, raw = T)2 6.003959 2.936142 2.045 0.0414 *
poly(rm, 3, raw = T)3 -0.159260 0.154874 -1.028 0.3043
--------------------------------------------------------------------------------------------------------------------------
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.781 on 500 degrees of freedom
Multiple R-squared: 0.7324, Adjusted R-squared: 0.7298
F-statistic: 273.8 on 5 and 500 DF, p-value: < 2.2e-16
Call:
lm(formula = medv ~ lstat + I(rm^3), data = Boston)
Residuals:
Min 1Q Median 3Q Max
-24.5407 -2.9667 -0.6511 2.0786 27.9885
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 18.312269 1.195934 15.31 <2e-16 ***
lstat -0.609726 0.039741 -15.34 <2e-16 ***
I(rm^3) 0.046324 0.003136 14.77 <2e-16 ***
-----------------------------------------------------
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.196 on 503 degrees of freedom
Multiple R-squared: 0.6821, Adjusted R-squared: 0.6808
F-statistic: 539.5 on 2 and 503 DF, p-value: < 2.2e-16