[R] Problems with dmvnorm and outer

Severin Kacianka severin.lists at kacianka.at
Sun Mar 28 09:19:17 CEST 2010


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))
}
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



More information about the R-help mailing list