Search code examples
rregressionlinear-regression

Output list of variables with significant p-values from several regressions in R


I have a dataframe that looks like the following.

consistent  admire  trust  judge 
3           3       2      4
5           1       3      6
2           4       5      1

I can run the regressions I need simultaneously using the following code. In the actual dataset, there are many more than 3 variables.

variables <- c("admire", "trust", "judge")
form <- paste("consistent ~ ",variables,"")
model <- form %>% 
  set_names(variables) %>%
  map(~lm(as.formula(.x), data = df))
map(model, summary)

This yields the output for the 3 following regressions.

summary(lm(consistent ~ admire, df))
summary(lm(consistent ~ trust, df))
summary(lm(consistent ~ judge, df))

I would like a list of the variables with significant p-values at p < 0.05. For example, if "admire" was significant and "judge" was significant, the output I am looking for would be something like:

admire, judge

Is there a way to do this that allows me to also run several regressions simultaneously? This question offers a similar answer, but I don't know how to apply it when I have several regressions.

Data:

structure(list(consistent = c(1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 
0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 
1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 
1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 
1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 
1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 
1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0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5, 4, 2, 1, 6, 7, 5, 2, 2, 4, 5, 6, 1, 5, 2, 4, 
5, 5, 2, 2, 3, 4, 5, 2, 2, 4, 1, 3, 4, 4, 4, 2, 3, 1, 4, 4, 3, 
2, 3, 1, 4, 2, 4, 4, 1, 5, 4, 4, 4, 4, 6, 1, 3, 5, 7, 2, 6, 1, 
5, 7, 5, 4, 2, 3, 6, 3, 1, 1, 2, 2, 5, 5, 2, 5, 4, 4, 5, 4, 4, 
3, 7, 4, 4, 4, 2, 5, 3, 6, 5, 4, 4, 4, 6, 4, 5, 5, 1, 5, 2, 6, 
4, 4, 1, 1, 4, 6, 1, 7, 1, 5, 2, 5, 4, 2, 3, 2, 6, 3, 2, 2, 1, 
1, 5, 4, 1, 1, 4, 1, 5, 1, 4, 3, 2, 3, 4, 1, 6, 1, 2, 1, 3, 5, 
5, 2, 1, 3, 4, 2, 4, 5, 4, 6, 3, 4, 6, 7, 6, 2, 4, 6, 2, 4, 5, 
1, 4, 1, 3, 2, 4, 1, 6, 4, 3, 1, 3, 4, 5, 1, 6, 1, 5, 1, 3, 3, 
1, 3, 4, 2, 4, 1, 1, 2, 2, 2, 3, 1, 6, 5, 4, 1, 7, 5, 6, 5, 2, 
3, 5, 4, 3, 4, 5, 7, 1, 5, 2, 5, 1, 3, 4, 3, 5, 1, 4, 2, 3, 4, 
1, 7, 5, 5, 2, 1, 2, 5, 6, 5, 5, 3, 1, 3, 1, 4, 1, 5, 2, 3, 5, 
6, 4, 4, 3, 2, 4, 1, 3, 4, 3, 4, 4, 1, 5)), row.names = c(NA, 
-450L), class = c("tbl_df", "tbl", "data.frame")) 

Solution

  • To fit many many simple linear regression models, I recommend Fast pairwise simple linear regression between variables in a data frame. Hmm... looks like I need to collect those functions in an R package...

    ## suppose your data frame is `df`
    ## response variable (LHS) in column 1
    ## independent variable (RHS) in other columns
    out <- general_paired_simpleLM(df[1], df[-1])
    #         LHS    RHS      alpha       beta     beta.se   beta.tv      beta.pv
    #1 consistent admire -0.1458455 0.18754326 0.008324192 22.529906 1.040756e-75
    #2 consistent  trust -0.2211250 0.19565589 0.007721387 25.339475 1.531499e-88
    #3 consistent  judge  0.3484851 0.04824981 0.014182420  3.402086 7.287372e-04
    #        sig         R2      F.fv         F.pv
    #1 0.3430602 0.53118295 507.59665 1.040756e-75
    #2 0.3212008 0.58902439 642.08902 1.531499e-88
    #3 0.4946862 0.02518459  11.57419 7.287372e-04
    

    To get what you want:

    with(out, RHS[beta.pv < 0.05])
    #[1] "admire" "trust"  "judge"