您如何从step()获得变量名称的结果列表?

时间:2014-08-07 07:37:40

标签: r

我正在使用r中的step()函数。获取step()结束的变量列表应该保留在模型中的方法是什么?假设我有一个带有响应向量的数据框和几个预测变量X,Y和Z. Step()得出结论,只有X和Y应该包含在最终模型中。如何获得向量c('X','Y')

-

在回答第一个答案时,我尝试了$ coefficient和names()。两者都不起作用。

以下是print("names"); print(names(df_lm_2$coefficients))

的输出
[1] "(Intercept)"                  "How.often2"                   "How.often5"                  
 [4] "How.often6"                   "How.often7"                   "How.often8"                  
 [7] "Bathroom.s.2"                 "Bathroom.s.3"                 "Bathroom.s.5"                
[10] "Bathroom.s.7"                 "Other.details..option.2.TRUE" "description_length"          
[13] "has_descriptionTrue"          "Other.details..option.6.TRUE" "Bedroom.s.2"                 
[16] "Bedroom.s.3"                  "Bedroom.s.4"                  "Bedroom.s.5"                 
[19] "Bedroom.s.6"                  "weekend1"                     "Other.details..option.7.TRUE"
[22] "Other.details..option.3.TRUE"

我最终想要的只是重要的列名,而不是每个因素的级别。对于上下文,以下是列名:

[1] "=------Colnames--------"
 [1] "Bathroom.s."              "Bedroom.s."               "bid_price"                "description_length"      
 [5] "has_description"          "hour_of_day"              "How.often"                "log_bid_price"           
 [9] "month"                    "Other.details..option.1." "Other.details..option.2." "Other.details..option.3."
[13] "Other.details..option.4." "Other.details..option.5." "Other.details..option.6." "Other.details..option.7."
[17] "req_source"               "Square.feet"              "Type.of.home"             "weekend"    

1 个答案:

答案 0 :(得分:0)

继续?step的帮助页面中的示例,

lm1 <- lm(Fertility ~ ., data = swiss)
slm1 <- step(lm1)

你可以这样做:

names(slm1$coefficients)

请查看names(slm1)以获得更多启示。