# setting values set.seed(1) s2<-1 t2<-10 mu<-5; n<-5 # defining the data x<-c(9.37, 10.18, 9.16, 11.60, 10.33) # mean of the normal posterior mu.n<-( mean(x)*n/s2 + mu/t2 )/( n/s2+1/t2) # variance of the normal posterior t2.n<-1/(n/s2+1/t2) ####metropolis part#### ##S = total num of simulations theta<-0 ; delta<-1 ; S<-500 ; THETA<-NULL count = 0; for(s in 1:S) { ## simulating our proposal theta.star<-rnorm(1,theta,sqrt(delta)) ##taking the log of the ratio r log.r<-( sum(dnorm(x,theta.star,sqrt(s2),log=TRUE)) + dnorm(theta.star,mu,sqrt(t2),log=TRUE) ) - ( sum(dnorm(x,theta,sqrt(s2),log=TRUE)) + dnorm(theta,mu,sqrt(t2),log=TRUE) ) if(log(runif(1))