rregressionmarginal-effectsfixest

Delta method for fixed effects regression using feols function


I have the following regression in R using the 'fixest' package. Yields are a function of N, N^2, P, K and S with producer by year fixed effects.

yield<- feols(yield ~ N + N_square + P + K + S |producer*year, data=data, se="hetero")

I need to use the delta method from the 'car' package to estimate the optimal rate of N and obtain the standard error. In the below example, using the marginal effect of nitrogen from my regression, I am finding the optimal rate of N at an input:output price ratio = 4.

deltaMethod(yield, "(4 - b1)/(2*b2)", parameterNames= paste("b", 0:2, sep=""))

My issue is I am unable to run the deltaMethod with the feols regression. I am given the following error:

Warning: In vcov.fixest(object, complete = FALSE):'complete' is not a valid argument of
function vcov.fixest (fyi, some of its
main arguments are 'vcov' and 'ssc').
Error in eval(g., envir) : object 'b1' not found

The deltaMethod works with lm functions. This is an issue for me, as I cannot run my regression instead as an lm function with the fixed effects as factors. This is because with my chosen data set and fixed effect variables it is extremely slow to run.

Is there any alternatives to the deltaMethod function that works with feols regressions?


Solution

  • @g-grothendieck's answer covers the main issue. (Which is to say that car::deltaMethod does work with fixest objects; you just have to specify the coefficient names in a particular way.) I'd also recommend updating your version of fixest, since you appear to be using an old release.

    But for posterity, let me quickly tackle the subsidiary question:

    Is there any alternatives to the deltaMethod function that works with feols regressions?

    You can use the "hypothesis" functionality of the (excellent) marginaleffects package. Please note that this is a relatively new feature, so you'll need to install the development version of marginaleffects at the time of writing this comment.

    Here's an example that replicates Gabor's one above.

    library(fixest)
    fm <- feols(conc ~ uptake + Treatment | Type, CO2, vcov = "hetero")
    
    # remotes::install_github("vincentarelbundock/marginaleffects") # dev version
    library(marginaleffects)
    
    marginaleffects(
      fm,
      newdata = "mean",
      hypothesis = "(4 - uptake)/(2 *  Treatment) = 0"
      ) |>
      summary() ## optional
    
    #> Average marginal effects 
    #>         Term   Effect Std. Error z value   Pr(>|z|)    2.5 %   97.5 %
    #> 1 hypothesis -0.06078    0.01845  -3.295 0.00098423 -0.09693 -0.02463
    #> 
    #> Model type:  fixest 
    #> Prediction type:  response 
    

    PS. For anyone else reading this, marginaleffects is something of a spiritual successor to margins, but is basically superior in every way (speed, model coverage, etc.)