|
setwd("C:/Users/IBM/Desktop/研一课程/2.2回归分析/回归作业") #设定当前的工作目录
shuju=read.table("shuju.txt",header=T)
shuju #读取数据
#前进法
#采用最优子集函数进行前进法,以调整复决定系数为标准
shuju.regforward <- regsubsets(y~x1+x2+x3+x4+x5+x6+x7,data=shuju[,-1],nvmax=7,method="forward")
shuju.regfs <- summary(shuju.regforward)
shuju.regfs
shuju.regfs$adjr2
coef(shuju.regforward,3)
#采用AIC原则自动选择模型-前进法
shuju.reg <- lm(y~.,data=shuju[,-1])
shuju.regforward2 <- step(shuju.reg,direction="forward")#按照AIC原则自动选择模型
summary(shuju.regforward2)
shuju.reg <- lm(y~x1+x4+x6,data=shuju[,-1])
#后退法
#采用最优子集函数进行后退法
shuju.regbackward <- regsubsets(y~x1+x2+x3+x4+x5+x6+x7,data=shuju[,-1],nvmax=7,method="backward")
shuju.regbs <- summary(shuju.regbackward)
shuju.regbs
shuju.regbs$adjr2
#采用AIC原则自动选择模型-后退法
shuju.reg <- lm(y~.,data=shuju[,-1])
shuju.regbackward2 <- step(shuju.reg,direction="backward",nvmax=3)#按照AIC原则自动选择模型
summary(shuju.regbackward2)
#逐步回归法
#采用AIC原则自动选择模型-逐步回归法
shuju.reg <- lm(y~.,data=shuju[,-1])
shuju.regboth <- step(shuju.reg,direction="both")#按照AIC原则自动选择模型
summary(shuju.regboth)
附录:数据为
years y
x1 x2 x3
x4 x5
x6 x7
1974 172.9
11246 681
105.9 10183
4110 11242
9
1975 352.94
10335 791
107.4 10414
3996 12693
6.5
1976 447.67
13156 607
114.4 13134
4689 16681
6
1977 404.02
6127 714
110.8 15033
6876 22131
4.75
1978 409.51
27419 911
99.4 17389
8636 31353
4.75
1979 619.71
25633 1231
91.4 21715
12339 43528
9.5
1980 1121.17
95684 2760
90.8 27075
16623 70752
10
1981 1506.94
105987 2651
86.3 31827
19937 125989
16
1982 1105.79
46230 2105
125.3 35393
24787 99468
10.5
1983 933.03
37165 3030
107.4 38823
25112 82478
10.5
1984 1008.54
48787 2810
106.6 46079
24414 54936
8.5
1985 1567.56
75808 2649
115.7 47871
22970 87135
6
1986 1960.06
123128 3031
110.1 54372
24403 129884
6.5
1987 2884.88
371406 3644
105.8 65602
30531 153044
5
1988 2556.72
198569 3690
101.6 74917
37861 215033
5.25
|