Alligators: multinomial - logistic regression
The data are stomach contents for 219 alligators (Agresti's published table quotes 221; the array shipped with this example sums to 219), cross-classified three ways: by which of 4 lakes the animal came from, by which of 2 size classes it fell into, and by which of 5 food categories its primary stomach content belonged to. The counts arrive as a 4-by-2-by-5 array X, so each lake-and-size cell holds between 16 and 41 animals distributed over the five food types. The question is whether the food an alligator eats depends on where it lives and how big it is, and if so, how those two factors combine.
Statistically this is a multinomial logistic regression: within each lake-size cell the five counts are a multinomial draw, and the log odds of each food type relative to a baseline food are modelled additively as a lake effect plus a size effect. The model fits it in the equivalent Poisson form instead, which is the standard multinomial-Poisson transformation:
\[X_{ijk} \sim \text{Poisson}(\mu_{ijk}), \qquad \log \mu_{ijk} = \lambda_{ij} + \alpha_k + \beta_{ik} + \gamma_{jk}\]
Here $\lambda_{ij}$ is a free parameter for each lake-size cell whose only job is to absorb that cell's total, so that the remaining structure reproduces the multinomial likelihood exactly. It is given a dflat() improper prior for that reason. The direct multinomial version is present in the source as commented-out code. Identifiability is imposed by corner-point contrasts, with $\alpha_1$, the whole first row of $\beta$, the whole first row of $\gamma$, and the first column of each of $\beta$ and $\gamma$ all fixed at zero; the derived quantities b and g recentre those contrasts to sum to zero so that they can be compared with Agresti's published analysis of the same table.
The dflat() prior is what currently prevents JuliaBUGS from compiling this model, as noted below, so the model definition and data on this page are for reference rather than for running. For the original analysis see Volume 2 of the classic BUGS examples and the OpenBUGS Alligators page.
The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_2.alligators, which ships with the package.
Model
using JuliaBUGS
example = JuliaBUGS.BUGSExamples.VOLUME_2.alligators
example.model_defquote
alpha[1] = 0
for k = 2:K
alpha[k] ~ dnorm(0, 1.0e-5)
end
for k = 1:K
beta[1, k] = 0
end
for i = 2:I
beta[i, 1] = 0
for k = 2:K
beta[i, k] ~ dnorm(0, 1.0e-5)
end
end
for k = 1:K
gamma[1, k] = 0
end
for j = 2:J
gamma[j, 1] = 0
for k = 2:K
gamma[j, k] ~ dnorm(0, 1.0e-5)
end
end
for i = 1:I
for j = 1:J
lambda[i, j] ~ dflat()
for k = 1:K
X[i, j, k] ~ dpois(mu[i, j, k])
mu[i, j, k] = exp(lambda[i, j] + alpha[k] + beta[i, k] + gamma[j, k])
end
end
end
for k = 1:K
for i = 1:I
b[i, k] = beta[i, k] - mean(beta[:, k])
end
for j = 1:J
g[j, k] = gamma[j, k] - mean(gamma[:, k])
end
end
endThe program as it appears in the original BUGS distribution:
print(example.original_syntax_program)model {
# PRIORS
alpha[1] <- 0; # zero contrast for baseline food
for (k in 2 : K) {
alpha[k] ~ dnorm(0, 0.00001) # vague priors
}
# Loop around lakes:
for (k in 1 : K){
beta[1, k] <- 0 # corner-point contrast with first lake
}
for (i in 2 : I) {
beta[i, 1] <- 0; # zero contrast for baseline food
for (k in 2 : K){
beta[i, k] ~ dnorm(0, 0.00001) # vague priors
}
}
# Loop around sizes:
for (k in 1 : K){
gamma[1, k] <- 0 # corner-point contrast with first size
}
for (j in 2 : J) {
gamma[j, 1] <- 0; # zero contrast for baseline food
for (k in 2 : K){
gamma[j, k] ~ dnorm(0, 0.00001) # vague priors
}
}
# LIKELIHOOD
for (i in 1 : I) { # loop around lakes
for (j in 1 : J) { # loop around sizes
# Fit standard Poisson regressions relative to baseline
lambda[i, j] ~ dflat() # vague priors
for (k in 1 : K) { # loop around foods
X[i, j, k] ~ dpois(mu[i, j, k])
log(mu[i, j, k]) <- lambda[i, j] + alpha[k] + beta[i, k] + gamma[j, k]
}
}
}
# TRANSFORM OUTPUT TO ENABLE COMPARISON
# WITH AGRESTI'S RESULTS
for (k in 1 : K) { # loop around foods
for (i in 1 : I) { # loop around lakes
b[i, k] <- beta[i, k] - mean(beta[, k]); # sum to zero constraint
}
for (j in 1 : J) { # loop around sizes
g[j, k] <- gamma[j, k] - mean(gamma[, k]); # sum to zero constraint
}
}
}Data
example.data(I = 4, J = 2, K = 5, X = [23 7; 5 13; 5 8; 16 17;;; 4 0; 11 8; 11 7; 19 1;;; 2 1; 1 6; 2 6; 1 0;;; 2 3; 0 1; 1 3; 2 1;;; 8 5; 3 0; 5 5; 3 3])Compiling the model
model = JuliaBUGS.compile(example.model_def, example.data, example.inits)BUGSModel (parameters are in transformed (unconstrained) space, with dimension 28):
Model parameters:
lambda[4, 2], lambda[4, 1], lambda[3, 2], lambda[3, 1], lambda[2, 2], lambda[2, 1], lambda[1, 2], lambda[1, 1]
alpha[5], alpha[4], alpha[3], alpha[2]
beta[4, 5], beta[4, 4], beta[4, 3], beta[4, 2], beta[3, 5], beta[3, 4], beta[3, 3], beta[3, 2], beta[2, 5], beta[2, 4], beta[2, 3], beta[2, 2]
gamma[2, 5], gamma[2, 4], gamma[2, 3], gamma[2, 2]
Variable sizes and types:
alpha: size = (5,), type = Vector{Int64}
b: size = (4, 5), type = Matrix{Float64}
gamma: size = (2, 5), type = Matrix{Int64}
lambda: size = (4, 2), type = Matrix{Float64}
K: type = Int64
X: size = (4, 2, 5), type = Array{Int64, 3}
mu: size = (4, 2, 5), type = Array{Float64, 3}
g: size = (2, 5), type = Matrix{Float64}
I: type = Int64
beta: size = (4, 5), type = Matrix{Int64}
J: type = Int64
The example's own initial values are passed in here. Several of these models fail from random starting values, so example.inits is not optional in practice; a second set is available as example.inits_alternative.
See Getting Started for the recipe that takes a compiled model to posterior samples.
Reference results
The posterior summaries published with the original example. A converged chain should reproduce these up to Monte Carlo error.
example.reference_results(var"b[1,2]" = (mean = -1.809, std = 0.4705), var"b[1,3]" = (mean = -0.337, std = 0.614), var"b[1,4]" = (mean = 0.5645, std = 0.5681), var"b[1,5]" = (mean = 0.2861, std = 0.3644), var"b[2,2]" = (mean = 0.8471, std = 0.3415), var"b[2,3]" = (mean = 0.9382, std = 0.5172), var"b[2,4]" = (mean = -1.279, std = 1.01), var"b[2,5]" = (mean = -0.6744, std = 0.54), var"b[3,2]" = (mean = 1.076, std = 0.3502), var"b[3,3]" = (mean = 1.451, std = 0.5103), var"b[3,4]" = (mean = 0.9416, std = 0.5918), var"b[3,5]" = (mean = 0.9967, std = 0.3929), var"b[4,2]" = (mean = -0.1136, std = 0.2929), var"b[4,3]" = (mean = -2.053, std = 1.007), var"b[4,4]" = (mean = -0.2273, std = 0.6213), var"b[4,5]" = (mean = -0.6084, std = 0.4099), var"g[1,2]" = (mean = 0.7646, std = 0.2024), var"g[1,3]" = (mean = -0.1807, std = 0.3109), var"g[1,4]" = (mean = -0.343, std = 0.3391), var"g[1,5]" = (mean = 0.1898, std = 0.235), var"g[2,2]" = (mean = -0.7646, std = 0.2024), var"g[2,3]" = (mean = 0.1807, std = 0.3109), var"g[2,4]" = (mean = 0.343, std = 0.3391), var"g[2,5]" = (mean = -0.1898, std = 0.235))