0

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?

Erin
  • 37
  • 4
  • You might look at `margins::dydx()` which I think uses the delta method for marginal effects. I'm surprised that `deltaMethod()` doesn't work for `fixef` objects - they have `vcov()` and `coef()` methods IIRC. Maybe you should tell us what the error is. – dash2 Jun 15 '22 at 14:32
  • Thanks for your comment. Included the error code in the main post now. – Erin Jun 15 '22 at 14:38
  • When I try `margins::dydx()` I get the following error: `Error in predict.fixest(model, newdata = data, type = type, se.fit = FALSE, : The variable producer is absent from the 'newdata' but is needed for prediction (it is a fixed-effect variable).` – Erin Jun 15 '22 at 14:45
  • And is that error correct? – dash2 Jun 15 '22 at 15:00
  • It's hard to see what's going on. I don't indeed see "b1" and "b2" in your model, so maybe that's why there's an error? Perhaps a [mcve] would help. – dash2 Jun 15 '22 at 15:01
  • Just to follow up on @g-grothendieck's answer below: Please note that you actually have two separate issues here. The first is a (harmless, but annoying) warning about the "complete" argument, which was fixed in a recent `fixest` release. So please update the package and then try again. The second is related to the way you write the parameter names as per Gabor;'s answer. – Grant Jun 15 '22 at 16:43

2 Answers2

3

@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.)

Grant
  • 1,536
  • 13
  • 25
2

In the absence of a reproducible example we will use the built-in CO2 data frame. (In the future please provide a reproducible example that can be used in answers -- see the info at the top of the tag page.)

1) The default method of deltaMethod does not support the parameterNames argument so use the original names.

library(fixest)
library(car)

fm <- feols(conc ~ uptake + Treatment | Type, CO2, vcov = "hetero")

deltaMethod(fm, "(4 - uptake)/(2 *  Treatmentchilled)")
##                                      Estimate        SE     2.5 %  97.5 %
## (4 - uptake)/(2 * Treatmentchilled) -0.060780  0.018446 -0.096934 -0.0246

2) Alternately it can work with just the coefficients and variance matrix so try this:

co <- setNames(coef(fm), c("b1", "b2"))
deltaMethod(co, "(4 - b1)/(2*b2)", vcov(fm))
##                    Estimate        SE     2.5 %  97.5 %
## (4 - b1)/(2 * b2) -0.060780  0.018446 -0.096934 -0.0246
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341