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,
family=f_gaussian(var.prior=pr_fixed(1), var.vec = ~ psi),
linpred="fitted", data=df
)The meaning of the arguments used is as follows:
family argument allows to set one of a number of
sampling distributions and possibly pass additional family-dependent
arguments. In this case the scalar observation level variance parameter
is set to a fixed value 1, and unequal variances are set to the vector
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_ -21.4 5.82 -3.68 0.111 -31.7 -21 -12.6 2766 1
##
## beta :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## (Intercept) 1.15 0.141 8.17 0.00258 0.926 1.15 1.39 3000 0.999
## x 2.80 0.238 11.78 0.00434 2.393 2.80 3.20 3000 1.000
##
## v_sigma :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## v_sigma 0.512 0.058 8.83 0.00126 0.422 0.509 0.612 2122 1
##
## linpred_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 2.324 0.312 7.45 0.00570 1.805 2.320 2.83 2991 1.001
## 2 3.331 0.200 16.66 0.00365 2.994 3.330 3.66 3000 1.000
## 3 3.270 0.255 12.80 0.00479 2.860 3.268 3.69 2840 1.000
## 4 3.203 0.400 8.01 0.00730 2.533 3.209 3.87 3000 1.001
## 5 1.890 0.288 6.57 0.00525 1.420 1.882 2.36 3000 0.999
## 6 1.277 0.458 2.79 0.00898 0.520 1.289 2.01 2595 1.001
## 7 0.946 0.254 3.72 0.00481 0.534 0.942 1.36 2794 1.000
## 8 1.171 0.277 4.23 0.00526 0.724 1.169 1.62 2773 1.000
## 9 1.143 0.270 4.24 0.00493 0.701 1.139 1.59 3000 0.999
## 10 2.747 0.254 10.79 0.00484 2.317 2.744 3.16 2769 1.000
## ... 65 elements suppressed ...
##
## v :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 0.2341 0.314 0.747 0.00573 -0.288 0.2341 0.7348 2998 1.001
## 2 -0.3457 0.222 -1.560 0.00405 -0.715 -0.3447 0.0169 3000 1.001
## 3 -0.2693 0.269 -1.001 0.00494 -0.721 -0.2714 0.1661 2962 1.001
## 4 -0.3425 0.399 -0.858 0.00729 -1.005 -0.3417 0.3221 3000 1.000
## 5 0.1459 0.299 0.488 0.00545 -0.346 0.1449 0.6436 3000 0.999
## 6 -0.4811 0.450 -1.070 0.00882 -1.225 -0.4678 0.2340 2600 1.001
## 7 -1.1237 0.259 -4.337 0.00492 -1.545 -1.1265 -0.6862 2771 1.000
## 8 -0.0984 0.288 -0.342 0.00539 -0.569 -0.0977 0.3707 2848 1.000
## 9 -0.1411 0.279 -0.505 0.00510 -0.600 -0.1491 0.3382 3000 1.000
## 10 -1.0962 0.269 -4.074 0.00506 -1.537 -1.0958 -0.6448 2830 1.000
## ... 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
## 95.89678 53.03294
## WAIC1 p_WAIC1 WAIC2 p_WAIC2
## 64.09088 21.23522 87.40103 32.89030
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,
family=f_gaussian(var.prior=pr_fixed(1), var.vec = ~ 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")