Skip to main content

Travis AppVeyor Codecov CircleCI ReadTheDocs

sklearn-lmer - Scikit-learn estimator wrappers for pymer4 wrapped LME4 mixed effects models

sklearn-lmer is a simple package to wrap the convienience of pymer4’s lme4 wrapping in a mostly sklearn compatible regressor class.

Refer to the documentation for examples and api.

Linear mixed effects regressions

Linear mixed effects regressions are great, but if you’re here, you probably already agree. You can find more infomration about them elsewhere, the links lme4 aren’t a bad place to start.

Installation

Mixing r and python used to be a bit more fraught, but rpy2 and conda seem to be working together better these days. To install first get a conda environment with the dependencies:

>>> conda create -n sklmer -c conda-forge numpy scipy rpy2 r-lme4 r-lmertest r-lsmeans tzlocal

Then pip install sklearn-lmer:

>>> pip install sklearn-lmer

Usage

It can be imported as:

>>> from sklmer import LmerRegressor

Now the mostly part of that compatiblity is that init does have two required paramters: a formula and the names of the columns holding independent variables and grouping variables (I’ve called this parameter X_cols even though it is more than just X). When I use this I’ve got my data in a dataframe and just pass dataframe.columns with X_cols like so

>>> df = pd.read_csv(os.path.join(get_resource_path(),'sample_data.csv'))
>>> lreg = LmerRegressor('DV ~ IV2 + (IV2|Group)', X_cols=df.columns)

If you want the best compatibility with sklearn it probably makes sense to split out the dataframe into X, y, and group variables, though since you’ve defined a formula it’s ok if the y and group columns are in X

>>> X = df.values
>>> y = df.DV.values
>>> groups = df.Group.values

Once you’ve done that, it seems to work fine with other sklearn tools, like cross_val_score

>>> logo = LeaveOneGroupOut()
>>> cross_val_score(lreg, X=X, y=y, cv=logo.split(X, groups=groups), scoring='neg_mean_squared_error')

Metadata

Release files for sklearn-lmer 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sklearn-lmer 0.0.5
File Size Uploaded
sklearn-lmer-0.0.5.tar.gz 21.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sklearn-lmer 0.0.5
File Interpreter ABI Platform
sklearn_lmer-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 30.9 kB

Release files / sklearn-lmer-0.0.5.tar.gz

Download URL sklearn-lmer-0.0.5.tar.gz
Size 21.6 kB
Tags Source
SHA-256 checksum
How to use checksums
414143e54ad192b353f7eb3df9b40b107d897a13576e62beac5ae49f69940b4e
BLAKE2b-256 checksum
How to use checksums
f5209b2a530faae05d92e001e552e575e3d1c5a006692000a188a09db1fd88c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191201 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.3

Release files / sklearn_lmer-0.0.5-py3-none-any.whl

Download URL sklearn_lmer-0.0.5-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44d3c72df7c0ff5c2908cb450910c0e66dc9df8fbe55581d9ac365188632879c
BLAKE2b-256 checksum
How to use checksums
6ef051b320a485d404d8e329496322439e8aa4c748aaab445c9fee4ad3ce88b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191201 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page