向量到矩阵格式的方差协方差矩阵

时间:2019-09-04 01:27:50

标签: r statistics

我有两个矢量形式的方差协方差矩阵NxN。 一个向量包含方差值,另一向量包含协方差。 我举了一个例子,用简化的N来说明。 真正的问题是1500x1500矩阵:

我所拥有的:

    library(tidyverse)
    N = 4
    names <- c("a","b","c","d")

     matrix_var_cov <- matrix(data = NA, nrow = N, ncol = N) %>% 
    `colnames<-`(.,names) %>% `rownames<-`(.,names)

    variance <- as.data.frame(c("aa","bb","cc","dd")) %>% `colnames<- 
    `(.,"Covariance")

    covariance <- as.data.frame(c("ab","ac","bc","ad","bd","cd")) %>% 
    `colnames<-`(.,"Variance")

从协方差数据框中可以看到,顺序是按列给出的。 我来自B栏:AB, 在C列中:AC和BC, 在D列中:AD,BD和CD等。

从我的角度来看,有两种方法可以解释我刚才所说的内容。

我需要什么作为输出:

output <- data.frame(
  c("aa","ab","ac","ad"),
  c("ab","bb","bc","bd"),
  c("ac","cb","cc","cd"),
  c("ad","bd","cd","dd")) %>% 
   `colnames<-`(.,names) %>% `rownames<-`(.,names)

所以我真正需要的是将DF与方差和协方差信息一起使用。 有什么聪明的方法可以做到这一点吗? 不,信息来源无法更改。

1 个答案:

答案 0 :(得分:0)

一个选项是直接指定对角线元素和非对角线元素。 gdata提供函数upperTrianglelowerTriangle,在这里我们可以按行提供数据(基R的lower.triupper.tri可以按行提供条目)。

# Sample data
mat <- matrix(data = NA, nrow = N, ncol = N, dim = list(names, names))
variance <- c("aa","bb","cc","dd")
covariance <- c("ab","ac","bc","ad","bd","cd")

library(gdata)
diag(mat) <- variance
lowerTriangle(mat, byrow = T) <- covariance
upperTriangle(mat, byrow = T) <- lowerTriangle(mat)
mat
#    a    b    c    d
#a "aa" "ab" "ac" "ad"
#b "ab" "bb" "bc" "bd"
#c "ac" "bc" "cc" "cd"
#d "ad" "bd" "cd" "dd"

我们可以通过(1)填充上三角矩阵,(2)转置矩阵以使下三角矩阵的输入顺序正确,以及(3)最后填充上三角矩阵,在基R中实现相同的效果

# Sample data
mat <- matrix(data = NA, nrow = N, ncol = N, dim = list(names, names))
variance <- c("aa","bb","cc","dd")
covariance <- c("ab","ac","bc","ad","bd","cd")

diag(mat) <- variance
mat[upper.tri(mat)] <- covariance
mat <- t(mat)
mat[upper.tri(mat)] <- covariance
mat
#    a    b    c    d
#a "aa" "ab" "ac" "ad"
#b "ab" "bb" "bc" "bd"
#c "ac" "bc" "cc" "cd"
#d "ad" "bd" "cd" "dd"

请注意,您的预期输出中似乎有一个错字,其中给出了"cb"条目,而您的协方差向量中不存在该条目。