correlation for two lists of data

2019-07-31 17:14发布

问题:

These two lists contain data something like this:

a = [1 2 1 3 1 2 1 1 1 2 1 1 2 1 4 1 ] 
b = [ 3480. 7080. 10440. 13200. 16800. 20400. 23880. 27480. 30840. 38040. 41520. 44880.  48480. 52080. 55680. 59280.]

How to find correlation using python by importing rpy2, I mean cor function. And the o/p has to lie between -1 and +1.

回答1:

from rpy2.robjects.vectors import FloatVector
from rpy2.robjects.packages import importr

stats = importr('stats')

a=[1, 2, 1, 3, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 4, 1 ]
b=[ 3480, 7080, 10440, 13200, 16800, 20400, 23880,
    27480, 30840, 38040, 41520, 44880, 48480, 52080, 55680, 59280]

result = stats.cor(FloatVector(a), FloatVector(b))

The documentation for rpy2 has many other examples about how to use it.