########## R script: binomBetaLeftHand ##########

# For MCMC analysis of a simple binomial/beta
# situation using JAGS.

# This version is tailored to the probability of left-handed
# example.

# Last changed: 24 AUG 2026

# Load in the rjags package and support functions:

library(rjags)
source("lab1SupportFunctions.R")
source("summMCMC.r")

# Set flag for whether slide projection is being used:

slideProjection <- FALSE
#slideProjection <- TRUE

# Set character inflation factors (for slide projection issues):

if (!slideProjection)
  cexMCMC <- 1

if (slideProjection)
  cexMCMC <- 4
  
# Set MCMC parameters:

nBurnin <- 5000000         # Length of burn-in.
nIter <- 5000000           # Size of the kept sample.
nThin <- 50              # Thinning factor. 

# Specify model in JAGS:

binomBetaModel <- function()
{
   for (i in 1:n) 
   {
      x[i] ~ dbern(p)
   }
   p ~ dbeta(2,18)
}
modelFileName <- file.path(getwd(),"binomBetaModel.txt")
writeModel(binomBetaModel,modelFileName)

# Set up input data list:

allData <- list(n=11,x=c(0,0,0,0,0,0,0,0,0,0,0))

# Initialise parameter:

parInit <- list(list(p=0.5))

# Run rjags:

currModel <- jags.model("binomBetaModel.txt",data=allData)
update(currModel,n.iter=nBurnin)
currMCMC <- jags.samples(currModel, 
                         variable.names=c("p"),
                         n.iter=nIter,thin=nThin)

pSamp <- as.numeric(currMCMC$p)

# Do summary plots:

summMCMC(list(cbind(pSamp)),parNames=list("p"),numerSummCex=cexMCMC)

########## End of binomBetaLeftHand ##########
