2
$\begingroup$

I recently switched from using Pymer4 to Statsmodels. I am working with a random intercept model, and I need to get the conditional coefficients estimated for each group (i.e., random + fixed effects) so I can plot them (ala Gelman 2006, Figure 2). In Pymer4, this is so simple! The code looks like this:

1. MLM = Lmer('outcome ~ 1 + (1|group)', data=df)
2. print(MLM.fit())
3. MLM.fixef

Line 3 (.fixef) gives me the actual coefficients for each group, easy-peasy. In Statsmodels, I can easily get the random effects, but I cannot for the life of me figure out how to get the actual coefficients for each group.

1. md = smf.mixedlm("outcome ~ 1", df, groups=groups)
2. mdf = md.fit()
3. mdf.random_effects 

The random effects by themselves are not useful to me—I really need the conditional coefficients, i.e., fixed effects + random effects. For this simple model I can calculate the group coefficients myself if need be (it should be just random effects + intercept):

pd.Series(mdf.random_effects)+mdf.params[0]

But my actual model has a bunch of fixed effects (4 categorical explanatory variables), so I don't think it is so simple to calculate the group coefficients. I think I cannot just use the intercept.

I feel like I must be missing something simple here, but I've been pouring through the source code and googling like mad with no success... Please help!

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.