Closed s3alfisc closed 3 weeks ago
@all-contributors please add @marcandre259 for bug
@s3alfisc
I've put up a pull request to add @marcandre259! :tada:
fixest
produces different values for sumFE
than PyFixest
:
fit = pf.feols(
fml = "l_homicide ~ cdl | state + year",
data = data,
weights = "popwt"
)
fit.fixef()
fit._sumFE[0:5]
# array([1.96134687, 1.984755 , 1.96358787, 2.00897643, 2.00393692])
fit = feols(
fml = l_homicide ~ cdl | state + year,
data = py$data,
weights = ~popwt
)
fit$sumFE[1:5]
# [1] 1.979555 2.000740 1.989869 2.009749 1.984734
In all likelihood, the bug hence stems from the fixef()
method, which does not seem to take weights into account at all.
In particular, the least squares problem solved via lsqr
does not deal with weights: https://github.com/py-econometrics/pyfixest/blob/5250fc32d5a3fdff10eca2980ab1890d7e537310/pyfixest/estimation/feols_.py#L1535
Hi @marcandre259 & also @leostimpfle , I found the bug:
For a regression without weights, we are fitting a regression model of the following form:
$$ [ Y = \begin{bmatrix} X & D \end{bmatrix} \begin{bmatrix} \beta \ \alpha \end{bmatrix}^{T} + \epsilon ] $$
After fitting $\hat{\beta}$ via FWL, we have $\hat{\beta}$.
We can then solve for $\hat{\alpha}$ via the a sparse solver in lsqr
.
$$ [ D'D\hat{\alpha} = D'(Y - X\hat{\beta}). ] $$
If we have weights, we need to multiply D, X and Y with the standard $\sqrt(weights)$, which we simply did not do!
All of this (minus the part about weights) is nicely explained in the lfe vignette: link.
I'll prepare the PR for this now + add some tests.
@leostimpfle, this is also what we have to use for retrieving the alphas in the Poisson class (with the correct weights from the Poisson fit), as the strategy to fit GLMs in PyFixest is to implement an iterated weighted least squares algo (and therefore, the link function does not matter). For the same reason, Fepois
can inherit the vcov class from Feols
.
For WLS estimation with newdata argument provided, the output of predict is incorrect.
For all other cases, it matched
fixest
.See examples below:
No newdata, no weights
No newdata, weights
newdata, no weights
newdata, weights
@all-contributors please add @marcandre259 for bug