2.5 An application
Here we consider the Bayesian probit model as an example.
The data generating model is \[\begin{align} Y_i&\sim \mathsf{Bin}(n_i,\pi_i),\\ \pi_i &= \Phi(\beta_0 + \beta_1 z_{i1} + \beta_2 z_{i2} + \beta_3 z_{i3}), \end{align}\] where \(z_{ij}\) are indicators and \(\Phi\) is the \(\mathsf N(0,1)\) distribution function. The data is shown in the table.
| y | n | z1 | z2 | z3 |
|---|---|---|---|---|
| 11 | 84 | 1 | 1 | 1 |
| 5 | 61 | 0 | 1 | 1 |
| 0 | 29 | 0 | 0 | 1 |
| 21 | 36 | 1 | 1 | 0 |
| 35 | 69 | 0 | 1 | 0 |
| 2 | 4 | 1 | 0 | 0 |
| 4 | 42 | 0 | 0 | 0 |
The likelihood is \[ L(\beta)\propto \prod_{i=1}^n\Phi(\beta_0 + \beta_1 z_{i1} + \beta_2 z_{i2} + \beta_3 z_{i3})^{y_i}(1-\Phi( \beta_0 + \beta_1 z_{i1} + \beta_2 z_{i2} + \beta_3 z_{i3}))^{n_i-y_i}. \] We choose a normal distribution as prior for each \(\beta_i\), \[ \beta_i \overset{iid}\sim \mathsf N(0,\lambda), \] with \(\lambda=10\). That is, the prior can be written as \[ p(\beta) \propto \exp\left( -\frac{1}{2\lambda}\sum_{j=0}^3 \beta_j^2 \right). \] We will use a random walk Metropolis Hastings algorithm with \(\varepsilon\overset{iid}\sim \mathsf N(0,\sigma^2)\).
Now, we implement this in R. First we load the data.
data.df <- read.csv("data/bayesProbit.dat", header=TRUE)
n <- nrow(data.df)
data.df$z0 <- rep(1, n)
col_order <- c('y', 'n','z0','z1','z2','z3')
data.df <- data.df[, col_order]Then implement the likelihood function and prior density. We do this on a log-scale.
logL <- function(beta){
p <- pnorm(
as.matrix(
data.df['z0'] * beta[1] + data.df['z1'] * beta[2] +
data.df['z2'] * beta[3] + data.df['z3'] * beta[4]
)
)
sum( data.df['y']*log(p) + (data.df['n']-data.df['y'])*log(1-p) )
}
lambda <- 10
logPrior <- function(beta){
-sum(beta^2)/(2*lambda)
}
logPosterior <- function(beta){ logL(beta) + logPrior(beta)}Now run the Markov chain. First a burn-in of 1000 steps, that we then discard. After that a longer run.
beta <- c(0,0,0,0) #Initial value
nIter <- 100000 #Number of MC steps
set.seed(42)
beta.mcmc <- mcmc.iter(beta,
logPosterior,
0.08,
1000)
beta.mcmc <- mcmc.iter(beta.mcmc$sample[nrow(beta.mcmc$sample),],
logPosterior,
0.08,
nIter)Now we do some diagnostics of the simulation. First check that the acceptance probability is reasonable.
## [1] 0.53389
Then we plot the trajectories of the parameters. We want to see that the trajectories appear stationary and that they are not stuck in one state. We only plot the first 1000 steps.
Figure 2.4: Trajectories of the Markov chain
Figure 2.5: Cumulative mean of the Markov chain
Figure 2.6: Posterior distribution
From this we can get point estimates, the mean of the posterior distribution, and credible intervals.
## [1] -1.1829531 0.3175842 1.1576232 -1.4132245
## [,1] [,2] [,3] [,4]
## 5% -1.5791352 0.01517242 0.7428645 -1.736278
## 95% -0.8206941 0.63006163 1.6063078 -1.101808
As a final check, let us verify that our estimates makes sense by comparing our data to our predictions.
beta.fit <- colMeans(beta.mcmc$sample)
p <- pnorm(as.matrix(data.df[3:6])%*%beta.fit)
y.pred <- data.df$n*p
y.pred## [,1]
## [1,] 11.0166258
## [2,] 4.5834439
## [3,] 0.1366877
## [4,] 22.1383420
## [5,] 33.8028200
## [6,] 0.7736728
## [7,] 4.9733827
This is similar to the data.