首页 > 解决方案 > 包仅与 S3 一起运行,“此 S4 类不可子集”

问题描述

我想将margins包应用于s4输出。

library(sure) # for residual function and sample data sets
library(MASS) # for polr function
library(VGAM)
library(margins)
rm(df1)
df1 <- df1
df1$x2 <- df2$y
df1$x3 <- df2$x
df1$y <- df3$x/10

这里有两个包。一个有s3 输出,一个s4

fit.polr.s3 <- polr(x2 ~ x + x3, data = df1, method = "probit") # S3
fit.polr.s4  <- vglm(x2 ~ x + x3, family=propodds, data=df1) # S4

summary(margins(fit.polr.s3, variables = "x", typ="response"))

 factor    AME     SE      z      p   lower  upper
      x 0.0011 0.0010 1.0853 0.2778 -0.0009 0.0031

但:

summary(margins(fit.polr.s4, variables = "x", typ="response"))

Error in `*tmp*`[["coefficients"]] : this S4 class is not subsettable

我需要该VGAM功能,无论如何将输出转换回s3. 或者让包裹接受s4

我很确定唯一的问题是应该如何使用@而不是$。我试图在包中替换它,但如果我输入margins. R它只显示:

function (model, ...) 
{
    UseMethod("margins")
}
<bytecode: 0x000001c2ebaca028>
<environment: namespace:margins>

编辑:

运行fit.polr.s4$coefficients <- fit.polr.s4@coefficients给出错误:Error in $<-(*tmp*, coefficients, value = c((Intercept):1 = 3.03597808578146, : no method for assigning subsets of this S4 class

> class(fit.polr.s4)
[1] "vglm"
attr(,"package")
[1] "VGAM"

标签: rmethodss4

解决方案


推荐阅读