我一直在遵循 rpy2 文档中有关使用 R 的 OOPS 的说明:https ://rpy2.readthedocs.io/en/version_2.8.x/robjects_oop.html 。我正在尝试创建一个 Python 类来调用 R 包 randomForestSRC 中的函数 rfsrc。
这在什么条件下起作用?rpy2 是否有一个固定的包列表,它允许您访问,如果有,这个列表上有 randomForestSRC 吗?
当我从 Jupyter Notebook (Python 3, R 3.5.1) 运行以下代码时,出现错误:Error in (function (f, signature = character(), where = topenv(parent.frame()), :没有找到“rfsrc”的通用函数
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1) # select the first mirror in the list
packnames = ('randomForestSRC', 'survival', 'tidyverse', 'magrittr', 'ggRandomForests', 'mlr')
utils.install_packages(StrVector(packnames))
from rpy2.robjects.packages import importr
randomForestSRC = importr('randomForestSRC')
from rpy2.robjects.methods import RS4Auto_Type
import six
class rfsrc(six.with_metaclass(RS4Auto_Type)):
__rname__ = 'rfsrc'
__rpackagename__ = 'randomForestSRC'
我还需要做什么才能让它发挥作用?
我也尝试了如下所示的手动方法并得到了同样的错误。
import rpy2.robjects as robjects
import rpy2.rinterface as rinterface
from rpy2.robjects.packages import importr
lme4 = importr("randomForestSRC")
getmethod = robjects.baseenv.get("getMethod")
StrVector = robjects.StrVector
class rfsrc(robjects.methods.RS4):
_coef = getmethod("rfsrc",
signature = StrVector(["rfsrc", ]),
where = "package:randomForestSRC")
def _call_get(self):
return self.do_slot("call")
def _call_set(self, value):
return self.do_slot("call", value)
call = property(_call_get, _call_set, None, "Get or set the RS4 slot 'call'.")
def coef(self):
""" fitted coefficients """
return self._coef(self)
有几个问题。我选择了一个可能是所有其他人的起源。
rpy2 是否有一个固定的包列表,它允许您访问,如果有,这个列表上有 randomForestSRC 吗?
rpy2
只能使用 R 安装中可用的 R 包。文档中有一些关于从 Python 安装 R 包的内容(另外,请注意 rpy2-2.9.x 文档的 URL):https ://rpy2.github.io/doc/v2.9.x/html/robjects_rpackages.html #installing-removing-r-packages
编辑:
该问题已编辑为现在关注错误消息no generic function found for 'rfsrc'
。该错误来自 R,因为未找到名为“rfsrc”的通用函数。
我认为这rfsrc
只是一个常规的 R 函数(不是通用的,请参阅https://github.com/cran/randomForestSRC/blob/master/R/rfsrc.R)。另外,我不是randomForest
很多使用 S4的包,您尝试使用的 rpy2 功能是专门为 R 的 S4 OOP 系统设计的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句