[R] A possible too old question on significant test of correlation matrix

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jul 10 13:48:13 CEST 2006


On Mon, 2006-07-10 at 16:22 +0800, Guo Wei-Wei wrote:
> Hi, Gavin, your program is excellent. Thank  you very much!
> 
> And I have two further questions.
> 
> 1. Since it is very possible that the data contains missing value and
> the program will failed against missing values, I have to delete all
> the cases contained NA. Can it be done pairwisely?

Yes, with a modification to accept and pass on argument "use", e.g.:

data(iris)
## copy data
iris2 <- iris
## simulate some missing values in Sepal.Length
iris2[sample(1:nrow(iris2), 5), 1] <- NA

## corProb matrix with missing values
temp <- corProb(iris2[,1:4], use = "pairwise.complete.obs")

See ?cor for the options you can specify for "use". You'll need to paste
in the functions below for this to work.

> 2. Can the program show t values instead of p values?

Yes - this is R! The function Bill Venables wrote uses F-values, so I
looked at what cor.test was doing and modified the function to compute
either t or F values and to return them or their p-values.

temp <- corProb(iris[,1:4])
temp <- corProb(iris[,1:4], type = "t")
temp <- corProb(iris[,1:4], type = "t", pval = FALSE)

For t-values you can do the different test as in ?cor.test :

## with different tests
temp <- corProb(iris[,1:4], type = "t",
                alternative = "less")
temp <- corProb(iris[,1:4], type = "t",
                alternative = "greater")

Hopefully that does what you wanted.

G

## New functions.

corProb <- function(X, dfr = nrow(X) - 2,
                    use = c("all.obs", "complete.obs",
                      "pairwise.complete.obs"),
                    alternative = c("two.sided", "less",
                      "greater"),
                    type = c("F", "t"),
                    pval = TRUE) {
  USE <- match.arg(use)
  ALTERNATIVE <- match.arg(alternative)
  R <- cor(X, use = USE)
  above <- row(R) < col(R)
  r2 <- R[above]^2
  TYPE <- match.arg(type)
  if(TYPE == "t") {
    Tstat <- sqrt(dfr) * R[above]/sqrt(1 - r2)
    if(pval) {
      p <- pt(Tstat, dfr)
      R[above] <- switch(ALTERNATIVE, less = p,
                         greater = 1 - p, 
                         two.sided = 2 * min(p, 1 - p))
    }
    else
      R[above] <- Tstat
  }
  else {
    Fstat <- r2 * dfr / (1 - r2)
    if(pval)
      R[above] <- 1 - pf(Fstat, 1, dfr)
    else
      R[above] <- Fstat
  }
  class(R) <- "corProb"
  attr(R, "type") <- TYPE
  attr(R, "pval") <- pval
  attr(R, "hypoth") <- ALTERNATIVE
  R
}

print.corProb <- function(x, digits = getOption("digits"),
                          quote = FALSE, na.print = "",
                          justify = "none", ...) {
  xx <- format(unclass(round(x, digits = 4)), digits = digits,
               justify = justify)
  if (any(ina <- is.na(x)))
    xx[ina] <- na.print
  cat("\nCorrelations are shown below the diagonal\n")
  if(attr(x, "pval"))
     cat(paste("P-values of the ", attr(x, "type"),
               "-statistics are shown above the diagonal\n\n",
               sep = ""))
  else
     cat(paste(attr(x, "type"),
               "-values are shown above the diagonal\n\n",
               sep = ""))
  if(attr(x, "type") == "t") 
    hypoth <- switch(attr(x, "hypoth"),
                     less = "less than 0",
                     greater = "greater than 0",
                     two.sided = "not equal to 0")
  cat(paste("alternative hypothesis: true correlation is",
            hypoth, "\n\n"))
  print.default(xx, quote = quote, ...)
  invisible(x)
}

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC & ENSIS, UCL Geography,  [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/cv/
 London, UK. WC1E 6BT.         [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list