【问题标题】:Manually creating a correlation matrix as input for rnorm_multi()手动创建相关矩阵作为 rnorm_multi() 的输入
【发布时间】:2023-01-07 05:38:45
【问题描述】:

我正在尝试使用来自faux 的函数rnorm_multi() 生成一个数据框。 对于我的x 论点,我想要一个相关矩阵,它包括从 0 到 1 的所有相关性,步长为 .01。

但是,无论我尝试什么,我都无法这样做,因为该函数返回错误Error in cormat(r, vars) : correlation matrix not positive definite。 有什么方法可以将这个特定矩阵用作rnorm_multi() 的输入相关矩阵吗?还是根本不可能?

library(faux)
# create an empty 100x100 matrix, fill the lower half with my desired values, transpose it and repeat to make it symmetrical. 

matrix <- matrix(rep(0, 100*100), nrow = 100) 
matrix[lower.tri(matrix)] <- seq(0, .99, .01) 
matrix <- t(matrix) 
matrix[lower.tri(matrix)] <- seq(0, .99, .01) 
diag(matrix) <- 1 # diagonal in correlation matrices should be 1


# generate a dataframe based on the correlation matrix from above

df <- rnorm_multi(100, 100, 0, 1, r = matrix, 
                  varnames = paste("x", seq(0, 1, .01), sep = "."))

# this returns

Error in cormat(r, vars) : correlation matrix not positive definite

【问题讨论】:

    标签: r matrix linear-algebra data-generation


    【解决方案1】:

    我遇到了同样的问题 - 有些值太接近 1。我使用 matrixcalc::is.positive.definite() 和矩阵的不同子集来帮助找到这些值的位置,然后手动将它们减少一点。

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多