Skip to content

Possible bug with MCMCpack metropolis sampler

2 messages · dcervone, Brian Ripley

#
Hi,

I'm having issues with what I believe is a bug in the MCMCpack's
MCMCmetrop1R function. I have code that basically looks like this:

posterior.sampler <- function(data, prior.mu){
   log.posterior <- function(theta) log.likelihood(data, theta) +
log.prior(prior.mu, theta)
   post.samples <- MCMCmetrop1R(log.posterior, theta.init=prior.mu,
burnin=100, mcmc=1000, thin=40, tune=1, verbose=0, logfun=T,
optim.method="BFGS")
   return(post.samples)
}

x <- c(1,1,1)
posterior.sampler(mydata, x)

After calling posterior.sampler, the value for x is different from what I
started with. Perhaps even more interesting, is that if I create a copy of
x, say x2, before running posterior.sampler, x2 is modified as well. And I
have checked that calling log.posterior is not what alters the value of x,
so that leads me to believe it must be MCMCmetrop1R. Also, if I use
"theta.init = c(1,1,1)" instead of "theta.init = prior.mu" then x is not
altered. All of the behavior I described persists if I just run the
MCMCmetorp1R call (with the variables renamed of course) on its own and not
as part of my posterior.sampler function.

Any idea what's going on?

Thanks,
Dan Cervone



--
View this message in context: http://r.789695.n4.nabble.com/Possible-bug-with-MCMCpack-metropolis-sampler-tp4639199.html
Sent from the R help mailing list archive at Nabble.com.
#
What did the maintainer say (see the posting guide and indeed the R FAQ 
for where to report BUGS)?  He is Cc:ed here.

This is not necessarily a bug: R functions can change their arguments 
(but we do expect that to be prominently documented).  KalmanLike is one 
user-visible example.
On 05/08/2012 03:38, dcervone wrote:
That is standard behaviour.  You did not 'create a copy' by

x2 <- x

(it that is what you did: you did not tell us).  R has 'copy on change' 
semantics, and it looks like it is the C++ code in MCMCpack which did 
not copy before change.
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.