Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do glmm in Python?

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
like image 556
Joost Avatar asked Mar 27 '17 10:03

Joost


1 Answers

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.

like image 133
sacuL Avatar answered Oct 23 '22 05:10

sacuL