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:
<- 75L # number of areas
m <- data.frame(
df area=1:m, # area indicator
x=runif(m) # covariate
)<- rnorm(m, sd=0.5) # true area effects
v <- 1 + 3*df$x + v # quantity of interest
theta <- runif(m, 0.5, 2) / sample(1:25, m, replace=TRUE) # given variances
psi $y <- rnorm(m, theta, sqrt(psi)) df
A sampler function for a model with a regression component and a random intercept is created by
library(mcmcsae)
<- y ~ reg(~ 1 + x, name="beta") + gen(factor = ~iid(area), name="v")
model <- create_sampler(model, sigma.fixed=TRUE, Q0=1/psi, linpred="fitted", data=df) sampler
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:
<- MCMCsim(sampler, store.all=TRUE, verbose=FALSE) sim
A summary of the results is obtained by
<- summary(sim)) (summ
## llh_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## llh_ -27.4 5.98 -4.59 0.121 -37.8 -27.2 -18.1 2441 1
##
## linpred_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 4.038 0.296 13.64 0.00549 3.550 4.034 4.52 2906 1.000
## 2 1.585 0.195 8.12 0.00356 1.269 1.580 1.91 3000 1.000
## 3 1.697 0.366 4.64 0.00674 1.101 1.692 2.29 2945 1.000
## 4 2.105 0.225 9.37 0.00411 1.740 2.104 2.47 2984 0.999
## 5 0.656 0.307 2.14 0.00569 0.150 0.657 1.17 2909 1.001
## 6 1.662 0.393 4.23 0.00752 0.997 1.667 2.31 2725 1.000
## 7 2.933 0.372 7.88 0.00680 2.326 2.944 3.54 3000 1.001
## 8 3.210 0.203 15.78 0.00371 2.884 3.206 3.54 3000 1.000
## 9 2.680 0.248 10.80 0.00467 2.272 2.676 3.09 2826 0.999
## 10 1.292 0.205 6.31 0.00374 0.960 1.287 1.63 3000 1.000
## ... 65 elements suppressed ...
##
## beta :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## (Intercept) 0.879 0.127 6.94 0.00584 0.668 0.881 1.09 470 1
## x 3.125 0.224 13.93 0.01112 2.765 3.117 3.50 407 1
##
## v_sigma :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## v_sigma 0.475 0.06 7.92 0.00154 0.382 0.471 0.58 1513 1
##
## v :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 0.368 0.301 1.222 0.00642 -0.121 0.372 0.8554 2203 1.000
## 2 0.618 0.217 2.845 0.00613 0.270 0.616 0.9776 1253 0.999
## 3 -0.306 0.362 -0.844 0.00662 -0.899 -0.320 0.2923 3000 1.000
## 4 -0.420 0.232 -1.814 0.00521 -0.803 -0.421 -0.0508 1973 0.999
## 5 -0.327 0.310 -1.053 0.00640 -0.832 -0.328 0.1841 2349 1.001
## 6 -0.581 0.391 -1.486 0.00741 -1.235 -0.576 0.0600 2785 1.000
## 7 -0.140 0.371 -0.376 0.00678 -0.759 -0.127 0.4623 3000 1.000
## 8 0.143 0.217 0.659 0.00601 -0.211 0.141 0.5132 1301 1.002
## 9 0.657 0.253 2.594 0.00599 0.245 0.658 1.0756 1789 1.000
## 10 -0.384 0.216 -1.777 0.00517 -0.739 -0.382 -0.0350 1742 1.001
## ... 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
compute_DIC(sim)
## DIC p_DIC
## 103.97561 49.10341
compute_WAIC(sim, show.progress=FALSE)
## WAIC1 p_WAIC1 WAIC2 p_WAIC2
## 76.29730 21.42812 98.84517 32.70206
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\):
plot(df$x, residuals(sim, mean.only=TRUE), xlab="x", ylab="residual"); abline(h=0)
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.
<- create_sampler(model, sigma.fixed=TRUE, Q0=1/psi,
sampler linpred="fitted", data=df, compute.weights=TRUE)
<- MCMCsim(sampler, store.all=TRUE, verbose=FALSE) sim
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")