######################################################################################## # Help Functions for the analysis of data from the Peer Review Game # Date: 6-8-11 # Copyright (C) 2011 Jeffrey T. Leek (http://www.biostat.jhsph.edu/~jleek/contact.html) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, see . # # Depends: RColorBrewer, MASS, lme4, pixmap ######################################################################################## library(RColorBrewer) library(lme4) library(pixmap) mypar <- function(a=1,b=1,brewer.n=12,brewer.name="Paired",...){ par(mar=c(2.5,2.5,1.6,1.1),mgp=c(1.5,.5,0)) par(mfrow=c(a,b),...) palette(brewer.pal(brewer.n,brewer.name)) } mypar() # The p.values.lmer function is reproduced from the blog: # http://blog.lib.umn.edu/moor0554/canoemoore/2010/09/lmer_p-values_lrt.html # and is due to Chrisotpher Moore (moor0554@umn.edu) p.values.lmer <- function(x) { summary.model <- summary(x) data.lmer <- data.frame(model.matrix(x)) names(data.lmer) <- names(fixef(x)) names(data.lmer) <- gsub(pattern=":", x=names(data.lmer), replacement=".", fixed=T) names(data.lmer) <- ifelse(names(data.lmer)=="(Intercept)", "Intercept", names(data.lmer)) string.call <- strsplit(x=as.character(x@call), split=" + (", fixed=T) var.dep <- unlist(strsplit(x=unlist(string.call)[2], " ~ ", fixed=T))[1] vars.fixef <- names(data.lmer) formula.ranef <- paste("+ (", string.call[[2]][-1], sep="") formula.ranef <- paste(formula.ranef, collapse=" ") formula.full <- as.formula(paste(var.dep, "~ -1 +", paste(vars.fixef, collapse=" + "), formula.ranef)) data.ranef <- data.frame(x@frame[, which(names(x@frame) %in% names(ranef(x)))]) names(data.ranef) <- names(ranef(x)) data.lmer <- data.frame(x@frame[, 1], data.lmer, data.ranef) names(data.lmer)[1] <- var.dep out.full <- lmer(formula.full, data=data.lmer, REML=F) p.value.LRT <- vector(length=length(vars.fixef)) for(i in 1:length(vars.fixef)) { formula.reduced <- as.formula(paste(var.dep, "~ -1 +", paste(vars.fixef[-i], collapse=" + "), formula.ranef)) out.reduced <- lmer(formula.reduced, data=data.lmer, REML=F) print(paste("Reduced by:", vars.fixef[i])) print(out.LRT <- data.frame(anova(out.full, out.reduced))) p.value.LRT[i] <- round(out.LRT[2, 7], 3) } summary.model@coefs <- cbind(summary.model@coefs, p.value.LRT) summary.model@methTitle <- c("\n", summary.model@methTitle, "\n(p-values from comparing nested models fit by maximum likelihood)") print(summary.model) } cplot <- function(x, y, radius, nv = 3,...){ plot(1,1,xlim=1.15*c(-radius,radius),ylim=1.15*c(-radius,radius),type="n",xaxt="n",yaxt="n",xlab="",ylab="",axes=F) xylim <- par("usr") plotdim <- par("pin") ymult <- (xylim[4] - xylim[3])/(xylim[2] - xylim[1]) * plotdim[1]/plotdim[2] angle.inc <- 2 * pi/nv angles <- seq(0, 2 * pi - angle.inc, by = angle.inc) xv <- cos(angles) * radius + x yv <- sin(angles) * radius * ymult + y points(xv,yv,...) return(list(x=xv,y=yv)) } cplotPicture <- function(x, y, radius, nv = 3,sz=0.4,...){ plot(1,1,xlim=1.15*c(-radius,radius),ylim=1.15*c(-radius,radius),type="n",xaxt="n",yaxt="n",xlab="",ylab="",axes=F) xylim <- par("usr") plotdim <- par("pin") ymult <- (xylim[4] - xylim[3])/(xylim[2] - xylim[1]) * plotdim[1]/plotdim[2] angle.inc <- 2 * pi/nv angles <- seq(0, 2 * pi - angle.inc, by = angle.inc) xv <- cos(angles) * radius + x yv <- sin(angles) * radius * ymult + y for(i in 1:length(xv)){ addlogo(manbw,c(xv[i]-sz,xv[i]+sz),c(yv[i]-sz,yv[i]+sz)) } # points(xv,yv,...) return(list(x=xv,y=yv)) } make.consecutive.int <- function(y) { oldWarn = getOption("warn") ## Turn off warnings. options(warn = -1) if(is.null(y)) {return(NULL)} if(!is.vector(y)) y = as.vector(as.character(y)) out <- as.integer(as.factor(as.character(y)))-1 options(warn = oldWarn) return(out) }