grantmcdermott / etwfe

Extended two-way fixed effects
https://grantmcdermott.com/etwfe/
Other
50 stars 11 forks source link

Are controls required? #6

Closed sbh4th closed 1 year ago

sbh4th commented 1 year ago

So thrilled for this and exactly what I was looking for yesterday! I'm wondering whether controls are required, as I can't seem to get your example to run without specifying controls (I'm starting very simply with my own analysis). All is well with your example:

> library(etwfe)
> data("mpdta", package = "did")
> mod = 
+     etwfe(
+         fml  = lemp ~ lpop, # outcome ~ controls
+         tvar = year,        # time variable
+         gvar = first.treat, gref=0, # group variable (with bespoke ref. level)
+         data = mpdta,       # dataset
+         vcov = ~countyreal  # vcov adjustment (here: clustered)
+     )
> mod
OLS estimation, Dep. Var.: lemp
Observations: 2,500 
Fixed-effects: first.treat: 4,  year: 5
Varying slopes: lpop (first.treat: 4),  lpop (year: 5)
Standard-errors: Clustered (countyreal) 
                                              Estimate Std. Error   t value   Pr(>|t|)    
.Dtreat:first.treat::2004:year::2004         -0.021248   0.021728 -0.977890 3.2860e-01    
.Dtreat:first.treat::2004:year::2005         -0.081850   0.027375 -2.989963 2.9279e-03 ** 
.Dtreat:first.treat::2004:year::2006         -0.137870   0.030795 -4.477097 9.3851e-06 ***
.Dtreat:first.treat::2004:year::2007         -0.109539   0.032322 -3.389024 7.5694e-04 ***
.Dtreat:first.treat::2006:year::2006          0.002537   0.018883  0.134344 8.9318e-01    
.Dtreat:first.treat::2006:year::2007         -0.045093   0.021987 -2.050907 4.0798e-02 *  
.Dtreat:first.treat::2007:year::2007         -0.045955   0.017975 -2.556568 1.0866e-02 *  
.Dtreat:first.treat::2004:year::2004:lpop_dm  0.004628   0.017584  0.263184 7.9252e-01    
... 6 coefficients remaining (display them with summary() or use argument n)
... 10 variables were removed because of collinearity (.Dtreat:first.treat::2006:year::2004, .Dtreat:first.treat::2006:year::2005 and 8 others [full set in $collin.var])
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 0.537131     Adj. R2: 0.87167 
                 Within R2: 8.449e-4

If I drop the controls altogether, it throws an error:

> mod =   
  etwfe(  
         fml  = lemp, # outcome ~ controls  
         tvar = year,        # time variable  
         gvar = first.treat, gref=0, # group variable (with bespoke ref. level)  
         data = mpdta,       # dataset  
         vcov = ~countyreal  # vcov adjustment (here: clustered)  
     )  
Error in etwfe(fml = lemp, tvar = year, gvar = first.treat, gref = 0,  : 
  object 'lemp' not found  

I also tried a few other iterations, e.g., fml = lemp ~ or fml = lemp ~ 1 but these also generate errors.

I know this is all in development, and am really thankful for the resource. Thanks again.

grantmcdermott commented 1 year ago

Hi Sam,

You were very close. You need a zero: y ~ 0

I believe this is documented in the ?etwfe helpfile (see the fml argument). But your issue makes me think that I should maybe add y ~ 1 as a second option. Let me mull it over for a bit.

Thanks very much for the kind words and please keep the issues coming if you run into anything else.

sbh4th commented 1 year ago

Dear Grant, thanks very much for the quick response. As soon as I commented I figured I had missed something obvious (like, RTFM!). I'll plead recently moving from Stata to R as a lame excuse, but in any case, this worked perfectly and clears it up. Many thanks!