Reputation: 1000
I have a dataframe (df) that looks like the following:
yourbehavior condition tryreduce comfortable vegvalidity
1 2 4 3 3
2 5 7 4 5
3 7 1 2 4
I would like to figure out a way to run the following regressions efficiently.
lm(yourbehavior ~ condition + tryreduce + (condition*tryreduce), data = df)
lm(yourbehavior ~ condition + comfortable + (condition*comfortable), data = df)
lm(yourbehavior ~ condition + vegvalidity + (condition*vegvalidity), data = df)
Does anyone know how I can perform these regression analyses simultaneously? I have several more in my actual dataset than I am including in this example.
I've read that using a for-loop is typically not the most efficient way to conduct these types of analyses in R. If possible, I'd prefer to use the tidyverse since that's what I understand best.
Upvotes: 3
Views: 519
Reputation: 93761
You could do something like this, which will create a list where each element is one of the regression models and the name of each list element will be the variable that is interacted with condition
:
library(tidyverse)
interaction.vars = c("tryreduce", "comfortable", "vegvalidity")
form = paste("yourbehavior ~ condition *", interaction.vars)
models = form %>%
set_names(interaction.vars) %>%
map(~lm(.x, data=df))
If you want to iterate over every column except the first two you could do:
interaction.vars = names(df)[!names(df) %in% c("yourbehavior","condition")]
Note that in a model formula a*b
is equivalent to a + b + a*b
, so you don't need to repeat each column name.
for loops aren't necessarily bad, and the map
function above is essentially iterating in the same way as a for loop. However, many functions in R are vectorized which is much faster and more succinct than looping.
Upvotes: 4