Hi, Laurent and list, Thanks a lot for all the informations! I read through your discussions at https://stat.ethz.ch/pipermail/r-devel/2009-November/055701.html So it is a R problem not rpy. :-) I tried to work around this problem using the following code: ----------------------------------------------------------- import rpy2.robjects as robjects import math
p1dist = robjects.r.c() p2dist = robjects.r.c() num = 30 for i in range(num): p1dist = robjects.r.c(p1dist, i) p2dist = robjects.r.c(p2dist, i+1) res = robjects.r['chisq.test'](p1dist, p2dist) print(str(res.r['p.value']).split()[-1]) ---------------------------------------------------------- It works well. However if the input values are not integer but float, it gives similar errors. ------------------------------------------------------------- import rpy2.robjects as robjects import math p1dist = robjects.r.c() p2dist = robjects.r.c() num = 30 for i in range(num): tmp1 = i * 0.1 + 0.01 tmp2 = i * 0.1 + 0.05 p1dist = robjects.r.c(p1dist, tmp1) p2dist = robjects.r.c(p2dist, tmp2) res = robjects.r['chisq.test'](p1dist, p2dist) print(str(res.r['p.value']).split()[-1]) ------------------------------------------------------- File "/usr/lib64/python2.6/site-packages/rpy2/robjects/__init__.py", line 422, in __call__ res = super(RFunction, self).__call__(*new_args, **new_kwargs) rinterface.RRuntimeError: Error in names(dimnames(x)) <- DNAME : 'names' attribute [6] must be the same length as the vector [2] I would like to try what you suggested - bind the values to a symbol in an environment. Could you please point me to an example? I didn't find such a case in the R or rpy manual. (there is sth like the follows, but I tried sth similar and not work) >>> robjects.r('pi') + 2 c(3.14159265358979, 2) >>> robjects.r('pi')[0] + 2 5.1415926535897931 Thanks! Hao > This is odd. > When looking at what is happening, the problem is likely rooted in > the use of deparse(substitute(x)) and deparse(substitute(y)) in the code > for chisq.test. > > This is what is happening: > > >>> f = robjects.r('''function(x) return(deparse(substitute(x)))''') > >>> tuple(f(robjects.FloatVector(range(17)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)',) > >>> tuple(f(robjects.FloatVector(range(18)))) > ('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17', ')') > > deparse(substitute()) starts splitting the output into several elements. > > R allows the use of anonymous objects, but when created at the C level > some functions appear to break. I am currently seeing this as a > potential problem with R. > > One workaround is to bind your objects to a symbol in a given > environment, and call the chisq.test on them. > Not so elegant, but I do not think of an other temporary solution. > > > L. > > > > Hao Fan wrote: >> Hi, List and Laurent, >> >> For the chisq function I tried to used through rpy2, I just did >> more test with the exact code as follows: >> ---------------------------------------------- >> import rpy2.robjects as robjects >> >> p1dist = [] >> p2dist = [] >> num = 17 >> for x in range(num): >> p1dist.append(x) >> p2dist.append(x-1) >> >> print p1dist >> print p2dist >> rp1dist = robjects.FloatVector(p1dist) >> rp2dist = robjects.FloatVector(p2dist) >> >> res = robjects.r['chisq.test'](rp1dist, rp2dist) >> print(str(res.r['p.value']).split()[-1]) >> ----------------------------------------------------- >> >> When I set num to 17, it works >> "Warning message: In function (x, y = NULL, correct = TRUE, p = >> rep(1/length(x), length(x)), : >> Chi-squared approximation may be incorrect >> 0.2350833" >> >> However, if I set num to 18 (add one more element to the two vectors >> to be compared by chisq.test), it doesn't work. >> The error message is similar to my previous post: >> File "/usr/lib64/python2.6/site-packages/rpy2/robjects/__init__.py", >> line 422, in __call__ >> res = super(RFunction, self).__call__(*new_args, **new_kwargs) >> rinterface.RRuntimeError: Error in names(dimnames(x)) <- DNAME : >> 'names' attribute [4] must be the same length as the vector [2] >> >> Could this mean that rpy2 doesn't take a long vector or I made some >> stupid mistake? >> >> Thanks! >> >> >> Hao >>> Hi Hao, >>> >>> The exact example that triggers the error may matter. >>> >>> I just tried the following with rpy2-2.1dev and it worked. >>> >>> x = robjects.FloatVector((1,2,3)) >>> y = robjects.FloatVector((2,3,4)) >>> >>> res = robjects.r['chisq.test'](x, y) >>> >>> I only get the following >>> Warning message: >>> In function (x, y = NULL, correct = TRUE, p = rep(1/length(x), >>> length(x)), : >>> Chi-squared approximation may be incorrect >>> >>> L. >>> >>> >>> >>> Hao Fan wrote: >>>> Hi, list >>>> >>>> I would like to calculate the fitness of two histograms, so I >>>> tried to use the chisq.test() in R through rpy2. >>>> The python code I have is as follows: >>>> ------------------- >>>> import rpy2.robjects as robjects >>>> >>>> p1dist = [X1, X2, ... Xm] >>>> p2dist = [Y1, Y2, ... Ym] >>>> >>>> rp1dist = robjects.FloatVector(p1dist) >>>> rp2dist = robjects.FloatVector(p2dist) >>>> >>>> robjects.r['chisq.test'](rp1dist, rp2dist) >>>> ------------------- >>>> >>>> Then I got the following error: >>>> File >>>> "/usr/lib64/python2.6/site-packages/rpy2/robjects/__init__.py", in >>>> __call__ >>>> res = super(RFunction, self).__call__(*new_args, **new_kwargs) >>>> rinterface.RRuntimeError: Error in names(dimnames(x)) <- DNAME : >>>> 'names' attribute [62] must be the same length as the vector [2] >>>> >>>> >>>> I also tried rpy_classic, so my code change to the follows: >>>> -------------------- >>>> import rpy2.robjects as robjects >>>> import rpy2.rpy_classic as rpy >>>> rpy.set_default_mode(rpy.NO_CONVERSION) >>>> >>>> p1dist = [X1, X2, ... Xm] >>>> p2dist = [Y1, Y2, ... Ym] >>>> >>>> rp1dist = robjects.FloatVector(p1dist) >>>> rp2dist = robjects.FloatVector(p2dist) >>>> >>>> robjects.r['chisq.test'](rp1dist, rp2dist) >>>> ----------------- >>>> >>>> Then I got similar error: >>>> File "/usr/lib64/python2.6/site-packages/rpy2/rpy_classic.py", line >>>> 214, in __call__ >>>> res = self.__sexp(*args_r, **kwargs_r) >>>> rinterface.RRuntimeError: Error in names(dimnames(x)) <- DNAME : >>>> 'names' attribute [62] must be the same length as the vector [2] >>>> >>>> >>>> I feel this problem is due to my wrong use of the R vector, but >>>> I can not find it. Any hints will be mostly appreciated! >>>> Thanks a lot in advance! >>>> >>>> >>>> Hao >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >> >> -- Hao Fan, PhD Department of Biopharmaceutical Sciences Mission Bay, Byers Hall 1700 4th Street, Suite 501 University of California, San Francisco San Francisco, CA 94158-2330 Tel +1 (415) 514-4258, 4232, 4233 Fax +1 (415) 514-4231, 4234 ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list