Example R programs and commands 38. Metropolis and Metropolis-Hastings sampling. ############################################################## # From Seefeld and Linder, p.203 --- Metropolis sampling, # target posterior beta(5,3) from proposal unif([0,1]) nchain<-1000; theta<-vector(length=nchain); theta[1]<-0.04; target <- function(th) { th^(5-1) * (1-th)^(3-1) } # unnormalized beta(5,3) for(i in 2:nchain){ thetaold <- theta[i-1]; thetanew <- runif(1); # sample the (symmetric) proposal pdf r <- target(thetanew)/target(thetaold); u <- runif(1); ifelse( u