python - pandas 和 rpy2 : Why does ezANOVA work via robjects. r 但不是 robjects.packages.importr?

标签 python r numpy pandas rpy2

像许多人一样,我希望停止跨越 R 和 Python 世界,而只是使用 Pandas、Pyr2、Numpy 等在 Python 中工作。我使用 R 包 ez 来实现其 ezANOVA 工具。 如果我以困难的方式做事,它会起作用,但为什么当我以简单的方式做事时,它不起作用?我不明白由此产生的错误:

File "/Users/malcomreynolds/analysis/r_with_pandas.py", line 38, in <module>
    res = ez.ezANOVA(data=testData, dv='score', wid='subjectid', between='block', detailed=True)
  File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 178, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 106, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in table(temp[, names(temp) == wid]) : 
  attempt to set an attribute on NULL

请参阅下面的完整可重现代码(需要一些 python 包:pyr2、pandas、numpy):

import pandas as pd
from rpy2 import robjects
from rpy2.robjects import pandas2ri
pandas2ri.activate()  # make pyr2 accept and auto-convert pandas dataframes
from rpy2.robjects.packages import importr
base = importr('base')
ez = importr('ez')
robjects.r['options'](warn=-1)  # ???
import numpy as np

"""Make pandas data from from scratch"""

score = np.random.normal(loc=10, scale=20, size=10)
subjectid = range(10)
block = ["Sugar"] * 5 + ["Salt"] * 5
testData = pd.DataFrame({'score':score, 'block':block, 'subjectid': subjectid})
# it looks just like a dataframe from R
print testData

"""HARD WAY: Use ezANOVA thorugh pyr2 *** THIS WORKS ***"""
anova1 = robjects.r("""
library(ez)
function(df) {
    # df gets passed in
    ezANOVA(
        data=df,
        dv=score,
        wid=subjectid,
        between=block,
        detailed=TRUE)
}
""")
print anova1(testData)


# this command shows that ez instance is setup properly
print ez.ezPrecis(data=testData)  # successful

"""EASY WAY: Import ez directly and use it """
# *** THIS APPROACH DOES NOT WORK ***
# yet, trying to use ez.ezANOVA yields an excpetion aboutthe wid value
# res = ez.ezANOVA(data=testData, dv='score', wid='subjectid', between='block', detailed=True)
# print res

# *** THIS APPROACH WORKS (and also uses my options change) ***
res = ez.ezANOVA(data=testData, dv=base.as_symbol('score'), wid=base.as_symbol('subjectid'), between=base.as_symbol('block'))
print res

最佳答案

在简单版本中,您将符号名称作为字符串传递。这与符号不同。

检查 Minimal example of rpy2 regression using pandas data frameas_symbol 的使用

关于python - pandas 和 rpy2 : Why does ezANOVA work via robjects. r 但不是 robjects.packages.importr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169496/

相关文章:

将大文件读入 R

r - 如何对 r 上同一类别内的变量进行 t 检验?

python - 给定另一个 numpy 数组,将 numpy.ndarray 的一些元素替换为零

python - numpy 中的均方根以及 numpy 矩阵和数组的并发症

numpy - 安装 OS X 10.8 (Mountain Lion) 后,我的 ipython 和库被禁用

python - 计算协方差矩阵——numpy.cov 和 numpy.dot 之间的区别?

python - 正则表达式在逗号之间选择和替换字符串不包括预期的逗号

删除矩阵 R 中重复的相邻值

python - 成对映射 numpy 数组

python - 列出所有无线网络 Python for PC