fit_sklearn()

Fit a sklearn model with output that is compatible with pymarginaleffects.

This function streamlines the process of fitting sklearn 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) 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 *

data = get_dataset()

model = fit_sklearn(
    formula="outcome ~ distance + incentive",
    data=data,
    engine=LinearRegression,
)

predictions(model)

Notes

The fitted model includes additional attributes:

  • data: The processed data after listwise deletion
  • formula: The original formula string
  • formula_engine: Set to “sklearn”
  • model: The fitted sklearn model object