Pairwise Correlation Table

前端 未结 4 1516
忘掉有多难
忘掉有多难 2020-12-30 04:59

I\'m new to R, so I apologize if this is a straightforward question, however I\'ve done quite a bit of searching this evening and can\'t seem to figure it out. I\'ve got a d

4条回答
  •  醉话见心
    2020-12-30 05:19

    Bill Venables offers this solution in this answer from the R mailing list to which I've made some slight modifications:

    cor.prob <- function(X, dfr = nrow(X) - 2) {
      R <- cor(X)
      above <- row(R) < col(R)
      r2 <- R[above]^2
      Fstat <- r2 * dfr / (1 - r2)
      R[above] <- 1 - pf(Fstat, 1, dfr)
    
      cor.mat <- t(R)
      cor.mat[upper.tri(cor.mat)] <- NA
      cor.mat
    }
    

    So let's test it out:

    set.seed(123)
    data <- matrix(rnorm(100), 20, 5)
    cor.prob(data)
    
              [,1]      [,2]      [,3]      [,4] [,5]
    [1,] 1.0000000        NA        NA        NA   NA
    [2,] 0.7005361 1.0000000        NA        NA   NA
    [3,] 0.5990483 0.6816955 1.0000000        NA   NA
    [4,] 0.6098357 0.3287116 0.5325167 1.0000000   NA
    [5,] 0.3364028 0.1121927 0.1329906 0.5962835    1
    

    Does that line up with cor.test?

    cor.test(data[,2], data[,3])
    
     Pearson's product-moment correlation
    data:  data[, 2] and data[, 3] 
    t = 0.4169, df = 18, p-value = 0.6817
    alternative hypothesis: true correlation is not equal to 0 
    95 percent confidence interval:
     -0.3603246  0.5178982 
    sample estimates:
           cor 
    0.09778865 
    

    Seems to work ok.

提交回复
热议问题