rregressionfixest

feols with step-wise fixed effects not working with custom formula


I need to feed a custom formula to feols and have it estimate multiple models (various fixed effects), but this seems to break the sw() function. Compare:

library(fixest)
feols(mpg ~ disp | sw(gear, gear + carb), data = mtcars)

(That works.) This does not work:

feols(as.formula("mpg ~ disp") | sw(gear, gear + carb), data = mtcars) 

Why? And how can I get that structure of formula input to work?


Solution

  • We may need paste to paste the strings togeher

    library(fixest)
    feols(as.formula(paste("mpg ~ disp", "sw(gear, gear + carb)", 
         sep = "|")), data = mtcars)
    

    -output

    Standard-errors: Clustered (gear) 
    Fixed-effects: gear
          Estimate Std. Error  t value Pr(>|t|) 
    disp -0.040774   0.015421 -2.64404  0.11821 
    ---
    Fixed-effects: gear + carb
          Estimate Std. Error  t value Pr(>|t|) 
    disp -0.018388   0.016009 -1.14861  0.36955 
    

    which gives same as

    > feols(mpg ~ disp | sw(gear, gear + carb), data = mtcars)
    Standard-errors: Clustered (gear) 
    Fixed-effects: gear
          Estimate Std. Error  t value Pr(>|t|) 
    disp -0.040774   0.015421 -2.64404  0.11821 
    ---
    Fixed-effects: gear + carb
          Estimate Std. Error  t value Pr(>|t|) 
    disp -0.018388   0.016009 -1.14861  0.36955 
    

    NOTE: paste is more efficient compared to reformulate. If it is a single expression, formula can be used an expression i.e.

    fmla <- mpg ~ disp | sw(gear, gear + carb)
    > feols(fmla, data = mtcars)
    Standard-errors: Clustered (gear) 
    Fixed-effects: gear
          Estimate Std. Error  t value Pr(>|t|) 
    disp -0.040774   0.015421 -2.64404  0.11821 
    ---
    Fixed-effects: gear + carb
          Estimate Std. Error  t value Pr(>|t|) 
    disp -0.018388   0.016009 -1.14861  0.36955