analyze_predictions

source

analyze_predictions(result_or_model, newdata=None)

Return fitted values from a model (and residuals/actuals on the estimation sample).

Parameters

Name Type Description Default
result_or_model Any A fitted model or a result object carrying .models. required
newdata pd.DataFrame | None Optional data frame to predict on. When omitted, predictions are for the estimation sample and the frame also includes the actual outcome and the residual. None

Returns

Name Type Description
PredictionResult df with a predicted column (plus actual and residual in-sample).

Examples

Basic — in-sample fitted values, actuals, and residuals:

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(load_kuznets(), load_kuznets_data_def(), set_panel=True)
model = ex.analyze_regression_table(
    df,
    dvs="gini_regional",
    idvs=["log_gdp_pc", "log_gdp_pc_sq", "log_gdp_pc_cu"],
    feffects=["country"],
)
ex.analyze_predictions(model).df.head()
actual predicted residual
0 0.085611 0.095395 -0.009784
1 0.080064 0.124973 -0.044909
2 0.212049 0.197058 0.014991
3 0.221831 0.238076 -0.016245
4 0.261234 0.282897 -0.021663

Advanced — predict on new data (here a fresh slice of the panel):

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(load_kuznets(), load_kuznets_data_def(), set_panel=True)
model = ex.analyze_regression_table(
    df,
    dvs="gini_regional",
    idvs=["log_gdp_pc", "log_gdp_pc_sq", "log_gdp_pc_cu"],
    feffects=["country"],
)
newdata = df[df["year"] == df["year"].max()]
ex.analyze_predictions(model, newdata=newdata).df.head()
/home/runner/work/expdpy/expdpy/.pixi/envs/docs/lib/python3.12/site-packages/pyfixest/estimation/post_estimation/prediction.py:125: UserWarning: Data types of fixed effects ['country'] do not match the model data. This leads to possible mismatched keys in the fixed effect dictionary -> NaN predictions for new levels.
  warnings.warn(
predicted
0 0.385494
1 0.365067
2 0.298891
3 -0.028847
4 0.269609