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
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
> 
> 


------------------------------------------------------------------------------
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

Reply via email to