我用corrgram包构建了一个相关矩阵,我做了一些配置。我想使用spearman相关方法。这段代码可以吗?
panel.shadeNtext <- function (x, y, corr = NULL, col.regions, ...)
{
corr <- cor(x, y, use = "pair")
results <- cor.test(x, y, alternative = "two.sided")
est <- results$p.value
stars <- ifelse(est < 0.001, "***",
ifelse(est < 0.01, "**",
ifelse(est < 0.05, "*", "")))
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 <- .4/strwidth("-X.xx")
fonts <- ifelse(stars != "", 2,1)
text(0.5, 0.5, paste0(r,"\n", stars), cex = cex.cor)
}
# Generate some sample data
sample.data <- matrix(rnorm(100), ncol=10)
# Call the corrgram function with the new panel functions
corrgram(sample.data, type="data", lower.panel=panel.shadeNtext,
upper.panel=NULL,cor.method="spearman")