【问题标题】:Iterative spearman correlation with cor.test?与cor.test的迭代spearman相关?
【发布时间】:2016-03-16 07:57:59
【问题描述】:

我是 R 的初学者,我在尝试在 R 中创建迭代 cor.test 时遇到了一些问题。我有一个包含 8 个不同采样点(第 1 到 8 列)的表,对于每个采样点,我测量了一个变量(VARIABLE1,第一行)和一系列物种的存在(行上的 OTU)。在这里您可以看到我的表格的摘录(称为“矩阵”):

row.names   1   2   3   4   5   6   7   8
VARIABLE1   1565    1809,83 1019    1909,83 756,33  631,67  529,83  436
OTU1    0   0   0   0   0   3   0   0
OTU2    0   0   0   0   0   0   13  0
OTU3    5   0   0   0   0   0   0   0
OTU4    0   0   0   0   0   0   0   0
OTU5    0   0   0   0   0   0   0   2
OTU6    0   0   19  0   9   236 59  2
OTU7    0   0   0   2   4   2   3   0
OTU8    0   0   10  5   0   0   7   0
OTU9    6   0   13  2   0   0   17  6
OTU10   0   0   0   0   0   3   0   0
OTU11   4   13  0   0   2   1   2   0
OTU12   0   0   0   0   0   101 1   0

我想计算 VARIABLE1 与每个 OTU 之间的 spearman 相关性。所以 VARIABLE1 必须保持固定,而 OTU 必须每次都改变。

我尝试使用“lapply”,但没有成功:

flip_matrix <- t(matrix)
variable1 <- flip_matrix[,1]
lapply(flip_matrix[1:107], function(x) cor.test(x, variable1, alternative='two.sided', method='spearman'))
 Error in cor.test.default(x, shoot_growth, alternative = "two.sided",  :  
            'x' e 'y' must be of the same length

我该如何解决这个问题?谢谢大家!!

【问题讨论】:

    标签: r matrix statistics iteration correlation


    【解决方案1】:

    你的意思是这样的吗?

    matrix <- matrix(
      c(1565, 1809.83, 1019, 1909.83, 756.33,  631.67, 529.83, 436,
        0,   0  , 0  , 0   ,0   ,3   ,0   ,0,
        0,   0,   0 ,  0   ,0   ,0   ,13  ,0,
        5 ,  0  , 0 ,  0   ,0   ,0   ,0   ,0,
        0 ,  0,   0  , 0   ,0   ,0   ,0   ,0,
        0 ,  0  , 0   ,0   ,0   ,0   ,0   ,2,
        0 ,  0  , 19  ,0   ,9   ,236 ,59  ,2,
        0 ,  0  , 0   ,2   ,4   ,2   ,3   ,0,
        0 ,  0  , 10  ,5   ,0   ,0   ,7   ,0,
        6 ,  0  , 13  ,2   ,0   ,0   ,17  ,6,
        0,   0 ,  0   ,0   ,0   ,3   ,0   ,0,
        4,   13,  0   ,0   ,2   ,1   ,2   ,0,
        0 ,  0 ,  0   ,0   ,0   ,101 ,1   ,0), ncol = 8, byrow = T)
    
    rownames(matrix) = c("VARIABLE1", paste("OTU", 1:12, sep = ""))
    
    test <- list()
    for (i in 2:nrow(matrix)) {
      test[[i]] <- cor.test(x = matrix[1,], y = matrix[i,], alternative="two.sided", method="spearman")
    }
    

    但我确实收到了警告消息,这可能是由于样本量小。

    【讨论】:

      【解决方案2】:

      使用 apply 而不是循环。您还可以获得测试的 p 值。

      df <- read.table(header=T,dec=",",text=c("row.names   1   2   3   4   5   6   7   8
      VARIABLE1   1565    1809,83 1019    1909,83 756,33  631,67  529,83  436
                                               OTU1    0   0   0   0   0   3   0   0
                                               OTU2    0   0   0   0   0   0   13  0
                                               OTU3    5   0   0   0   0   0   0   0
                                               OTU4    0   0   0   0   0   0   0   0
                                               OTU5    0   0   0   0   0   0   0   2
                                               OTU6    0   0   19  0   9   236 59  2
                                               OTU7    0   0   0   2   4   2   3   0
                                               OTU8    0   0   10  5   0   0   7   0
                                               OTU9    6   0   13  2   0   0   17  6
                                               OTU10   0   0   0   0   0   3   0   0
                                               OTU11   4   13  0   0   2   1   2   0
                                               OTU12   0   0   0   0   0   101 1   0"))
      dft <- t(df[,-1]) 
      res <- apply(dft[,-1], 2, function(x,y) cor.test(x,y,method = "spearman"),dft[,1])
      data.frame(do.call(rbind,res))
      

      或者使用Hmisc包的rcorr函数

      library(Hmisc)
      rcorr(dft,type = "spearman")
      

      【讨论】:

        猜你喜欢
        • 2018-02-01
        • 1970-01-01
        • 2020-10-02
        • 2018-05-09
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        • 2013-03-02
        • 2018-01-01
        相关资源
        最近更新 更多