py-econometrics / pyfixest

Fast High-Dimensional Fixed Effects Regression in Python following fixest-syntax
https://py-econometrics.github.io/pyfixest/
MIT License
176 stars 35 forks source link

Fepois `predict` with `newdata` #703

Open leostimpfle opened 1 week ago

leostimpfle commented 1 week ago

Closes #468

leostimpfle commented 1 week ago

predict still does not work with newdata and fixed effects (see #467). Something doesn't quite work in the fixef method inherited from feols. I'll investigate further.

codecov[bot] commented 1 week ago

Codecov Report

Attention: Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pyfixest/estimation/fepois_.py 0.00% 6 Missing :warning:
Flag Coverage Δ
core-tests 77.79% <0.00%> (+0.10%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
pyfixest/estimation/fepois_.py 86.79% <0.00%> (-0.42%) :arrow_down:
s3alfisc commented 1 week ago

If I change to the following

    def predict(
        self,
        newdata: Optional[DataFrameType] = None,
        atol: float = 1e-6,
        btol: float = 1e-6,
        type: PredictionType = "link",
    ) -> np.ndarray:

        #if newdata is not None and self._has_fixef:
        #    raise NotImplementedError()

        y_hat = super().predict(newdata=newdata, type=type, atol=atol, btol=btol)
        if type == "response":
            y_hat = np.exp(y_hat)
        return y_hat

predict works with newdata and fixed effects for type = "response":

%load_ext autoreload
%autoreload 2

import pyfixest as pf
data = pf.get_data(model = "Fepois")

fit = pf.fepois("Y ~ X1 | f1", data = data)
fit.predict(newdata = data.dropna(), type = "response")[0:5]
# array([2.7803393 , 2.81678101, 3.20245421, 3.3940836 , 3.38641651])
fit.predict(type = "response")[0:5]
# array([2.78032513, 2.81699512, 3.20381613, 3.39393412, 3.38457835])