fit_sklearn()
Fit a sklearn model with output that is compatible with pymarginaleffects.
This function streamlines the process of fitting sklearn models by:
- Parsing the formula
- Handling missing values
- Creating model matrices
- 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) sklearn model class (e.g., LinearRegression, LogisticRegression)
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.
Returns
(ModelSklearn) A fitted model wrapped in the ModelSklearn class for compatibility with marginaleffects.
Examples
from sklearn.linear_model import LinearRegression
from marginaleffects import *
= get_dataset()
data
= fit_sklearn(
model ="outcome ~ distance + incentive",
formula=data,
data=LinearRegression,
engine
)
predictions(model)
Notes
The fitted model includes additional attributes:
data
: The processed data after listwise deletionformula
: The original formula stringformula_engine
: Set to “sklearn”model
: The fitted sklearn model object