Hierarchical Models
The batting example: 1. Number of hits each player attains 2. The number of opportunities at bat 3. The primary fielding position of that player
Child purchasing cafeteria lunch 1. Number of days each child bought lunch 2. Number of days of school attended 3. School and district of the child
Surgeons saving the life of a patient i.e., coin flip 1. Number of recoveries 2. Number of surgeries 3. Hospital and city of the surgery
Each parameter is informed by the other individuals
Bernouli likelihood distribution: - \(y_1\) ~ dbern(\(\theta\))
prior distribition is a beta density: \(\theta\) ~ dbeta(\(a\), \(b\))
Can be reorganized as:
\(\theta\) ~ dbeta(\(\omega\), (\(k\) - 2) + 1, (1 - \(\omega\))(\(K\) - 2) + 1)
The value \(K\) controls how close \(\theta\) is to \(\theta\)
Minted coins tend to have a bias near \(\omega\) inherent to the minting process. Some have a value of \(\theta\) greater than \(\omega\), while others have \(\theta\) values less than \(\omega\). The larger the \(K\) is, the more internal consistency that the manufacturing process mints coins with \(\theta\) values closer to \(\omega\). The estimated credible \(\theta\) will be near the proportion of observed heads. Larger \(K\) increases certainty.
Figure 9.1 A model of hierarchical dependencies for data from a single coin. The chain of arrows illustrates the chain of dependencies in Equations 9.2, 9.4, and 9.5. (At the top of the diagram, the second instantiation of the arrow to \(\omega\) is shown in grey instead of black merely to suggest that it is the same dependence already indicated by the first arrow in black.)
Posterior distribution over \(\omega\)
\[p(\theta, \omega|y)=\frac{p(y|\theta, \omega) p(\theta, \omega)}{p(y)} =\frac{p(y|\theta)p(\theta|\omega)p(\omega)}{p(y)}\]
We can use a grid approximation to get a picture of what is occurring. For a binomial distribution (0, 1), grid approximations are simpler and readily graphed.
Figure 9.2:
\(A_\omega\) = 2, \(B_\omega\) = 2, and \(K\) = 100. Top right demonstrates a marginal distribution of the prior evident by the tipped nature of the distribution. - Summing/collapsing the joint prior. - \(p\)(\(\omega\)) axis scale is density. - Prior puts more credibility on \(\theta\) values near \(\omega\). – Beta densities - Middle row is the likelihood function. (9 heads and 3 tails)
Example:
Another example:
Representation of the 3D space:
model {for (i in 1 : Ntotal) {y[i] ~ dbern (theta[s[i]])} for (s in 1 : Nsubj) {theta[s] ~ dbeta (omega * (kappa - 2) + 1, (1 - omega) * (kappa - 2) + 1)} omega ~ dbeta (1,1) kappa < − kappaMinusTwo + 2 kappaMinusTwo ~ dgamma (0.01, 0.01) # mean = 1, sd = 10 (generic vague)}
Read the data file : myData = read.csv ("TherapeuticTouchData.csv")
# Load the relevant model functions into R's working memory :
source ("Jags - Ydich - XnomSsubj - MbernBetaOmegaKappa.R")
Generate the MCMC chain : mcmcCoda = genMCMC (data = myData, sName ="s", yName ="y", numSavedSteps = 20000, thinSteps = 10)
# Display diagnostics of chain, for specified parameters :
diagMCMC (codaObject = mcmcCoda, parName ="omega")
diagMCMC (codaObject = mcmcCoda, parName ="kappa")
diagMCMC (codaObject = mcmcCoda, parName ="theta[1]")
# Get summary statistics of chain :
smryMCMC (mcmcCoda, compVal = 0.5, diffIdVec = c (1,14,28), compValDiff = 0.0)
# Display posterior information :
plotMCMC (mcmcCoda, data = myData, sName ="s", yName ="y", compVal = 0.5, diffIdVec = c (1,14,28), compValDiff = 0.0)
z = aggregate (y, by = list (s), FUN = sum)$x
N = aggregate (rep(1, length(y)), by = list(s), FUN = sum)$x
dataList = list (z = z, N = N, Nsubj = length(unique(s)))
model {for(s in 1 : Nsubj) {z[s] ~ dbin(theta[s], N[s])theta[s] ~ dbeta(omega*(kappa - 2) + 1, (1 - omega) * (kappa - 2) + 1)} omega ~ dbeta(1,1)
kappa < − kappaMinusTwo + 2
kappaMinusTwo ~ dgamma (0.01, 0.01) # mean = 1, sd = 10 (generic vague)}
source ("Jags-Ydich-XnomSsubj-MbinomBetaOmegaKappa.R")
model {for(s in 1 : Nsubj) {z[s] ~ dbin (theta[s], N[s]) theta [s] ~ dbeta (omega[c[s]] * (kappa [c[s]] - 2) + 1, (1 - omega [c[s]]) * (kappa [c[s]] - 2) + 1)} for (c in 1 : Ncat) {omega[c] ~ dbeta (omega0 * (kappa0 - 2) + 1, (1 - omega$\omega$) * (kappa0 - 2) + 1)
kappa [c] <− kappaMinusTwo [c] + 2 kappaMinusTwo [c] ~ dgamma (0.01, 0.01) #mean = 1, sd = 10 (generic vague)} omega0 ~ dbeta (1.0, 1.0)
kappa0 <− kappaMinusTwo0 + 2 kappaMinusTwoO ~ dgamma (0.01, 0.01) #mean = 1, sd = 10 (generic vague)}
Player, PriPos, Hits, AtBats, PlayerNumber, PriPosNumber Fernando Abad, Pitcher, 1,7,1,1 Bobby Abreu.Left Field, 53,219,2,7 Tony Abreu, 2nd Base, 18,70,3,4 Dustin Ackley, 2nd Base, 137,607,4,4 Matt Adams, 1st Base, 21,86,5,3 …[943 more rows] …
# Read the data
myData = read.csv ("BattingAverage.csv")
# Load the relevant model into R's working memory :
source ("Jags - Ybinom - XnomSsubjCcat - MbinomBetaOmegaKappa.R")
# Generate the MCMC chain :
mcmcCoda = genMCMC (data = myData, zName ="Hits", NName ="AtBats", sName ="Player", cName ="PriPos", numSavedSteps = 11000, thinSteps = 20)
# Display diagnostics of chain, for specified parameters :
for (parName in c ("omega[1]","omega0","kappa[1]","kappa0","theta[1]")) {diagMCMC (codaObject = mcmcCoda, parName = parName, saveName = fileNameRoot, saveType = graphFileType)}
# Get summary statistics of chain :
summarylnfo = smryMCMC (mcmcCoda, compVal = NULL)
# Display posterior information :
plotMCMC (mcmcCoda, data = myData, zName ="Hits", NName ="AtBats", sName ="Player", cName ="PriPos", compVal = NULL, diffCList = list (c ("Pitcher","Catcher"), c ("Catcher","1st Base")), diffSList = list (c ("Kyle Blanks","Bruce Chen"), c ("Mike Leake","Wandy Rodriguez"), c ("Andrew McCutchen","Brett Jackson"), c ("ShinSoo Choo","Ichiro Suzuki")), compValDiff = 0.0)
gammaShRaFromMeanSD(1.0, 10)
## $shape
## [1] 0.01
##
## $rate
## [1] 0.01
gammaShRaFromModeSD(1, 10)
## $shape
## [1] 1.105125
##
## $rate
## [1] 0.1051249
plot.new()
k=seq(0,200,length=10001)
openGraph(height=7,width=7)
layout(matrix(1:3,ncol=1))
plot(k, dgamma(k,1.105125,0.105125), ylab="dgamma(k)",
type="l", main="Gamma Distributions (SD=10)")
lines(k, dgamma(k,0.01,0.01), col="lightgreen")
legend("topright", c("Mode 1","Mean 1"),
lty=c(1,1), col=c("black","lightgreen"), text.col=c("black","lightgreen"))
plot(k, dgamma(k,1.105125,0.105125), ylab="dgamma(k)",
type="l", ylim=c(.07,.08), main="Gamma Distributions’s (SD=10), zoomed in")
lines(k, dgamma(k,0.01,0.01), col="lightgreen")
legend("topright", c("Mode 1","Mean 1"),
lty=c(1,1), col=c("black","lightgreen"), text.col=c("black","lightgreen"))
plot(k, dgamma(k,1.105125,0.105125), ylab="dgamma(k)",
type="l", ylim=c(0,8.0e-5), main="Gamma Distributions’s (SD=10), zoomed in")
lines(k, dgamma(k,0.01,0.01), col="lightgreen")
legend("topright", c("Mode 1","Mean 1"),
lty=c(1,1), col=c("black","lightgreen"), text.col=c("black","lightgreen"))
#### C.)
myData$s = factor(myData$s)
source("Jags-Ydich-XnomSsubj-MbinomBetaOmegaKappa1.R")
mcmcCoda = genMCMC(data=myData, sName="s", yName="y",
numSavedSteps=20000, thinSteps=1)
diagMCMC(codaObject=mcmcCoda, parName="omega")
diagMCMC(codaObject=mcmcCoda, parName="kappa")
diagMCMC(codaObject=mcmcCoda, parName="theta[1]")
smryMCMC(mcmcCoda,
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
plotMCMC(mcmcCoda, data=myData, sName="s", yName="y",
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
myData$s = factor(myData$s)
source("Jags-Ydich-XnomSsubj-MbinomBetaOmegaKappa2.R")
mcmcCoda = genMCMC(data=myData, sName="s", yName="y",
numSavedSteps=20000, thinSteps=10)
diagMCMC(codaObject=mcmcCoda, parName="omega")
diagMCMC(codaObject=mcmcCoda, parName="kappa")
diagMCMC(codaObject=mcmcCoda, parName="theta[1]")
smryMCMC(mcmcCoda,
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
plotMCMC(mcmcCoda, data=myData, sName="s", yName="y",
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
Yes, when K is increased the amount of shrinkage increases and the skews to the right.
myData$s = factor(myData$s)
source("Jags-Ydich-XnomSsubj-MbinomBetaOmegaKappa_z1.R")
mcmcCoda = genMCMC(data=myData, sName="s", yName="y",
numSavedSteps=20000, thinSteps=1)
smryMCMC(mcmcCoda,
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
plotMCMC(mcmcCoda, data=myData, sName="s", yName="y",
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
myData$s = factor(myData$s)
source("Jags-Ydich-XnomSsubj-MbinomBetaOmegaKappa_z2.R")
mcmcCoda = genMCMC(data=myData, sName="s", yName="y",
numSavedSteps=20000, thinSteps=1)
smryMCMC(mcmcCoda,
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
plotMCMC(mcmcCoda, data=myData, sName="s", yName="y",
compVal=0.5, diffIdVec=c(1,14,28), compValDiff=0.0)
Gamma with a mean of 1.0
Gamma with a mean of 1.0
The first prior is more appropriate to use because in the second prior data visualization is not possible. There mean = 1.
ht = c(rep(1,30),rep(0,100-30),
rep(1,40),rep(0,100-40),
rep(1,50),rep(0,100-50),
rep(1,60),rep(0,100-60),
rep(1,70),rep(0,100-70))
id = factor(c(rep("group1",100),
rep("group2",100),
rep("group3",100),
rep("group4",100),
rep("group5",100)))
newdata = data.frame(y=ht, s=id)
source("jags-Ydich-XnomSsubj-MbernBetaOmegaKappa.R")
mcmcCoda = genMCMC(data = newdata, sName ="s", yName ="y", numSavedSteps = 20000, saveName = fileNameRoot, thinSteps = 10)
summar$y_i$nfo = smryMCMC(mcmcCoda, compVal=0.5,
diffIdVec=c(1,2,3,4,5), compValDiff=0.0,
saveName=fileNameRoot)
plotMCMC(mcmcCoda, data=myData, sName="s", yName="y",
compVal=0.5,
diffIdVec=c(1,2,3,4,5), compValDiff=0.0,
saveName="9.3-Attempt", saveType="png")
Gamma with a mean of 1.0
Reducing the number of chains to a single chain had two effects. The first and most obvious is that running the simulations takes considerably longer to complete, (17.97s for 3 chains and 43.89s for 1 chain.) The second effect was that in the single chain simulation there was an error that was created where it wouldn’t run the last iteration in the chain which makes sense because there is only one chain being run.