Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过RPy调用lme4.lmer输出的方差分析_Python_R_Rpy2_Anova_Lme4 - Fatal编程技术网

Python 通过RPy调用lme4.lmer输出的方差分析

Python 通过RPy调用lme4.lmer输出的方差分析,python,r,rpy2,anova,lme4,Python,R,Rpy2,Anova,Lme4,我试图分析通过RPy使用lme4.lmer()生成的一组线性模型的偏差。显示了一个完整的示例,其中我导入了我的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

我试图分析通过RPy使用lme4.lmer()生成的一组线性模型的偏差。显示了一个完整的示例,其中我导入了我的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

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

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

现在考虑改写你的R代码而不使用变量名,因为这将更接近于当前实现中发生的与RPY2< /C> >,因为数据DATAFRAME和拟合模型不绑定到变量名。这将给出以下内容(注意:“更接近”而不是“相等”-关于这一点的详细信息只会分散对要点的注意力):

结果是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

这是
merMod
对象的
anova
方法中的一个错误:它本质上是由于传递给R的对象的名称太长而导致的,因此当
deparse()
d时,它们最终成为具有(意外地)多个元素的字符向量。这是固定的;现在还有一个(未记录的)参数
model.names
,可以指定该参数来覆盖模型名称的取消划分


您可以使用
devtools::install\u github(“lme4”、“lme4”)
安装开发版本,否则您可能需要等待一段时间才能发布补丁版本。。。除了构造调用,使传递给R的语言对象在被分解时更短之外,想不出其他解决方法…

这对于通过这么多层进行调试来说有点棘手。你能让Rpy从R中为你提供更多的跟踪/调试信息吗(例如
traceback()
?)我认为向R发送
选项(error=recover)
可能会有所帮助。。。双模型方差分析通过RPy有效吗?是的,双模型方差分析有效。此外,我更新了我的粘贴以显示3个模型。在哪里传递选项(错误=恢复)?正如我所说的,我相信问题在于RPy没有正确地传递输入列表。。。我试图将它们作为[res[0]、res[1]、res[2]]传递,但这也不起作用(R抱怨不知道如何处理列表),用
lm
拟合三个模型并使用
anova()
通过RPy对它们起作用吗?(即,这是lme4特有的问题吗?)是的,这似乎是lme4特有的问题。谢谢你的回复!然而,遗憾的是,您的示例仍然产生了一个非常类似的错误。看,啊。问题来自于数据的匿名性。让我重写一下答案。如果你还没有看到这个,这会引发。该漏洞已经关闭,但无法修复我的问题,ben bolker说他无法在RPy中复制,我也无法在纯R中复制。您能帮我们解决一下吗?我使用devtools从github下载了该版本,但我仍然得到完全相同的错误。在这里查看我的R会话信息
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))
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
# 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])