2

I'm trying to subset variables by significant P-values, and I attempted with the following code, but it only selects all variables instead of selecting by condition. Could anyone help me to correct the problem?

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

Thanks!

lydias
  • 841
  • 1
  • 14
  • 32

1 Answers1

6

An alternative solution to Martin's great answer (in the comments section) using the broom package. Unfortunately, you haven't posted an data, so I'm using the mtcars dataset as a demo:

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"

The main advantage is that by obtaining the model's output as a dataframe you can use variable names, and not variable positions and row names, to manipulate the data.

I'm using options(scipen = 999) to make it easier to check that filtering works (i.e. not using the scientific notation of numbers in the dataframe).

AntoniosK
  • 15,991
  • 2
  • 19
  • 32