如何使用plot_model来表示R中二次变量和虚拟变量之间的相互作用系数?

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

我正在尝试绘制 R 中多级逻辑回归中的平方变量和虚拟变量之间的交互作用。虽然我可以毫无问题地绘制这些与非二次变量的交互作用,但我不知道如何使用互动。

我可以完美地绘制这些交互:

plot_model(model, type="eff", 
            pred.type = "re", 
            terms = c("first_variable[all]","dummy_variable"),  show.data = F,ci.lvl=.95)

但是每当我尝试绘制这个

plot_model(model, type="eff", 
            pred.type = "re", 
            terms = c("I(first_variable^2)[all]","dummy_variable"),  show.data = F,ci.lvl=.95)


我总是得到这样的回应:

Error: Some of the specified 
条款
 were not found in the model. Maybe misspelled?

我尝试过拼写

first_variable^2
as.is(first_variable^2)
,但似乎没有任何效果。它是我的回归中包含的一个变量:
(glmer(y~first_variable+second_variable+I(first_variable^2)+I(first_variable^2):second_variable+(1|level2)+(1|level3), data = df3, family = binomial, nAGQ=1))

有什么办法可以解决这个问题吗?

r plot interaction multi-level quadratic
1个回答
0
投票

我最近对我自制的绘制非线性效应的函数发表了评论,但我刚刚了解到 sjPlot 现在可以很好地做到这一点(在我制作它的时候还做不到)。

使用时您的代码不起作用

terms = c("I(first_variable^2)[all]")

只需使用

terms = c("first_variable[all]")
即可获得所需的输出。除非您试图在不合并线性分量的情况下隔离与二次项的相互作用?

在下面的示例中,我得到了与二次项相互作用的图:

hdp <- read.csv("https://stats.idre.ucla.edu/stat/data/hdp.csv")
m <- glmer(remission ~ Sex*RBC + Sex*I(RBC^2) +
             (1 | DID), data = hdp, family = binomial(link = "logit"))

sjPlot::plot_model(m, type="eff", pred.type = "re", terms = c("RBC[all]", "Sex"), show.data = F, ci.lvl = .95)

绘制时我也得到相同的输出:

sjPlot::plot_model(m, type="pred", pred.type = "fe", terms = c("RBC[all]", "Sex"), show.data = F, ci.lvl = .95)
© www.soinside.com 2019 - 2024. All rights reserved.