随机效应的点图

时间:2012-06-20 15:40:29

标签: r plot lme4

我有一个广义的混合效果模型,如下所示:

d <- data.frame(
    g = sample(c("A","B","C","D","E"), 250, replace=TRUE),
    x = runif(250, max=100),
    y = sample(c(0,1), 250, replace=TRUE)
)

require(lme4)

fm <- lmer(y ~ x + (1 + x | g), data=d, family=binomial)

我想使用dotplot绘制截距的随机效果,但不绘制x的随机斜率分量。我的问题是我似乎无法弄清楚如何访问只是拦截组件而不是随机斜率。

例如,我想要的是这个情节的左侧:

dotplot(ranef(fm, postVar=TRUE))

enter image description here

即使我认为它应该使用dotplot(ranef(fm, postVar=TRUE)$g[,2])也不会给我我想要的东西!我看了str(fm),但没有看到任何帮助我更接近的东西。

非常感谢任何帮助和提示!

4 个答案:

答案 0 :(得分:3)

您的原始代码几乎就在那里:

dotplot(ranef(fm, postVar=TRUE))$g[1]

为每个情节释放比例的额外提示:

dotplot(ranef(fm, postVar=TRUE),
        scales = list(x =list(relation = 'free')))

答案 1 :(得分:2)

你需要更加聪明地删除其他随机效果列:

re <- ranef(fm,postVar = TRUE)
re$g$x <- NULL
dotplot(re)

你的另一个方法不起作用的原因是被调度的dotplot方法仍然期望它的输入看起来像一个元素列表,就像它来自ranef一样。所以你只需要进入并删除那个有问题的列,但保持结构的其余部分不受影响。

答案 2 :(得分:2)

你不是在看正确的str()。

re <- ranef(fm, postVar = TRUE)
str(re)

这会显示一个列表,其中第一个项目的数据框包含您想要的内容。我只需从中删除$ x列。

re[[1]]$x <- NULL

答案 3 :(得分:2)

这应该让你非常接近。我一直想将lme4对象的支持添加到coefplot,所以这可能是我的催化剂。

theRan <- ranef(fm, postVar=TRUE)
pv <- attr(theRan$g, "postVar")
se <- pv[1, 1, ]
theIntercepts <- theRan$g[, 1, drop=F]
theFrame <- cbind(theIntercepts, se)
names(theFrame)[1] <- "Intercept"
theFrame$Low <- with(theFrame, Intercept - 2 * se)
theFrame$High <- with(theFrame, Intercept + 2 * se)
theFrame$Variable <- rownames(theFrame)
ggplot(theFrame, aes(y=Intercept, x=reorder(Variable, Intercept))) + geom_linerange(aes(ymin=Low, ymax=High), colour="black") + geom_point(, colour="blue") + coord_flip() + labs(y="Intercept", x=NULL)
相关问题