首页 文章

迭代spearman与cor.test的相关性?

提问于
浏览
1

我是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

我怎么解决这个问题?谢谢大家!!

2 回答

  • 0

    使用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")
    
  • 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")
    }
    

    但是我收到了警告信息,可能是因为样本量很小 .

相关问题