pmvnorm错误; sigma不是协方差矩阵

时间:2016-04-18 20:52:34

标签: r matrix covariance

我在stata中制作了variance-cov矩阵,我希望在pmvtnorm中的R中使用,例如:

library(mvtnorm)
library(matrixcalc)

sigma = read.csv(file="c:/Users/../sigma1.csv", header=F, sep=",")
sigma <- as.matrix(sigma) 

is.symmetric.matrix(sigma)
is.positive.definite(sigma)

m = nrow(sigma)
Fn = pmvnorm(lower=rep(-Inf, m), upper=rep(0, m), mean=rep(0, m), sigma=sigma)

当我运行此序列时,我收到错误消息:

Error in checkmvArgs(lower = lower, upper = upper, mean = mean, corr = corr,  : 
  ‘sigma’ is not a covariance matrix

检查正定性和对称性都显示为真,并且我导入到R i的大多数矩阵已经在类似的stata命令中使用而没有问题... sigma1.csv的链接:

link for sigma1.csv

1 个答案:

答案 0 :(得分:0)

sigma <- read.csv(file = "data/sigma.csv", header = F, sep = ",")
mat <- matrix(0, nrow = nrow(sigma), ncol = ncol(sigma))
sigma <- as.matrix(sigma)
attributes(sigma) <- attributes(mat)
# str(sigma)
# is.symmetric.matrix(sigma)
# is.positive.definite(sigma)
m <- nrow(sigma)
Fn <- pmvnorm(
  lower = rep(-Inf, m), upper = rep(0, m),
  mean = rep(0, m), sigma = sigma
)
Fn
[1] 6.747892e-29
attr(,"error")
[1] 2.005038e-31
attr(,"msg")
[1] "Normal Completion"

只需确保sigma是矩阵