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)