简体   繁体   中英

Function to run regression model with different covariates, outcome variables, and fixed effects

I want to write a function that to modify a regression model efficiently, taking arguments for (at minimum) the outcome variable, treatment variables, and fixes effects. It would be great to go even deeper, like modifying weights. For instance, I often want to write a model with the same covariates for many different outcomes. If I want to modify the covariate set, I'd like to do that in one line rather than going through each regression individually.

library(fixest)
dataset <- mtcars
covs_1 <- c("cyl", "disp","hp")
depvar_1 <- c("mpg")
treatment_1 <- c("vs")

# function to run models based on dep variable and covariate set
runmod <- function(depvar, treatment,covs) {
  feols(as.formula(paste(c(depvar, " ~ ", treatment, paste(covs, collapse = " + ")),
        data = data, cluster = "state")))
}
runmod(depvar_1, treatment_1, covs_1)

Note that fixest natively supports multiple estimations, and you can set up formula macros to compactly add your covariates:

library(fixest)

# ..cov: macro stacking all the covariates
setFixest_fml(..cov = ~ cyl + disp + hp)

# multiple estimation:
#  c(dep1, dep2): multiple dep. vars
# sw(var1, var2): multiple expl. vars (sw means step wise)
multiest = feols(c(mpg, qsec) ~ sw(vs, am) + ..cov, mtcars)
etable(multiest)
#>                           model 1           model 2            model 3            model 4
#> Dependent Var.:               mpg               mpg               qsec               qsec
#>                                                                                          
#> (Intercept)      36.01*** (4.466)  30.48*** (2.866)   17.77*** (1.441)  24.41*** (0.9152)
#> vs                -0.9692 (1.918)                     2.272** (0.6190)                   
#> cyl               -1.446 (0.9167)  -0.8345 (0.7571)   -0.0530 (0.2958) -0.8165** (0.2418)
#> disp            -0.0182. (0.0106)  -0.0077 (0.0107)   0.0088* (0.0034)    0.0031 (0.0034)
#> hp               -0.0159 (0.0151) -0.0330* (0.0156) -0.0178** (0.0049)  -0.0091. (0.0050)
#> am                                   3.445* (1.454)                    -2.199*** (0.4643)
#> _______________ _________________ _________________ __________________ __________________
#> S.E. type                Standard          Standard           Standard           Standard
#> Observations                   32                32                 32                 32
#> R2                        0.77006           0.80785            0.72765            0.77703
#> Adj. R2                   0.73600           0.77939            0.68731            0.74400

See the documentation on multiple estimations here and on formula macros there .

I hope this works:

library(fixest)
dataset <- mtcars
covs_1 <- c("cyl", "disp", "hp")
depvar_1 <- c("mpg")
treatment_1 <- c("vs")
# function to run models based on dep variable and covariate set
runmod <- function(depvar, treatment, covs) {
  
  formula <- paste(depvar,
                   " ~ ",
                   treatment,
                   " | ",
                   paste(covs, collapse = " + "))
  
  feols(as.formula(formula), dataset, cluster = "am")
}
runmod(depvar_1, treatment_1, covs_1)

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