[R] error message when using outer function

Reinman, Grant grant.reinman at pw.utc.com
Mon Mar 5 20:38:01 CET 2007


Dear R-users,
I have two sets of code that appear to me to be equivalent, shown below, and
yet I get the error message 

"Error in dim(robj) <- c(dX, dY) : dim<- : dims [product 4] do not match the
length of object [1]"

after executing the assignment to logdens2.  Both functions post.a1 and
post.a2 return the same values when run alone and not embedded in the
function outer.  I would appreciate help in understanding the differences
between these two sets of code.  

The code in post.a1 is from Gelman, Carlin, Stern, and Rubin's Bayesian Data
Analysis solutions, problem 3.5.  I was trying to modify this code in
post.a2 when I ran into this error.


post.a1 <- function(mu,sd,y){
  ldens <- 0
  for (i in 1:length(y)) ldens <- ldens + log(dnorm(y[i],mu,sd))
  ldens}
y <- c(10,10,12,11,9)
mugrid <- c(10,11)
sdgrid <- c(1,1.2)
logdens1 <- outer (mugrid, sdgrid, post.a1, y)
#*** no error messages ***


post.a2 <- function(mu,sd,y)
{
  ldens <- sum(log(dnorm(y,mu,sd)))
  ldens
}
y <- c(10,10,12,11,9)
mugrid <- c(10,11)
sdgrid <- c(1,1.2)
logdens2 <- outer (mugrid, sdgrid, post.a2, y)
#***error message occurs here ***

Thank You!

Grant Reinman 
e-mail:grant.reinman at pw.utc.com



More information about the R-help mailing list