Package {DAST}


Type: Package
Title: Spatio-Temporal Disaggregation for Maps with Changing Areal Boundaries
Version: 0.1.0
Description: Tools for spatio-temporal disaggregation of areal data across multiple time points, including support for changing polygon boundaries. Implements methods for spatially aggregated log-Gaussian Cox process models with changing areal boundaries as described in Ripstein, Brown and Stafford (2026) "Spatio-Temporal Disaggregation with Changing Areal Boundaries" <doi:10.48550/arXiv.2606.25074>. Combines polygon-level observations, population rasters and optional covariate rasters to infer fine-scale spatial fields over time. Models can be efficiently fit using 'TMB' (Template Model Builder) and adaptive Gauss-Hermite quadrature for fast approximate inference or via 'tmbstan' for MCMC.
License: MIT + file LICENSE
Encoding: UTF-8
URL: https://github.com/nripstein/DAST
LinkingTo: TMB, RcppEigen
Imports: aghq, cowplot, disaggregation, fmesher, ggplot2, Matrix, numDeriv, rSPDE, sf, sparseMVN, terra, TMB
Suggests: knitr, rmarkdown, rstan, testthat (≥ 3.0.0), tmbstan
RoxygenNote: 7.3.3
VignetteBuilder: knitr
Config/testthat/edition: 3
NeedsCompilation: yes
Packaged: 2026-06-25 02:47:03 UTC; NoahRipstein
Author: Noah Ripstein [aut, cre]
Maintainer: Noah Ripstein <noah.ripstein@mail.utoronto.ca>
Repository: CRAN
Date/Publication: 2026-06-30 19:40:02 UTC

Extract AGHQ draw blocks for theta and random effects

Description

Extract AGHQ draw blocks for theta and random effects

Usage

aghq_extract_draw_blocks(samps, coef_meta, time_varying_betas)

(Internal) Build combined mesh from a list of sf polygons

Description

Just in case some maps have additional polygons outside the first extent

Usage

build_combined_mesh(polygon_list, mesh_args, make_mesh)

Arguments

polygon_list

List of 'sf' objects (same CRS).

mesh_args

Passed to 'build_mesh()'.

Value

An 'inla.mesh' object or 'NULL' if 'make_mesh = FALSE'.


Canonicalize posterior draw parameter names

Description

Canonicalize parameter draw names for consistency across shared vs. time-varying intercepts/slopes while leaving random effects and hyperparameters unchanged.

Usage

canonicalize_draw_names(old_names, coef_meta, time_varying_betas)

Arguments

old_names

Character vector of draw names.

coef_meta

List with coefficient metadata from 'compute_coef_meta()'.

time_varying_betas

Logical indicating whether time-varying betas are used.

Value

Character vector of canonicalized draw names.


Compute coefficient metadata (number of timepoints, covariates, and their names)

Description

Compute coefficient metadata (number of timepoints, covariates, and their names)

Usage

compute_coef_meta(data)

Arguments

data

A disag_data_mmap object.


Fit a multi-map disaggregation model (via AGHQ, TMB, or MCMC)

Description

Top-level fitting wrapper with engine dispatch and engine-specific argument handling. Engine-specific controls should be supplied via engine.args.

Usage

disag_model_mmap(
  data,
  priors = NULL,
  family = "poisson",
  link = "log",
  engine = c("AGHQ", "TMB", "MCMC"),
  time_varying_betas = FALSE,
  fixed_effect_betas = TRUE,
  engine.args = NULL,
  aghq_k = 2,
  field = TRUE,
  iid = TRUE,
  silent = TRUE,
  starting_values = NULL,
  optimizer = NULL,
  verbose = FALSE,
  ...
)

Arguments

data

A disag_data_mmap object.

priors

Optional named list of prior overrides.

family

One of "gaussian", "binomial", "poisson", or "negbinomial".

link

One of "identity", "logit", or "log".

engine

Character; one of "AGHQ", "TMB", or "MCMC". The MCMC engine uses tmbstan.

time_varying_betas

Logical; if TRUE, each time point has its own fixed-effect.

fixed_effect_betas

Logical; if TRUE (default), beta coefficients are treated as fixed effects in the AGHQ outer parameter block (current behavior). If FALSE and engine = "AGHQ", beta coefficients are moved to TMB random effects so they are integrated in the inner Laplace step.

engine.args

Optional named list of engine-specific options. Supported AGHQ keys are aghq_k, optimizer, and outer_derivative_method. Supported TMB keys are iterations, hess_control_parscale, hess_control_ndeps, and outer_derivative_method. outer_derivative_method may be "tmb" (default) or "finite_difference". The finite-difference option affects only the outer fixed/hyperparameter optimization and Hessian; TMB still handles the inner Laplace approximation. Supported MCMC keys are chains, iter, warmup, thin, cores, seed, refresh, laplace, lower, upper, and control. Additional named MCMC keys are passed through to tmbstan::tmbstan() and rstan::sampling(). iter is the total number of Stan iterations, including warmup.

aghq_k

Deprecated at wrapper level; use engine.args = list(aghq_k = ...). Retained for backward compatibility.

field

Logical; include spatial field?

iid

Logical; include IID polygon effects?

silent

Logical; pass through to engine fit function.

starting_values

Optional named list of starting values.

optimizer

Deprecated at wrapper level; use engine.args = list(optimizer = ...). Retained for backward compatibility.

verbose

Logical; print runtime diagnostics.

...

Additional arguments. Engine-specific arguments passed via ... are deprecated in this wrapper and should be moved to engine.args.

Value

A fitted model object of class disag_model_mmap_tmb, disag_model_mmap_aghq, or disag_model_mmap_mcmc (all also inherit disag_model_mmap).


Fit a multi-map disaggregation model via TMB + AGHQ

Description

Builds the TMB ADFun object for a multi-map disaggregation model, then fits the model via AGHQ with desired number of quadrature points.

Usage

disag_model_mmap_aghq(
  data,
  priors = NULL,
  family = "poisson",
  link = "log",
  time_varying_betas = FALSE,
  fixed_effect_betas = TRUE,
  aghq_k = 1,
  field = TRUE,
  iid = TRUE,
  silent = TRUE,
  starting_values = NULL,
  optimizer = NULL,
  outer_derivative_method = "tmb",
  verbose = FALSE
)

Arguments

data

A 'disag_data_mmap' object (from 'prepare_data_mmap()').

priors

Optional named list of prior specifications (see internal helper).

family

One of "gaussian", "binomial", "poisson", or "negbinomial".

link

One of "identity", "logit", or "log".

time_varying_betas

Logical; if TRUE, each time point has its own fixed-effect

fixed_effect_betas

Logical; if TRUE (default), beta coefficients are in AGHQ outer parameters. If FALSE, active betas are treated as TMB random effects.

aghq_k

Integer >= 1: number of quadrature nodes for AGHQ ('1' = Laplace).

field

Logical: include the spatial random field?

iid

Logical: include polygon-specific IID effects?

silent

Logical: if TRUE, suppress TMB's console output.

starting_values

Optional named list of starting parameter values.

optimizer

Optional optimizer name passed to AGHQ control.

outer_derivative_method

Character; "tmb" (default) uses TMB's analytic outer gradient and "finite_difference" uses finite differences of obj$fn for outer optimization and Hessian construction. The finite-difference option is only supported with optimizer = "nlminb". The inner Laplace approximation is still handled by TMB.

verbose

Logical: if TRUE, print total runtime.

Value

An object of class 'disag_model_mmap_aghq' (a list with '$aghq_model', '$data', and '$model_setup').


Fit a multi-map disaggregation model via tmbstan MCMC

Description

Builds the shared TMB ADFun object for a multi-map disaggregation model, then samples from it with tmbstan::tmbstan(). This engine supports parameter estimation only; prediction is not implemented for MCMC fits.

Usage

disag_model_mmap_mcmc(
  data,
  priors = NULL,
  family = "poisson",
  link = "log",
  time_varying_betas = FALSE,
  fixed_effect_betas = TRUE,
  chains = 4L,
  iter = 2000L,
  warmup = NULL,
  thin = 1L,
  cores = NULL,
  seed = NULL,
  refresh = NULL,
  laplace = FALSE,
  lower = numeric(0),
  upper = numeric(0),
  control = NULL,
  field = TRUE,
  iid = TRUE,
  silent = TRUE,
  starting_values = NULL,
  verbose = FALSE,
  ...
)

Arguments

data

A 'disag_data_mmap' object (from 'prepare_data_mmap()').

priors

Optional named list of prior specifications.

family

One of 'gaussian', 'binomial', 'poisson', or 'negbinomial'.

link

One of 'identity', 'logit', or 'log'.

time_varying_betas

Logical; if TRUE, each time point has its own fixed-effect.

fixed_effect_betas

Logical; if TRUE (default), active beta coefficients are sampled as fixed effects. If FALSE, active beta coefficients are included in the TMB random-effect block.

chains

Integer >= 1; number of MCMC chains.

iter

Integer >= 1; total Stan iterations per chain, including warmup.

warmup

Integer >= 0 and less than iter; warmup iterations per chain. Defaults to floor(iter / 2).

thin

Integer >= 1; thinning interval.

cores

Integer >= 1; number of cores passed to Stan. Defaults to getOption("mc.cores", chains).

seed

Optional positive integer seed.

refresh

Optional integer >= 0; Stan progress refresh interval.

laplace

Logical; passed to tmbstan::tmbstan(). Defaults to FALSE for full posterior sampling.

lower

Numeric lower bounds passed to tmbstan::tmbstan().

upper

Numeric upper bounds passed to tmbstan::tmbstan().

control

Optional list passed to rstan::sampling().

field

Logical: include the spatial random field?

iid

Logical: include polygon-specific IID effects?

silent

Logical: if TRUE, suppress TMB/tmbstan console output.

starting_values

Optional named list of starting parameter values.

verbose

Logical: if TRUE, print total runtime.

...

Additional arguments passed through to tmbstan::tmbstan() and rstan::sampling().

Value

An object of class 'disag_model_mmap_mcmc' with components stanfit, obj, data, and model_setup.


Fit a multi-map disaggregation model via TMB

Description

Builds the TMB ADFun object for a multi-map disaggregation model, then fits the model by maximizing the TMB objective and approximates uncertainty via the optimized Hessian.

Usage

disag_model_mmap_tmb(
  data,
  priors = NULL,
  family = "poisson",
  link = "log",
  time_varying_betas = FALSE,
  fixed_effect_betas = TRUE,
  iterations = 1000,
  field = TRUE,
  iid = TRUE,
  hess_control_parscale = NULL,
  hess_control_ndeps = 1e-04,
  outer_derivative_method = "tmb",
  silent = TRUE,
  starting_values = NULL,
  verbose = FALSE
)

Arguments

data

A 'disag_data_mmap' object (from 'prepare_data_mmap()').

priors

Optional named list of prior specifications (see internal helper).

family

One of 'gaussian', 'binomial', 'poisson', or 'negbinomial'.

link

One of 'identity', 'logit', or 'log'.

time_varying_betas

Logical; if TRUE, each time point has its own fixed-effect

fixed_effect_betas

Logical; if TRUE (default), active beta coefficients are treated as fixed effects. If FALSE, active beta coefficients are treated as random effects in the inner Laplace step.

iterations

Integer >= 1: maximum number of optimizer iterations.

field

Logical: include the spatial random field?

iid

Logical: include polygon-specific IID effects?

hess_control_parscale

Optional numeric vector for scaling the Hessian steps.

hess_control_ndeps

Numeric; relative step size for Hessian finite-difference (default 1e-4).

outer_derivative_method

Character; "tmb" (default) uses TMB's analytic outer gradient and "finite_difference" uses finite differences of obj$fn for outer optimization and Hessian construction. The inner Laplace approximation is still handled by TMB.

silent

Logical: if TRUE, suppress TMB's console output.

starting_values

Optional named list of starting parameter values.

verbose

Logical: if TRUE, print total runtime.

Value

An object of class 'disag_model_mmap_tmb' (a list with '$obj', '$opt', '$sd_out', '$data', and '$model_setup').


Get categorical levels from a raster layer

Description

Extracts categorical levels from a raster layer, either from defined levels or from unique values in the raster.

Usage

get_categorical_levels(raster_layer)

Arguments

raster_layer

A SpatRaster layer

Value

Character vector of level names


Construct design and projection matrices for prediction

Description

Internal helper for 'predict.disag_model_mmap_aghq()'. Builds per-time design matrices (with intercept), the SPDE projection matrix, and the coordinate table for raster reconstruction.

Usage

get_predict_matrices(
  data,
  new_data = NULL,
  expected_cov_names = NULL,
  time_varying_betas = FALSE
)

Arguments

data

A 'disag_data_mmap' object (from 'prepare_data_mmap()').

new_data

Optional new covariate data: - a single 'SpatRaster' (recycled across all times), or - a list of length 'length(data$time_points)' of 'SpatRaster' objects.

expected_cov_names

Character vector of training covariate names (order matters). If length 0, predictions are intercept-only regardless of provided rasters.

time_varying_betas

Logical; used for clearer error messages when aligning layers.

Value

A list with elements: - 'X_list': list of design matrices (each n_cells x p, with "Intercept"). - 'A': SPDE projection matrix (n_cells x n_knots). - 'coords': data.frame of x/y coordinates for each cell.


Get Default Prior Values for Disaggregation Model

Description

Calculates the default Penalized Complexity (PC) prior parameters and Gaussian priors that will be used by disag_model_mmap() if the user does not provide overrides.

Usage

get_priors(data)

Arguments

data

A disag_data_mmap object (output from prepare_data_mmap).

Details

The default priors are dynamic and depend on the input data:

Value

A named list of prior specifications.

Examples

# Create minimal polygon and covariate inputs for one time point.
polygons <- sf::st_sf(
  area_id = 1:2,
  response = c(10, 12),
  geometry = sf::st_sfc(
    sf::st_polygon(list(rbind(c(0, 0), c(1, 0), c(1, 2), c(0, 2), c(0, 0)))),
    sf::st_polygon(list(rbind(c(1, 0), c(2, 0), c(2, 2), c(1, 2), c(1, 0)))),
    crs = 3857
  )
)
covariate <- terra::rast(
  ncols = 2, nrows = 2, xmin = 0, xmax = 2, ymin = 0, ymax = 2,
  crs = "EPSG:3857"
)
terra::values(covariate) <- c(1, 2, 3, 4)

data <- suppressMessages(prepare_data_mmap(
  polygon_shapefile_list = list(polygons),
  covariate_rasters_list = list(covariate),
  make_mesh = FALSE
))

# Inspect defaults and modify a prior for a later model fit.
defaults <- get_priors(data)
defaults[c("prior_rho_min", "prior_sigma_max")]
my_priors <- defaults
my_priors$prior_rho_prob <- 0.05

Detect if a raster layer is categorical

Description

Determines if a raster layer should be treated as categorical based on multiple criteria: 1. Explicit definition in categorical_covariate_baselines 2. Presence of defined levels in the raster 3. Small number of unique values

Usage

is_categorical_layer(
  raster_layer,
  layer_name,
  categorical_baselines = NULL,
  max_categories = 10
)

Arguments

raster_layer

A SpatRaster layer to check

layer_name

The name of the layer

categorical_baselines

Named list of categorical baselines from disag_data_mmap

max_categories

Maximum number of unique values to consider categorical (default = 10)

Value

Logical indicating if the layer should be treated as categorical


Build the TMB ADFun object for multi-map disaggregation

Description

Internal helper. Converts data, priors, and model settings into the list of inputs required by 'TMB::MakeADFun()'.

Usage

make_model_object_mmap(
  data,
  priors = NULL,
  family = "gaussian",
  link = "identity",
  time_varying_betas = FALSE,
  fixed_effect_betas = TRUE,
  field = TRUE,
  iid = TRUE,
  silent = TRUE,
  starting_values = NULL,
  optimizer = NULL,
  verbose = FALSE
)

Arguments

data

A 'disag_data_mmap' object.

priors

NULL or named list overriding default hyperpriors.

family

One of "gaussian", "binomial", "poisson", "negbinomial".

link

One of "identity", "logit", "log".

time_varying_betas

Logical; if TRUE, each time point has its own fixed-effect

fixed_effect_betas

Logical; if FALSE, active beta coefficients are included in TMB random effects (for AGHQ inner-Laplace treatment).

field

Logical: include spatial field?

iid

Logical: include IID polygon effects?

silent

Logical: pass to 'MakeADFun()' to suppress output.

starting_values

NULL or named list of starting values.

optimizer

Optional; For changing the arguments used in AGHQ.

verbose

Logical: if TRUE, print details throughout including runtime.

Value

A 'TMB::ADFun' object ready for 'marginal_laplace_tmb()'.


Normalize fixed-effect parameter names

Description

Normalize fixed-effect parameter names to consistent labels.

Usage

normalize_fixed_names(nm, coef_meta, time_varying_betas)

Arguments

nm

Character vector of parameter names.

coef_meta

List with coefficient metadata from 'compute_coef_meta()'.

time_varying_betas

Logical indicating whether time-varying betas are used.

Value

Character vector of normalized parameter names.


Convert a TMB parameter vector to a named parameter list using TMB's parList Falls back to slice_params_tmb if parList fails (e.g., shape mismatch)

Description

Convert a TMB parameter vector to a named parameter list using TMB's parList Falls back to slice_params_tmb if parList fails (e.g., shape mismatch)

Usage

parvec_to_param_list(model_output, par_vec)

Visual summary plot of prepared data

Description

Combines polygons, aggregation raster, mesh, and (if present) a covariate into a 2x2 grid.

Usage

## S3 method for class 'disag_data_mmap'
plot(x, y = NULL, ..., covariate = 1, time = 1, max_categories = 10)

Arguments

x

A 'disag_data_mmap' object.

y

Not used (required for S3 method compatibility).

...

Additional arguments passed to plot_prepare_summary.

covariate

Integer or name of the covariate to display (default = 1).

time

Integer time-slice (default = 1).

max_categories

Maximum number of unique values to consider categorical (default = 10).

Value

A ggdraw object (from cowplot) which can be printed.


Plot the offset raster

Description

Draws the aggregation pixel values used in the fit

Usage

plot_aggregation_raster(disag_data, time = 1)

Arguments

disag_data

A 'disag_data_mmap' object.

time

Integer time-slice (default = 1).

Value

A ggplot2 object.


Plot a single covariate raster

Description

Renders one layer of the covariate raster stack, preserving the raster's CRS, and coloring by value with a Viridis scale. Automatically detects and handles categorical covariates with appropriate discrete color scales.

Usage

plot_covariate_raster(disag_data, covariate = 1, time = 1, max_categories = 10)

Arguments

disag_data

A 'disag_data_mmap' object.

covariate

Integer index or name of the covariate layer.

time

Integer time-slice (default = 1).

max_categories

Maximum number of unique values to consider categorical (default = 10).

Value

A ggplot2 object.


Plot the SPDE mesh with custom outer/inner boundaries

Description

Plot the SPDE mesh with custom outer/inner boundaries

Usage

plot_mesh(
  disag_data,
  edge_col = "grey70",
  edge_size = 0.2,
  outer_col = "black",
  outer_size = 1,
  inner_col = "blue",
  inner_size = 1,
  node_col = "black",
  node_size = 0.5
)

Arguments

disag_data

A 'disag_data_mmap' object.

edge_col

Colour for internal mesh edges (default = "grey70").

edge_size

Line width for those edges (default = 0.2).

outer_col

Colour for the outer perimeter (default = "black").

outer_size

Line width for the outer perimeter (default = 1).

inner_col

Colour for any inner perimeter (default = "blue").

inner_size

Line width for inner perimeter (default = 1).

node_col

Colour for mesh nodes (default = "black").

node_size

Size for mesh nodes (default = 0.5).

Value

A ggplot2 object.


Plot polygon response data

Description

Draws the prepared polygons colored by the response variable, with an optional title.

Usage

plot_polygons(disag_data, time = 1, show_title = TRUE)

Arguments

disag_data

A 'disag_data_mmap' object.

time

Integer index of time-slice to plot (default = 1).

show_title

Logical; if TRUE (default), add a title "Response at time X".

Value

A ggplot2 object.


Visual summary plot of prepared data

Description

Combines polygons, aggregation raster, mesh, and (if present) a covariate into a 2x2 grid.

Usage

plot_prepare_summary(disag_data, covariate = 1, time = 1, max_categories = 10)

Arguments

disag_data

A 'disag_data_mmap' object.

covariate

Integer or name of the covariate to display (default = 1).

time

Integer time-slice (default = 1).

max_categories

Maximum number of unique values to consider categorical (default = 10).

Value

A ggdraw object (from cowplot) which can be printed.


Predict mean & credible intervals for AGHQ-fitted disaggregation model

Description

Given a 'disag_model_mmap_aghq' object, draws from the AGHQ marginal, builds per-cell posterior samples, and returns means and credible-interval rasters.

Usage

## S3 method for class 'disag_model_mmap_aghq'
predict(
  object,
  new_data = NULL,
  predict_iid = FALSE,
  N = 1000,
  CI = 0.95,
  verbose = FALSE,
  ...
)

Arguments

object

A 'disag_model_mmap_aghq' fit (from 'disag_model_mmap_aghq()').

new_data

Optional covariates for prediction (see helper).

predict_iid

Currently not implemented; must be FALSE.

N

Number of marginal draws to sample (default 1000).

CI

Credible-interval level in (0,1) (default 0.95).

verbose

If TRUE, prints runtime in minutes.

...

Unused.

Value

An object of class 'disag_prediction_mmap_aghq' containing: - 'mean_prediction': list of SpatRasters ('prediction', 'field', 'covariates'). - 'uncertainty_prediction': list with 'predictions_ci$lower' & 'upper'.


Prediction guard for MCMC-fitted multi-map disaggregation models

Description

Prediction is intentionally not implemented for MCMC fits. This method provides a clear error directing users to the parameter-estimation outputs.

Usage

## S3 method for class 'disag_model_mmap_mcmc'
predict(object, ...)

Arguments

object

A fitted 'disag_model_mmap_mcmc' object.

...

Unused.

Value

This function always errors.


Predict for Multi-Map Disaggregation Model fit with TMB

Description

Predict for Multi-Map Disaggregation Model fit with TMB

Usage

## S3 method for class 'disag_model_mmap_tmb'
predict(object, new_data = NULL, predict_iid = FALSE, N = 100, CI = 0.95, ...)

Arguments

object

A fitted disag_model_mmap_tmb object.

new_data

Optionally, a new SpatRaster (or list of them) for prediction.

predict_iid

Logical. If TRUE, include the polygon iid effect in predictions.

N

Number of Monte Carlo draws for uncertainty estimation.

CI

Credible interval level (default 0.95).

...

Further arguments.

Value

An object of class 'disag_prediction_mmap' (also a list) with: - 'mean_prediction': a list containing time-layered 'SpatRaster's named 'time_<time point>': 'prediction' (response-scale mean prediction), 'field' (spatial-field contribution, or 'NULL' when no field was fitted), 'iid' (polygon IID contribution when requested and supported, otherwise 'NULL'), and 'covariates' (covariate-only linear predictor). - 'uncertainty_prediction': a list containing 'realisations', a list of one 'SpatRaster' stack per time point with 'N' Monte Carlo draws, and 'predictions_ci', a list with time-layered 'SpatRaster's 'lower' and 'upper' containing cell-wise credible bounds at level 'CI'.


Predict mean for multi-map disaggregation (TMB)

Description

Given a fitted TMB model object and optional new covariate data, compute the mean-only prediction (no uncertainty) for one raster.

Usage

predict_model_mmap(
  model_output,
  new_data = NULL,
  predict_iid = FALSE,
  newdata = NULL
)

Arguments

model_output

A 'disag_model_mmap_tmb' model fit.

new_data

Optional SpatRaster (or list) of new covariates.

predict_iid

Logical; if TRUE, include the IID polygon effect.

Value

A list with components: - prediction: SpatRaster of the mean prediction on the response scale. - field: SpatRaster of the spatial field component (or NULL). - iid: SpatRaster of the IID effect (or NULL). - covariates: SpatRaster of the linear predictor from covariates only.


Predict on a single raster for multi-map disaggregation (TMB)

Description

Apply the linear predictor, add spatial field and IID components, then transform via the link to return a SpatRaster prediction.

Usage

predict_single_raster_mmap(model_parameters, objects, link_function)

Arguments

model_parameters

Named list of parameter vectors (split by name).

objects

List from 'setup_objects_mmap' containing data and projectors.

link_function

Character; one of 'identity', 'log', or 'logit'.

Value

A list with components: - prediction: SpatRaster on the response scale. - field: SpatRaster of field contribution (or NULL). - iid: SpatRaster of IID contribution (or NULL). - covariates: SpatRaster of the covariate linear predictor.


Estimate uncertainty via Monte Carlo for multi-map disaggregation (TMB)

Description

Draw Monte Carlo samples of model parameters, propagate through the prediction function, and compute credible intervals at each cell.

Usage

predict_uncertainty_mmap(
  model_output,
  new_data = NULL,
  predict_iid = FALSE,
  N = 100,
  CI = 0.95,
  newdata = NULL
)

Arguments

model_output

A 'disag_model_mmap_tmb' model fit.

new_data

Optional SpatRaster (or list) of new covariates.

predict_iid

Logical; if TRUE, include the IID polygon effect.

N

Integer; number of Monte Carlo draws (default 100).

CI

Numeric in (0,1); credible-interval level (default 0.95).

Value

A list with components: - realisations: list of SpatRasters of each draw. - predictions_ci: list with 'lower' and 'upper' SpatRaster stacks.


Prepare multi-map disaggregation data

Description

Given lists of polygon sf's, covariate rasters, and aggregation rasters, combines them into a single 'disag_data_mmap' object ready for model fitting.

Usage

prepare_data_mmap(
  polygon_shapefile_list,
  covariate_rasters_list = NULL,
  aggregation_rasters_list = NULL,
  id_var = "area_id",
  response_var = "response",
  categorical_covariate_baselines = NULL,
  sample_size_var = NULL,
  mesh_args = NULL,
  na_action = FALSE,
  make_mesh = TRUE,
  verbose = FALSE
)

Arguments

polygon_shapefile_list

List of 'sf' polygon objects, one per time point.

covariate_rasters_list

Optional list of 'SpatRaster' stacks; may be NULL.

aggregation_rasters_list

Optional list of 'SpatRaster'; if NULL, uses uniform counts.

id_var

Name of the polygon ID column in each 'sf'.

response_var

Name of the response column.

categorical_covariate_baselines

Named list; names are categorical raster layers and values are baseline levels to drop (either level labels or numeric codes).

sample_size_var

Name of the sample-size column (for binomial models); may be NULL.

mesh_args

Passed to 'build_mesh()'.

na_action

Logical; if TRUE, drop or impute NAs instead of stopping.

make_mesh

Logical; if TRUE, build the spatial mesh over all polygons.

verbose

Logical; if TRUE, print timing info.

Value

An object of class 'disag_data_mmap' with components including - 'polygon_data', 'covariate_data', 'aggregation_pixels', ... - 'categorical_covariate_baselines' (normalized baseline labels) - 'categorical_covariate_schema' (internal encoding schema used for fit/predict consistency)


Process a single time point for prepare_data_mmap()

Description

Internal helper called by 'prepare_data_mmap()'. For time index 't', it: 1. Validates the polygon sf and rasters. 2. Handles NAs in the response. 3. Builds or validates the aggregation raster. 4. Extracts and merges covariate + aggregation pixel data. 5. Computes coordinates for mesh fitting and for prediction. 6. Computes the start/end pixel indices per polygon.

Usage

prepare_time_point(
  t,
  poly_sf,
  cov_rasters,
  agg_raster,
  id_var,
  response_var,
  sample_size_var,
  na_action,
  categorical_schema
)

Arguments

t

Integer time-point index (used for messaging).

poly_sf

An 'sf' polygon object for time 't'.

cov_rasters

A 'SpatRaster' of covariates for time 't', or NULL.

agg_raster

A 'SpatRaster' of aggregation weights for time 't', or NULL.

id_var

Name of the polygon ID column.

response_var

Name of the response column.

sample_size_var

Name of the sample-size column, or NULL.

na_action

Logical; if TRUE, drop/impute NAs instead of erroring.

categorical_schema

Internal schema from 'build_categorical_schema()'.

Value

A list with elements: - 'poly_data': data.frame of polygon-level info (incl. 'poly_local_id' & 'time'). - 'cov_data': data.frame of pixel-level covariates + 'poly_local_id' + 'time' + 'cell'. - 'agg_pixels': numeric vector of aggregation weights per pixel. - 'coords_fit': coords for mesh-building (only used pixels). - 'coords_pred': coords for full-extent prediction. - 'start_end_index': integer matrix of 0-indexed start/end for each polygon.


Print method for 'disag_data_mmap' objects

Description

Displays a brief overview of a multi-map disaggregation dataset: number of time points, total polygons, and total pixels.

Usage

## S3 method for class 'disag_data_mmap'
print(x, ...)

Arguments

x

A 'disag_data_mmap' object.

...

Additional arguments (unused).

Value

Invisibly returns the original 'disag_data_mmap' object.


Print method for 'disag_model_mmap_aghq' objects

Description

Displays a brief overview of a multi-map disaggregation model: model family, link function, and components included.

Usage

## S3 method for class 'disag_model_mmap_aghq'
print(x, ..., max_print = 30)

Arguments

x

A 'disag_model_mmap_aghq' object.

...

Additional arguments (not used).

max_print

Maximum number of random effects details to print.

Value

Invisibly returns the original 'disag_model_mmap_aghq' object.


Print method for 'disag_model_mmap_mcmc' objects

Description

Displays a brief overview of a multi-map disaggregation model fit with the MCMC engine.

Usage

## S3 method for class 'disag_model_mmap_mcmc'
print(x, ...)

Arguments

x

A 'disag_model_mmap_mcmc' object.

...

Additional arguments (unused).

Value

Invisibly returns the original object.


Print method for 'summary.disag_model_mmap_aghq' objects (direct approach)

Description

Displays the summary information for a multi-map disaggregation model in a well-formatted way, directly using the AGHQ model's summary information.

Usage

## S3 method for class 'summary.disag_model_mmap_aghq'
print(x, ...)

Arguments

x

A 'summary.disag_model_mmap_aghq' object.

...

Additional arguments (not used).

Value

Invisibly returns the original summary object.


Print method for 'summary.disag_model_mmap_mcmc' objects

Description

Displays parameter estimates and MCMC diagnostics for an MCMC-fitted disaggregation model.

Usage

## S3 method for class 'summary.disag_model_mmap_mcmc'
print(x, ..., max_print = 30)

Arguments

x

A 'summary.disag_model_mmap_mcmc' object.

...

Additional arguments (unused).

max_print

Maximum number of parameter rows to print.

Value

Invisibly returns the original summary object.


Rename AGHQ model parameter names

Description

Rename parameter entries in an AGHQ model object (marginals, optimizer outputs, modes, Hessians) using canonicalized names based on coefficient metadata and time-varying structure.

Usage

rename_aghq_model_names(aghq_model, coef_meta, time_varying_betas)

Arguments

aghq_model

Fitted AGHQ model object.

coef_meta

List with coefficient metadata from 'compute_coef_meta()'.

time_varying_betas

Logical indicating whether time-varying betas are used.

Value

AGHQ model object with normalized parameter names in key components.


Prepare prediction objects for multi-map disaggregation (TMB)

Description

Constructs the covariate rasters, field projector, and IID shapefile objects needed by the single-raster prediction routines.

Usage

setup_objects_mmap(
  model_output,
  new_data = NULL,
  predict_iid = FALSE,
  time_index = NULL,
  use_training = FALSE
)

Arguments

model_output

A 'disag_model_mmap_tmb' model fit.

new_data

Optional SpatRaster (or list) of new covariates.

predict_iid

Logical; if TRUE, include the IID polygon effect.

Value

A list with elements: - covariates: SpatRaster of covariate layers. - field_objects: list with 'coords' matrix and 'Amatrix' projector (or NULL). - iid_objects: list with 'shapefile' and 'template' (or NULL).


Summary function for disag_data_mmap objects

Description

Prints counts of time points, polygons, pixels, per-time largest/smallest polygon sizes, number of covariates and their summaries and a mesh summary

Usage

## S3 method for class 'disag_data_mmap'
summary(object, ...)

Arguments

object

A 'disag_data_mmap' object (from 'prepare_data_mmap()').

...

Additional arguments (unused).

Value

Invisibly returns a list with components: - 'n_times', 'n_polygons', 'n_pixels' - 'per_time': data.frame with 'time', 'min_pixels', 'max_pixels' - 'n_covariates', 'covariate_summaries' (named list of summaries) - 'mesh_nodes', 'mesh_triangles'


Summary method for 'disag_model_mmap_aghq' objects (direct approach)

Description

Creates a simplified summary of a multi-map disaggregation model fit with AGHQ, directly using the AGHQ model's summary information.

Usage

## S3 method for class 'disag_model_mmap_aghq'
summary(object, ...)

Arguments

object

A 'disag_model_mmap_aghq' object.

...

Additional arguments (not used).

Value

An object of class 'summary.disag_model_mmap_aghq' containing the summary information.


Summary method for 'disag_model_mmap_mcmc' objects

Description

Summarizes parameter estimates and MCMC diagnostics from the underlying stanfit returned by tmbstan::tmbstan().

Usage

## S3 method for class 'disag_model_mmap_mcmc'
summary(object, pars = NULL, probs = c(0.025, 0.5, 0.975), ...)

Arguments

object

A 'disag_model_mmap_mcmc' object.

pars

Optional parameter names passed to summary.stanfit().

probs

Numeric vector of quantile probabilities.

...

Additional arguments passed to summary.stanfit().

Value

An object of class 'summary.disag_model_mmap_mcmc'.


Validate inputs to prepare_data_mmap()

Description

Check that all list-arguments are the same length (where required), that required arguments have the correct type, and that 'id_var', 'response_var', and 'sample_size_var' are valid strings.

Usage

validate_prepare_data_inputs(
  polygon_shapefile_list,
  covariate_rasters_list,
  aggregation_rasters_list,
  id_var,
  response_var,
  sample_size_var,
  make_mesh,
  categorical_covariate_baselines = NULL
)

Arguments

polygon_shapefile_list

A list of 'sf' objects.

covariate_rasters_list

NULL or a list of 'SpatRaster' objects.

aggregation_rasters_list

NULL or a list of 'SpatRaster' objects.

id_var

Character of length 1: name of polygon ID column.

response_var

Character of length 1: name of response column.

sample_size_var

NULL or character of length 1: sample-size column.

make_mesh

Logical flag indicating whether to build a mesh.

categorical_covariate_baselines

passed from prepare_data_mmap

Value

Invisibly 'TRUE' if all checks pass; otherwise stops with an error.


Validate covariate layer consistency across time

Description

Validate covariate layer consistency across time

Usage

validate_timevarying_covariates(
  covariate_rasters_list,
  time_varying_betas,
  where = "make_model_object_mmap()"
)

Arguments

covariate_rasters_list

list or NULL; each element is a multilayer raster/brick/spatRaster

time_varying_betas

logical; when TRUE, enforce identical layer names and order across time

where

character; caller label for clearer error messages