简体   繁体   中英

Running lmer (Linear Mixed Effects Regression) in Python

I'd like to ask some questions about running lmer (Linear Mixed Effects Regression) models in Python.

Here are the two lines(or formulas) that I had run in the lme4 package(in R). Is there any way I could fit the models as below in Python?

  1. TEST1 <- score ~ p1 + p2 + p3 + (1|v1) + (1|v2), data = df, control = lmerControl(boundary.tol = 1e-4, calc.derivs = FALSE))
  2. TEST2 <- score ~ (1|v1) + (1|v2), data = df, control = lmerControl(boundary.tol = 1e-4, calc.derivs = FALSE))

If you aren't required to actually run the model in Python, you could call and run the LMER models in R directly from your Python environment.

You could do this through Rpy2 & rmagic or through Pymer4 . Both options allow you to use the lme4 package in R with the option to call them from a Python environment like jupyter notebooks.

I wrote a tutorial on how you could do this with examples that is available here: https://towardsdatascience.com/how-to-run-linear-mixed-effects-models-in-python-jupyter-notebooks-4f8079c4b589

As EJJ noted, there are implementations of LMER in Python such as in statsmodels and Tensorflow but they appear less intuitive to use than the above method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM