子集变量的显着P值

时间:2017-12-17 22:44:49

标签: r subset extraction p-value dimensionality-reduction

我正在尝试通过显着的P值来对变量进行子集化,并尝试使用以下代码,但它只选择所有变量而不是按条件选择。有人可以帮我纠正这个问题吗?

myvars <- names(summary(backward_lm)$coefficients[,4] < 0.05)
happiness_reduced <- happiness_nomis[myvars]

谢谢!

1 个答案:

答案 0 :(得分:6)

使用broom包的Martin的完美答案(在评论部分中)的替代解决方案。不幸的是,您还没有发布数据,所以我使用mtcars数据集作为演示:

library(broom)

# build model
m = lm(disp ~ ., data = mtcars)

# create a dataframe frm model's output
tm = tidy(m)

# visualise dataframe of the model
# (using non scientific notation of numbers)
options(scipen = 999)
tm

#           term    estimate   std.error   statistic       p.value
# 1  (Intercept)  -5.8119829 228.0609389 -0.02548434 0.97990925639
# 2          mpg   1.9398052   2.5976340  0.74675849 0.46348865035
# 3          cyl  15.3889587  12.1518291  1.26639032 0.21924091701
# 4           hp   0.6649525   0.2259928  2.94236093 0.00777972543
# 5         drat   8.8116809  19.7390767  0.44640796 0.65987184728
# 6           wt  86.7111730  16.1127236  5.38153418 0.00002448671
# 7         qsec -12.9742622   8.6227190 -1.50466021 0.14730421493
# 8           vs -12.1152075  25.2579953 -0.47965832 0.63642812949
# 9           am  -7.9135864  25.6183932 -0.30890253 0.76043942893
# 10        gear   5.1265224  18.0578153  0.28389494 0.77927112074
# 11        carb -30.1067073   7.5513212 -3.98694566 0.00067029676

# get variables with p value less than 0.05
tm$term[tm$p.value < 0.05]

# [1] "hp"   "wt"   "carb"

主要优点是,通过将模型的输出作为数据帧获取,您可以使用变量名称而不是变量位置和行名称来操作数据。

我正在使用options(scipen = 999)来更轻松地检查过滤是否有效(即不使用数据框中数字的科学记数法)。