The basic area-level model (Fay and Herriot 1979; Rao and Molina 2015) is given by \[ y_i | \theta_i \stackrel{\mathrm{iid}}{\sim} {\cal N} (\theta_i, \psi_i) \,, \\ \theta_i = \beta' x_i + v_i \,, \] where \(i\) runs from 1 to \(m\), the number of areas, \(\beta\) is a vector of regression coefficients for given covariates \(x_i\), and \(v_i \stackrel{\mathrm{iid}}{\sim} {\cal N} (0, \sigma_v^2)\) are independent random area effects. For each area an observation \(y_i\) is available with given variance \(\psi_i\).
First we generate some data according to this model:
m <- 75L # number of areas
df <- data.frame(
area=1:m, # area indicator
x=runif(m) # covariate
)
v <- rnorm(m, sd=0.5) # true area effects
theta <- 1 + 3*df$x + v # quantity of interest
psi <- runif(m, 0.5, 2) / sample(1:25, m, replace=TRUE) # given variances
df$y <- rnorm(m, theta, sqrt(psi))
A sampler function for a model with a regression component and a random intercept is created by
library(mcmcsae)
model <- y ~ reg(~ 1 + x, name="beta") + gen(factor = ~iid(area), name="v")
sampler <- create_sampler(model, sigma.fixed=TRUE, Q0=1/psi, linpred="fitted", data=df)
The meaning of the arguments used here is as follows:
sigma.fixed=TRUE
signifies that the observation level
variance parameter is fixed at 1. In this case it means that the
variances are known and given by psi
.Q0=1/psi
the precisions are set to the vector
1/psi
.linpred="fitted"
indicates that we wish to obtain
samples from the posterior distribution for the vector \(\theta\) of small area means.data
is the data.frame
in which variables
used in the model specification are looked up.An MCMC simulation using this sampler function is then carried out as follows:
A summary of the results is obtained by
## llh_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## llh_ -17.5 5.86 -2.99 0.118 -27.6 -17.2 -8.45 2453 1
##
## linpred_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 2.20 0.367 6.00 0.00698 1.59 2.20 2.81 2768 1.000
## 2 2.50 0.246 10.17 0.00454 2.11 2.50 2.92 2934 1.001
## 3 3.01 0.214 14.06 0.00414 2.66 3.00 3.36 2667 0.999
## 4 3.93 0.287 13.69 0.00528 3.46 3.92 4.40 2953 1.000
## 5 2.31 0.245 9.44 0.00457 1.91 2.32 2.71 2876 0.999
## 6 1.59 0.234 6.78 0.00449 1.20 1.59 1.98 2722 1.000
## 7 2.08 0.234 8.87 0.00428 1.69 2.08 2.46 3000 0.999
## 8 2.34 0.227 10.33 0.00424 1.97 2.34 2.72 2848 1.000
## 9 2.72 0.253 10.77 0.00464 2.30 2.73 3.14 2965 0.999
## 10 1.69 0.202 8.36 0.00369 1.36 1.69 2.03 3000 1.000
## ... 65 elements suppressed ...
##
## beta :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## (Intercept) 1.03 0.131 7.9 0.00781 0.823 1.03 1.25 281 1.02
## x 3.01 0.266 11.3 0.01350 2.560 3.01 3.44 388 1.01
##
## v_sigma :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## v_sigma 0.491 0.0559 8.79 0.00128 0.407 0.489 0.589 1907 1
##
## v :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 -0.00708 0.365 -0.0194 0.00693 -0.625 -0.0134 0.5963 2779 1.001
## 2 0.64802 0.253 2.5634 0.00504 0.241 0.6484 1.0634 2519 1.002
## 3 0.16433 0.225 0.7303 0.00557 -0.195 0.1603 0.5348 1631 0.999
## 4 0.25531 0.299 0.8548 0.00686 -0.234 0.2492 0.7445 1893 0.999
## 5 0.59223 0.252 2.3533 0.00542 0.172 0.5977 0.9898 2158 1.001
## 6 -0.03407 0.240 -0.1420 0.00527 -0.427 -0.0339 0.3632 2073 1.001
## 7 -0.32248 0.240 -1.3413 0.00496 -0.725 -0.3177 0.0668 2346 1.000
## 8 -0.12703 0.235 -0.5408 0.00519 -0.513 -0.1294 0.2606 2047 1.001
## 9 0.06983 0.256 0.2732 0.00523 -0.364 0.0781 0.4914 2391 1.000
## 10 0.17099 0.216 0.7911 0.00740 -0.193 0.1744 0.5309 854 1.003
## ... 65 elements suppressed ...
In this example we can compare the model parameter estimates to the ‘true’ parameter values that have been used to generate the data. In the next plots we compare the estimated and ‘true’ random effects, as well as the model estimates and ‘true’ estimands. In the latter plot, the original ‘direct’ estimates are added as red triangles.
plot(v, summ$v[, "Mean"], xlab="true v", ylab="posterior mean"); abline(0, 1)
plot(theta, summ$linpred_[, "Mean"], xlab="true theta", ylab="estimated"); abline(0, 1)
points(theta, df$y, col=2, pch=2)
We can compute model selection measures DIC and WAIC by
## DIC p_DIC
## 87.65858 52.63415
## WAIC1 p_WAIC1 WAIC2 p_WAIC2
## 55.63238 20.61850 78.75724 32.18093
Posterior means of residuals can be extracted from the simulation
output using method residuals
. Here is a plot of (posterior
means of) residuals against covariate \(x\):
A linear predictor in a linear model can be expressed as a weighted
sum of the response variable. If we set
compute.weights=TRUE
then such weights are computed for all
linear predictors specified in argument linpred
. In this
case it means that a set of weights is computed for each area.
sampler <- create_sampler(model, sigma.fixed=TRUE, Q0=1/psi,
linpred="fitted", data=df, compute.weights=TRUE)
sim <- MCMCsim(sampler, store.all=TRUE, verbose=FALSE)
Now the weights
method returns a matrix of weights, in
this case a 75 \(\times\) 75 matrix
\(w_{ij}\) holding the weight of direct
estimate \(i\) in linear predictor
\(j\). To verify that the weights
applied to the direct estimates yield the model-based estimates we plot
them against each other. Also shown is a plot of the weight of the
direct estimate for each area in the predictor for that same area,
against the variance of the direct estimate.
plot(summ$linpred_[, "Mean"], crossprod(weights(sim), df$y),
xlab="estimate", ylab="weighted average")
abline(0, 1)
plot(psi, diag(weights(sim)), ylab="weight")