« MT2 Extra Credit Opportunity | Main | MCMC Sampling Stuff »

Bug fixes

Hey guys: a couple of bug fixes. First of all, there was a mistake in my "outline" of the metropolis code. I had the inequality for the acceptance ratio logic going the wrong direction. The corrected code follows:

## Sample Metropolis-Hastings algorithm
# function to compute log(h(theta))
log.h <- function(theta,data){
met <- function(data,initials,NREP){
  # Constants
  n <- dim(data)[1]
  p <- length(initials)
  # Empty matrix to hold samples
  chain <- matrix(NA,NREP+1,p)
  # Initialize the chain
  chain[1,] <- initials
  for (i in 2:(NREP+1)){
    # Parameters of the proposal
    meen <-
    var <-
    # Draw a proposal value
    thetastar <- rnorm(1,meen, var)
    # Compute the rejection ratio
    logr <- log.h(thetastar) + dnorm(chain[i-1,],meen,sqrt(var),log=T)
       - log.h(chain[i-1,]) - dnorm(thetastar,meen,sqrt(var),log=T)
    # Decide whether to accept or reject
    if (logr > 0){
      chain[i,] <- thetastar
    } elseif (logr > log(runif(1))) {
      chain[i,] <- thetastar
    } else {
      chain[i,] <- chain[i-1,]

Second, it appears that in the GeoBugs extra credit opportunity, the maps aren't going to load in because it doesn't like negative x values. One solution is to simply add a large constant to all of the longitude values in the polygon file produced by R so that they are positive.