[R] Possible bug with MCMCpack metropolis sampler
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Aug 5 08:38:37 CEST 2012
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:
> 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
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.
> 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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list