如何在 R 中向 GLM 模型添加偏移量?

问题描述 投票:0回答:1

我正在解决 Agresti 的分类数据分析练习 4.10。

他提供桌子

     Aortic | Mitral

<55       4       1
       1259    2082

55+       7       9
       1417    1647

行代表年龄组,列代表心脏瓣膜类型,每个单元格中的两个数字是死亡人数(顶部)和总时间长度(底部)。然后他提出了模型 $$ \log(\mu_{ij}) = lpha + 1 imes \log(t_{ij}) + eta_1 a_i + eta_2 u_j $$ 其中$t_{ij}$为单元格$(i,j)的总时间长度,$$a_i$为“55+”的指示变量,$ u_j$ “二尖瓣”指标。

练习 4.10 然后要求找到 $ eta_1.$ 的 95% 轮廓似然置信区间。我明白这在数学上意味着什么,但我在

R
中的实际编码方面遇到了麻烦。我正在使用
R
库“ProfileLikelihood”。

这是我的尝试:

heart_table <- data.frame(
  y = c(9,7,1,4),
  tot_time = c(1647,1417,2082,1259),
  age = c(1,1,0,0), #1 means 55+
  valve = c(1,0,1,0) #1 means mitral
)
heart_prof <- profilelike.glm(
  y ~ valve,
  data = heart_table,
  family = poisson(),
  offset.glm = log(tot_time),
  profile.theta = "age",
  lo.theta = -.5,
  hi.theta = 3.5
)

它会产生错误

Error: object 'tot_time' not found

如果我引用

log(tot_time)
我会得到一个不同的错误,即

heart_prof <- profilelike.glm(
  y ~ valve,
  data = heart_table,
  family = poisson(),
  offset.glm = "log(tot_time)",
  profile.theta = "age",
  lo.theta = -.5,
  hi.theta = 3.5
)

产生

Error in model.frame.default(formula = y ~ -1 + X + offset(pi * theta.off) +  : 
  invalid type (list) for variable 'offset(glm.off)'

我也尝试过使用公式

y ~ valve + offset.glm(log(tot_time))

有错误

Error in offset.glm(log(tot_time)) : could not find function "offset.glm"

还有公式

y ~ valve + offset(log(tot_time))

不会产生错误(它会运行)。然而,这感觉是错误的,因为 profilelike.glm 的文档指出“不应提供偏移量。而是使用 offset.glm。”

如有任何帮助,我们将不胜感激。

ProfileLikelihood 文档:https://cran.r-project.org/web/packages/ProfileLikelihood/ProfileLikelihood.pdf

r offset
1个回答
1
投票

我没有,也不知道那个包裹,但你不需要它。您可以在基数 R 中拟合泊松回归模型,并且可以通过调用

confint
分析可能性来获得置信区间。考虑:

> heart_table <- data.frame(
+   y = c(9,7,1,4),
+   tot_time = c(1647,1417,2082,1259),
+   age = c(1,1,0,0), #1 means 55+
+   valve = c(1,0,1,0) #1 means mitral
+ )
> m = glm(y~valve+offset(log(tot_time)), data=heart_table, family="poisson")
> summary(m)

Call:
glm(formula = y ~ valve + offset(log(tot_time)), family = "poisson", 
    data = heart_table)

Deviance Residuals: 
      1        2        3        4  
 1.9095   0.4718  -2.3931  -0.5383  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  -5.4942     0.3015 -18.222   <2e-16 ***
valve        -0.4271     0.4369  -0.978    0.328    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 10.8405  on 3  degrees of freedom
Residual deviance:  9.8857  on 2  degrees of freedom
AIC: 27.013

Number of Fisher Scoring iterations: 5

> confint(m)
Waiting for profiling to be done...
                2.5 %     97.5 %
(Intercept) -6.149598 -4.9562364
valve       -1.302631  0.4365193
© www.soinside.com 2019 - 2024. All rights reserved.