1

I am trying to replicate the following R code for GLMER

lm1 <- glmer(correct~type+(1+type|subject_id) + (1|category), df %>% filter(type!="target"), family = binomial())
lmnull <- glmer(correct~1+(1+type|subject_id) + (1|category), df %>% filter(type!="target"), family = binomial())

to Python using

import statsmodels.formula.api as smf
df = df[df['type'] != 'target']
model = smf.mixedlm("correct~ C(type) + .......", df, groups="subject_id*").fit()
model = smf.mixedlm("correct~ 1 + .......", df, groups="subject_id*").fit()

I am stucked with trying to find a way to replicate the R formula. Anyone has any idea on how to do it? I am using the same dataframe for both codes.

angie866
  • 77
  • 6
  • 1
    You have *crossed random effects* here. The [statsmodel docs](https://www.statsmodels.org/devel/mixed_linear.html) say: "To include crossed random effects in a model, it is necessary to treat the entire dataset as a single group. The variance components arguments to the model can then be used to define models with various combinations of crossed and non-crossed random effects.". See https://stackoverflow.com/questions/50052421/mixed-models-with-two-random-effects-statsmodels – Ben Bolker Jul 05 '22 at 13:42
  • 1
    The only problem I'm having with coding your example (see also https://stats.stackexchange.com/questions/444790/specifying-mixed-effects-model-formulas) is that all the examples I can find of crossed random effects use scalar (intercept) random effects only, and I'm having trouble finding clear documentation of the `vc_formula` syntax ... – Ben Bolker Jul 05 '22 at 13:50
  • I have the exact same problem. @BenBolker if you find something useful, please reply here – angie866 Jul 05 '22 at 14:30

0 Answers0