1

I want to create multiple linear models at once from the columns of a dataframe.

    x1 <- rnorm(30, mean = 1, sd = 1)
    x2 <- rnorm(30, mean = 1, sd = 2)
    x3 <- rnorm(30, mean = 1, sd = 3)
    eps <- rnorm(30)
    y <- 2+x1+3*x2+x3+eps
    df <- data.frame(x1,x2,x3,y)

I tried using

df %>% purrr::map(~lm(y ~ .x))
lapply(df, lm(y~.x))

But I don't know how to use these functions with dataframes and the lm function.

I wanted the output to be a list of models similar to this

mod1 <- lm(y~x1)
mod2 <- lm(y~x2)
mod3 <- lm(y~x3)
list <- list(mod1,mod2,mod3)

2 Answers2

1

Assuming y is the fourth column, you could try:

lapply(df[-4], function(x) lm(df$y ~ x))

(note if its not the fourth column you could generalize to lapply(df[-grep("y", names(df))], function(x) lm(df$y ~ x)))

This produces a named list of length 3, each element containing the results of the lm function (and each element is named x1, x2, x3):

$x1

Call:
lm(formula = df$y ~ x)

Coefficients:
(Intercept)            x  
      5.503        2.272  


$x2

Call:
lm(formula = df$y ~ x)

Coefficients:
(Intercept)            x  
      4.241        2.825  


$x3

Call:
lm(formula = df$y ~ x)

Coefficients:
(Intercept)            x  
     7.2815       0.8999  
jpsmith
  • 11,023
  • 5
  • 15
  • 36
1

Another option by first converting to longer format using pivot_longer and nest per group and pull the model per group like this:

library(tidyverse)
df %>%
  pivot_longer(cols = x1:x3) %>%
  group_by(name) %>%
  nest() %>%
  mutate(model = map(data, ~ lm(y ~ value, data = .))) %>%  
  pull(model)
#> [[1]]
#> 
#> Call:
#> lm(formula = y ~ value, data = .)
#> 
#> Coefficients:
#> (Intercept)        value  
#>      10.173       -1.515  
#> 
#> 
#> [[2]]
#> 
#> Call:
#> lm(formula = y ~ value, data = .)
#> 
#> Coefficients:
#> (Intercept)        value  
#>       4.355        2.744  
#> 
#> 
#> [[3]]
#> 
#> Call:
#> lm(formula = y ~ value, data = .)
#> 
#> Coefficients:
#> (Intercept)        value  
#>      7.8368       0.5657

Created on 2023-03-16 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53