fit_statsmodels()
Fit a statsmodels model with output that is compatible with pymarginaleffects.
This function streamlines the process of fitting statsmodels models by: 1. Parsing the formula 2. Handling missing values 3. Creating model matrices 4. Fitting the model with specified options
Parameters
formula
: (str) Model formula
- Example: “outcome ~ distance + incentive”
data
: (pandas.DataFrame) Dataframe with the response variable and predictors.
engine
: (callable) statsmodels model class (e.g., OLS, Logit)
kwargs_engine
: (dict, default={}) Additional arguments passed to the model initialization.
- Example: {‘weights’: weights_array}
kwargs_fit
: (dict, default={}) Additional arguments passed to the model’s fit method.
- Example: {‘cov_type’: ‘HC3’}
Returns
(ModelStatsmodels) A fitted model wrapped in the ModelStatsmodels class for compatibility with marginaleffects.
Examples
from marginaleffects import fit_statsmodels, get_dataset, predictions, slopes, comparisons
import statsmodels.api as sm
= get_dataset()
data
# Model with robust standard errors {.unnumbered}
= fit_statsmodels(
model_robust ="outcome ~ distance + incentive",
formula=data,
data=sm.OLS,
engine={"cov_type": "HC3"}
kwargs_fit
)
print(predictions(model_robust))
print(slopes(model_robust))
print(comparisons(model_robust))
Notes
The fitted model includes additional attributes:
data
: The processed data after listwise deletionformula
: The original formula stringformula_engine
: Set to “statsmodels”model
: The fitted statsmodels model object