# [R] error in an univariate integration involving a bivariate normal CDF

William Dunlap wdunlap at tibco.com
Tue Jan 10 16:21:59 CET 2017

```  integrand=function(xx,y=1,MU=MU,Sigma=Sigma)
{
dnorm(xx,mean=MU,sd=sqrt(Sigma[1,1]))*pmvnorm(lower=c(-Inf,-Inf),upper=c(xx,y),mean=MU,sigma=Sigma)

}

The integrand must be a function that returns a vector the length of
its first argument (integrate will pass it a short vector of input
values).  It looks like your function expects 'xx' to be a scalar.
You can use the Vectorize function to fix things up.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Jan 9, 2017 at 3:28 PM, Jingqin luo via R-help
<r-help at r-project.org> wrote:
> R users,I encountered some error message when trying a univariate integration involving a bivariate normal CDF with mean MU and variance matrix Sigma. Below are my code:MU=c(1,3)
> Sigma=matrix(c(1,0.5,0.5,1.5),2,2)
> integrand=function(xx,y=1,MU=MU,Sigma=Sigma)
>   {
>     dnorm(xx,mean=MU,sd=sqrt(Sigma[1,1]))*pmvnorm(lower=c(-Inf,-Inf),upper=c(xx,y),mean=MU,sigma=Sigma)
>
>   }
>   integrate(integrand,y=1,MU=MU,Sigma=Sigma,lower=-Inf,upper=Inf,subdivisions=1000)gave : Error in checkmvArgs(lower = lower, upper = upper, mean = mean, corr = corr, : ‘diag(sigma)’ and ‘lower’ are of different length.The error came from the pmvnorm() part. I will appreciate any hints to this error.> sessionInfo()
> R version 3.3.1 (2016-06-21)
> Platform: x86_64-redhat-linux-gnu (64-bit)
> Running under: Red Hat Enterprise Linux Server release 6.2 (Santiago)
>
> locale:
>   LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>   LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>   LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>   LC_PAPER=en_US.UTF-8       LC_NAME=C
>  LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
>  stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
>   pbivnorm_0.6.0     ggplot2_1.0.0      pROC_1.8           copula_0.999-11
>   mvtnorm_1.0-0      lcmix_0.3          nnls_1.4           MASS_7.3-45
>   matrixStats_0.10.0 R.methodsS3_1.6.1  psych_1.6.9
>
> loaded via a namespace (and not attached):
>   Rcpp_0.12.1      munsell_0.4.2    mnormt_1.5-1     ADGofTest_0.3
>   colorspace_1.2-4 pspline_1.0-16   lattice_0.20-29  stringr_0.6.2
>   plyr_1.8.1       tcltk_3.3.1      tools_3.3.1      parallel_3.3.1
>  grid_3.3.1       gtable_0.1.2     digest_0.6.4     Matrix_1.1-4
>  reshape2_1.4     labeling_0.3     scales_0.2.4     gsl_1.9-10
>  stats4_3.3.1     stabledist_0.6-6 foreign_0.8-66   proto_0.3-10
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help