首页 > 解决方案 > 具有二次项的 emmip (emmeans)

问题描述

当您有二次项时,是否可以用 emmip 绘制 geeglm 模型的边际(对数赔率)均值?我已经重复测量数据,并且模型更适合治疗 x 时间平方项以及具有线性时间的交互项。

我只想能够可视化数据中的预测曲线。如果可能的话,我不知道如何指定它。我试过了:

mod3 <- geeglm(outcome ~ treatment*time + treatment*time_sq, data = dat, id = id, family = "binomial", corstr = "exchangeable"))
mod3a.rg <- ref_grid(mod3, at = list(time = c(1,2,3,4,5,6), time_sq = c(1,4,9,16,25,36)))
emmip(mod3a.rg, treatment ~ time)

标签: rglmemmeans

解决方案


我认为您的 mod3 没有正确包含您的二次项(很难说,因为您没有包含可重现的代码)。这将让您正确地包含时间的平方项:

mod3 <- geeglm(outcome ~ treatment*time + treatment*I(time^2), data =
dat, id = id, family = "binomial", corstr = "exchangeable"))

添加plotit = TRUE到您的通话中emmip()

emmip(mod3a.rg, treatment ~ time, plotit = TRUE)

这是一个简单的可重现示例,其中包含 MASS 中的储蓄数据集,用于比较的遥远包

library(MASS)
data(savings, package="faraway") 

#fit model with polynomial term
mod <- lm(sr ~ ddpi+I(ddpi^2))
summary(mod)

摘要产生此输出,请注意二次项的附加系数

> Call: lm(formula = sr ~ ddpi + I(ddpi^2), data = savings)
> 
> Residuals:
>     Min      1Q  Median      3Q     Max 
> -8.5601 -2.5612  0.5546  2.5735  7.8080 
> 
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)
>(Intercept)  5.13038    1.43472   3.576 0.000821 ***
>ddpi         1.75752    0.53772   3.268 0.002026 ** 
>I(ddpi^2)   -0.09299    0.03612  -2.574 0.013262 *  
> --- Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> Residual standard error: 4.079 on 47 degrees of freedom Multiple
> R-squared:  0.205,    Adjusted R-squared:  0.1711  F-statistic: 6.059 on
> 2 and 47 DF,  p-value: 0.004559

如果您没有在I()摘要中包含二次项,则只会包含 ddpi 项。

mod2 <- lm(sr ~ ddpi+ddpi^2)
summary(mod2)

生成以下摘要,其系数仅为 ddpi

> lm(formula = sr ~ ddpi + ddpi^2, data = savings)
> 
> Residuals:
>     Min      1Q  Median      3Q     Max 
> -8.5535 -3.7349  0.9835  2.7720  9.3104 
> 
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)     
>(Intercept)   7.8830     1.0110   7.797 4.46e-10 *** 
>ddpi          0.4758     0.2146   2.217   0.0314 *  
> --- Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> Residual standard error: 4.311 on 48 degrees of freedom Multiple
> R-squared:  0.0929,   Adjusted R-squared:  0.074  F-statistic: 4.916 on
> 1 and 48 DF,  p-value: 0.03139

推荐阅读