2015-11-12 60 views
0

我與mtcars數據集工作,並採用線性迴歸如何找到線性迴歸模型的子集選擇?

data(mtcars) 
fit<- lm(mpg ~.,mtcars);summary(fit) 

當我擬合模型與LM也這樣表示

Call: 
lm(formula = mpg ~ ., data = mtcars) 

Residuals: 
    Min  1Q Median  3Q  Max 
-3.5087 -1.3584 -0.0948 0.7745 4.6251 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|) 
(Intercept) 23.87913 20.06582 1.190 0.2525 
cyl6  -2.64870 3.04089 -0.871 0.3975 
cyl8  -0.33616 7.15954 -0.047 0.9632 
disp   0.03555 0.03190 1.114 0.2827 
hp   -0.07051 0.03943 -1.788 0.0939 . 
drat   1.18283 2.48348 0.476 0.6407 
wt   -4.52978 2.53875 -1.784 0.0946 . 
qsec   0.36784 0.93540 0.393 0.6997 
vs1   1.93085 2.87126 0.672 0.5115 
amManual  1.21212 3.21355 0.377 0.7113 
gear4  1.11435 3.79952 0.293 0.7733 
gear5  2.52840 3.73636 0.677 0.5089 
carb2  -0.97935 2.31797 -0.423 0.6787 
carb3  2.99964 4.29355 0.699 0.4955 
carb4  1.09142 4.44962 0.245 0.8096 
carb6  4.47757 6.38406 0.701 0.4938 
carb8  7.25041 8.36057 0.867 0.3995 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 2.833 on 15 degrees of freedom 
Multiple R-squared: 0.8931, Adjusted R-squared: 0.779 
F-statistic: 7.83 on 16 and 15 DF, p-value: 0.000124 

結果我發現沒有變量被標記在爲顯著0.05顯着水平。

要找出重要變量,我想要做子集選擇以找出最佳的一對可變參數作爲具有響應變量mpg的預測變量。

回答

1

程序包leaps中的函數regsubsets執行最佳子集迴歸(請​​參見?leaps)。修改您的代碼:

library(leaps) 
    regfit <- regsubsets(mpg ~., data = mtcars) 
    summary(regfit) 
    # or for a more visual display 
    plot(regfit,scale="Cp")