是否可以在Python中执行glmm?

时间:2017-03-27 10:06:19

标签: python spss data-analysis

是否可以在Python中执行glmm(如SPSS中的GENLINMIXED分析)? 我是statsmodels的忠实粉丝,但这个图书馆似乎并不支持glmm ......还有其他选择吗?

-edit -

决定用R和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

1 个答案:

答案 0 :(得分:4)

根据this(诚然,不是最近的)帖子,仍然没有一个非常好的解决方案来运行Python中的glmms。但是,如果您只是寻找免费(并且更灵活!)替代在SPSS中运行测试,请查看R的lme4包。您甚至可以使用rpy2之类的包,并调用R直接来自Python,但这可能是一个小小的错误。