This example is taken from the R2BayesX package (Umlauf et al. 2015) and is about undernutrition of new born children in Zambia. The data is loaded with

data("ZambiaNutrition", package = "R2BayesX")
head(ZambiaNutrition)
##        stunting  mbmi agechild district memployment meducation urban gender
## 1893 -0.6817840 22.33       29       12         yes         no   yes   male
## 1894 -1.5876900 22.33       57       12         yes         no   yes female
## 1895  0.3110890 18.66       16       12         yes         no   yes female
## 1896  0.0067044 18.66       46       12         yes         no   yes   male
## 1897  1.8765000 24.00       28       12         yes    primary   yes female
## 1898  0.1009190 24.22        9       12         yes    primary   yes   male

Here, the primary interest is to model the dependence of stunting of newborn children, with an age ranging from 0 to 5 years, on covariates such as the body mass index of the mother, the age of the child and others. Moreover, we apply a full distributional regression model with ๐šœ๐š๐šž๐š—๐š๐š’๐š—๐šโˆผ๐’ฉ(ฮผ=ฮทฮผ,logโก(ฯƒ)=ฮทฯƒ)), \texttt{stunting} \sim \mathcal{N}(\mu = \eta_{\mu}, \log(\sigma) = \eta_{\sigma})), where the predictors ฮทฮผ\eta_{\mu} and ฮทฯƒ\eta_{\sigma} are specified by the following formula

f <- list(
  stunting ~ memployment + urban + gender + meducation +
    s(mbmi) + s(agechild) + s(district, bs = "mrf", xt = list("penalty" = K)) +
    s(district, bs = "re"),
  sigma  ~ memployment + urban + gender + meducation +
    s(mbmi) + s(agechild) + s(district, bs = "mrf", xt = list("penalty" = K)) +
    s(district, bs = "re")
)

Note that for setting up the Markov random field smooth term a penalty matrix K needs to be provided. The penalty matrix forces penalization for neighboring regions of the districts in Zambia. To compute the K matrix, we need the spatial information about the regions in Zambia, which is shipped as a "bnd" object in the R2BayesX package and can be loaded with

data("ZambiaBnd", package = "R2BayesX")

The K matrix can then be computed using function neighbormatrix()

K <- neighbormatrix(ZambiaBnd)
head(K)
##    87 76 67 92 97 94 86 85 83 71 45 44 41 32 96 95 91 74 63 65 62 61 53 52 42
## 87  1  0  0  0  0 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 76  0  3  0  0  0  0  0 -1 -1 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 67  0  0  2  0  0  0  0  0  0 -1 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 92  0  0  0  3 -1 -1  0  0  0  0  0  0  0  0 -1  0  0  0  0  0  0  0  0  0  0
## 97  0  0  0 -1  5 -1  0  0  0  0  0  0  0  0 -1 -1 -1  0  0  0  0  0  0  0  0
## 94 -1  0  0 -1 -1  6 -1  0  0  0  0  0  0  0  0  0 -1 -1  0  0  0  0  0  0  0
##    34 99 93 98 84 75 88 81 43 89 73 72 68 69 66 36 35 26 23 14 12 13 11 33 28
## 87  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 76  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 67  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 92  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 97  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 94  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##    22 21 15 31 25 24 27
## 87  0  0  0  0  0  0  0
## 76  0  0  0  0  0  0  0
## 67  0  0  0  0  0  0  0
## 92  0  0  0  0  0  0  0
## 97  0  0  0  0  0  0  0
## 94  0  0  0  0  0  0  0
## Also need to transform to factor for
## setting up the MRF smooth.
ZambiaNutrition$district <- as.factor(ZambiaNutrition$district)

## Now note that not all regions are observed,
## therefore we need to remove those regions
## from the penalty matrix
rn <- rownames(K)
lv <- levels(ZambiaNutrition$district)
i <- rn %in% lv
K <- K[i, i]

Then, the model can be estimated with

set.seed(321)
b <- bamlss(f, data = ZambiaNutrition, family = "gaussian")

and the estimated univariate effects are plotted with

plot(b)

The plot indicates that only the effect of variable mbmi on the standard deviation is not significant according the 95% credible intervals and basically follows the zero horizontal line.

To visualize the structured and unstructured spatial effects we predict using the district information

## First, note that we have the structured id = 'mrf1' and unstructured
## spatial effect id = 're2', also indicated in the model summary
summary(b)
## 
## Call:
## bamlss(formula = f, family = "gaussian", data = ZambiaNutrition)
## ---
## Family: gaussian 
## Link function: mu = identity, sigma = log
## *---
## Formula mu:
## ---
## stunting ~ memployment + urban + gender + meducation + s(mbmi) + 
##     s(agechild) + s(district, bs = "mrf", xt = list(penalty = K)) + 
##     s(district, bs = "re")
## -
## Parametric coefficients:
##                        Mean      2.5%       50%     97.5% parameters
## (Intercept)        0.112529  0.054828  0.112681  0.164667      0.111
## memploymentno     -0.009084 -0.034573 -0.009223  0.017676     -0.010
## urbanno           -0.088765 -0.133189 -0.089647 -0.046362     -0.084
## genderfemale       0.059346  0.034196  0.059542  0.083778      0.059
## meducationno      -0.180105 -0.236164 -0.180668 -0.122967     -0.177
## meducationprimary -0.054820 -0.105983 -0.055103 -0.004124     -0.053
## -
## Acceptance probability:
##       Mean 2.5% 50% 97.5%
## alpha    1    1   1     1
## -
## Smooth terms:
##                                  Mean      2.5%       50%     97.5% parameters
## s(mbmi).tau21               1.989e-02 6.390e-05 1.726e-03 1.707e-01      0.017
## s(mbmi).alpha               1.000e+00 1.000e+00 1.000e+00 1.000e+00         NA
## s(mbmi).edf                 1.362e+00 1.004e+00 1.094e+00 3.026e+00      1.597
## s(agechild).tau21           1.610e+00 3.141e-01 1.110e+00 5.845e+00      4.538
## s(agechild).alpha           1.000e+00 1.000e+00 1.000e+00 1.000e+00         NA
## s(agechild).edf             6.293e+00 4.828e+00 6.266e+00 7.949e+00      7.738
## s(district,id='mrf1').tau21 3.522e-03 9.975e-04 3.376e-03 6.735e-03      0.004
## s(district,id='mrf1').alpha 1.000e+00 1.000e+00 1.000e+00 1.000e+00         NA
## s(district,id='mrf1').edf   3.009e+01 1.829e+01 3.069e+01 3.739e+01     32.503
## s(district,id='re2').tau21  4.185e-03 7.007e-05 1.200e-03 2.398e-02      0.001
## s(district,id='re2').alpha  1.000e+00 1.000e+00 1.000e+00 1.000e+00         NA
## s(district,id='re2').edf    1.108e+01 4.548e-01 6.298e+00 3.613e+01      5.838
## ---
## Formula sigma:
## ---
## sigma ~ memployment + urban + gender + meducation + s(mbmi) + 
##     s(agechild) + s(district, bs = "mrf", xt = list(penalty = K)) + 
##     s(district, bs = "re")
## -
## Parametric coefficients:
##                        Mean      2.5%       50%     97.5% parameters
## (Intercept)       -0.136161 -0.178850 -0.136434 -0.093026     -0.057
## memploymentno     -0.015871 -0.038755 -0.016025  0.004711     -0.012
## urbanno            0.036372 -0.001177  0.036592  0.071661     -0.002
## genderfemale       0.026074  0.006312  0.025828  0.047740      0.024
## meducationno       0.031556 -0.017279  0.031632  0.076734      0.028
## meducationprimary -0.030436 -0.073680 -0.029466  0.009285     -0.026
## -
## Acceptance probability:
##         Mean   2.5%    50% 97.5%
## alpha 0.9265 0.6355 0.9794     1
## -
## Smooth terms:
##                                  Mean      2.5%       50%     97.5% parameters
## s(mbmi).tau21               2.118e-01 1.142e-04 3.442e-02 1.236e+00      8.608
## s(mbmi).alpha               9.542e-01 7.160e-01 9.984e-01 1.000e+00         NA
## s(mbmi).edf                 2.473e+00 1.010e+00 2.192e+00 5.460e+00      7.598
## s(agechild).tau21           1.039e-01 3.084e-03 3.952e-02 5.668e-01      0.018
## s(agechild).alpha           9.673e-01 7.819e-01 9.981e-01 1.000e+00         NA
## s(agechild).edf             3.443e+00 1.745e+00 3.259e+00 5.939e+00      2.693
## s(district,id='mrf1').tau21 1.581e-03 5.461e-04 1.462e-03 3.335e-03      0.001
## s(district,id='mrf1').alpha 7.567e-01 1.740e-01 8.424e-01 1.000e+00         NA
## s(district,id='mrf1').edf   2.599e+01 1.646e+01 2.623e+01 3.480e+01     26.427
## s(district,id='re2').tau21  3.932e-03 9.136e-05 3.013e-03 1.412e-02      0.013
## s(district,id='re2').alpha  8.523e-01 2.838e-01 9.713e-01 1.000e+00         NA
## s(district,id='re2').edf    1.656e+01 8.596e-01 1.671e+01 3.480e+01     34.209
## ---
## Sampler summary:
## -
## DIC = 12616.56 logLik = -6262.296 pd = 91.9672
## runtime = 111.894
## ---
## Optimizer summary:
## -
## AICc = 12682.49 edf = 130.603 logLik = -6206.995
## logPost = -5766.632 nobs = 4847 runtime = 10.888
## Now, to predict the spatial effects we set up new data.
nd <- data.frame("district" = unique(ZambiaNutrition$district))

## Predict for the structured spatial effects.
p_str <- predict(b, newdata = nd, term = "s(district,id='mrf1')", intercept = FALSE)

## And the unstructured spatial effect.
p_unstr <- predict(b, newdata = nd, term = "s(district,id='re2')", intercept = FALSE)

Now, to visualize the effects we plot all maps using the same range for the color legends

r_mu <- range(c(p_str$mu, p_unstr$mu))
r_mu <- c(-1 * max(abs(r_mu)), max(abs(r_mu)))

r_sigma <- range(c(p_str$sigma, p_unstr$sigma))
r_sigma <- c(-1 * max(abs(r_sigma)), max(abs(r_sigma)))

and plot the effects using a diverging color legend.

## MRF smooth effect.
plotmap(ZambiaBnd, x = p_str$mu, id = nd$district, color = diverge_hcl, range = r_mu,
  main = expression(mu), shift = 0.1, title = "MRF", mdensity = 20)
plotmap(ZambiaBnd, x = p_str$sigma, id = nd$district, color = diverge_hcl, range = r_sigma,
  main = expression(sigma), shift = 0.1, title = "MRF", mdensity = 20)

## Random effects.
plotmap(ZambiaBnd, x = p_unstr$mu, id = nd$district, color = diverge_hcl, range = r_mu,
  shift = 0.1, title = "Random effect", mdensity = 20)
plotmap(ZambiaBnd, x = p_unstr$sigma, id = nd$district, color = diverge_hcl, range = r_sigma,
  shift = 0.1, title = "Random effect", mdensity = 20)

The maps clearly show that the unstructured spatial effect seems to very small, if existent at all when looking the 95% credible intervals:

## Again predict, but now additionally compute 95% credible intervals
## using function c95().
p_unstr <- predict(b, newdata = nd, term = "s(district,id='re2')", intercept = FALSE, FUN = c95)

## Test if all effects contain zero, i.e., are not significant
## according the 95% credible intervals.
all(p_unstr$mu[["2.5%"]] < 0 & p_unstr$mu[["97.5%"]] > 0)
## [1] TRUE
all(p_unstr$sigma[["2.5%"]] < 0 & p_unstr$sigma[["97.5%"]] > 0)
## [1] TRUE

References

Umlauf, Nikolaus, Daniel Adler, Thomas Kneib, Stefan Lang, and Achim Zeileis. 2015. โ€œStructured Additive Regression Models: An R Interface to BayesX.โ€ Journal of Statistical Software 63 (1): 1โ€“46. https://doi.org/10.18637/jss.v063.i21.
Umlauf, Nikolaus, Nadja Klein, Achim Zeileis, and Thorsten Simon. 2024. bamlss: Bayesian Additive Models for Location Scale and Shape (and Beyond). https://doi.org/10.32614/CRAN.package.bamlss.