correlation matrix in R

后端 未结 1 874
独厮守ぢ
独厮守ぢ 2021-01-27 04:15

I am correlating several matrix, which contains [lat,lon,time] dimensions. For now, I am doing this by creating a function with a loop like:

  GetCorrelation <         


        
相关标签:
1条回答
  • 2021-01-27 04:41

    The command is actually very simple: cor(df):

    ddf = structure(list(vnum1 = c(0.715897737792417, 0.299617190296728, 
    -1.02251319233659, -0.862117012932794, 1.44997632983787, 1.65615043993346, 
    0.945107258196299, 0.568115024409375, 1.20791502923882, -1.04639514112998
    ), vnum2 = c(0.287509825313464, 0.19995830883272, 0.848034866852686, 
    0.544316479703411, 0.160545825958252, 0.398045151494443, 0.121440409682691, 
    0.0364419857505709, 0.105769601417705, 0.217918869107962), vint1 = c(2L, 
    7L, 2L, 3L, 4L, 10L, 5L, 4L, 2L, 9L), vint2 = c(8L, 12L, 6L, 
    5L, 3L, 3L, 7L, 12L, 11L, 8L)), .Names = c("vnum1", "vnum2", 
    "vint1", "vint2"), row.names = c(NA, 10L), class = "data.frame")
    
    ddf
            vnum1      vnum2 vint1 vint2
    1   0.7158977 0.28750983     2     8
    2   0.2996172 0.19995831     7    12
    3  -1.0225132 0.84803487     2     6
    4  -0.8621170 0.54431648     3     5
    5   1.4499763 0.16054583     4     3
    6   1.6561504 0.39804515    10     3
    7   0.9451073 0.12144041     5     7
    8   0.5681150 0.03644199     4    12
    9   1.2079150 0.10576960     2    11
    10 -1.0463951 0.21791887     9     8
    
    cor(ddf)
    
                vnum1      vnum2       vint1       vint2
    vnum1  1.00000000 -0.5660315  0.09141771 -0.08964374
    vnum2 -0.56603151  1.0000000 -0.17904374 -0.47127521
    vint1  0.09141771 -0.1790437  1.00000000 -0.15697672
    vint2 -0.08964374 -0.4712752 -0.15697672  1.00000000
    > 
    

    You can extend it according to your requirements.

    For matrices following may be useful:

    https://stats.stackexchange.com/questions/24980/correlation-between-matrices-in-r

    cor(matrix1, matrix2)
    cor(c(matrix1), c(matrix2)).
    cor(c(as.matrix(matrix1)), c(as.matrix(matrix2)))
    

    how do i calculate correlation between corresponding columns of two matrices and not getting other correlations as output

    diag(cor(a,b))
    mapply(cor(a,b))
    colCors(a,b)
    

    http://pbil.univ-lyon1.fr/ADE-4/ade4-html/mantel.rtest.html Mantel test (correlation between two distance matrices (in R).)

    library(ade4)
    mantel.rtest(m1, m2, nrepet = 99)
    

    You may also try converting matrix to vector (by as.vector(m) or as.vector(t(m)), combine these vectors as columns of a dataframe and then run cor(df). Since each column would represent one matrix, cor(df) will represent matrix of correlation between matrices.

    0 讨论(0)
提交回复
热议问题