我正在尝试使用二项式 logit 链接函数绘制混合模型的边际效应。
我的问题是我最初使用缩放的连续变量运行模型,然后在ggpredict()
函数中使用它。
但是,当我尝试绘制边际效应时,边际效应的 x 轴使用缩放变量,我想用原始连续未缩放数据绘制它。从而使其更具可解释性。
我不确定如何做到这一点。我附上了一个最小可重复的例子。
#data
dat <- data.frame(age = seq(1,5, by = 1),
sex = as.factor(c("M", "F")),
cluster = runif(20, min=0, max=100),
household = runif(50, min=0, max=100),
temp = runif(100, min=0, max=100),
prec = runif(100, min=0, max=100),
hum = runif(100, min=0, max=100),
disease = c(1,0))
#scale continuous variables
pvars <- c("temp", "prec", "hum")
datsc <- dat
datsc[pvars] <- lapply(datsc[pvars],scale)
#setting the control to run faster
contr1 <- glmerControl(optimizer = "nloptwrap", calc.derivs = FALSE)
#run glmer model
model1 <- glmer(disease ~ hum + temp + prec + (1|cluster/household),
family = binomial("logit"),
data = datsc, control = contr1)
modelsummary<- summary(model1)
#calculate marginal effects
humidity <- data.frame(ggpredict(model1, term = "hum [all]", type = "fe"))
humidity_plot <- ggplot(data=humidity, aes(x=x, y=predicted)) +
geom_line(size = 1) +
geom_ribbon(aes(ymin=conf.low, ymax=conf.high, fill = group), linetype=2, alpha=0.1) +
scale_y_continuous(limits = c(0, 1), breaks = c(seq(0,1, by = 0.2))) +
xlab("Humidity") +
ylab("Probability of Disease") + theme_classic()
任何帮助将非常感激。