As we’ve seen, one of the most popular ways to do Bayesian inference in practice is to use algorithms to generate samples (at least approximately) from the posterior distribution. As we’ve also seen, these techniques are time consuming and computationally intensive - a good candidate for a computing challenge!
In this challenge, we’ll be returning to the Gompertz function; that way, you can build on the familiarity you’ve gained with this curve, and focus on the computing task rather than having to learn a new nonlinear model.
The problem will be similar to challenge 5 - we’ll generate data in the same way, using the same “GenerateGompertz” function, and your job will be to provide a functon which can analyze it and return a matrix of samples from the posterior distribution of the \(\beta\) and \(\gamma\) parameters.
The prior distributions on \(\beta_0, \beta_1, \gamma_0,\mbox{ and } \gamma_1\) should be independent standard normals, and the priors on \(\sigma^2\) and \(b\) should be \(\mbox{gamma}(1,1)\).
Your function should be named: ‘FitGompertzBayes’, and it should accept a “GompertzData” object as follows:
FitGompertzBayes <- function(gdat, nsamples=1000){
if (!("GompertzData" %in% class(gdat))){
stop("This function should be used only on specially formatted GompertzData objects")
}
# fit model
# return results - should be a matrix with nsamples rows and 4 columns
out_samples <- matrix(rnorm(nsamples*4), ncol = 4)
# Order of columns:
colnames(out_samples) <- c("beta0", "beta1", "gamma0", "gamma1")
return(outsamples)
}
As in the previous challenge, \(\beta,\ \gamma,\ \mbox{b, and } \sigma^2\) can all be assumed to be non-negative for this problem.
Scoring will have three components: