lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
361 stars 59 forks source link

How to insert the name of a variable (in character form) into a formula? #427

Closed 95goo closed 11 months ago

95goo commented 11 months ago

hi this is my code:

reg_table <- function(outcome) {
reg_1 <- feols(data = cat, !!sym(outcome) ~ treated)
reg_2 <- feols(data = cat, !!sym(outcome) ~ treated | color) }

export <- reg_table("happiness")
export <- reg_table("income")

I get the following error:

error in feols(data = cat, !!sym(outcome) ~ treated) :
the variable outcome is in the LHS of the formula but not in the data set

I do 100% have the "happiness" and "income" variables in the"cat" data frame. The code works without !!sym(outcome). My goal is: within the function, !!sym(outcome) to dynamically specify the dependent variable in the regression formula. I have to do this for many dependent variables and have more models than I have shared above, so this seems to be the most efficient way to do this. Any advice here would be so helpful.

lrberge commented 11 months ago

Hi. The error is normal. You should construct the proper fomrula before sending it to feols.

BTW your use case is covered with the (native) dot square bracket operator to interpolate variables into formulas: .[outcome] ~ treated

library(fixest)
y = "Petal.Length"
feols(.[y] ~ Sepal.Length, iris)
#> OLS estimation, Dep. Var.: Petal.Length
#> Observations: 150
#> Standard-errors: IID
#>              Estimate Std. Error  t value  Pr(>|t|)
#> (Intercept)  -7.10144   0.506662 -14.0161 < 2.2e-16 ***
#> Sepal.Length  1.85843   0.085856  21.6460 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 0.86201   Adj. R2: 0.758333

See https://lrberge.github.io/fixest/articles/fixest_walkthrough.html#the-dot-square-bracket-operator.

Finally, please try to provide a fully reproducible example using dummy data (like in my example). It helps a lot to pinpoint the problem.