More ANOVA Data in R

In Catherine’s phylogenetic research, she has had the need recently to do some ANOVA analysis on a data set for her current project. Luckily, R has it’s stats module which has good support for this analysis via it’s cor function. However, the cor function, only returns the correlation matrix.

However, there is other relevant information generated from ANOVA that is relevant to the work that Catherine is doing, and is returned on a pair of columns from the cor.test function. She was mostly interested in the p-value, but cor.test also makes available a few other data fields.

To meet Catherine’s immediate need, I wrote the following function, which returns a list of matrices of results from cor.test, the first being the p-values, the second being the t-values, then the parameter, and finally the correlation.

corValues <- function(x) {
    if (!is.matrix(x))
        x <- as.matrix(x)

    size <- attributes(x)$dim[2]
    p = matrix(nrow=size, ncol=size)
    t = matrix(nrow=size, ncol=size)
    df = matrix(nrow=size, ncol=size)
    cor = matrix(nrow=size, ncol=size)

    i <- 1
    while(i <= size) {
        j <- i
        while (j <= size) {
            rv <- cor.test(x[,i], x[,j])
            t[i,j] = rv$statistic
            t[j,i] = rv$statistic
            df[i,j] = rv$parameter
            df[j,i] = rv$parameter
            p[i,j] = rv$p.value
            p[j,i] = rv$p.value
            cor[i,j] = rv$estimate
            cor[j,i] = rv$estimate
            j <- j + 1
        i <- i + 1
    list(p, t, df, cor)

It’s noticeably slower than the cor implementation, but it works fast enough. Mainly, I’d like to see this cleaned up to the point that I can at least take arguments similarly to the way the available methods do, but if you’ve got a matrix of data you want more than just correlation values for, the above does work fairly well.