[R] Correlelogram with partial correlation coefficients
Bill Anderegg
anderegg at princeton.edu
Tue May 13 23:33:00 CEST 2014
Hi, I am trying to construct a correlelogram plot but plot partial correlation
coefficients, rather than normal coefficients. I've been using the
corrgram() and ggm() libraries to do correlelograms and partial correlation
analysis respectively, but I can't figure out how to combine them. Here is
my code (my dataset is "climvar4" and is a matrix with 11 climate variables with 51
observations per variable. so in each cell of the correlelogram I want to display the partial correlation between variable x and variable y, while accounting for all
other variables):
library(ggm)
library(corrgram)
panel.shadeNtextP <- function (x, y, corr = NULL, col.regions, ...)
{
corr <- pcor(x, y, var(climvar4))
results <- pcor.test(corr,10,51)
est <- results$p.value
stars <- ifelse(est < 5e-4, "***",
ifelse(est < 5e-3, "**",
ifelse(est < 5e-2, "*", "")))
ncol <- 14
pal <- col.regions(ncol)
col.ind <- as.numeric(cut(corr, breaks = seq(from = -1, to = 1,
length = ncol + 1),
include.lowest = TRUE))
usr <- par("usr")
rect(usr[1], usr[3], usr[2], usr[4], col = pal[col.ind],
border = NA)
box(col = "lightgray")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- formatC(corr, digits = 2, format = "f")
cex.cor <- .8/strwidth("-X.xx")
fonts <- ifelse(stars != "", 2,1)
# option 1: stars:
text(0.5, 0.4, paste0(r,"\n", stars), cex = cex.cor)
# option 2: bolding:
#text(0.5, 0.5, r, cex = cex.cor, font=fonts)
}
corrgram(climvar4, type="data", order=F, lower.panel=panel.shadeNtextP,
upper.panel=NULL, cex=1.3)
Thanks so much in advance!
Bill
More information about the R-help
mailing list