简体   繁体   English

熊猫和rpy2:为什么ezANOVA通过robjects.r而不是robjects.packages.importr起作用?

[英]pandas and rpy2: Why does ezANOVA work via robjects.r but not robjects.packages.importr?

Like many, I'm hoping to stop straddling R and Python worlds and just work in Python using Pandas, Pyr2, Numpy, etc. I'm using the R package ez for its ezANOVA facility. 与许多人一样,我希望不再跨越R和Python世界,而只是使用Pandas,Pyr2,Numpy等在Python中工作。我将R包ez用于ezANOVA工具。 It works if I do things the hard way, but why doesn't it work when I do them the easy way? 如果我用困难的方式做事,它会起作用,但是当我以简单的方式做事时,为什么它不起作用? I don't understand the resulting error: 我不明白所产生的错误:

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

See below for full reproducible code (requires some python packages: pyr2, pandas, numpy): 参见以下完整的可复制代码(需要一些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

In the easy version you are passing symbol names as strings. 在简单版本中,您将符号名称作为字符串传递。 This is not the same as a symbol. 这与符号不同。

Check the use of as_symbol in Minimal example of rpy2 regression using pandas data frame 检查使用的as_symbolrpy2回归最小例如使用大熊猫数据帧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM