sklmer.LmerRegressor

class sklmer.LmerRegressor(formula, X_cols, predict_rfx=False, family='gaussian', fit_kwargs={})[source]

A regressor that wraps pymer4’s lme4 implementation.

This regressor requires a formula be defined in LME4’s style, see pymer4’s cheatsheet: http://eshinjolly.com/pymer4/rfx_cheatsheet.html

Parameters
formulastr

Lmer formatted formula string.

X_colslist

List of the names of the X columns.

predict_rfx: bool, default=’False’

Whether or not the predict method should use random effects in the prediction.

family: str, default=’gausian’

What family of distributions to use for the link function for the generalized model.

fit_kwargs: dict, defalut=’{}’

Dictionary of options to pass to lmer fit. See http://eshinjolly.com/pymer4/api.html

__init__(self, formula, X_cols, predict_rfx=False, family='gaussian', fit_kwargs={})[source]

Initialize self. See help(type(self)) for accurate signature.

fit(self, X=None, y=None, data=None)[source]

Fit the specified mixed effects model.

Parameters
Xarray-like, shape (n_samples, n_features)

The training input samples.

yarray-like, shape (n_samples,)

The target values (class labels in classification, real numbers in regression).

data: pandas.DataFrame

Data can also be passed to fit as a dataframe.

Returns
selfobject

Returns self.

get_params(self, deep=True)

Get parameters for this estimator.

Parameters
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsmapping of string to any

Parameter names mapped to their values.

predict(self, X=None, data=None, **kwargs)[source]

Predict based on the fitted mixed effects model.

Will use random effects if the estimators predict_rfx attribute is true.

Parameters
Xarray-like, shape (n_samples, n_features)

The training input samples.

data: pandas.DataFrame

Data can also be passed as a dataframe.

**kwargs:

Passed through to the pymer4.Lmer.predict method

Returns
yndarray, shape (n_samples,)

Returns predicted values

score(self, X, y, sample_weight=None)

Return the coefficient of determination R^2 of the prediction.

The coefficient R^2 is defined as (1 - u/v), where u is the residual sum of squares ((y_true - y_pred) ** 2).sum() and v is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a R^2 score of 0.0.

Parameters
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead, shape = (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns
scorefloat

R^2 of self.predict(X) wrt. y.

Notes

The R2 score used when calling score on a regressor will use multioutput='uniform_average' from version 0.23 to keep consistent with r2_score(). This will influence the score method of all the multioutput regressors (except for MultiOutputRegressor). To specify the default value manually and avoid the warning, please either call r2_score() directly or make a custom scorer with make_scorer() (the built-in scorer 'r2' uses multioutput='uniform_average').

set_params(self, **params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters
**paramsdict

Estimator parameters.

Returns
selfobject

Estimator instance.

Examples using sklmer.LmerRegressor