[R] number of items to replace is not a multiple of replacement length

Milan Bouchet-Valat nalimilan at club.fr
Fri Nov 18 14:52:27 CET 2011


Le vendredi 18 novembre 2011 à 04:45 -0800, Gyanendra Pokharel a écrit :
> Hi all, following is my R -code and shows the error given below
> > n <- 100
> > k<-2
> > x1 <-c(1, 3);x2 <- c(2,5)
> > X <- matrix(c(0,0), nrow = 2, ncol = n)
> > for(i in 1:k)
> + X[i, ] <- mh1.epidemic(n,x1[i],x2[i])
> Error in X[i, ] <- mh1.epidemic(n,x1[i],x2[i]): 
>  number of items to replace is not a multiple of replacement length
> 
> > psi <-t(apply(X, c(1,2), cumsum))
> > for(i in 1:nrow(psi)){
> + psi[i,] <psi[i,]/(1:ncol(psi))
> + print(Gelman.Rubin(psi))
> + 
> + }
> Can some one suggest me what this error means and how could I fix it?
The message is relatively clear. ;-)

It means the number of elements in the mh1.epidemic(n,x1[i],x2[i])
vector isn't a multiple of the number of elements in X[i, ]. In the
standard case, you want these two vectors to be of the same length, and
there are too many or too few elements in mh1.epidemic(n,x1[i],x2[i]).

You can check that with by looking at:
length(mh1.epidemic(n,x1[i],x2[i]))
and
length(X[i, ])


Cheers



More information about the R-help mailing list