5

Is it possible to do glmm in Python (like the GENLINMIXED analysis in SPSS)? I'm a big fan of statsmodels, but this library doesn't seem to support glmm... Are there any alternatives?

-edit-

Decided to do it with R and r2py...

def RunAnalyseMLMlogit(dataset, outcomevars, meeneemvars, randintercept, randslope):

    from rpy2.robjects import pandas2ri
    from rpy2.robjects.packages import importr
    base = importr('base')
    stats = importr('stats')
    lme4 = importr('lme4')

    #data
    with SavReaderNp(dataset) as reader_np:
        array = reader_np.to_structured_array()

    df = pd.DataFrame(array)

    variabelen = ' '.join(outcomevars) + ' ~ ' + '+'.join(meeneemvars)
    randintercept2 = ['(1|'+i+')' for i in randintercept]
    intercept = '+'.join(randintercept2)
    randslope2 = ['(1+'+meeneemvars[0]+'|'+i+')' for i in randslope]
    slope = ' '.join(randslope2)

    pandas2ri.activate()
    r_df = pandas2ri.py2ri(df)

    #model
    #random intercepts + random slopes
    if len(randslope) > 0:
        formula = variabelen + '+' + intercept + '+' + slope

    #only random intercepts
    else:
        formula = variabelen + '+' + intercept

    model = lme4.glmer(formula, data=r_df, family= 'binomial')
    resultaat = base.summary(model).rx2('coefficients')
    uitkomst = base.summary(model)

    return uitkomst
Joost
  • 480
  • 1
  • 5
  • 15

1 Answers1

5

According to this (admittedly, not so recent) post, there still isn't a very good solution to running glmms in Python. However, if you're just looking for a free (and much more flexible!) alternative to running your tests in SPSS, look into the lme4 package for R. You could potentially even use a package such as rpy2, and call R directly from Python, but this might be a little buggy.

sacuL
  • 49,704
  • 8
  • 81
  • 106