简体   繁体   中英

Running several regressions with changing outcome variable in R

I have a dataframe (df) that looks like the following:

condition  dv1  dv2  dv3
1          2    4    3
2          5    7    4
3          7    1    2

In order to run several regressions simultaneously, I have used code like this:

dfdv <- df[,2:4]  
output <- lm(as.matrix(dfdv) ~ condition, data = df)
summary(output)

This gives me all the dv ~ condition regressions for dv1, dv2, and dv3. These are the regressions that are given in the output:

dv1 ~ condition
dv2 ~ condition
dv3 ~ condition

However, I would now like to control for different conditions in the regression. Specifically, I would like to figure out a way to run the following regressions efficiently.

dv1 ~ condition + dv1
dv1 ~ condition + dv2
dv1 ~ condition + dv3

I tried the following using a similar principle to what worked above, and it didn't work.

dfdv <- df[,2:4]  
output2 <- lm(dv1 ~ condition + as.matrix(dfdv), data = df)
summary(output2)

It gave me a single regression using all the dv's in that one regression as opposed to the regressions that I wanted. This is what it gave me:

dv1 ~ condition + dv1 + dv2 + dv3

Does anyone know how I can perform these regression analyses using simple code? I have several more in my actual dataset than I am including in this example.

We can use lapply for that. Loop through the column names that are 'dv', create formula with reformulate by specifying the response and dependent variable, and apply the lm

v1 <- grep('^dv\\d+$", names(df), value = TRUE)
lapply(v1, function(dv) lm(reformulate(c(dv, "condition"),
           dv), data = df))

Or create the formula with paste

lapply(v1, function(dv) lm(paste0(dv, " ~ ", "condition + ", dv), data = df))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM