I am attempting to compare two beta distributions under a Bayesian framework. (My data are survival rates, so they fall between 0 and 1 and are best fit with a beta distribution. After estimating alpha and beta, I will moment match to compare means.) However, the beta likelihood is returning "0" for the given initial values, which makes estimating the accept/reject ratio for MCMC in the given code (at the end of my question below) impossible to calculate.
I am gathering a rudimentary understanding of why this occurs, but am in a bit of a hurry and would rather not dig too far into the technical literature, so am hoping someone might have an easily-implementable solution I overlooked. One suggestion would be to somehow incorporate code similar to that which I found in a "Bayesian Model for Practicing Ecologists" lab (which did not include solutions):
L.vector = dbeta(y,a,b, log=TRUE)
like=sum(L.vector[is.finite(L.vector)])
However, I am not sure where to insert this code, whether using "log=TRUE" is appropriate here, or whether there would even be enough instances with finite values to succeed.
My code is below, thank you SO MUCH to anyone who takes the time to give suggestions.
likelihood <- function(parameters){
alpha.1=parameters[1]; beta.1=parameters[2]; alpha.2=parameters[3]; beta.2=parameters[4]
prod(dbeta(data.1, alpha.1, beta.1)) * prod(dbeta(data.2, alpha.2, beta.2))
}
prior <- function(parameters){
alpha.1=parameters[1]; beta.1=parameters[2]; alpha.2 =parameters[3]; beta.2=parameters[4]
dgamma(alpha.1, shape.prior, rate.prior) * dgamma(beta.1, shape.prior, rate.prior) * dgamma(alpha.2, shape.prior, rate.prior) * dgamma(beta.2, shape.prior, rate.prior)}
posterior <- function(parameters) {likelihood(parameters) * prior(parameters)}
#starting values
alpha.1 = .5; beta.1 = .5; alpha.2 = .5; beta.2 = .5
parameters <- c(alpha.1, beta.1, alpha.2, beta.2)
#MCMC w/Metropolis
n.iter <- 100000
results <- matrix(0, nrow=n.iter, ncol=4)
results[1, ] <- parameters
for (iteration in 2:n.iter){
candidate <- parameters + runif(4)
ratio <- posterior(candidate)/posterior(parameters)
if (runif(1,0,1) < ratio) {
parameters <- candidate
results[iteration, ] <- parameters
}
}
Also, thanks to Kruschke 2013 and the person who gave code at Bayesian equivalent of two sample t-test? for getting me to this point!