The basic area-level model (Fay and Herriot 1979; Rao and Molina 2015) is given by \[ y_i | \theta_i \stackrel{\mathrm{ind}}{\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{ind}}{\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_ -23.6 6.12 -3.86 0.125 -34.3 -23.3 -14.2 2399 1
##
## linpred_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 2.57 0.248 10.33 0.00466 2.15 2.57 2.97 2838 1.001
## 2 3.43 0.307 11.17 0.00588 2.93 3.42 3.94 2719 1.001
## 3 3.69 0.200 18.44 0.00366 3.36 3.69 4.02 3000 1.001
## 4 2.05 0.250 8.18 0.00458 1.63 2.05 2.44 2984 1.000
## 5 3.54 0.308 11.49 0.00563 3.03 3.55 4.05 3000 0.999
## 6 2.51 0.277 9.06 0.00513 2.06 2.52 2.96 2924 0.999
## 7 3.50 0.205 17.10 0.00375 3.16 3.50 3.82 2977 1.001
## 8 1.59 0.181 8.76 0.00333 1.29 1.59 1.88 2960 1.000
## 9 3.85 0.187 20.60 0.00342 3.54 3.85 4.16 2990 1.000
## 10 2.57 0.237 10.85 0.00432 2.17 2.57 2.96 3000 1.000
## ... 65 elements suppressed ...
##
## beta :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## (Intercept) 1.22 0.138 8.86 0.00693 1.01 1.22 1.45 396 1.01
## x 2.72 0.235 11.56 0.01258 2.32 2.73 3.09 350 1.01
##
## v_sigma :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## v_sigma 0.472 0.0588 8.03 0.00157 0.379 0.468 0.573 1398 1
##
## v :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 0.000374 0.253 0.00148 0.00548 -0.4278 0.000158 0.414 2138 1
## 2 0.309746 0.308 1.00636 0.00602 -0.1981 0.303631 0.816 2612 1
## 3 0.293517 0.216 1.35646 0.00599 -0.0666 0.292252 0.648 1304 1
## 4 -0.759548 0.257 -2.95129 0.00553 -1.1895 -0.752820 -0.346 2166 1
## 5 -0.354127 0.316 -1.12054 0.00693 -0.8871 -0.341982 0.174 2082 1
## 6 0.179679 0.280 0.64177 0.00595 -0.2908 0.180190 0.630 2217 1
## 7 0.161345 0.215 0.75064 0.00507 -0.2049 0.164465 0.503 1797 1
## 8 0.083997 0.207 0.40592 0.00643 -0.2600 0.083744 0.410 1036 1
## 9 -0.089915 0.221 -0.40769 0.00804 -0.4535 -0.086396 0.269 752 1
## 10 -0.375779 0.242 -1.55236 0.00472 -0.7816 -0.376625 0.021 2630 1
## ... 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
## 97.30685 50.12496
## WAIC1 p_WAIC1 WAIC2 p_WAIC2
## 68.30391 21.12553 90.94138 32.44427
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")