# Coarse-to-fine spatial GLMM: Application examples

This vignette demonstrates how to estimate spatial generalized linear mixed models (GLMMs) using the spCF package for smoothing/denoising, prediction, and multiscale analysis. The spatial process in the GLMMs is trained via a coarse-to-fine approach that sequentially learns spatial patterns from coarser to finer scales. This spatial GLMM framework is particularly suitable for moderate-to-large samples. See Murakami et al. (2026) for further details.

# Example 1: Count data modeling for disease mapping

## Data and setup

Let us load the required packages

    library(spCF)
    library(sf)
    library(CARBayesdata)

The pollutionhealthdata dataset included in the CARBayesdata package is used here. This dataset comprises panel data on respiratory hospitalisations across 271 zones in Greater Glasgow. For simplicity, the center coordinates of each zone are used for spatial modeling.

In this example, the observed number of hospitalisations in 2011 (observed) is modeled for smoothing and predicting the latent risk of respiratory disease (i.e., disease mapping).

    data(pollutionhealthdata)
    dat      <- pollutionhealthdata[pollutionhealthdata$year==2011,]

The response variable (observed), covariates (pm10, jsa, price), and an offset variable (expected) are specified as follows:

    y        <- dat[,"observed"]             # count data
    x        <- dat[,c("pm10","jsa","price")]# covariates
    offset   <- log(dat[,"expected"])        # offset variable

The center coordinates are extracted as:

    data(GGHB.IZ)                            # polygons of the 271 zones
    coords   <- st_coordinates(st_centroid(GGHB.IZ))# coordinates
    #> Warning: st_centroid assumes attributes are constant over geometries

observed is spatially plotted as follows:

    GGHB.IZ$y <- y
    plot(GGHB.IZ[,"y"],lwd=0.01,axes=TRUE, key.pos=4,nbreaks=50)

## Coarse-to-fine spatial GLMM (CF-GLM)

In CF-GLM, the spatial process is defined as a sum of scalewise processes, where the number of spatial scales, R, is optimized via holdout validation. A smaller R, corresponding to early stopping, allows the spatial process to capture only coarse-scale patterns, whereas a larger R enables the spatial process to represent finer-scale patterns. The `cf_glm_hv` function performs holdout validation as follows:

    mod_hv   <- cf_glm_hv(y = y, x = x, offset=offset,
                          coords = coords, family=poisson())
    #> --- Deviance: Basic GLM ---
    #> 203.031
    #> --- Deviance: Learning multi-scale spatial process ---
    #> 202.2523 (Scale  1)
    #> 201.9962 (Scale  2)
    #> 201.7486 (Scale  3)
    #> 201.4805 (Scale  4)
    #> 200.9291 (Scale  5)
    #> 200.1901 (Scale  6)
    #> 199.3576 (Scale  7)
    #> 198.5565 (Scale  8)
    #> 197.4712 (Scale  9)
    #> 196.2794 (Scale 10)
    #> 194.2914 (Scale 11)
    #> 192.3719 (Scale 12)
    #> 190.5494 (Scale 13)
    #> 189.2792 (Scale 14)
    #> 188.3475 (Scale 15)
    #> 188.0789 (Scale 16)
    #> 188.0789 (Scale 17) no improvement
    #> 188.0789 (Scale 18) no improvement
    #> 188.0789 (Scale 19) no improvement
    #> 188.0789 (Scale 20) no improvement
    #> 187.6913 (Scale 21)
    #> 187.6913 (Scale 22) no improvement
    #> 187.6513 (Scale 23)
    #> 187.0113 (Scale 24)
    #> 187.0113 (Scale 25) no improvement
    #> 186.5719 (Scale 26)
    #> 186.5719 (Scale 27) no improvement
    #> 186.5719 (Scale 28) no improvement
    #> 186.5719 (Scale 29) no improvement
    #> 186.5719 (Scale 30) no improvement
    #>  184.422 (Scale 31)
    #>  184.422 (Scale 32) no improvement
    #> 181.8319 (Scale 33)
    #> 181.8319 (Scale 34) no improvement
    #> 181.8319 (Scale 35) no improvement
    #> 181.8319 (Scale 36) no improvement
    #>
    #> -> Selected finest scale: 33 (bandwidth:  628.402)
    #>

As shown in the output, the deviance loss for the validation samples gradually decreases as learning proceeds from the coarsest scale (Scale 1) to finer scales.

After holdout validation, the full model is trained using the `cf_glm` function:

    mod      <- cf_glm(y = y, x = x, offset=offset,
                       coords = coords, mod_hv = mod_hv)
    #> --- Learning multi-scale spatial process ---
    #>  Scale  1 (bandwidth:18301.1)
    #>  Scale  2 (bandwidth:16470.99)
    #>  Scale  3 (bandwidth:14823.89)
    #>  Scale  4 (bandwidth:13341.5)
    #>  Scale  5 (bandwidth:12007.35)
    #>  Scale  6 (bandwidth:10806.62)
    #>  Scale  7 (bandwidth:9725.956)
    #>  Scale  8 (bandwidth:8753.36)
    #>  Scale  9 (bandwidth:7878.024)
    #>  Scale 10 (bandwidth:7090.222)
    #>  Scale 11 (bandwidth:6381.2)
    #>  Scale 12 (bandwidth:5743.08)
    #>  Scale 13 (bandwidth:5168.772)
    #>  Scale 14 (bandwidth:4651.895)
    #>  Scale 15 (bandwidth:4186.705)
    #>  Scale 16 (bandwidth:3768.035)
    #>  Scale 17 (bandwidth:3391.231) no improvement (skipped)
    #>  Scale 18 (bandwidth:3052.108) no improvement (skipped)
    #>  Scale 19 (bandwidth:2746.897) no improvement (skipped)
    #>  Scale 20 (bandwidth:2472.208) no improvement (skipped)
    #>  Scale 21 (bandwidth:2224.987)
    #>  Scale 22 (bandwidth:2002.488) no improvement (skipped)
    #>  Scale 23 (bandwidth:1802.239)
    #>  Scale 24 (bandwidth:1622.015)
    #>  Scale 25 (bandwidth:1459.814) no improvement (skipped)
    #>  Scale 26 (bandwidth:1313.832)
    #>  Scale 27 (bandwidth:1182.449) no improvement (skipped)
    #>  Scale 28 (bandwidth:1064.204) no improvement (skipped)
    #>  Scale 29 (bandwidth:957.7839) no improvement (skipped)
    #>  Scale 30 (bandwidth:862.0055) no improvement (skipped)
    #>  Scale 31 (bandwidth:775.8049)
    #>  Scale 32 (bandwidth:698.2244) no improvement (skipped)
    #>  Scale 33 (bandwidth:628.402)

The estimated regression coefficients, standard deviations of the scalewise components, and error statistics are displayed as follows:

    mod
    #> Call:
    #> cf_glm(y = y, x = x, coords = coords, offset = offset, mod_hv = mod_hv)
    #>
    #> ---- Coefficients -------------------------------------
    #> c("-0.52944879", " 0.01228421", " 0.06969130", "-0.15399214")c("0.191501840", "0.012981721", "0.005711098", "0.034412197")c("-0.90479239", "-0.01315997", " 0.05849754", "-0.22144004")c("-0.15410518", " 0.03772838", " 0.08088505", "-0.08654423")
    #>
    #> ---- Deviance losses (influential elements only) ------
    #> c("xb", "spatial_scale1", "spatial_scale2", "spatial_scale3", "spatial_scale4", "spatial_scale5", "spatial_scale6", "spatial_scale7", "spatial_scale8", "spatial_scale9", "spatial_scale10", "spatial_scale11", "spatial_scale12", "spatial_scale13", "spatial_scale14", "spatial_scale15", "spatial_scale16", "spatial_scale21", "spatial_scale23", "spatial_scale24", "spatial_scale26", "spatial_scale31", "spatial_scale33")c("0.271116285", "0.004461572", "0.001366108", "0.001402668", "0.001492172", "0.001975312", "0.002852430", "0.003068554", "0.003418404", "0.004055008", "0.004486946", "0.005784973", "0.006560437", "0.006829363", "0.006909698", "0.007577075", "0.008556750", "0.029136342", "0.027819594", "0.026608055", "0.026104704", "0.055181303", "0.028855846")
    #>
    #> ---- Error statistics ---------------------------------
    #> c("validation_Pseudo-R2", "validation_RMSE", "validation_MAE")c(" 0.7733305", "15.1119402", "11.4634187")

## Disease mapping

### Predictive values

The predictive means, representing the denoised risk of respiratory disease, and their standard deviations (SDs) are mapped as follows:

    # Predictive mean
    GGHB.IZ$pred   <- mod$pred$pred
    plot(GGHB.IZ[,c("pred")],lwd=0.01,axes=TRUE, key.pos=4,nbreaks=50)

    # Predictive SD
    GGHB.IZ$pred_sd<- mod$pred$pred_sd
    plot(GGHB.IZ[,c("pred_sd")],pal = function(n) hcl.colors(n, "Viridis"), lwd=0.01,
         axes=TRUE, key.pos=4)

The result suggests that both disease risk and their uncertainty increase in the central area.

### Multiscale spatial pattern/feature extraction

The `sp_scalewise` function extracts scalewise spatial processes with bandwidth values falling within a pre-specified range. For example, the following commands extract the large- and small-scale processes, corresponding to bandwidth ranges of 4000+ and 0–4000, respectively.

    mod_s1      <- sp_scalewise(mod,bw_range=c(4000,Inf)) # Large scale (4000 <= bandwidth)
    mod_s2      <- sp_scalewise(mod,bw_range=c(0,4000))   # Small scale (bandwidth <= 4000)

The extracted scalewise processes are mapped as follows:

    GGHB.IZ$z1  <- mod_s1$pred$pred
    GGHB.IZ$z2  <- mod_s2$pred$pred
    plot(GGHB.IZ[,c("z1","z2")],lwd=0.01,axes=TRUE,key.pos=4, nbreaks=50)

The `sp_scalewise` function is useful for multiscale spatial pattern analysis (or feature extraction), which is commonly conducted in ecological, epidemiological, and environmental studies.

# Example 2: Binary data modeling and prediction

## Data and setup

Let us load the required packages

    library(spCF)
    library(sp)
    library(sf)

The meuse dataset from the sp package consists of observations at 155 sample sites in a floodplain along the River Meuse. In this example, we consider a binary response variable (`flood`), which takes the value 1 if the flooding frequency class is once every two years (i.e., flood-prone area) and 0 otherwise. The binary response is predicted over 3,103 regularly spaced grid cells (meuse.grid). The covariate considered is the distance to the river (`dist`).

    ### Data at samples sites
    data(meuse)
    flood    <- ifelse(meuse$ffreq==1, 1, 0 )# Binary response variable
    coords   <- meuse[,c("x","y")]           # Coordinates
    x        <- meuse[,"dist"]               # Covariate

    ### Data at prediction sites
    data(meuse.grid)
    coords0  <- meuse.grid[,c("x","y")]      # Coordinates
    x0       <- meuse.grid[,"dist"]          # Covariate

`flood` is spatially plotted as follows:

    obs_s    <- st_as_sf( data.frame(coords, flood), coords= c("x","y"), crs=28992)
    plot(obs_s[,"flood"], pch = 20, key.pos=4, axes=TRUE)

## Coarse-to-fine spatial GLMM (CF-GLM)

The code implementing the CF-GLM is essentially the same as in the previous example, except that `family` is set to `binomial()` for modeling binary data:

    set.seed(1234) # For this vignette, training samples are fixed
    mod_hv   <- cf_glm_hv(y = flood, x = x, coords = coords, family=binomial())
    #> --- Deviance: Basic GLM ---
    #> 49.90513
    #> --- Deviance: Learning multi-scale spatial process ---
    #> 37.20079 (Scale  1)
    #> 33.21432 (Scale  2)
    #> 31.46662 (Scale  3)
    #> 30.63932 (Scale  4)
    #> 30.20188 (Scale  5)
    #> 29.98584 (Scale  6)
    #>  29.7708 (Scale  7)
    #> 29.53233 (Scale  8)
    #> 29.36235 (Scale  9)
    #> 29.21385 (Scale 10)
    #> 29.21385 (Scale 11) no improvement
    #> 29.21385 (Scale 12) no improvement
    #> 29.21385 (Scale 13) no improvement
    #> 29.21385 (Scale 14) no improvement
    #>
    #> -> Selected finest scale: 10 (bandwidth: 556.7079)
    #>

In the subsequent full model training using the `cf_glm` function, the covariates (`x0`) and coordinates (`coords0`) at the prediction sites are also specified to enable spatial prediction:

    mod      <- cf_glm(y = flood, x=x, coords = coords,
                       x0=x0, coords0 = coords0, mod_hv = mod_hv)
    #> --- Learning multi-scale spatial process ---
    #>  Scale  1 (bandwidth:1436.96)
    #>  Scale  2 (bandwidth:1293.264)
    #>  Scale  3 (bandwidth:1163.938)
    #>  Scale  4 (bandwidth:1047.544)
    #>  Scale  5 (bandwidth:942.7897)
    #>  Scale  6 (bandwidth:848.5107)
    #>  Scale  7 (bandwidth:763.6596)
    #>  Scale  8 (bandwidth:687.2937)
    #>  Scale  9 (bandwidth:618.5643)
    #>  Scale 10 (bandwidth:556.7079)

The estimated regression coefficients, standard deviations of the scalewise components, and error statistics are displayed as follows:

    mod
    #> Call:
    #> cf_glm(y = flood, x = x, coords = coords, x0 = x0, coords0 = coords0,     mod_hv = mod_hv)
    #>
    #> ---- Coefficients -------------------------------------
    #> c(" 1.157697", "-3.694865")c("0.4328405", "1.4469782")c(" 0.3093296", "-6.5309421")c(" 2.0060643", "-0.8587874")
    #>
    #> ---- Deviance losses (influential elements only) ------
    #> c("xb", "spatial_scale1", "spatial_scale2", "spatial_scale3", "spatial_scale4", "spatial_scale5", "spatial_scale6", "spatial_scale7", "spatial_scale8", "spatial_scale9", "spatial_scale10")c("0.73048268", "0.71870473", "0.35770913", "0.19613685", "0.10685822", "0.07766296", "0.07091737", "0.07353630", "0.09175729", "0.10432759", "0.12669346")
    #>
    #> ---- Error statistics ---------------------------------
    #> c("validation_Pseudo-R2", "validation_RMSE", "validation_MAE")c("0.4531565", "0.3395546", "0.2730460")

## Mapping

### Predictive values

The predictive values and their standard deviations at the grid cells are mapped as follows:

    ### Convert gridded points to gridded polygons (for clear visualization)
    meuse.grid_sp             <- meuse.grid
    coordinates(meuse.grid_sp)<- c("x", "y")
    gridded(meuse.grid_sp)    <- TRUE
    meuse.grid_sf             <- st_as_sf(as(meuse.grid_sp, "SpatialPolygons"))
    st_crs(meuse.grid_sf)     <- 28992

    ### Mapping predictive mean and standard deviations
    meuse.grid_sf$pred        <- mod$pred0$pred   # Predictive mean
    meuse.grid_sf$pred_sd     <- mod$pred0$pred_sd# Predictive standard deviations
    plot(meuse.grid_sf[,"pred"], border = NA, nbreaks = 20, key.pos=4,axes=TRUE)

    plot(meuse.grid_sf[,"pred_sd"], pal = function(n) hcl.colors(n, "Viridis"),
         border = NA,key.pos=4,axes=TRUE)

### Multiscale spatial pattern/feature extraction

Let us extract the large- and small-scale processes, corresponding to bandwidth ranges of 1000+ and 0–1000, respectively.

    mod_s1<- sp_scalewise(mod,bw_range=c(1000,Inf)) # Large scale (1000 <= bandwidth)
    mod_s2<- sp_scalewise(mod,bw_range=c(0,1000))   # Small scale (0 <= bandwidth <= 1000)

The extracted scalewise processes are mapped as follows:

    meuse.grid_sf$z1    <- mod_s1$pred0$pred
    meuse.grid_sf$z2    <- mod_s2$pred0$pred
    plot(meuse.grid_sf[,c("z1","z2")], border=NA, nbreaks=20, key.pos=4, axes=TRUE)

## Interactive mapping

The maps above are static. `spCFmap()` opens a Shiny application that draws the same results over a basemap. Without arguments it launches the full application, whose bundled demo data include meuse and the Greater Glasgow health data used in the two examples above:

    spCFmap()

Passing a fitted model maps that object directly, with `crs` naming the coordinate reference system of the coordinates given to the model. For the binary example above the coordinates are in the Dutch RD grid (EPSG:28992), and for the count example in the British National Grid (EPSG:27700):

    spCFmap(mod, crs = 28992)   # the flood-probability model of Example 2

A model fitted without prediction sites – as in Example 1, where `cf_glm` was called without `x0`/`coords0` – is mapped at its sample sites instead, so both examples can be explored this way.

### Reference

Murakami, Daisuke, Alexis Comber, Takahiro Yoshida, Narumasa Tsutsumida, Chris Brunsdon, and Tomoki Nakaya. 2026. Coarse-to-Fine Spatial GLMM for Scalable Prediction and Multiscale Analysis. ArXiv.