python - 命名列表等效于 rpy2/dataframe 访问

标签 python r pandas rpy2

我正在尝试从 MNP 中复制一个示例R 中的包,以两种不同的方式来自 rpy2。首先,我只是将 robjects.r 与一个完全复制并粘贴 R 代码的字符串一起使用:

import rpy2.robjects as robjects
import rpy2.robjects.numpy2ri
import rpy2.robjects.pandas2ri
import rpy2.robjects.packages as rpackages

robjects.pandas2ri.activate()
mnp = rpackages.importr('MNP')
base = rpackages.importr('base')

r = robjects.r
r.data('detergent')
rcmd = '''\
mnp(choice ~ 1, choiceX = list(Surf=SurfPrice, Tide=TidePrice,
Wisk=WiskPrice, EraPlus=EraPlusPrice,
Solo=SoloPrice, All=AllPrice),
cXnames = "price", data = detergent, n.draws = 500, burnin = 100,
thin = 3, verbose = TRUE)'''

res = r(rcmd)

这工作正常并重现了我可以直接在 R 中执行的操作。我还想尝试使用 python 可访问对象运行此代码,从数据帧传入数据:

import rpy2.rlike.container as rlc
df = robjects.pandas2ri.ri2py(r['detergent'])

choiceX = rlc.TaggedList(['SurfPrice', 'TidePrice', 'WiskPrice', 'EraPlusPrice', 'SoloPrice', 'AllPrice'], 
                         tags=('Surf', 'Tide', 'Wisk', 'EraPlus', 'Solo', 'All'))

res = mnp.mnp('choice ~ 1', 
              choiceX=['SurfPrice', 'TidePrice', 'WiskPrice', 'EraPlusPrice', 'SoloPrice', 'AllPrice'],
              cXnames='price', 
              data=df, n_draws=500, burnin=100,
              thin=3, verbose=True)

失败并出现错误:

Error in xmatrix.mnp(formula, data = eval.parent(data), choiceX = call$choiceX,  : 
  Error: Invalid input for `choiceX.'
 You must specify the choice-specific varaibles at least for all non-base categories.

在另一个 SO response 中建议用 rpy2 TaggedList 替换 R 命名列表.如果我删除 MNP 的 choiceXcXnames 参数(它们是可选的),代码会运行,因此看起来 pandas 数据帧正在正确传递。

我不确定一旦 TaggedList 进入 R 后是否没有被正确解释为命名列表,或者 MNP 是否存在一些问题没有将 choiceX 的内容与 Pandas 数据框。

有人知道这里会发生什么吗?

更新

按照@lgautier 的建议,我将代码修改为:

choiceX = rlc.TaggedList([base.as_symbol('SurfPrice'), base.as_symbol('TidePrice'), 
                          base.as_symbol('WiskPrice'), base.as_symbol('EraPlusPrice'), 
                          base.as_symbol('SoloPrice'), base.as_symbol('AllPrice')], 
                         tags=('Surf', 'Tide', 'Wisk', 'EraPlus', 'Solo', 'All'))

res = mnp.mnp(robjects.Formula('choice ~ 1'), 
              choiceX=choiceX,
              cXnames='price', 
              data=df, n_draws=500, burnin=100,
              thin=3, verbose=True)

但是,我收到与之前发布的错误相同的错误。

更新 2

按照@lgautier 建议的解决方法,以下代码:

choiceX = rlc.TaggedList([base.as_symbol('SurfPrice'),
                          base.as_symbol('TidePrice'), 
                          base.as_symbol('WiskPrice'),
                          base.as_symbol('EraPlusPrice'), 
                          base.as_symbol('SoloPrice'),
                          base.as_symbol('AllPrice')], 
                         tags=('Surf', 'Tide', 'Wisk',
                               'EraPlus', 'Solo', 'All'))

choiceX = robjects.conversion.py2ro(choiceX)
# add the names
choiceX.names = robjects.vectors.StrVector(('Surf', 'Tide',
                                            'Wisk', 'EraPlus',
                                            'Solo', 'All'))

res = mnp.mnp(robjects.Formula('choice ~ 1'), 
              choiceX=choiceX,
              cXnames='price', 
              data=df, n_draws=500, burnin=100,
              thin=3, verbose=True)

仍然会产生错误(尽管是不同的错误):

Error in as.vector(x, mode) : 
  cannot coerce type 'symbol' to vector of type 'any'
---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-21-7de5ad805801> in <module>()
      3               cXnames='price',
      4               data=df, n_draws=500, burnin=100,
----> 5               thin=3, verbose=True)

/Users/lev/anaconda/envs/rmnptest/lib/python2.7/site-packages/rpy2-2.5.6-py2.7-macosx-10.5-x86_64.egg/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs)
    168                 v = kwargs.pop(k)
    169                 kwargs[r_k] = v
--> 170         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
    171 
    172 pattern_link = re.compile(r'\\link\{(.+?)\}')

/Users/lev/anaconda/envs/rmnptest/lib/python2.7/site-packages/rpy2-2.5.6-py2.7-macosx-10.5-x86_64.egg/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs)
     98         for k, v in kwargs.items():
     99             new_kwargs[k] = conversion.py2ri(v)
--> 100         res = super(Function, self).__call__(*new_args, **new_kwargs)
    101         res = conversion.ri2ro(res)
    102         return res

RRuntimeError: Error in as.vector(x, mode) : 
  cannot coerce type 'symbol' to vector of type 'any'

最佳答案

Python 代码与您的 R 不对应。您发帖后就明白了这一点,请在下面提供详细信息。总结是 R 符号和 Python 字符串是不等价的(尽管 R 在某些地方允许这两种符号混淆了它自己的用户——例如,library("MNP")library(MNP ) 将起作用)。

这与这个问题没有什么不同:pandas and rpy2: Why does ezANOVA work via robjects.r but not robjects.packages.importr?

...除了 choiceX 将是一个未计算的 R 表达式,而不仅仅是一个符号。

R代码是:

data(detergent)
mnp(choice ~ 1,
    # ^- this is a "formula", which is an expression in R
    choiceX = list(Surf=SurfPrice, Tide=TidePrice,
                   Wisk=WiskPrice, EraPlus=EraPlusPrice,
                   Solo=SoloPrice, All=AllPrice),
    # ^- this is a list of objects, but with the cautionary note
    #    that R evaluates expressions in argument lazily. Therefore
    #    the safest is to have it as an R expression (it may or may
    #    not work if evaluated, but this depends on the code in
    #    `mnp`)
    cXnames = "price",
    # ^- this is a string
    data = detergent,
    n.draws = 500, burnin = 100,
    thin = 3, verbose = TRUE)

您拥有的 Python 是(带有关于差异的注释):

choiceX = rlc.TaggedList(['SurfPrice', 'TidePrice', 'WiskPrice',
                          'EraPlusPrice', 'SoloPrice', 'AllPrice'], 
                         tags=('Surf', 'Tide', 'Wisk',
                               'EraPlus', 'Solo', 'All'))
# ^- this is a "tagged list", and the R equivalent would be
#    list(Surf="SurfPrice", Tide="TidePrice", Wisk="WiskPrice",
#         EraPlus="EraPlusPrice", Solo="SoloPrice", All="AllPrice")
#    Something closer to your R code above would be:
#    rlc.TaggedList([as_symbol('SurfPrice'), as_symbol('TidePrice'),
#                   ...
#                   tags=('Surf', 'Tide', ...))

res = mnp.mnp('choice ~ 1', 
              # ^- this is a string. To make it an R formula, do
              # robjects.Formula('choice ~ 1')
              choiceX=['SurfPrice', 'TidePrice', 'WiskPrice',
                       'EraPlusPrice', 'SoloPrice', 'AllPrice'],
              # ^- this should be choiceX defined above, I guess
              cXnames='price',
              # ^- this is a string, like in R 
              data=df,
              n_draws=500, burnin=100,
              thin=3, verbose=True)

编辑:

现在这意味着下面应该可以工作了

choiceX = robjects.rinterface.parse("""
    list(Surf=SurfPrice, Tide=TidePrice,
         Wisk=WiskPrice, EraPlus=EraPlusPrice,
         Solo=SoloPrice, All=AllPrice)""")

目前 rpy2 没有提供很多用于构建 R 表达式的实用程序。如果变量名是 Python 级别的参数 你可以考虑这样的事情:

rcode = 'list('+''.join('%s=%s' % (k,v) \
                        for k,v in \
                        (('Surf','SurfPrice'),
                         ('Tide', 'TidePrice'),
                         ('Wisk','WiskPrice'),
                         ('EraPlus','EraPlusPrice'),
                         ('Solo','SoloPrice'),
                         ('All','AllPrice'))) + ')'
choiceX = robjects.rinterface.parse(rcode)

关于python - 命名列表等效于 rpy2/dataframe 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190863/

相关文章:

python - 取行中不带标签的数值,正则表达式

python - 如何解决“PytestDeprecationWarning : pytest. config global is deprecated

Python继承与字典突变

python - 不断收到非类型错误

r - 如何获得R中回归分析生成的四个图?

r - 具有许多变量的数据集的 bestglm 替代方案

r - 将一个目录中的大量文件合并到R中的数据框中

python - 如何计算数据框列中的出现次数?

python - 在编写修复程序后将自定义修复程序添加到 lib2to3

python - 缺少可选依赖项 'tables' 。在 Pandas to_hdf