使用来自混合效应模型(lme4)和模型平均(MuMIn)的二项式数据绘制逻辑回归结果 [英] Plotting results of logistic regression with binomial data from mixed effects model (lme4) with model averaging (MuMIn)

查看:660
本文介绍了使用来自混合效应模型(lme4)和模型平均(MuMIn)的二项式数据绘制逻辑回归结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示逻辑回归的结果.我的模型适合使用lme4软件包中的glmer(),然后使用MuMIn进行模型平均.

I'm trying to display the results of a logistic regression. My model was fit using glmer() from the lme4 package, I then used MuMIn for model averaging.

使用mtcars数据集的模型的简化版本:

Simplified version of my model using the mtcars dataset:

glmer(vs ~ wt +  am + (1|carb), database, family = binomial, na.action = "na.fail")

我想要的输出是两个图,显示了vs = 1的预测概率,一个表示wt,它是连续的,一个表示am,它是二项式的.

My desired output is two plots that show the predicted probability that vs=1, one for wt, which is continuous, one for am, which is binomial.

已更新:

在@KamilBartoń发表评论后,我做了很多工作:

I got this much working after comments from @KamilBartoń:

database <- mtcars

# Scale data
database$wt <- scale(mtcars$wt)
database$am <- scale(mtcars$am)

# Make global model
model.1 <- glmer(vs ~ wt + am + (1|carb), database, family = binomial, na.action = "na.fail")

# Model selection
model.1.set <- dredge(model.1, rank = "AICc")

# Get models with <10 delta AICc
top.models.1 <- get.models(model.1.set,subset = delta<10)

# Model averaging
model.1.avg <- model.avg(top.models.1)

# make dataframe with all values set to their mean
xweight <- as.data.frame(lapply(lapply(database[, -1], mean), rep, 100))

# add new sequence of wt to xweight along range of data
xweight$wt <- (wt = seq(min(database$wt), max(database$wt), length = 100))

# predict new values
yweight <- predict(model.1.avg, newdata = xweight, type="response", re.form=NA)

# Make plot 
plot(database$wt, database$vs, pch = 20, xlab = "WEIGHT (g)", ylab = "VS")

# Add predicted line
lines(xweight$wt, yweight)

产生:

剩下的问题是数据缩放并围绕0居中,这意味着无法解释图形.我可以使用@BenBolker对

The remaining issue is that the data are scaled and centred around 0, meaning interpretation of the graph is impossible. I'm able to unscale the data using an answer from @BenBolker to this question but this does not display correctly:

## Ben Bolker's unscale function:
## scale variable x using center/scale attributes of variable y
scfun <- function(x,y) {
  scale(x,
        center=attr(y,"scaled:center"),
        scale=attr(y,"scaled:scale"))
        }

## scale prediction frame with scale values of original data -- for all variables
xweight_sc <- transform(xweight,
                        wt = scfun(wt, database$wt),
                        am = scfun(am, database$am))

# predict new values
yweight <- predict(model.1.avg, newdata = xweight_sc, type="response", re.form=NA)

# Make plot 
plot(mtcars$wt, mtcars$vs, pch = 20, xlab = "WEIGHT (g)", ylab = "VS")

# Add predicted line
lines(xweight$wt, yweight)

产生:

我可以看到绘图线在那里,但是它在错误的位置.我已经尝试了几种不同的方法,但无法解决问题所在. 我做错了什么?

I can see the plot line is there but it's in the wrong place. I've tried this a few different ways but can't work out what the problem is. What have I done wrong?

还存在另一个问题:如何为am绘制二项式图?

Also, another remaining issue: How do I make a binomial plot for am?

推荐答案

您可以使用 ggeffects-package ,使用ggpredict()ggeffect()(有关这两个功能的区别,请参见?ggpredict,第一个调用predict(),第二个调用effects::Effect()).

You can use the ggeffects-package for this, either with ggpredict() or ggeffect() (see ?ggpredict for the difference for these two functions, the first calls predict(), the latter effects::Effect()).

library(ggeffects)
library(sjmisc)
library(lme4)
data(mtcars)

mtcars <- std(mtcars, wt)
mtcars$am <- as.factor(mtcars$am)

m <- glmer(vs ~ wt_z + am + (1|carb), mtcars, family = binomial, na.action = "na.fail")

# Note the use of the "all"-tag here, see help for details
ggpredict(m, "wt_z [all]") %>% plot()

ggpredict(m, "am") %>% plot()

这篇关于使用来自混合效应模型(lme4)和模型平均(MuMIn)的二项式数据绘制逻辑回归结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆