MCMC simulations in R for a simple poisson model I would like to put together my own R function for MCMC simulations rather than using a R package or Winbugs. This is purely to ensure I understand what is going on.
I have used the R Nimble package to construct a very simple model and to run the simulations.
The model code is as follows and from what I can see has ran successfully:
library(nimble)
ModelCode <- nimbleCode({ 
  for (i in 1:N){
  theta[i] ~ dgamma(alpha,beta) 
  lambda[i] <- theta[i]*t[i] 
  x[i] ~ dpois(lambda[i])
}
  alpha ~ dunif(0.0,100.0)
  beta ~ dunif(0.0,100.0)
})
Consts <- list(N = 10, t = c(15,12,8,8,6,5,5,4,4,3)) 
Data <- list(x = c(4,40,0,10,14,31,2,4,13,4))
Initial <- list(alpha = 99, beta = 99, theta = rep(0.1, Consts$N))
mcmc.out <- nimbleMCMC(code = ModelCode, constants = Consts, data = Data, inits = Initial, 
                       nchains = 2, niter = 2000, summary = TRUE, WAIC = TRUE, 
                       monitors = c('alpha','beta','lambda'))

Using this R Nimble package I have been able to obtain summary statistics from the posterior distribution as needed. However, as this is a simple model I wanted to put together an R function that does this from scratch in order to understand more fully what is going on.
There are some simple examples of the metropolis Hastings algorithm online that I am looking at but it is not clear to me how to expand these for two initial unknowns (alpha and beta) as per the model above. Some code for sampling an exponential distribution is shown below.
log_exp_target = function(x){
  return(dexp(x,rate=1, log=TRUE))
}

easyMCMC = function(log_target, niter, startval, proposalsd){
  x = rep(0,niter)
  x[1] = startval     
  for(i in 2:niter){
    currentx = x[i-1]
    proposedx = rnorm(1,mean=currentx,sd=proposalsd) 
    A = exp(log_target(proposedx) - log_target(currentx))
    if(runif(1)<A){
      x[i] = proposedx       # accept move with probabily min(1,A)
    } else {
      x[i] = currentx        # otherwise "reject" move, and stay where we are
    }
  }
  return(x)
}

I will continue reading but any code for this would be gratefully received.
 A: The simplest way, although not necessarily the best, is just to loop over the  parameters at each iteration.  In this version, startval and proposalsd are vectors of length equal to the number of parameters:
       easyMCMC = function(log_target, niter, startval, proposalsd){
          nparms = length(startval)
          x = matrix(0,niter,nparms)
          x[1,] = startval     
          for (i in 2:niter){
            proposedx = currentx = x[i-1,]
            for (j in 1:nparms) {
              proposedx[j] = rnorm(1,mean=currentx[j],sd=proposalsd[j]) 
              A = exp(log_target(proposedx) - log_target(currentx))
              if (runif(1)<A){
                currentx[j] = proposedx[j] # accept the proposal if don't reject 
              } else {
                proposedx[j] = currentx[j] # undo the proposal if reject
              }
            }
            x[i,] = currentx
          }
          return(x)
        }

I have tried to adhere to your code as closely as possible, however, some improvements are there to be had; for example, at the end of each iteration, proposedx equals currentx equals x[i-1,], so that assignment doesn't need to happen at the top of the iteration loop as long as proposedx and currentx are initialized outside the loop.
