AIC准则选三个变量的r语言代码

setwd("C:/Users/IBM/Desktop/研一课程/2.2回归分析/回归作业")  #设定当前的工作目录
shuju=read.table("shuju.txt",header=T)
shuju   #读取数据
#采用AIC原则自动选择模型-前进法
library(MASS)
stepAIC(lm(y~.,data=shuju[,-1]))  #146.11
stepAIC(lm(y~,data=shuju[,-1]))  #181.53
stepAIC(lm(y~x3,data=shuju[,-1]))   #204.33
stepAIC(lm(y~x4,data=shuju[,-1]))   #172.52min
stepAIC(lm(y~x5,data=shuju[,-1]))  #182.18
stepAIC(lm(y~x6,data=shuju[,-1]))  #172.76
stepAIC(lm(y~x7,data=shuju[,-1])) #204.22
stepAIC(lm(y~x4+x1,data=shuju[,-1]))  #155.75min
stepAIC(lm(y~x4+x2,data=shuju[,-1]))  #174.14
stepAIC(lm(y~x4+x3,data=shuju[,-1]))  #172.9
stepAIC(lm(y~x4+x5,data=shuju[,-1]))  #173.71
stepAIC(lm(y~x4+x6,data=shuju[,-1]))  #168.67
stepAIC(lm(y~x4+x7,data=shuju[,-1]))  #174.35
stepAIC(lm(y~x4+x1+x2,data=shuju[,-1]))  #157.21
stepAIC(lm(y~x4+x1+x3,data=shuju[,-1]))  #157.5
stepAIC(lm(y~x4+x1+x5,data=shuju[,-1]))  #157.65
stepAIC(lm(y~x4+x1+x6,data=shuju[,-1]))  #148.55min
stepAIC(lm(y~x4+x1+x7,data=shuju[,-1]))  #155.63


setwd("C:/Users/IBM/Desktop/研一课程/2.2回归分析/回归作业")  #设定当前的工作目录
shuju=read.table("shuju.txt",header=T)
shuju   #读取数据
#采用AIC原则自动选择模型-后退法
library(MASS)
stepAIC(lm(y~x1+x2+x3+x4+x5+x6,data=shuju[,-1]))  #146.11min
stepAIC(lm(y~x1+x2+x3+x4+x5+x7,data=shuju[,-1]))  #159.96
stepAIC(lm(y~x1+x2+x3+x6+x5+x7,data=shuju[,-1]))  #153.58
stepAIC(lm(y~x1+x2+x6+x4+x5+x7,data=shuju[,-1]))  #149.11
stepAIC(lm(y~x1+x6+x3+x4+x5+x7,data=shuju[,-1]))  #148.08
stepAIC(lm(y~x6+x2+x3+x4+x5+x7,data=shuju[,-1]))  #165.79
stepAIC(lm(y~x1+x2+x3+x4+x5,data=shuju[,-1]))  #161.14
stepAIC(lm(y~x1+x2+x3+x4+x6,data=shuju[,-1]))  #151.88min
stepAIC(lm(y~x1+x2+x3+x5+x6,data=shuju[,-1]))  #153.83
stepAIC(lm(y~x1+x3+x4+x5+x6,data=shuju[,-1]))  #150.08
stepAIC(lm(y~x2+x3+x4+x5+x6,data=shuju[,-1]))  #163.79
stepAIC(lm(y~x1+x2+x3+x4,data=shuju[,-1]))  #159.17
stepAIC(lm(y~x1+x2+x3+x6,data=shuju[,-1]))  #152.87
stepAIC(lm(y~x1+x2+x4+x6,data=shuju[,-1]))  #150.49
stepAIC(lm(y~x1+x3+x4+x6,data=shuju[,-1]))  #150.2min
stepAIC(lm(y~x1+x3+x4,data=shuju[,-1]))  #AIC=157.5
stepAIC(lm(y~x1+x3+x6,data=shuju[,-1])) #AIC=155.12
stepAIC(lm(y~x1+x4+x6,data=shuju[,-1])) #AIC=148.55min

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

你可能感兴趣的:(AIC准则选三个变量的r语言代码)