dependent variable in R FEOLS function (fixest)

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.

Something along these lines

make_form1 <- function(x) formula(x ~ drat)
vars <- mtcars[2:9]
lf <- sapply(vars,make_form1) 
lm(lf[1][[1]],mtcars)
#> 
#> Call:
#> lm(formula = lf[1][[1]], data = mtcars)
#> 
#> Coefficients:
#> (Intercept)         drat  
#>      14.596       -2.338

Created on 2023-07-18 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.