#Oskar hagberg 2012-04-26 # A function that plots coefficient estimates the LM function # with 95 % confidence intervals. If sort = TRUEm the effect # estimates are plotted in P value order. # By default, singificant estimates get red, but the color can # be changed by providing another value for signif. plotCoeff <- function(LM,sort = FALSE,signcol = "red"){ Coeff = summary(LM)\$coeff if(sort) Coeff = Coeff[order(Coeff[,"Pr(>|t|)"]),] plotidx = row.names(Coeff) != "(Intercept)" colors = ifelse(Coeff[plotidx,"Pr(>|t|)"]<0.05,signcol,"black") tqtile = qt(0.975,LM\$df.residual) estimate = Coeff[plotidx,"Estimate"] leftCI = estimate-tqtile*Coeff[plotidx,"Std. Error"] rightCI = estimate+tqtile*Coeff[plotidx,"Std. Error"] yvals = 1:sum(plotidx) plot( c(leftCI,estimate,rightCI), rep(yvals,3), axes = FALSE, ylab = "", xlab = "", main = "95 % Confidence Intervals for Coefficient Estimates", type = "n" ) grid() axis(1) axis(2,at = yvals,labels = row.names(Coeff)[plotidx],las = 1,tick = FALSE,line =NA) for(ii in 1:sum(plotidx)) lines(c(leftCI[ii],rightCI[ii]),c(ii,ii),lwd = 7,col = colors[ii]) points(estimate,1:sum(plotidx),pch = 16,col = "white",cex = 1) }