# Posterior simulation under the Normal-Normal hierarchical model # # Computation of the posterior distribution of (theta,mu,tau) via # simulation following the factorization: # # p(theta,mu,tau|y) propto p(tau|y)p(mu|tau,y)p(theta|mu,tau,y) # # 1) Computation of density p(tau|y) as in (5.21) with # p(tau) propto 1, at p. 139, Gelman's book # # Arguments: e=c(y1,...,y_n); v=c(sigma2_1,...,sigma2_n); # x.tau=seq(0,1,.001). # # NOTE In beta-blockers example y_j values are the empirical logits # and sigma2_j values are the approximate sampling variances, # computed as from (5.23) and (5.24), p. 150, Gelman's book. # dtau.r_function(e,v,x.tau) { n_length(e) d.tau_c() for(i in 1:length(x.tau)) { prec_1/(x.tau[i]^2+v) v.mu_( sum( prec ) )^(-1) mu.bar_(prec%*%e) * v.mu d.tau[i]_sqrt(v.mu) for(j in 1:n) { d.tau[i]_d.tau[i]* (v[j]+x.tau[i]^2)^(-1/2)* exp(-((e[j]-mu.bar)^2)/(2*(v[j]+x.tau[i]^2))) }} d.tau } # # Draw tau from p(tau|y) # Inverse cdf method # Arguments: d.tau=dtau.r output; m=#draws # tau.r_function(d.tau,m) { cn_sum(d.tau) dn.tau_d.tau/cn f.tau_cumsum(dn.tau) tau_c() for(i in 1:m) { u_max(runif(1),f.tau[1]) cat(u,"\n") tau[i]_x.tau[length(f.tau[f.tau