From ligges@statistik.uni-dortmund.de  Tue Jan 21 09:49:10 2003
From: ligges@statistik.uni-dortmund.de (ligges@statistik.uni-dortmund.de)
Date: Tue Jan 21 09:49:10 2003
Subject: [Rd] package mvtnorm: sigma parameter in pmvnorm() (PR#2478)
Message-ID: <200301210826.JAA13940@pubhealth.ku.dk>

jerome@hivnet.ubc.ca wrote:
> Full_Name: Jerome Asselin
> Version: 1.6.2
> OS: RedHat Linux 7.2
> Submission from: (NULL) (142.103.173.179)
> 
> 
> 
> pmvnorm() may fail for a univariate distribution when
> its parameter "sigma" is defined as a matrix. It will
> fail if sigma < 1.
> 
> library(mvtnorm)
> 
> #THIS WORKS
> 
>>pmvnorm(lower=-Inf,upper=2,mean=0,sigma=matrix(1.5))
> 
> [1] 0.9487648
> attr(,"error")
> [1] 0
> attr(,"msg")
> [1] "univariate: using pnorm"
> 
> #THIS FAILS
> 
>>pmvnorm(lower=-Inf,upper=2,mean=0,sigma=matrix(.5))
> 
> Error in checkmvArgs(lower = lower, upper = upper, mean = mean, corr = corr,  :
>         diag(sigma) and lower are of different length
> 
> #THIS WORKS
> 
>>pmvnorm(lower=-Inf,upper=2,mean=0,sigma=.5)
> 
> [1] 0.9976611
> attr(,"error")
> [1] 0
> attr(,"msg")
> [1] "univariate: using pnorm"

The bug is in checkmvArgs():

  if (!is.null(sigma)) {
          if (!is.matrix(sigma)) {
             if (length(sigma) == 1)
                 UNI <- TRUE
             if (length(sigma) != length(lower))
                stop("diag(sigma) and lower are of different length")
          } else {
+           if (length(diag(sigma)) != length(lower))
+              stop("diag(sigma) and lower are of different length")
             if (length(sigma) == 1) {
                 UNI <- TRUE
                 sigma <- sigma[1,1]
             }
-           if (length(diag(sigma)) != length(lower))
-              stop("diag(sigma) and lower are of different length")

          }
     }


The fix is to change the position of the validation, i.e. before the 
matrix is coerced to a vector of length 1.

Uwe Ligges



[Rd] package mvtnorm: sigma parameter in pmvnorm() (PR#2478)
ligges@statistik.uni-dortmund.de