sergiocorreia / ivreghdfe

Run IV/2SLS with many levels of fixed effects (i.e. ivreg2+reghdfe)
MIT License
82 stars 29 forks source link

Incorrect coefficient with weights #38

Closed mcaceresb closed 2 years ago

mcaceresb commented 2 years ago

It seems that ivreghdfe does not use weights when applying the HDFE transformation? Here is an example:

which ivreghdfe
which reghdfe
which ftools
set seed 42

qui {
    clear
    set obs 10000
    gen g   = ceil(exp(rnormal() * 3))
    gen p   = ceil(runiform() * 10)
    gen e   = rnormal()
    gen z   = rnormal() * 0.5 * (p > 8)
    egen mz = mean(z), by(g p)
    gen x   = 0.2 * mz + e / 4
    gen y   = (2 * rnormal() + 5 * ((g / 50) - 0.5) + 3 * x + e) > 0
}

qui ivreghdfe y (x = mz), absorb(g)
disp %-24s "full sample", _b[x]
gen obs = 1
collapse (mean) y x mz (sum) obs, by(g p)
qui ivreghdfe y (x = mz) [fw = obs], absorb(g)
disp %-24s "collapsed", _b[x]

qui {
    egen dy  = mean(y),  by(g)
    egen dx  = mean(x),  by(g)
    egen dmz = mean(mz), by(g)
    replace dy  = y  - dy
    replace dx  = x  - dx
    replace dmz = mz - dmz
    gstats transform (demean) y x mz [fw = obs], by(g) replace
}

qui ivregress 2sls dy (dx = dmz) [fw = obs], noconstant
disp %-24s "de-mean no weights", _b[dx]
qui ivregress 2sls y  (x  =  mz) [fw = obs], noconstant
disp %-24s "de-mean with weights", _b[x]

The output:


/homes/nber/caceres-dua54762/ado/plus/i/ivreghdfe.ado
*! ivreghdfe 1.1.0  25Feb2021
*! ivreg2 4.1.11  22Nov2019
*! authors cfb & mes
*! see end of file for version comments

/homes/nber/caceres-dua54762/ado/plus/r/reghdfe.ado
*! version 6.12.1 27June2021

/homes/nber/caceres-dua54762/ado/plus/f/ftools.ado
*! version 2.48.0 29mar2021

full sample              -.12088464
collapsed                -.08857211
de-mean no weights       -.0885721
de-mean with weights     -.12088463
sergiocorreia commented 2 years ago

Thanks for spotting the bug! It turns out that when I recoded ivreghdfe due to reghdfe 6 update, I forgot to pass by the weight locals. Should be updated now on v1.2:

sysuse auto, clear
reghdfe price weight (length=displacement) [fw=trunk], a( foreign) version(3) // test against old version of reghdfe
ivreghdfe price weight (length=displacement) [fw=trunk], a(foreign) // version 1.2 20oct2021
which ivreghdfe
mcaceresb commented 2 years ago

@sergiocorreia Just tried it and it seems to have done the trick; thanks!

jmcastro2109 commented 2 years ago

Hi,

where can I find v1.2 ?

miketcassidy commented 2 years ago

I'm finding that the same issue persists with the most recent version: ivreghdfe 1.1.1 14Dec2021.

In particular, ivreghdfe produces the correct coefficients in two situations: (1) with fixed effects in absorb, but without weights; and (2) without fixed effects in absorb (i.e., just a constant), but with weights.

However, the coefficients are incorrect with both absorbed FE's and weights.

Any ideas?

tobiasrenkin commented 1 year ago

I can confirm that 1.2 fixes this. I think this bug (and the fix) need to be mentioned in the changelog of 1.2, so that people are aware that older versions of ivreghdfe do not support weights (except in the irrelevant case where there are no FE and one may just directly use ivreg2 instead).