编程语言
首页 > 编程语言> > 是否可以在Python中执行glmm?

是否可以在Python中执行glmm?

作者:互联网

是否可以在Python中执行glmm(如SPSS中的GENLINMIXED分析)?
我是statsmodels的忠实粉丝,但这个库似乎不支持glmm …有没有其他选择?

-编辑-

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

解决方法:

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

标签:python,data-analysis,spss
来源: https://codeday.me/bug/20190627/1307840.html