我正在尝试分析通过RPy用lme4.lmer()生成的一组线性模型的偏差。 This notebook here显示了一个完整的示例,其中包括我导入dep,加载文件,运行lme4.lmer()以及使它们无法运行方差分析的情况。

为了您的方便,这里还是粘贴了一条失败的行,我希望看到它。

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和拟合的模型未绑定到变量名。这将给出以下内容(请注意:“更紧密”而不是“相等”-有关此的详细信息只会分散重点):

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])

08-24 23:04