Mhsim = function(bstart,v,w,x,y,nmc) { # function to perform MH (Metropolis-Hastings) simulations for a # log-linear Poisson model with 1 covariate and a Gaussian prior # on the (intercept,slope) parameter vector. # INPUTS: # bstart: 2-vector of starting values for the parameters # v = 2-vector of prior variances # w = 2-vector of variances for metropolis-hastings steps # x = n-vector of predictors # y = n-vector of the (Poisson) responses # nmc = positive integer; the number of steps in the MH algorithm # OUTPUTS: # bmat = (nmc+1)x2 matrix of simulated parameter values; bmat[i+1,] # is the simulated value of the parameter vector on the i'th step # of the MH algorithm # lups = (nmc+1) vector of the log of the unnormalized posterior #########################################################################3 bmat = matrix(NA,nrow=nmc+1,ncol=2) colnames(bmat) = c("intercept","slope") lups = rep(NA,nmc+1) t = c(sum(y),sum(x*y)) bmat[1,] = bstart lups[1] = Lup(bstart,v,t,x) for(i in 1:nmc) { temp = Mhstep(bmat[i,],lups[i],v,w,t,x) bmat[i+1,] = temp$bnew lups[i+1] = temp$lupnew } mhout = list(bmat=bmat,lups=lups) return(mhout) }