-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathR_reg2.Rmd
159 lines (93 loc) · 2.17 KB
/
R_reg2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
title: "R的回归分析之二"
author: "李峰"
date: '`r strftime(Sys.time(), format = "%B %d, %Y")`'
output:
html_document: default
---
---
#### 1. 回归建模策略
```{r}
CarData<-read.table(file="CarData.txt",header=TRUE)
```
$R^2$和$Adjusted$ $R^2$的公式
\begin{equation}\label{eq:1}
R^2=\frac{SSR}{SST},
\end{equation}
\begin{equation}\label{eq:2}
{\bar{R}}^2=\frac{SSR/p}{SST/(n-1)},
\end{equation}
```{r}
Fit1<-lm(log(MPG)~weight+horsepower,data=CarData)
Fit2<-lm(log(MPG)~weight+horsepower+displacement,data=CarData)
summary(Fit1)
summary(Fit2)
anova(Fit1,Fit2)
```
#### 2. AIC和BIC信息准则
对回归分析而言,
\begin{equation}\label{eq:3}
AIC=n+nln(2\pi)+nln(RSS/n)+2(p+1),
\end{equation}
\begin{equation}\label{eq:4}
BIC=n+nln(2\pi)+nln(RSS/n)+ln(n)(p+1),
\end{equation}
p是参数个数。
```{r}
Fit1<-lm(log(MPG)~weight+horsepower,data=CarData)
Fit2<-lm(log(MPG)~weight+horsepower+displacement,data=CarData)
AIC(Fit1,Fit2)
```
```{r}
(392+392*log(2*pi)+392*log(9.587/392)+2*4)
```
```{r}
BIC(Fit1,Fit2)
```
#### 3. 变量筛选策略
```{r}
Fit<-lm(log(MPG)~weight+horsepower+displacement+cylinders+acceleration,data=CarData)
library("MASS")
stepAIC(Fit,direction="backward")
```
```{r}
stepAIC(Fit,direction="both")
```
#### 4. 全子集回归
```{r}
library("leaps")
leapsFit<-regsubsets(log(MPG)~weight+horsepower+displacement+cylinders+acceleration,data=CarData,nbest=2)
summary(leapsFit)
coef(leapsFit, c(3,5))
```
```{r}
plot(leapsFit,scale="bic")
```
```{r}
plot(leapsFit,scale="adjr2")
```
```{r}
library("car")
subsets(leapsFit,statistic="cp",main="全子集回归模型评价图",legend=TRUE)
abline(1,1,lty=2,col="red")
```
#### 5. 虚拟变量的回归分析
```{r}
CarData$ModelYear<-as.factor(CarData$ModelYear)
Fit<-lm(log(MPG)~ModelYear,data=CarData)
summary(Fit)
```
```{r}
(table(CarData$ModelYear))
```
```{r}
tmp<-subset(CarData,CarData$ModelYear=="70"|CarData$ModelYear=="71")
tresult<-t.test(log(MPG)~ModelYear,data=tmp,paired=FALSE,var.equal=TRUE)
```
```{r}
(tresult$estimate[1]-tresult$estimate[2])
```
```{r}
Fit<-lm(log(MPG)~weight+horsepower+ModelYear,data=CarData)
summary(Fit)
```