Home > Software engineering >  Making function to repeat felm model for multiple models using lapply
Making function to repeat felm model for multiple models using lapply

Time:07-31

I am trying to run a series of models in r based on the same structure but a slightly shifted set of variables. I am using an felm model from the lfe package with multiple covariates, fixed effects and a clustered variable.

My basic model is the following:

model <- felm(formula = outcome1 ~ mainx cov1 cov2 cov3 | fe |0| clustercov, data = df)

This works, and allows me to plot the residuals just fine. However, I now need to run this for a series of alternative outcome variables: outcome1, outcome2, outcome3, etc. These are all similarly constructed variables (numeric, etc.) but their values are different. All of the other variables remain the same, with the exception of this outcome variable.

I have some experience using lapply and function() but I am unable to determine how to insert the felm model into this formatting so that I can make the model repeat for different outcome variables. Is this possible with felm, and how should I go about structuring this?

CodePudding user response:

It tends to be easiest to construct the formula as a string and the to coerce it to formula.

paste0("outcome", 1:4, " ~ mainx   cov1   cov2   cov3 | fe |0| clustercov") |> 
  lapply(as.formula)
#> [[1]]
#> outcome1 ~ mainx   cov1   cov2   cov3 | fe | 0 | clustercov
#> <environment: 0x5597656aaeb8>
#> 
#> [[2]]
#> outcome2 ~ mainx   cov1   cov2   cov3 | fe | 0 | clustercov
#> <environment: 0x5597656aaeb8>
#> 
#> [[3]]
#> outcome3 ~ mainx   cov1   cov2   cov3 | fe | 0 | clustercov
#> <environment: 0x5597656aaeb8>
#> 
#> [[4]]
#> outcome4 ~ mainx   cov1   cov2   cov3 | fe | 0 | clustercov
#> <environment: 0x5597656aaeb8>

Created on 2022-07-30 by the reprex package (v2.0.1)

You can also put the felm() function right into the lapply():

paste0("outcome", 1:4, " ~ mainx   cov1   cov2   cov3 | fe |0| clustercov") |> 
  lapply(\(x) felm(as.formula(x),  data = df))
  • Related