I am self-studying Bayesian statistics from the book Computational Bayesian Statistics by Turkman et al, but I am stuck on Problem 6.3 from the book:
Suppose we want to consider a Binomial (unknown $\theta, n) \land$ Beta model, in particular, a binomial sampling model $x \mid \theta, n \sim \text{Bi}(n, \theta)$. Assume that $\theta \sim \text{Be}(a_0,b_0)$ and $h(n) \propto \frac{1}{n^2}$. Find the conditional posterior distributions $h(\theta \mid n, x)$ and $h(n \mid \theta, x)$.
Next, describe and implement a Gibbs sampling algorithm to generate $(n_m, \theta_m) \sim h(n, \theta \mid x)$. Plot the joint posterior $h(n, \theta \mid x)$, and plot on top of the same figure the simulated posterior draws $(n_m , \theta_m), m = 1, ..., 50$ (connected by line segments showing the moves). Use $x = 50$, and $(a_0,b_0) = (1,4)$, a grid on $0.01 \leq \theta \leq 0.99$ and $x \leq n \leq 500$. Use the R function lgamma(n+1)
to evaluate $\ln{(n!)}$.
Finally, implement Metropolis-Hastings posterior simulation. Add the simulated posterior draws on top of the plot from above.
I was able to find the conditional posterior distributions $h(\theta \mid n, x)$ and $h(n \mid \theta, x)$ to be $$h(\theta \mid n, x) = \text{Be}(a_0 + x, b_0 + n - x)$$ and $$h(n, \theta \mid x) \propto \frac{1}{n^2} \theta^{a_0 + x - 1}(1 - \theta)^{n - x + b_0 - 1} \frac{n!}{(n - x)!},$$ but I'm not sure how to implement the Gibbs sampler in this case. I'm new to R, so I'd appreciate any help in this manner! Thanks in advance.