2013-08-26 20 views
3

我运行glm并得到结果。现在我想得到那些在95%显着的预测变量的名称,即p值小于或等于显着性水平5e-2。我运行:如何矢量化提取重要的预测变量?

fit <- glm(data=dfa, formula=response~.) 
sig <- summary(fit)$coefficients[,4] 

(Intercept)  close0  close1  close2  close3  close4  closema  open0 
0.000000e+00 3.147425e-19 7.210909e-04 1.046019e-02 4.117580e-03 2.778701e-01 2.829958e-05 0.000000e+00 
     open1  open2  open3  open4  openma   low0   low1   low2 
8.627202e-30 1.138499e-02 1.112236e-03 7.422145e-03 3.967735e-03 3.036329e-42 3.033847e-05 3.237155e-01 
     low3   low4  lowma  high0  high1  high2  high3  high4 
8.198750e-01 6.647138e-02 4.350488e-05 6.177130e-58 2.625192e-02 4.143373e-01 3.964651e-01 3.694272e-01 
     highma  volume0  volume1  volume2  volume3  volume4  volumema 
1.416310e-05 8.027502e-02 1.975302e-01 1.630341e-09 8.979313e-03 1.274195e-06 8.246661e-01 

> str(sig) 
    Named num [1:31] 0.00 3.15e-19 7.21e-04 1.05e-02 4.12e-03 ... 
    - attr(*, "names")= chr [1:31] "(Intercept)" "close0" "close1" "close2" ... 

什么是“命名的数字”类型呢?

我想有列名的数组喜欢这样,因为这些预测变量具有以下显着性水平5E-2即

best <- c('close0', 'close1', 'close2', 'close3', 'closema', ... etc) 

注意p值的close4是不存在的......我怎么能以矢量化的方式提取这些列名称?

更新:我制定了如何做一个循环

fit <- glm(data=dfa, formula=response~.) 
summary(fit) 
sig <- summary(fit)$coefficients[,4] 
best <- NULL 
columnLabels <- names(sig) 
for (columnLabel in columnLabels) { 
    if (as.numeric(sig[columnLabel]) <= 5e-2) { 
     if (is.null(best)) { 
      best <- columnLabel 
     } else { 
      best <- c(best, columnLabel) 
     } 
    } 
} 
+0

请问'name(sig <5e-2)'是吗? – James

+0

谢谢,但不,不起作用,''就像给我所有列名称。 –

回答

5

names(sig)[sig <= 0.05]是你在找什么。 names(sig)返回所有名称,sig <= 0.05有助于提取所需的子集。

4
utils::data(anorexia, package="MASS") 

anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt), 
       family = gaussian, data = anorexia) 
summary(anorex.1) 

ll<-summary(anorex.1)$coefficients 

ll<-as.data.frame(ll) 

ll 
       Estimate Std. Error t value  Pr(>|t|) 
(Intercept) 49.7711090 13.3909581 3.716770 0.0004101067 
Prewt  -0.5655388 0.1611824 -3.508689 0.0008034250 
TreatCont -4.0970655 1.8934926 -2.163761 0.0339993147 
TreatFT  4.5630627 2.1333359 2.138933 0.0360350847 

rownames(ll[ll[,4]<0.005,]) 

[1] "(Intercept)" "Prewt"