python - pandas and rpy2: Why does ezANOVA work via robjects.r but not robjects.packages.importr? -
like many, i'm hoping stop straddling r , python worlds , work in python using pandas, pyr2, numpy, etc. i'm using r package ez ezanova facility. it works if things hard way, why doesn't work when them easy way? don't understand 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 set attribute on null
see below full reproducible code (requires python packages: pyr2, pandas, numpy):
import pandas pd rpy2 import robjects rpy2.robjects import pandas2ri pandas2ri.activate() # make pyr2 accept , auto-convert pandas dataframes rpy2.robjects.packages import importr base = importr('base') ez = importr('ez') robjects.r['options'](warn=-1) # ??? import numpy np """make pandas data 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}) # looks dataframe r print testdata """hard way: use ezanova thorugh pyr2 *** 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) # command shows ez instance setup print ez.ezprecis(data=testdata) # successful """easy way: import ez directly , use """ # *** approach not work *** # yet, trying use ez.ezanova yields excpetion aboutthe wid value # res = ez.ezanova(data=testdata, dv='score', wid='subjectid', between='block', detailed=true) # print res # *** approach works (and uses 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 easy version passing symbol names strings. not same symbol.
check use of as_symbol
in minimal example of rpy2 regression using pandas data frame
Comments
Post a Comment