gpt4 book ai didi

python - 通过 RPy 对 lme4.lmer 输出调用方差分析

转载 作者:行者123 更新时间:2023-12-01 05:32:22 25 4
gpt4 key购买 nike

我正在尝试通过 RPy 分析使用 lme4.lmer() 生成的一组线性模型的偏差。 This notebook here 显示了一个完整的示例,其中我导入了我的 deps、加载了我的文件、运行了我的 lme4.lmer() 并未能让 anova 在它们上运行。

为了您的方便,这里再次粘贴了失败的行,我希望看到它的工作。

compare = stats.anova(res[0], res[1], res[2])
Error in Ops.data.frame(data, data[[1]]) :
list of length 3 not meaningful
In addition: Warning message:
In anova.merMod(<S4 object of class "lmerMod">, <S4 object of class "lmerMod">, :
failed to find unique model names, assigning generic names

---------------------------------------------------------------------------
RRuntimeError Traceback (most recent call last)
<ipython-input-47-fe0ffa3b55de> in <module>()
----> 1 compare = stats.anova(res[0], res[1], res[2])

/usr/lib64/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, args, **kwargs)
84 v = kwargs.pop(k)
85 kwargs[r_k] = v
---> 86 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)

/usr/lib64/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs)
33 for k, v in kwargs.iteritems():
34 new_kwargs[k] = conversion.py2ri(v)
---> 35 res = super(Function, self).__call__(*new_args, **new_kwargs)
36 res = conversion.ri2py(res)
37 return res

RRuntimeError: Error in Ops.data.frame(data, data[[1]]) :
list of length 3 not meaningful

这段代码在 R 中完美运行:

> mydata = read.csv("http://chymera.eu/data/test/r_data.csv")
> library(lme4)
Loading required package: lattice
Loading required package: Matrix
> lme1 = lme4.lmer(formula='RT~cat2 + (1|ID)', data=mydata, REML=FALSE)
Error: could not find function "lme4.lmer"
> lme1 = lmer(formula='RT~cat1 + (1|ID)', data=mydata, REML=FALSE)
> lme2 = lmer(formula='RT~cat2 + (1|ID)', data=mydata, REML=FALSE)
> anova(lme1,lme2)
> lme3 = lmer(formula='RT~cat2*cat1 + (1|ID)', data=mydata, REML=FALSE)
> stats::anova(lme1, lme2, lme3)
Data: mydata
Models:
lme1: RT ~ cat1 + (1 | ID)
lme2: RT ~ cat2 + (1 | ID)
lme3: RT ~ cat2 * cat1 + (1 | ID)
Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
lme1 4 116.68 122.29 -54.342 108.68
lme2 4 149.59 155.19 -70.793 141.59 0.000 0 1
lme3 6 117.19 125.59 -52.594 105.19 36.398 2 1.248e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

你能帮我让它在 RPy 中运行吗?

最佳答案

当在 R stats::anova() 中时,可能是从函数调用中未计算的表达式推断模型名称。这里是 lme1'lme2lme3

现在考虑在不使用变量名的情况下重写 R 代码,因为这更接近于当前使用 rpy2 实现中发生的情况,因为数据 DataFrame 和拟合模型未绑定(bind)到变量名。这将给出以下内容(注意:“更接近”而不是“等于” - 有关此的详细信息只会分散要点):

stats::anova(lmer(formula='RT~cat1 + (1|ID)',
data=read.csv("http://chymera.eu/data/test/r_data.csv"),
REML=FALSE),
lmer(formula='RT~cat2 + (1|ID)',
data=read.csv("http://chymera.eu/data/test/r_data.csv"),
REML=FALSE),
lmer(formula='RT~cat2*cat1 + (1|ID)',
data=read.csv("http://chymera.eu/data/test/r_data.csv"),
REML=FALSE))

结果是 R 中的错​​误。

Error in names(mods) <- sub("@env$", "", mNms) : 
'names' attribute [6] must be the same length as the vector [3]
In addition: Warning message:
In anova.merMod(lmer(formula = "RT~cat1 + (1|ID)", data = read.csv("http://chymera.eu/data/test/r_data.csv"), :
failed to find unique model names, assigning generic names

这表明 R 函数 lme4:::anova.meMod 所做的假设很容易被违反,应该通知包的作者。

它还显示表达式将用于识别结果文本输出中的模型。

以下内容可能缺乏一点优雅,但应该是一种解决方法,也是一种保持模型标签简短的方法。

# bind the DataFrame to an R symbol
robjects.globalenv['dataf'] = dfr
# build models, letting R fetch the symbol `dataf` when it is evaluating
# the parameters in the function call
res = list()
for formula in formulae:
lme_res = lme4.lmer(formula=formula, data=base.as_symbol("dataf"), REML='false')
res.append(lme_res)
# This is enough to work around the problem
compare = stats.anova(res[0], res[1], res[2])

# if not happy with the model names displayed by `compare`,
# globalenv can be filled further
names = list()
for i, value in enumerate(res):
names.append('lme%i' % i)
robjects.globalenv[names[i]] = value
# call `anova`
compare = stats.anova(*[base.as_symbol(x) for x in names])

关于python - 通过 RPy 对 lme4.lmer 输出调用方差分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19928662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com