I'm having trouble plotting random intercepts from a clmm() model with 4 random effects in 31 countries.

I tried following this SO post: In R, plotting random effects from lmer (lme4 package) using qqmath or dotplot: how to make it look fancy? However, I cannot get the confidence intervals to show up. I've managed to use dotchart to plot the intercepts by country.

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)

Error: Discrete value supplied to continuous scale

Here is another way I tried to plot them:

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

Error in dotchart(df[, 2], labels = df[, 1]) + geom_errorbarh(aes(xmax = df[, : non-numeric argument to binary operator

有帮助吗?

解决方案

Found a solution based on R.H.B Christensen (2013) “A Tutorial on fitting Cumulative Link Mixed Models with clmm2 from the ordinal Package” pg. 5.

First plot intercept points for all 31 countries, the add labels using axis(), then add CI’s using segments().

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])

Can put this code into another loop to plot the Betas of the random effects

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)])
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top