# 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, sigma.fixed=TRUE, Q0=1/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 part of the model
- `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`.
- the function expects precisions rather than variances, and with `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 \(\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_ -31.7 6.65   -4.76 0.144 -43.2 -31.3 -21.4  2141     1
    ##
    ## linpred_ :
    ##    Mean    SD t-value    MCSE q0.05 q0.5 q0.95 n_eff R_hat
    ## 1  1.84 0.269    6.85 0.00491  1.40 1.85  2.29  3000 1.001
    ## 2  1.87 0.499    3.75 0.00972  1.02 1.89  2.68  2639 1.001
    ## 3  4.11 0.225   18.31 0.00410  3.75 4.11  4.49  3000 1.000
    ## 4  2.34 0.300    7.78 0.00550  1.85 2.34  2.84  2982 1.000
    ## 5  2.15 0.467    4.61 0.00856  1.39 2.14  2.93  2974 1.000
    ## 6  3.09 0.463    6.66 0.00975  2.31 3.10  3.84  2261 1.000
    ## 7  2.95 0.463    6.38 0.00903  2.20 2.95  3.71  2626 1.000
    ## 8  1.82 0.472    3.87 0.00884  1.04 1.83  2.58  2847 1.001
    ## 9  2.30 0.176   13.05 0.00323  2.01 2.29  2.58  2962 0.999
    ## 10 1.73 0.178    9.71 0.00326  1.44 1.73  2.03  3000 1.001
    ## ... 65 elements suppressed ...
    ##
    ## beta :
    ##              Mean    SD t-value    MCSE q0.05  q0.5 q0.95 n_eff R_hat
    ## (Intercept) 0.899 0.147    6.13 0.00271 0.655 0.897  1.14  2940     1
    ## x           3.147 0.260   12.08 0.00476 2.709 3.152  3.57  3000     1
    ##
    ## v_sigma :
    ##          Mean     SD t-value    MCSE q0.05  q0.5 q0.95 n_eff R_hat
    ## v_sigma 0.568 0.0708    8.02 0.00175 0.459 0.563 0.689  1644     1
    ##
    ## v :
    ##       Mean    SD t-value    MCSE  q0.05    q0.5   q0.95 n_eff R_hat
    ## 1   0.0275 0.276  0.0997 0.00504 -0.430  0.0296  0.4828  3000 1.001
    ## 2  -0.3900 0.495 -0.7886 0.00959 -1.217 -0.3841  0.4212  2659 1.000
    ## 3   0.0969 0.255  0.3800 0.00465 -0.318  0.0936  0.5199  3000 0.999
    ## 4  -0.2195 0.305 -0.7199 0.00557 -0.719 -0.2152  0.2806  3000 1.000
    ## 5   0.1919 0.467  0.4111 0.00859 -0.571  0.1825  0.9733  2951 1.000
    ## 6  -0.9433 0.461 -2.0466 0.00933 -1.704 -0.9349 -0.1951  2440 1.001
    ## 7  -0.3226 0.459 -0.7032 0.00838 -1.048 -0.3311  0.4205  3000 1.000
    ## 8  -0.5522 0.466 -1.1838 0.00873 -1.328 -0.5343  0.1882  2857 1.001
    ## 9   0.1800 0.191  0.9411 0.00361 -0.137  0.1763  0.4992  2806 1.000
    ## 10 -0.2219 0.197 -1.1277 0.00359 -0.540 -0.2247  0.0967  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

    compute_DIC(sim)

    ##       DIC     p_DIC
    ## 116.34716  53.04106

    compute_WAIC(sim, show.progress=FALSE)

    ##     WAIC1   p_WAIC1     WAIC2   p_WAIC2
    ##  86.92288  23.60730 111.94541  36.11857

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, 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 \(\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.