绘制clmm的随机截距

时间:2014-04-28 15:01:39

标签: r ordinal ggplot2

我无法在31个国家/地区绘制来自clmm()模型的随机截距,其中包含4种随机效应。

我尝试过关注此帖子:In R, plotting random effects from lmer (lme4 package) using qqmath or dotplot: how to make it look fancy?但是,我无法获得显示的置信区间。我设法使用dotchart按国家/地区绘制截距。

library(ggplot2)
library(ordinal)

 # create data frame with intercepts and variances of all random effects
 # the first column are the grouping factor, followed by 5 columns of intercepts, 
 # columns 7-11 are the variances.
randoms <- as.data.frame(ranef(nodual.logit, condVar = F))
var     <- as.data.frame(condVar(nodual.logit))
df      <- merge(randoms, var, by ="row.names")

 # calculate the CI
df[,7:11] <- (1.96*(sqrt(df[,7:11])/sqrt(length(df[,1]))))

 # dot plot of intercepts and CI.
p <- ggplot(df,aes(as.factor(Row.names),df[,2]))
p <- p + geom_hline(yintercept=0) + 
     geom_errorbar(aes(xmax=df[,2]+df[,7], xmin=df[,2]-df[,7]), width=0, color="black") + 
     geom_point(aes(size=2))
p <- p + coord_flip()
print(p)
  

错误:提供给连续刻度的离散值

这是我尝试绘制它们的另一种方式:

D <- dotchart(df[,2], labels = df[,1])
D <- D + geom_errorbarh(aes(xmax=df[,2]+df[,7], xmin=df[,2]-df[,7],))
  

dotchart中的错误(df [,2],labels = df [,1])+ geom_errorbarh(aes(xmax = df [,:二元运算符的非数字参数

1 个答案:

答案 0 :(得分:0)

找到基于R.H.B Christensen (2013) “A Tutorial on fitting Cumulative Link Mixed Models with clmm2 from the ordinal Package” pg的解决方案。 5。

首先对所有31个国家/地区绘制拦截点,使用axis()添加标签,然后使用segments()添加CI。

plot(1:31,df[,2], ylim=range(df[,2]), axes =F, ylab ="intercept") 
abline(h = 0, lty=2)
axis(1, at=1:31, labels = df[,1], las =2)
axis(2, at= seq(-2,2, by=.5))
for(i in 1:31) segments(i, df[i,2]+df[i,7], i, df[i,2]-df[i, 7])

可以将此代码放入另一个循环中以绘制随机效果的Betas

for(n in 2:6) plot(1:31,df[,n], ylim=range(df[,n]),axes =F, ylab =colnames(df[n]))+
abline(h = 0, lty=2)+
axis(1, at=1:31, labels = df[,1], las =2)+
axis(2, at= seq(-2,2, by=.5))+
for(i in 1:31) segments(i, df[i,n]+df[i,(n+5)], i, df[i,n]-df[i, (n+5)])
相关问题