Learn R Programming

Model-Based Geostatistics

mbg is an R package for model-based geostatistics.

The mbg package provides a simple interface to run spatial machine learning models and geostatistical models that estimate a continuous (raster) surface from point-referenced observations and, optionally, a set of raster covariates. The package also includes functions to summarize raster estimates by (polygon) region while preserving uncertainty.

The mbg package combines features from the sf, terra, and data.table packages for spatial data processing; caret for spatial ML models; and R-INLA for geostatistical models.


Using the package

You can install the latest stable version of the mbg package from CRAN:

install.packages("mbg")

Some core package functions rely on R-INLA, which is not available on CRAN. If you do not already have the INLA package installed, you can download it following these instructions.

After installing and package and loading it using library(mbg), you can access the package vignette by running help(mbg), or get documentation for a specific function by running e.g. help(MbgModelRunner).


Package workflow

A typical MBG workflow includes the following steps:

  1. Load point data on outcomes, raster covariate surfaces, and a raster population surface
  2. (Optional): Run machine learning models relating the input covariate surfaces to the outcome, producing predictive raster surfaces from a variety of methods
  3. Prepare inputs for the geostatistical model. This includes the outcomes point data, model specifications, a spatial 2-D mesh, and either the input covariate surfaces or the ML predictive surfaces
  4. Run the geostatistical model. This model predicts the outcome as a linear combination of the raster surfaces and a SPDE approximation to a Gaussian process over space.
  5. Using the model fit, generate gridded predictions of the outcome across the entire study area. Uncertainty is captured by generating 250 posterior predictive draws at each pixel location.
  6. Summarize predictive draws as raster surfaces by taking the mean, median, and 95% uncertainty interval bounds of draws at each pixel location
  7. (Optional): Aggregate from pixels to administrative boundaries, preserving uncertainty

For more details, see the introductory vignette.

Copy Link

Version

Install

install.packages('mbg')

Monthly Downloads

346

Version

1.1.0

License

MIT + file LICENSE

Maintainer

Nathaniel Henry

Last Published

April 26th, 2025

Functions in mbg (1.1.0)

logging_add_indent

Add global indent for logging
logging_start_timer

Start logging timer
summarize_draws

Summarize draws
.onAttach

Behavior when attaching the mbg package
logging_stop_timer

End logging timer
make_time_stamp

Make time stamp
vif_covariate_select

Run variance inflation factor (VIF) selection on input covariates
values_to_raster

Insert values into a raster
run_regression_submodels

Run regression sub-models
rmse_raster_to_point

Generate RMSE from an estimated raster surface and point data
make_world_template_raster

Make world template raster
fit_inla_model

Fit INLA model
prepare_inla_data_stack

Prepare data stack for INLA
build_aggregation_table_validation

Validation: Build aggregation table
build_id_raster

Build ID raster
aggregate_raster_to_polygons

Aggregate a raster to polygons
calculate_pixel_fractions_single_polygon

Calculate fractional pixels in a polygon
dissolve_sf_by_attribute

Dissolve sf object by attribute
aggregate_draws_to_polygons

Aggregate grid cell draws to polygons
MbgModelRunner

MBG model runner class
build_aggregation_table

Build aggregation table
logging_get_timer_log

Get timer log
generate_cell_draws_and_summarize

Generate cell draws and summary rasters from INLA model
aggregate_raster_to_polygons_validation

Aggregate a raster to polygons: validation
aggregate_draws_to_polygons_validation

Aggregate grid cell draws to polygons: validation
logging_drop_indent

Remove global indent for logging
get_current_year

Helper function to get the current year
log_posterior_density

Generate log posterior predictive density from a geostatistical surface onto point data
load_covariates

Load covariates