Closed poliquin closed 2 months ago
Ooof. I will look into this when I find some time, but here's some background for now.
Some days I regret introducing hypotheses()
, and I would strongly discourage calling it sequentially like you are trying to do. That involves many lots of catching and modifying calls, quosures, and esoteric things. Doing this in a "safe" way would require a lot of behind the scenes tracking and a ton of code complexity.
The hypotheses()
function will always work in the future, but please be aware that I may eventually raise an error when users try to call it twice on an object.
In my own personal work, always try to define my hypotheses directly in the hypothesis
argument, rather than as a second call. I recommend taking a look at the specification of complex hypotheses using functions here (requires the dev version of marginaleffects
:
https://marginaleffects.com/vignettes/hypothesis.html#functions
BTW, have you looked at the etwfe
package? It uses marginaleffects
under the hood to implement the Wooldridge (2021) strategy:
Thanks for the quick reply! Also, I wasn't aware of etwfe
, so I'm glad I mentioned what I was doing. The package implements exactly the test I need. And I'll try to avoid multiple calls to hypotheses
...
Great!
FYI, the new version on Github now raises an error when a user tries to call hypotheses()
twice.
Description of the bug
I am trying to use multiple calls to
hypotheses
to test for differences in treatment effects across groups and cohorts, and to calculate averages of these effects. The problem is basically this:slopes
to get marginal effects. (In my example, treatment effects by two covariates with two levels: sex and cohort.)hypotheses
with the object returned by Step 1 and a matrix to estimate/test two linear combinations of the marginal effects. (In my example, whether the treatment effect varies in one of the covariates: sex differences in treatment effects within cohorts.)hypotheses
again on the object returned by Step 2 to estimate/test the average of the estimates from step 2. (In my example, the simple average of the cohort differences in treatment effects by sex.)Step 3 fails because the second call to
hypotheses
expects a hypothesis with length equal to the number of terms returned byslopes
in Step 1 (4 in my example). This is unexpected because Step 3 is being called with the object from Step 2, which has fewer dimensions (it is length 2).Example
Created on 2024-04-30 with reprex v2.0.2
Session info
Further Comments
I realize I can get around this in various ways, but I found the behavior unexpected since I'm passing an object with two terms to the second call to
hypotheses
. The above example is simple, but my true use case is estimating expanded TWFE difference-in-differences models with cohort/time heterogeneity (Wooldridge 2021) in which there are a lot of coefficients. I then want to use a bunch of sequential calls tohypotheses
to generate several quantities of interest on the way to calculating an ATT. For example, going from model estimates to cohort-by-covariate-specific treatment effects, and then using the cohort-specific effects in another call to hypotheses to look for differences in effects within cohort, and then another call to average across cohorts and eventually arrive at a single estimate of the ATT. I would love to accomplish this with a simple series of calls tohypotheses
: