Repository navigation
Description
Describe the bug
I would like to compute a regularized mixed model. After using .fit_regularized() the .summary() method of the results object returns:
Traceback (most recent call last):
Cell In[36], line 2
result.summary()
File ~/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/statsmodels/regression/mixed_linear_model.py:2723 in summary
sdf[jj, 0] = self.cov_re[i, j]
File ~/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/pandas/core/frame.py:3761 in __getitem__
indexer = self.columns.get_loc(key)
File ~/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/pandas/core/indexes/base.py:3654 in get_loc
raise KeyError(key) from err
KeyError: (0, 0)
Code Sample, a copy-pastable example if possible
from statsmodels.regression.mixed_linear_model import MixedLM
import pandas as pd
# recreate dataframe
df = pd.DataFrame.from_dict(
{'Cognition': {0: 1.6918916455399657, 1: -0.8659740639055752, 2: -0.7260401001097657,
3: -0.3573630652646015, 4: 3.2169557249585408, 5: -1.351852569895234,
6: -1.1175958349091746, 7: 1.4282603161187524, 8: 0.007664156491438558,
9: 1.9810784349343789},
'group': {0: 'MDD', 1: 'HC', 2: 'MDD', 3: 'SZ', 4: 'SZ', 5: 'MDD', 6: 'HC',
7: 'SZ', 8: 'HC', 9: 'MDD'},
'HIP_rh': {0: 0.6030087333308467, 1: -0.2753717308509293, 2: 0.08376122085063317,
3: -1.1422867560432115, 4: -0.11920370317144337, 5: 0.6322097293812017,
6: -2.7717229213732795, 7: -1.4557272887144321, 8: 1.094959587039391,
9: 1.0869845349546525}}
)
# set up model
model = MixedLM.from_formula(formula="Cognition ~ HIP_rh",
data=df,
groups=df["group"]
)
# this works
result = model.fit()
result.summary()
# this doesn't work
result = model.fit_regularized(method='l1',alpha=0.5)
result.summary()When only using .fit(), everything works fine.
Details
Note: As you can see, there are many issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates.
Note: Please be sure you are using the latest released version of statsmodels, or a recent build of main. If your problem has been fixed in an unreleased version, you might be able to use main until a new release occurs.
Note: If you are using a released version, have you verified that the bug exists in the main branch of this repository? It helps the limited resources if we know problems exist in the current main branch so that they do not need to check whether the code sample produces a bug in the next release.
If the issue has not been resolved, please file it in the issue tracker.
Expected Output
Print a results table of the fitted regularized mixed model.
Output of import statsmodels.api as sm; sm.show_versions()
Details
INSTALLED VERSIONS
Python: 3.8.16.final.0
OS: Linux 5.15.0-71-generic #78-Ubuntu SMP Tue Apr 18 09:00:29 UTC 2023 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
statsmodels
Installed: 0.14.0 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/statsmodels)
Required Dependencies
cython: Not installed
numpy: 1.21.5 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/numpy)
scipy: 1.10.1 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/scipy)
pandas: 2.0.1 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/pandas)
dateutil: 2.8.2 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/dateutil)
patsy: 0.5.3 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/patsy)
Optional Dependencies
matplotlib: 3.7.1 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/matplotlib)
backend: QtAgg
cvxopt: Not installed
joblib: 1.2.0 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/joblib)
Developer Tools
IPython: 8.12.2 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/IPython)
jinja2: 3.1.2 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/jinja2)
sphinx: 7.0.1 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/sphinx)
pygments: 2.15.1 (/home/johannes.wiesner/.conda/envs/csp_wiesner_johannes/lib/python3.8/site-packages/pygments)
pytest: Not installed
virtualenv: Not installed
Activity
kshedden commentedon Jun 5, 2023
I'm away from a computer this week so can't check this specifically. But in general summary will not work when a model is fit using regularization. summary wants to show standard errors, Wald tests, etc. none of which are available for models fit with regularization. The only meaningful output for regularized fits are the parameter estimates (results.params).
JohannesWiesner commentedon Jun 6, 2023
Ah good to know, thanks! Maybe in this case,
.summary()should not be not available at all forMixedLMResultsWrapper?BUG: Use unwrapped results in MixedLM.fit_regularized