nested_lmer <- function(x, remove=0) { # Design specification: mtc nested within levels of cv x$design <- factor(paste(x$cv, x$mtc, sep="")) x$design <- C(x$design, matrix(c(-.5, +.5, 0, 0, # (1) mtc | cv=1 - invalid 0, 0, -.5, +.5, # (2) mtc | cv=2 - valid -.5, -.5, +.5, +.5 # (3) cv ), 4, 3), 3) # Remove subjects with zero-entry design cells if requested if (remove) { x.n <- table(x$ID, x$design) temp <- vector() for (i in 1:dim(x.n)[1]) { temp[i] <- any(x.n[i,]==0) } vid <- attr(x.n, "dimnames")[[1]][temp] # vid contains ids with 0 v <- vector() for (i in length(vid) ) { temp <- which(x$ID == vid[i]) v <- c(v, temp) # v contains records of ids } if(length(v) > 0) {x <- x[-v, ]} } print(table(x$ID, x$design)) m <- (lmer(rt ~ design + (1|ID), x )) m }