You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Convergence problem in documentation for MixLM #5546
I'm trying to learn mixed linear models in statmodels.
Was checking out this documentation that should be generated on stable build. In older versions, there was no problems, but now it raises convergence problems in some estimations.
I can follow up with more detail later, but here is a quick response to your question. I think it is helpful to discuss this by comparing to R.
I used the Python code below, which is a lightly modified version of what you posted above:
import statsmodels.api as sm
import statsmodels.formula.api as smf
data = sm.datasets.get_rdataset('dietox','geepack').data
data.to_csv("dietox.csv") # for comparison to R
model1 = smf.mixedlm('Weight ~ Time', data, groups=data["Pig"]) # Random Intercept
result1 = model1.fit()
model2 = sm.MixedLM.from_formula("Weight ~ Time", data, re_formula="Time", groups=data["Pig"]) # Random time-slope
result2 = model2.fit()
Here is the R code that I used for comparison:
library(lme4)
da = read.csv("dietox.csv")
model1 = lmer(Weight ~ Time + (1 | Pig), data=da)
model2 = lmer(Weight ~ Time + (Time | Pig), data=da)
For the first model, in statsmodels we get:
In [14]: result1.summary()
Out[14]:
<class 'statsmodels.iolib.summary2.Summary'>
"""
Mixed Linear Model Regression Results
========================================================
Model: MixedLM Dependent Variable: Weight
No. Observations: 861 Method: REML
No. Groups: 72 Scale: 11.3669
Min. group size: 11 Likelihood: -2404.7753
Max. group size: 12 Converged: Yes
Mean group size: 12.0
--------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
--------------------------------------------------------
Intercept 15.724 0.788 19.952 0.000 14.179 17.268
Time 6.943 0.033 207.939 0.000 6.877 7.008
Group Var 40.394 2.149
========================================================
In R we get:
Linear mixed model fit by REML ['lmerMod']
Formula: Weight ~ Time + (1 | Pig)
Data: da
REML criterion at convergence: 4809.6
Scaled residuals:
Min 1Q Median 3Q Max
-4.7118 -0.5696 -0.0943 0.4877 4.7732
Random effects:
Groups Name Variance Std.Dev.
Pig (Intercept) 40.39 6.356
Residual 11.37 3.371
Number of obs: 861, groups: Pig, 72
Fixed effects:
Estimate Std. Error t value
(Intercept) 15.72352 0.78805 19.95
Time 6.94251 0.03339 207.94
Correlation of Fixed Effects:
(Intr)
Time -0.275
Note that -2 times the likelihood reported by Statsmodels gives the same number as the "REML criterion" in R. This strongly suggests that the two fitting procedures are converging to the same solution (which is also evident from looking at the parameter estimates, which are essentially identical).
For the second model, in Statsmodels we get (with convergence warnings):
In [15]: result2.summary()
Out[15]:
<class 'statsmodels.iolib.summary2.Summary'>
"""
Mixed Linear Model Regression Results
===========================================================
Model: MixedLM Dependent Variable: Weight
No. Observations: 861 Method: REML
No. Groups: 72 Scale: 5.7891
Min. group size: 11 Likelihood: -2220.3890
Max. group size: 12 Converged: No
Mean group size: 12.0
-----------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
-----------------------------------------------------------
Intercept 15.739 0.672 23.438 0.000 14.423 17.055
Time 6.939 0.085 81.326 0.000 6.772 7.106
Group Var 30.266 4.271
Group x Time Cov 0.746 0.304
Time Var 0.483 0.046
===========================================================
"""
In R we get (with no warnings):
> summary(model2)
Linear mixed model fit by REML ['lmerMod']
Formula: Weight ~ Time + (Time | Pig)
Data: da
REML criterion at convergence: 4434.1
Scaled residuals:
Min 1Q Median 3Q Max
-6.4286 -0.5529 -0.0416 0.4841 3.5624
Random effects:
Groups Name Variance Std.Dev. Corr
Pig (Intercept) 19.4935 4.415
Time 0.4161 0.645 0.10
Residual 6.0375 2.457
Number of obs: 861, groups: Pig, 72
Fixed effects:
Estimate Std. Error t value
(Intercept) 15.73865 0.55013 28.61
Time 6.93901 0.07983 86.93
Correlation of Fixed Effects:
(Intr)
Time 0.005
The fixed effects parameter estimates are nearly the same, but the variance parameter estimates are different. Statsmodels gives the worse objective function criterion (2 * 2220.3890 = 4440.778 > 4434.1), which suggests that the R solution is closer to what we want (the RMLE).
Update: Use method='cg' in the statsmodels fit and you will get the same result as R with no warnings:
In [23]: result2 = model2.fit(method="cg")
In [24]: result2.summary()
Out[24]:
<class 'statsmodels.iolib.summary2.Summary'>
"""
Mixed Linear Model Regression Results
===========================================================
Model: MixedLM Dependent Variable: Weight
No. Observations: 861 Method: REML
No. Groups: 72 Scale: 6.0374
Min. group size: 11 Likelihood: -2217.0475
Max. group size: 12 Converged: Yes
Mean group size: 12.0
-----------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
-----------------------------------------------------------
Intercept 15.739 0.550 28.608 0.000 14.660 16.817
Time 6.939 0.080 86.927 0.000 6.783 7.095
Group Var 19.496 1.560
Group x Time Cov 0.294 0.153
Time Var 0.416 0.033
===========================================================
"""
Activity
kshedden commentedon Mar 26, 2019
I can follow up with more detail later, but here is a quick response to your question. I think it is helpful to discuss this by comparing to R.
I used the Python code below, which is a lightly modified version of what you posted above:
Here is the R code that I used for comparison:
For the first model, in statsmodels we get:
In R we get:
Note that -2 times the likelihood reported by Statsmodels gives the same number as the "REML criterion" in R. This strongly suggests that the two fitting procedures are converging to the same solution (which is also evident from looking at the parameter estimates, which are essentially identical).
For the second model, in Statsmodels we get (with convergence warnings):
In R we get (with no warnings):
The fixed effects parameter estimates are nearly the same, but the variance parameter estimates are different. Statsmodels gives the worse objective function criterion (2 * 2220.3890 = 4440.778 > 4434.1), which suggests that the R solution is closer to what we want (the RMLE).
Update: Use method='cg' in the statsmodels fit and you will get the same result as R with no warnings:
kshedden commentedon Mar 26, 2019
The current default approach in master is to try bfgs, then lbfgs:
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/regression/mixed_linear_model.py#L1996
Not much study went into to setting this default. We could add cg somewhere.
bashtage commentedon May 7, 2019
This has been added, so closing.