我使用rstan::stan()开发了一些软件包。我创建了一个函数,其返回值是由S4生成的一个rstan::stan()类对象。为了方便地访问估计值或为数据添加信息,我希望创建一个新的S4类对象,它继承了rstan::stan()的S4类,这样就有了新的插槽。
此外,新的S4class对象也可以用于rstan中的任何函数,如rstan::traceplot()。
fit <- rstan::stan( model_name=scr, data=data) # This is a fictitious code.假设我们得到了名为S4 (stanfit)的fit对象。
定义一个扩展的标准类
InheritedClass <- setClass("InheritedClass",
# New slots
representation(slotA="character",
slotB="character",
slotC="numeric"),
contains = "stanfit"
)要使用现有的S4类对象(即fit )来创建继承的类的S4对象,我只需要输入添加的新插槽的值,即slotA、slotB、slotC。
使用以下代码,我们可以将旧类的S4对象转换为继承的类:
fit2 <- as(fit,"InheritedClass")使用它,我们可以编辑槽,如下所示:
fit2@slotA <- "aaaaaaaaaaaa"发布于 2018-12-05 17:24:25
见help(setClass)。我相信这会像
setClass("classname", slots = c(foo = "numeric", bar = "character"),
contains = "stanfit")我相信您必须将rstan包含在包中描述文件的Imports:行中,这样它才能找到stanfit的S4类定义。
https://stackoverflow.com/questions/53627065
复制相似问题