使用 rpy2 从 Python 调用 R 库 DirichletReg

Call R library DirichletReg from Python using rpy2

我正在尝试使用 Python 进行 Dirichlet 回归。不幸的是,我找不到可以完成这项工作的 Python 软件包。所以我尝试使用 rpy2 调用 R 库 DirichletReg。但是,如何调用 DirichReg(Y ~ X1 + X2 + X3, data=predictorData) where Y = DR_data(compositionalData) 等回归函数对我来说不是很直观。我在rpy2的文档中看到了调用线性回归函数lm的例子。但我的情况略有不同,因为 Y 不是 table 中的列名,而是 R 对象 DR_data.

我想知道执行此操作的正确方法是什么,或者是否有 Python 狄利克雷回归包。

您可以将对象从 python 发送到 "Formula" 环境中。此示例来自 rpy2 文档:

import array
from rpy2.robjects import IntVector, Formula
from rpy2.robjects.packages import importr
stats = importr('stats')

x = IntVector(range(1, 11))
y = x.ro + stats.rnorm(10, sd=0.2)

fmla = Formula('y ~ x')
env = fmla.environment
env['x'] = x
env['y'] = y

fit = stats.lm(fmla)

您还可以在 R 环境中(在公式之外)创建命名变量。参见 here. Worst case scenario, you move some your python data into R through rpy2, then issue the commands directly in R through the rpy2 bridge as described here