library(tidyverse)
library(cmdstanr)
library(posterior)
library(bayesplot)
Exercise 3
In this exercise, you will complete the analysis for a Bernoulli model in Stan. Please make sure you change the YAML option to eval: true
.
Data
You’ll use the Therapeutic Touch data discussed in the lectures. Specifically, there are two participants: S02
, who guessed the correct hand 2 times out of 10, and S08
, who guessed the correct hand 8 times out of 10.
# Data for S01 (1 out of 10)
<- list(
data_s02 N = 10,
y = c(1, 1, 0, 0, 0, 0, 0, 0, 0, 0)
)# Data for S28 (8 out of 10)
<- list(
data_s28 N = 10,
y = c(1, 1, 1, 1, 1, 1, 1, 1, 0, 0)
)
Q1: Compile the attached “beta-bernoulli-pp.stan” file with the following code (Make sure the Stan file is in the same folder as this qmd):
data {
int<lower=0> N; // number of observations
array[N] int<lower=0,upper=1> y; // y
}parameters {
real<lower=0,upper=1> theta; // theta parameter
}model {
2, 2); // prior: Beta(2, 2)
theta ~ beta(// model: Bernoulli
y ~ bernoulli(theta);
}generated quantities {
real prior_theta = beta_rng(2, 2);
array[N] int prior_ytilde;
array[N] int ytilde;
for (i in 1:N) {
ytilde[i] = bernoulli_rng(theta);
prior_ytilde[i] = bernoulli_rng(prior_theta);
} }
<- cmdstan_model("beta-bernoulli-pp.stan") bern_mod
Q2: As the model compiled successfully, the following performs MCMC sampling for participant S02
to estimate the “ability” of guessing the correct hand (i.e., \(\theta\)). Repeat the sampling for participant S28
.
<- bern_mod$sample(data_s02)
s02_fit # Add code for `s28_fit`
Q3: Plot the posterior distribution of \(\theta\) for the two participants.
# First, extract theta
# Note: an MCMC draw is simply a simulated value of the parameter
# from the posterior distribution
<- s02_fit$draws("theta")
theta_s02 mcmc_hist(theta_s02)
# For S28, try mcmc_dens()
<- s28_fit$draws("theta") theta_s28
Q4: Use the following code to obtain the posterior distribution of the difference in \(\theta\) between the two participants. Compute the probability that \(\theta\) for participant S28
is greater than that of participant S02
.
<- theta_s28 - theta_s02
theta_diff # Compute the probability that theta_s28 > theta_s02