Skip to content
Prev 179874 / 398502 Next

function for nice correlation output with significance symbols

On 5/10/2009 3:26 PM, Martin Batholdy wrote:
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.