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_ -16.5 5.99 -2.75 0.119 -27 -16.1 -7.28 2535 1
##
## beta :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## (Intercept) 0.816 0.122 6.71 0.00224 0.613 0.817 1.02 2933 1
## x 3.295 0.234 14.09 0.00431 2.908 3.294 3.68 2948 1
##
## v_sigma :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## v_sigma 0.474 0.0566 8.38 0.00138 0.387 0.471 0.574 1689 1
##
## linpred_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 4.04 0.220 18.36 0.00402 3.675 4.034 4.40 3000 0.999
## 2 1.44 0.262 5.49 0.00479 1.013 1.445 1.86 3000 1.000
## 3 2.26 0.342 6.60 0.00645 1.692 2.256 2.80 2813 0.999
## 4 1.73 0.250 6.89 0.00469 1.304 1.725 2.14 2852 1.000
## 5 3.73 0.258 14.47 0.00470 3.304 3.731 4.15 3000 0.999
## 6 1.33 0.321 4.14 0.00601 0.807 1.336 1.85 2864 0.999
## 7 1.01 0.297 3.39 0.00547 0.514 1.017 1.48 2940 1.000
## 8 3.25 0.230 14.11 0.00437 2.872 3.252 3.63 2780 1.001
## 9 3.93 0.237 16.58 0.00451 3.543 3.925 4.32 2760 1.000
## 10 0.70 0.310 2.26 0.00565 0.187 0.706 1.20 3000 1.001
## ... 65 elements suppressed ...
##
## v :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 0.178 0.243 0.732 0.00452 -0.2206 0.180 0.5654 2879 1.000
## 2 -0.410 0.266 -1.542 0.00486 -0.8593 -0.407 0.0175 3000 1.000
## 3 0.263 0.344 0.764 0.00644 -0.2983 0.261 0.8147 2860 0.999
## 4 -0.435 0.253 -1.716 0.00474 -0.8543 -0.440 -0.0132 2853 1.000
## 5 -0.118 0.275 -0.431 0.00502 -0.5631 -0.117 0.3359 3000 0.999
## 6 0.504 0.321 1.570 0.00591 -0.0147 0.507 1.0144 2953 0.999
## 7 -0.161 0.297 -0.542 0.00554 -0.6498 -0.156 0.3222 2882 0.999
## 8 -0.821 0.254 -3.228 0.00464 -1.2459 -0.819 -0.4110 3000 1.000
## 9 0.316 0.254 1.243 0.00487 -0.0885 0.308 0.7466 2724 1.001
## 10 -0.549 0.315 -1.745 0.00575 -1.0862 -0.546 -0.0382 3000 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
## DIC p_DIC
## 85.57392 52.64448
## WAIC1 p_WAIC1 WAIC2 p_WAIC2
## 54.35053 21.40862 78.47102 33.46886
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")