Nothing
poisson.glmMCMC <- function(Y, offset, X.standardised, K, p, which.miss, n.miss, burnin, n.sample, thin, MALA, n.beta.block, list.block, prior.mean.beta, prior.var.beta, verbose, chain)
{
# Rcpp::sourceCpp("src/CARBayes.cpp")
# source("R/common.functions.R")
##########################################
#### Generate the initial parameter values
##########################################
#### Generate initial values for each chain
mod.glm <- glm(Y~X.standardised-1, offset=offset, family="quasipoisson")
beta.mean <- mod.glm$coefficients
beta.sd <- 10 * sqrt(diag(summary(mod.glm)$cov.scaled))
beta <- rnorm(n=length(beta.mean), mean=beta.mean, sd=beta.sd)
###################################################################
#### Compute the fitted values based on the current parameter values
####################################################################
fitted <- exp(as.numeric(X.standardised %*% beta) + offset)
Y.DA <- Y
########################################
#### Set up the MCMC model run quantities
#########################################
#### Matrices to store samples
n.keep <- floor((n.sample - burnin)/thin)
samples.beta <- array(NA, c(n.keep, p))
samples.loglike <- array(NA, c(n.keep, K))
samples.fitted <- array(NA, c(n.keep, K))
if(n.miss>0) samples.Y <- array(NA, c(n.keep, n.miss))
#### Metropolis quantities
accept <- rep(0,2)
proposal.sd.beta <- 0.01
#### Start timer
if(verbose)
{
cat("\nMarkov chain", chain, "- generating", n.keep, "post burnin and thinned samples.\n", sep = " ")
progressBar <- txtProgressBar(style = 3)
percentage.points<-round((1:100/100)*n.sample)
}else
{
percentage.points<-round((1:100/100)*n.sample)
}
######################
#### Run an MCMC chain
######################
for(j in 1:n.sample)
{
######################################
#### Sample from Y - data augmentation
######################################
if(n.miss>0)
{
Y.DA[which.miss==0] <- rpois(n=n.miss, lambda=fitted[which.miss==0])
}else
{}
#####################
#### Sample from beta
#####################
offset.temp <- offset
if(MALA)
{
temp <- poissonbetaupdateMALA(X.standardised, K, p, beta, offset.temp, Y.DA, prior.mean.beta, prior.var.beta, n.beta.block, proposal.sd.beta, list.block)
}else
{
temp <- poissonbetaupdateRW(X.standardised, K, p, beta, offset.temp, Y.DA, prior.mean.beta, prior.var.beta, n.beta.block, proposal.sd.beta, list.block)
}
beta <- temp[[1]]
accept[1] <- accept[1] + temp[[2]]
accept[2] <- accept[2] + n.beta.block
#########################
## Calculate the deviance
#########################
lp <- as.numeric(X.standardised %*% beta) + offset
fitted <- exp(lp)
loglike <- dpois(x=as.numeric(Y), lambda=fitted, log=TRUE)
###################
## Save the results
###################
if(j > burnin & (j-burnin)%%thin==0)
{
ele <- (j - burnin) / thin
samples.beta[ele, ] <- beta
samples.loglike[ele, ] <- loglike
samples.fitted[ele, ] <- fitted
if(n.miss>0) samples.Y[ele, ] <-Y.DA[which.miss==0]
}else
{}
########################################
## Self tune the acceptance probabilties
########################################
if(ceiling(j/100)==floor(j/100) & j < burnin)
{
#### Update the proposal sds
if(p>2)
{
proposal.sd.beta <- common.accceptrates1(accept[1:2], proposal.sd.beta, 40, 50)
}else
{
proposal.sd.beta <- common.accceptrates1(accept[1:2], proposal.sd.beta, 30, 40)
}
accept <- rep(0,2)
}else
{}
################################
## print progress to the console
################################
if(j %in% percentage.points & verbose)
{
setTxtProgressBar(progressBar, j/n.sample)
}
}
#### Close the progress bar if used
if(verbose)
{
close(progressBar)
}else
{}
############################################
#### Return the results to the main function
############################################
#### Compile the results
if(n.miss==0) samples.Y = NA
chain.results <- list(samples.beta=samples.beta, samples.loglike=samples.loglike, samples.fitted=samples.fitted,
samples.Y=samples.Y, accept=accept)
#### Return the results
return(chain.results)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.