# Generalized linear model: Bioassay model with Metropolis algorithm

Metropolis algorithm: Replicate the computations for the bioassay example of BDA3 Section 3.7 using the Metropolis algorithm. The Metropolis algorithm is described in BDA3 Chapter 11.2. More information on the bioassay data can be found in Section 3.7 in BDA3, and in **Chapter 3 notes**.

Implement the Metropolis algorithm as an R function for the bioassay data. Use the Gaussian prior as in Assignment 4, that is \[
\begin{aligned}
\begin{bmatrix}
\alpha \\ \beta
\end{bmatrix}
\sim
\text{N} \left( \mu_0, \Sigma_0 \right), \qquad
\text{where} \quad
\mu_0 = \begin{bmatrix} 0 \\ 10 \end{bmatrix} \quad \text{and} \quad
\Sigma_0 = \begin{bmatrix} 2^2 & 12 \\ 12 & 10^2 \end{bmatrix}.
\end{aligned}
\]

Compute with log-densities. Reasons are explained on BDA3 page 261 and Lecture video 4.1. Remember that \(p_1/p_0=\exp(\log(p_1)-\log(p_0))\). For your convenience we have provided functions that will evaluate the log-likelihood for given \(\alpha\) and \(\beta\) (see `bioassaylp()`

in the `aaltobda`

package). Notice that you still need to add the prior yourself and remember the unnormalized log posterior is simply the sum of log-likelihood and log-prior. For evaluating the log of the Gaussian prior you can use the function `dmvnorm`

from package `aaltobda`

.

Use a simple (normal) proposal distribution. Example proposals are \(\alpha^* \sim N(\alpha_{t-1}, \sigma = 1)\) and \(\beta^* \sim N(\beta_{t-1}, \sigma = 5)\). There is no need to try to find optimal proposal but test some different values for the jump scale (\(\sigma\)). Remember to report the one you used. Efficient proposals are discussed in BDA3 p. 295–297 (not part of the course). In real-life a pre-run could be made with an automatic adaptive control to adapt the proposal distribution.

Include in the report the following:

- Describe in your own words in one paragraph the basic idea of the Metropolis algorithm (see BDA3 Section 11.2, and Lecture video 5.1).
- The proposal distribution (related to
*jumping rule*) you used. Describe briefly in words how you chose the final proposal distribution you used for the reported results.
- The initial points of your Metropolis chains (or the explicit mechanism for generating them).
- Report the chain length or the number of iterations for each chain. Run the simulations long enough for approximate convergence (see BDA Section 11.4, and Lecture 5.2).
- Report the warm-up length (see BDA Section 11.4, and Lecture 5.2).
- The number of Metropolis chains used. It is important that multiple Metropolis chains are run for evaluating convergence (see BDA Section 11.4, and Lecture 5.2).
- Plot all chains for \(\alpha\) in a single line-plot. Overlapping the chains in this way helps in visually assessing whether chains have converged or not.
- Do the same for \(\beta\).

In complex scenarios, visual assessment is not sufficient and \(\widehat{R}\) is a more robust indicator of convergence of the Markov chains. Use \(\widehat{R}\) for convergence analysis. You can either use Eq. (11.4) in BDA3 or the more recent version described in the article *Rank-normalization, folding, and localization: An improved \(\widehat{R}\) for assessing convergence of MCMC*. You should specify which \(\widehat{R}\) you used. In R the best choice is to use function `rhat_basic()`

from the package `posterior`

(this function implements the version described in the above mentioned article). Remember to remove the warm-up sample before computing \(\widehat{R}\). Report the \(\widehat{R}\) values for \(\alpha\) and \(\beta\) separately. Report the values for the proposal distribution you finally used.

- Describe briefly in your own words the basic idea of \(\widehat{R}\) and how to to interpret the obtained \(\widehat{R}\) values.
- Tell whether you obtained good \(\widehat{R}\) with first try, or whether you needed to run more iterations or how did you modify the proposal distribution.

Plot the draws for \(\alpha\) and \(\beta\) (scatter plot) and include this plot in your report. You can compare the results to BDA3 Figure 3.3b to verify that your code gives sensible results. Notice though that the results in Figure 3.3b are generated from the posterior with a uniform prior, so even when if your algorithm works perfectly, the results will look slightly different (although fairly similar).

**Q3: **
Is the implementation of `density_ratio`

function included ?
**Q4: **
Is the implementation of `metropolis_bioassay`

function included ?
**Q5: **
2 a) Is the brief description of Metropolis-Hastings algorithm included (and it’s not complete nonsense)? Provide also a brief comment on how clear you think that description is (and potentially mention errors if you see them).
**Q6: **
2 b) Is the proposal/jumping distribution reported?
**Q7: **
2 c) Are the starting points or the mechanism how they are generated reported?
**Q8: **
2 d) Is the number of draws per chain reported?
**Q9: **
2 e) Is the warm-up length reported?
**Q10: **
2 f) Is the number of chains reported?
**Q11: **
2 g) and 2 h) Are line plots of the chains included? (Separate plots for alpha and beta)
- No plots are included
- Yes, but both plots are in a single figure, or the plots are scatter plots (scatter plots aren’t useful for visual convergence evaluation).
- Yes, but only a plot for alpha or beta is included.
- Yes, separate line plots for both alpha and beta are included.

**Q12: **
Is there a discussion on the convergence of the chains?
- No discussion on convergence.
- Yes, but the discussion is not convincing.
- Yes, discussed in the report.

**Q13: **
Is it mentioned which implementation of Rhat is used? Two possible ways to compute R-hat would be:
**…**
**…**

It is OK as long as it is mentioned (or evident from the code) which of the above is used.
**Q14: **
Is the brief description of Rhat included (and it’s not complete nonsense)? Provide also a brief comment on how clear you think that description is (and potentially mention errors if you see them).
**Q15: **
Are the Rhat-values for alpha and beta reported?
- No
- Yes, but incorrectly computed
- Yes, but computed separately for each chain
- Yes, but only for alpha or beta
- Yes, single values both for alpha and beta

**Q16: **
Is the interpretation of R-hat values correct (**…**)?
- No interpretation or discussion about the R-hat values, or conclusions clearly wrong
- Interpretation somewhat correct
- Interpretation correct

**Q17: **
Does the report contain a scatter plot about the draws? Do the results look reasonable, that is, roughly like in the Figure below **…**?
- No plot included
- Plot included, but the results do not look like in the figure above
- Plot included, and the results look roughly like in the figure above

# Overall quality of the report

**Q18: **
Does the report include comment on whether AI was used, and if AI was used, explanation on how it was used?
**Q19: **
Does the report follow the formatting instructions?
- Not at all
- Little
- Mostly
- Yes

**Q20: **
In case the report doesn’t fully follow the general and formatting instructions, specify the instructions that have not been followed. If applicable, specify the page of the report, where this difference is visible. This will help the other student to improve their reports so that they are easier to read and review. If applicable, specify the page of the report, where this difference in formatting is visible.
**Q21: **
Please also provide feedback on the presentation (e.g. text, layout, flow of the responses, figures, figure captions). Part of the course is practicing making data analysis reports. By providing feedback on the report presentation, other students can learn what they can improve or what they already did well. You should be able to provide constructive or positive feedback for all non-empty and non-nonsense reports. If you think the report is perfect, and you can’t come up with any suggestions how to improve, you can provide feedback on what you liked and why you think some part of the report is better than yours.