2
votes

I have variance-cov matrices made in stata, that i want to use in R in pmvtnorm, for example:

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)

When i run this sequence I get the error message:

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

Checking for positive definiteness and symmetry both show true, and most of the matrices i import to R i have already used in similar stata commands without a problem... Link for sigma1.csv:

link for sigma1.csv

1
Could you add sigma to your question, so we can see the matrix itself? - cgage1
I get the same error with corr as well, but anyway this is not correlation matrix but var-cov shouldnt i do conversion before - nov
Hmm, it is very odd that you are getting this error. The only 2 things left I can think of are: 1. The matrix is too large for pmvnorm to handle, or 2. Some sort of rounding issue is happening when the the values are being read into Fn. Sorry I couldn't help, hopefully someone that can comes across this! - cgage1
rownames(sigma) <- colnames(sigma) - user20650
No probs: when in doubt check the code... looking at pmvnorm leads to mvtnorm:::checkmvArgs, and then to isTRUE(all.equal(sigma, t(sigma))) - user20650

1 Answers

1
votes
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"

Just make sure sigma is a matrix