Correlation/p values of all combinations of all rows of two matrices

后端 未结 4 1241
旧时难觅i
旧时难觅i 2020-12-20 04:20

I would like to calculate the correlation and the p value of that correlatio of each species (bac) to each of the factors (fac) in a second data frame. Both were measured at

4条回答
  •  眼角桃花
    2020-12-20 05:08

    You can just pass the full matrices to the cor function (or psych::corr.test)and it takes care of finding the correlation of the relevant columns.

    For example

    cor(t(fac), t(bac))
    #            bac1        bac2        bac3        bac4        bac5
    # fac1  0.9899495 -0.07559289 -0.60000000 -0.60000000 -0.07559289
    # fac2  0.9899495 -0.07559289 -0.60000000 -0.60000000 -0.07559289
    # fac3 -0.3207135  0.94285714 -0.07559289 -0.07559289 -0.48571429
    # fac4 -0.8000000 -0.32071349  0.98994949  0.98994949 -0.32071349
    # fac5 -0.3207135 -0.48571429 -0.07559289 -0.07559289  0.94285714
    # fac6         NA          NA          NA          NA          NA
    

    You can then turn this in to long format using reshape2::melt

    reshape2::melt(cor(t(fac), t(bac)))
    #    Var1 Var2       value
    # 1  fac1 bac1  0.98994949
    # 2  fac2 bac1  0.98994949
    # 3  fac3 bac1 -0.32071349
    # 4  fac4 bac1 -0.80000000
    # ---
    # ---
    

    To get the p-values use the same approach

    test <- psych::corr.test(t(fac), t(bac), adjust="none")
    

    And melt as before and join

    merge(melt(test$r, value.name="cor"), melt(test$p, value.name="p-value"), by=c("Var1", "Var2"))
    #   Var1 Var2         cor    p-value
    # 1 fac1 bac1  0.98994949 0.01005051
    # 2 fac1 bac2 -0.07559289 0.92440711
    # 3 fac1 bac3 -0.60000000 0.40000000
    # 4 fac1 bac4 -0.60000000 0.40000000
    # 5 fac1 bac5 -0.07559289 0.92440711
    # 6 fac2 bac1  0.98994949 0.01005051
    

提交回复
热议问题