计算协方差矩阵而不用for循环

时间:2016-06-29 07:02:53

标签: r matrix covariance lapply

如何在不使用for循环的情况下计算协方差矩阵?

这是一个矩阵:

ts <- structure(c(-0.63, NaN, -0.3, 0.48, 1.24, 1.39, 0.13, -0.03, 
-0.03, 0.32, 0.38, 0.32, -0.05, 0.22, 0.02, -0.04, -0.38, -0.05, 
0.57, -0.14, 0.05, 0.59, -1.07, NaN), .Dim = c(6L, 4L))

ts
      [,1]  [,2]  [,3]  [,4]
[1,] -0.63  0.13 -0.05  0.57
[2,]   NaN -0.03  0.22 -0.14
[3,] -0.30 -0.03  0.02  0.05
[4,]  0.48  0.32 -0.04  0.59
[5,]  1.24  0.38 -0.38 -1.07
[6,]  1.39  0.32 -0.05   NaN

我想计算一个协方差矩阵,该矩阵给出矩阵的四个col的所有可能对的协方差,输出格式为:

                    c11, c12, c13, c14,    
                    c21, c22, c23, c24,    
                    c31, c32, c33, c34,    
                    c41, c42, c43, c44

我可以用两个for循环来做到这一点:

csst <- matrix(0, nrow = 4, ncol = 4) # create empty covariance matrix to store the output of the loop

for(q in 1:4){ # loop over rows

  for(r in q:4){ # loop over columns with r>=q

      i <- which(!is.nan(ts[, q]))
      j <- which(!is.nan(ts[, r]))
      k <- intersect(i, j)
      nk <- length(k)

    # store value in matrix  
    csst[q, r] <-  sum((((ts[k, q] - mean(ts[k, q])) * (ts[k, r] - mean(ts[k, r]))) / (nk-1)))

    # make matrix symmetrical
    csst[r, q] <- csst[q, r] 
  }
}

结果是:

csst
           [,1]        [,2]        [,3]       [,4]
[1,]  0.8091300  0.12709500 -0.07910000 -0.4817833
[2,]  0.1270950  0.03397667 -0.02720667 -0.0352500
[3,] -0.0791000 -0.02720667  0.03734667  0.0811750
[4,] -0.4817833 -0.03525000  0.08117500  0.4600000

我已尝试使用expand.gridcombnlapply,但无法获得相同的结果。目标是使用更高效的代码和更少的输入来执行此操作。

1 个答案:

答案 0 :(得分:3)

怎么样:

cov(ts, use = "pairwise.complete")
           [,1]        [,2]        [,3]       [,4]
[1,]  0.8091300  0.12709500 -0.07910000 -0.4817833
[2,]  0.1270950  0.03397667 -0.02720667 -0.0352500
[3,] -0.0791000 -0.02720667  0.03734667  0.0811750
[4,] -0.4817833 -0.03525000  0.08117500  0.4600000