【问题标题】:Offset interaction term偏移交互项
【发布时间】:2019-04-09 08:02:34
【问题描述】:

我想在 r 中拟合 coxph 模型并抵消一个主效应和交互项。

我尝试在变量前面使用偏移命令,但它给出了错误:model.frame.default 中的错误(公式 = Surv(时间 = X,事件 = Delta)〜: 可变长度不同(为 'offset(D:Y)' 找到)

我们可以生成一些玩具数据,比如

require(survival)
D = rbinom(100, 1, 0.5)
Y = rbinom(100, 1, 0.5)
X = rexp(100, 1)
Delta = rbinom(100, 1, 0.5)
coxph(Surv(time = X, event = Delta) ~ D + offset(Y) + offset(D:Y))

我想抵消 Y 和 D:Y 但它一直给我错误。也许我对如何使用“偏移量”有误。

【问题讨论】:

    标签: r


    【解决方案1】:

    尽管您在公式表达式中使用表达式 D:Y,但它首先由 offset 处理,它实际上并不“知道”如何处理 R 公式中的“:”运算符 -解析上下文。相反,它会给您一条警告消息,暗示它正在被解析为整数序列运算符。

    Error in model.frame.default(formula = Surv(time = X, event = Delta) ~  : 
      variable lengths differ (found for 'offset(D:Y)')
    In addition: Warning messages:
    1: In D:Y : numerical expression has 100 elements: only the first used
    

    如果你想使用 D 和 Y 的乘积作为额外的“交互”偏移,那么你可以这样做:

    > coxph(Surv(time = X, event = Delta) ~ D + offset(Y) + offset(I(D*Y)))
    Call:
    coxph(formula = Surv(time = X, event = Delta) ~ D + offset(Y) + 
        offset(I(D * Y)))
    
         coef exp(coef) se(coef)      z        p
    D -0.9959    0.3694   0.2825 -3.525 0.000423
    
    Likelihood ratio test=12.26  on 1 df, p=0.0004635
    n= 100, number of events= 53 
    

    看着它,我想甚至可能会删除 I(),因为 offset 并没有使用公式解析逻辑。事实证明确实如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      相关资源
      最近更新 更多