[R] function for nice correlation output with significance symbols
Chuck Cleland
ccleland at optonline.net
Sun May 10 22:36:34 CEST 2009
On 5/10/2009 3:26 PM, Martin Batholdy wrote:
> hi,
>
> I am searching for a nice function which computes correlations out of a
> data.frame and adds asterix-signs after each correlation when they are
> significant...
>
> ...or a function which just show correlations greater than x in the output.
>
> thanks!
This might be a starting point:
corstars <- function(x){
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
mystars <- ifelse(p < .01, "**|", ifelse(p < .05, "* |", " |"))
R <- format(round(cbind(rep(-1.111, ncol(x)), R), 3))[,-1]
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
diag(Rnew) <- paste(diag(R), " |", sep="")
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- paste(colnames(x), "|", sep="")
Rnew <- as.data.frame(Rnew)
return(Rnew)
}
corstars(swiss[,1:4])
Fertility| Agriculture| Examination| Education|
Fertility 1.000 | 0.353* | -0.646**| -0.664**|
Agriculture 0.353* | 1.000 | -0.687**| -0.640**|
Examination -0.646**| -0.687**| 1.000 | 0.698**|
Education -0.664**| -0.640**| 0.698**| 1.000 |
At the very least, you could add a note that indicates what different
numbers of stars mean.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list