Han - 8 months ago 62

R Question

I have tried to plot random effects after

`clmm`

`library(ordinal)`

library(lattice)

###I am using the wine data in the ordinal package

d<-data.frame(wine)

result<-clmm(rating~1+temp+contact+(1+temp|judge), data=d)

###In my actual codes, I put "as.ordered(rating)" instead of "rating".

Then, I am trying to plot random effects of temp and judge:

`dotplot(ranef(result, condVar=TRUE))`

Then, the error message appears: "Error in sort.list(y):'x' must be atomic for `sort.list' Have you called 'sort' on a list?"

My best guess is that rating is implemented as ordered under

`clmm`

`result2<-lmer(as.numeric(rating)~1+temp+contact+(1+temp|judge), data=d)`

dotplot(ranef(result2, condVar=TRUE))

If you could give any comment or suggestion on how to circumvent the seemingly-conflicting situation in which using

`clmm`

`dotplot`

Answer

I started out trying to hack the guts of `lme4:::dotplot.ranef.mer`

, but it turned out to be easier to do this with `reshape2::melt`

and `ggplot2`

. Sorry if you have a really strong preference for `lattice`

...

Note by the way that, technically, these aren't "confidence intervals" (since the predicted conditional modes are not "estimates" in the technical, frequentist sense) ...

Rearrange point estimates and SEs of conditional modes into a useful shape:

```
library(reshape2)
melt.ranef.clmm <- function(re,cv) {
reList <- lapply(re,
function(x) {
x$id <- reorder(factor(rownames(x)),x[,1])
return(melt(x,id.var="id"))
})
cvList <- lapply(cv,melt,id.var=NULL,value.name="se")
mm <- Map(cbind,reList,cvList)
return(mm)
}
```

Apply this to a model (`m1`

is a fitted model):

```
ss <- melt.ranef.clmm(ranef(m1),condVar(m1))
```

Plot:

```
library(ggplot2); theme_set(theme_bw())
ggplot(ss[[1]],aes(value,id))+
geom_errorbarh(aes(xmin=value-1.96*se,xmax=value+1.96*se),
height=0)+
ggtitle(names(ss)[1])+
geom_point(colour="blue")+
facet_wrap(~variable,scale="free_x")
```