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, β is a vector of regression coefficients for given covariates xi, and $v_i \stackrel{\mathrm{iid}}{\sim} {\cal N} (0, \sigma_v^2)$ are independent random area effects. For each area an observation yi is available with given variance ψ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 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 θ 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_ -22.8 5.99 -3.81 0.122 -33.2 -22.6 -13.4 2412 1
##
## linpred_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 1.91 0.360 5.29 0.00668 1.331 1.91 2.51 2913 1.000
## 2 1.58 0.264 5.96 0.00483 1.151 1.58 2.02 3000 1.000
## 3 1.88 0.201 9.33 0.00373 1.545 1.88 2.21 2908 0.999
## 4 1.28 0.269 4.77 0.00491 0.833 1.28 1.71 3000 1.001
## 5 2.15 0.230 9.33 0.00421 1.767 2.14 2.53 2997 1.001
## 6 3.50 0.224 15.57 0.00415 3.132 3.49 3.87 2924 1.001
## 7 2.74 0.229 11.97 0.00432 2.372 2.73 3.11 2799 1.000
## 8 2.05 0.202 10.13 0.00368 1.719 2.04 2.38 3000 1.000
## 9 2.94 0.225 13.06 0.00415 2.564 2.95 3.31 2947 1.000
## 10 2.12 0.300 7.09 0.00570 1.626 2.13 2.60 2766 1.000
## ... 65 elements suppressed ...
##
## beta :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## (Intercept) 1.25 0.164 7.63 0.00300 0.98 1.25 1.52 2990 1
## x 2.67 0.278 9.62 0.00514 2.23 2.68 3.14 2922 1
##
## v_sigma :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## v_sigma 0.53 0.0603 8.79 0.00152 0.437 0.528 0.632 1580 1
##
## v :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 -0.1667 0.362 -0.460 0.00678 -0.7497 -0.1692 0.431 2852 1.000
## 2 0.1783 0.286 0.624 0.00522 -0.2943 0.1794 0.658 3000 1.000
## 3 -0.2121 0.216 -0.984 0.00397 -0.5617 -0.2177 0.153 2950 0.999
## 4 -0.1045 0.290 -0.360 0.00544 -0.5825 -0.1027 0.372 2845 1.001
## 5 0.0437 0.242 0.181 0.00442 -0.3604 0.0454 0.435 3000 1.000
## 6 0.2927 0.236 1.241 0.00432 -0.0888 0.2908 0.677 2988 1.001
## 7 0.7020 0.241 2.911 0.00448 0.3120 0.7018 1.103 2896 1.000
## 8 -0.0921 0.215 -0.429 0.00392 -0.4381 -0.0893 0.260 3000 0.999
## 9 -0.5850 0.248 -2.359 0.00459 -1.0003 -0.5789 -0.182 2926 1.000
## 10 0.8156 0.309 2.637 0.00565 0.2985 0.8219 1.304 3000 0.999
## ... 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
## 99.58333 53.94897
## WAIC1 p_WAIC1 WAIC2 p_WAIC2
## 67.24184 21.59842 90.77154 33.36327
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 × 75 matrix wij
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")