许多人一样,我希望能制止跨界R和Python的世界,只是在使用熊猫,Pyr2,numpy的,等我使用它ezANOVA设施将R包EZ Python的工作。 它的工作原理,如果我做的事情了艰辛的道路,但为什么当我做这些简单的方法不工作? 我不明白所产生的误差:
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,熊猫,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