# Basic area-level model

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:

- the first argument is a formula specifying the response variable and the linear predictor to model the mean of the sampling distribution
- the `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:

    sim <- MCMCsim(sampler, store.all=TRUE, verbose=FALSE)

A summary of the results is obtained by

    (summ <- summary(sim))

    ## llh_ :
    ##      Mean   SD t-value  MCSE q0.05  q0.5 q0.95 n_eff R_hat
    ## llh_  -16 5.87   -2.73 0.121 -26.1 -15.7 -6.82  2362     1
    ##
    ## beta :
    ##              Mean    SD t-value    MCSE q0.05  q0.5 q0.95 n_eff R_hat
    ## (Intercept) 0.995 0.129    7.73 0.00235 0.781 0.997  1.21  3000     1
    ## x           3.226 0.238   13.55 0.00435 2.836 3.224  3.62  3000     1
    ##
    ## v_sigma :
    ##          Mean     SD t-value    MCSE q0.05  q0.5 q0.95 n_eff R_hat
    ## v_sigma 0.451 0.0528    8.55 0.00119  0.37 0.447  0.54  1977     1
    ##
    ## linpred_ :
    ##    Mean    SD t-value    MCSE q0.05 q0.5 q0.95 n_eff R_hat
    ## 1  3.39 0.191   17.77 0.00348  3.08 3.39  3.71  3000 1.000
    ## 2  3.06 0.172   17.81 0.00314  2.78 3.06  3.35  3000 1.000
    ## 3  4.02 0.289   13.93 0.00538  3.54 4.02  4.50  2885 1.001
    ## 4  3.60 0.226   15.97 0.00420  3.23 3.60  3.97  2886 1.000
    ## 5  2.62 0.209   12.56 0.00438  2.27 2.62  2.96  2275 1.002
    ## 6  1.62 0.241    6.71 0.00459  1.23 1.62  2.02  2766 1.000
    ## 7  2.87 0.197   14.56 0.00363  2.54 2.87  3.19  2948 0.999
    ## 8  1.83 0.196    9.38 0.00357  1.51 1.83  2.16  3000 0.999
    ## 9  3.06 0.309    9.89 0.00601  2.56 3.06  3.56  2651 1.000
    ## 10 3.52 0.197   17.87 0.00360  3.20 3.52  3.85  2983 0.999
    ## ... 65 elements suppressed ...
    ##
    ## v :
    ##       Mean    SD t-value    MCSE   q0.05    q0.5    q0.95 n_eff R_hat
    ## 1   0.1846 0.202   0.912 0.00369 -0.1491  0.1840  0.51421  3000 1.000
    ## 2  -0.3329 0.190  -1.756 0.00346 -0.6429 -0.3356 -0.02205  3000 1.000
    ## 3  -0.0527 0.295  -0.179 0.00539 -0.5494 -0.0517  0.42803  3000 1.001
    ## 4  -0.0899 0.238  -0.378 0.00441 -0.4733 -0.0915  0.29270  2913 1.000
    ## 5  -0.7117 0.222  -3.211 0.00425 -1.0773 -0.7064 -0.35556  2722 1.001
    ## 6   0.1989 0.254   0.784 0.00481 -0.2180  0.2026  0.61016  2786 1.001
    ## 7   0.2907 0.203   1.433 0.00374 -0.0485  0.2930  0.61781  2949 0.999
    ## 8  -0.3388 0.203  -1.667 0.00371 -0.6736 -0.3379 -0.00114  3000 1.000
    ## 9   0.1993 0.310   0.642 0.00610 -0.3149  0.2028  0.70116  2587 1.000
    ## 10  0.8736 0.207   4.219 0.00380  0.5345  0.8732  1.21027  2964 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

    compute_DIC(sim)

    ##      DIC    p_DIC
    ## 83.76179 51.72019

    compute_WAIC(sim, show.progress=FALSE)

    ##    WAIC1  p_WAIC1    WAIC2  p_WAIC2
    ## 53.36682 21.32897 76.75660 33.02386

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.

    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")

## References

Fay, R. E., and R. A. Herriot. 1979. “Estimates of Income for Small Places: An Application of James-Stein Procedures to Census Data.” Journal of the American Statistical Association 74 (366): 269–77.
Rao, J. N. K., and I. Molina. 2015. Small Area Estimation. John Wiley & Sons.