Home > Software design >  Is there an easier way to add controls into R regression (in STATA we use global) without having to
Is there an easier way to add controls into R regression (in STATA we use global) without having to

Time:07-11

Suppose I run several regressions but with all the same controls and different variables of interest. It would be nice to specify a list of controls that I could add to the regression without having to type out every control.

In STATA we can simply type global controls y z and then include

reg dep $controls

In R I was hoping it would be something as such

controls <- "log(mkt)   mb   lt   ni"
model <- lm(dep ~ indep   controls, data=df)
summary(model)

This does not seem to work and haven't been able to find another way. Do not feel like moving back to STATA for this project just so that I can have a neater looking code.

CodePudding user response:

I would suggest using reformulate. Here is a reproducible example using the mtcars dataset.

controls <- c("disp", "drat")
model <- lm(reformulate(c("wt", controls), response = "mpg"), data = mtcars)

The key is to provide controls as a character vector of your (log-transformed) control variables.


So in your case:

controls <- c("log(mkt)", "mb", "lt", "ni")
model <- lm(reformulate(c("indep", controls), response = "dep"), data = df)
summary(model)
  • Related