Search code examples
rlapplysummary

How can I get the p.value of all regressions using lapply function


I am using an example, with mtcars, to later use it in the analysis of my data. I can get a list of summaries and the coef function on each regressions. My question is how can I get the p.value of all regressions in a list using lapply function?

Here is my code

    library(data.table)
    regressions <- 
    data.table(mtcars)[, 
          .(Myregressions = lapply(.SD, function(x) summary(lm(mpg ~ x)))), 
          .SDcols = -1]

Regressions$MyRegressions
Regressions[, lapply(MyRegressions, coef)]

Solution

  • We can loop over the 'MyRegressions' and extract the column

    regressions[, lapply(Myregressions, function(x) coef(x)[, "Pr(>|t|)"])]
    

    If we need to extract only selected values

    regressions[, list(lapply(Myregressions, function(x) {
          x1 <- coef(x)[, "Pr(>|t|)"]
          x1[x1 < 0.05]}))]$V1