-->

Python列表转换成的R数值向量(converting a Python list to an R

2019-09-19 16:36发布

我需要适合我的数据到一个Beta分布并获取阿尔法参数。

我一直在编码Python,但似乎没有要在SciPy的任何测试函数拟合。 无论我做在Matlab,这我不是太熟悉,或在Python与R和它的一切fitdistr功能。 所以我去了后者。

from rpy2.robjects.packages import importr

MASS = importr('MASS')

然后我需要浮子我numpy的矢量在范围[0,1)和我将它传递给fitdistr

myVector = myVector.tolist()
MASS.fitdistr(myVector,"beta")

太糟糕了,它想某种其他载体。 没有RPY和应该rpy2做了我所有的转换?

Error in function (x, densfun, start, ...)  : 
  'x' must be a non-empty numeric vector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 82, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 34, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in function (x, densfun, start, ...)  : 
  'x' must be a non-empty numeric vector

什么我需要在这里做什么?

Answer 1:

答案在这里: 转换Python对象为rpy2

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

见http://rpy.sourceforge.net/rpy2/doc/html/numpy.html :



文章来源: converting a Python list to an R numeric vector