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_ -23.2 5.96 -3.89 0.119 -33.5 -22.9 -14.1 2511 1
##
## beta :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## (Intercept) 0.844 0.157 5.37 0.00287 0.582 0.844 1.10 3000 1.000
## x 3.316 0.271 12.23 0.00495 2.867 3.313 3.76 3000 0.999
##
## v_sigma :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## v_sigma 0.558 0.0642 8.69 0.00139 0.457 0.555 0.668 2136 1
##
## linpred_ :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 3.85 0.192 20.01 0.00351 3.54 3.85 4.17 3000 1.000
## 2 2.67 0.306 8.71 0.00574 2.18 2.66 3.16 2843 1.001
## 3 3.64 0.221 16.44 0.00409 3.27 3.64 4.00 2923 1.000
## 4 4.36 0.279 15.63 0.00520 3.90 4.36 4.82 2879 1.002
## 5 3.18 0.168 18.98 0.00306 2.91 3.18 3.46 3000 1.000
## 6 2.36 0.252 9.35 0.00467 1.95 2.36 2.78 2913 0.999
## 7 3.55 0.208 17.10 0.00384 3.21 3.55 3.90 2928 1.001
## 8 2.27 0.262 8.64 0.00480 1.84 2.27 2.70 2989 1.000
## 9 3.75 0.266 14.09 0.00486 3.32 3.76 4.18 3000 1.000
## 10 2.50 0.202 12.34 0.00386 2.17 2.50 2.83 2745 1.000
## ... 65 elements suppressed ...
##
## v :
## Mean SD t-value MCSE q0.05 q0.5 q0.95 n_eff R_hat
## 1 0.1902 0.219 0.869 0.00400 -0.1828 0.1953 0.5398 3000 1.000
## 2 0.1668 0.309 0.540 0.00580 -0.3393 0.1595 0.6761 2837 1.001
## 3 -0.3238 0.250 -1.294 0.00461 -0.7272 -0.3210 0.0953 2947 1.000
## 4 0.4036 0.296 1.362 0.00543 -0.0829 0.4071 0.8884 2978 1.002
## 5 0.3558 0.181 1.963 0.00341 0.0539 0.3508 0.6582 2824 1.000
## 6 1.2024 0.273 4.411 0.00498 0.7596 1.1996 1.6493 2993 0.999
## 7 0.6994 0.220 3.178 0.00405 0.3337 0.6984 1.0584 2951 1.001
## 8 0.2003 0.274 0.732 0.00501 -0.2474 0.2024 0.6493 2981 0.999
## 9 -0.0375 0.284 -0.132 0.00519 -0.5094 -0.0389 0.4233 3000 1.000
## 10 -0.0445 0.212 -0.210 0.00404 -0.3882 -0.0397 0.2951 2764 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
## 100.18510 53.77971
## WAIC1 p_WAIC1 WAIC2 p_WAIC2
## 67.64486 21.26132 91.81870 33.34823
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")