[R] Problems with dmvnorm and outer

David Winsemius dwinsemius at comcast.net
Sun Mar 28 15:00:22 CEST 2010


On Mar 28, 2010, at 3:19 AM, Severin Kacianka wrote:

> Hello,
>
> I try to create a persp-plot for the bivariate normal distribution.  
> There are a few solutions online ([1],[2]), but I did not want to  
> hard-code the formula like [2] did or use a kernel density estimate  
> like [1].
>
> My plan was to use dmvnorm, and use outer to calculate the z- 
> dimension for each point. Here is what I tried:
> library(mvtnorm)
> x<-seq(0,1,len=40)
> y<-x
> g<-function(a,b) {
> 	dmvnorm(x=c(a,b))

The construction "c(a,b)" would in the later function call turn those  
two 40 element vectors into an 80 element vector. See if you get  
better success with cbind.

> }
> z<-outer(x,y,g)
>
> Which results in the following error:
> Error in dim(robj) <- c(dX, dY) :
>  dims [product 1600] do not match the length of object [1]
>
> What causes this error? If I set g to be a simple function like:
> g<-function(a,b){
> 	a*b
> }
> Everything works fine. Why does it fail when I pass the parameters  
> to dmvnorm?
>
> Thank you for your time,
> Severin
>
> [1]https://stat.ethz.ch/pipermail/r-help/2003-September/038314.html
> [2]http://www.stat.ucl.ac.be/ISpersonnel/lecoutre/stats/fichiers/_gallery.pdf
>


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list