This repository contains an R-based application for fitting a Bayesian spatial model to plot-level carbon data and producing spatial predictions and uncertainty maps across a landscape.
The application is designed to be run through a graphical user interface (GUI). Users interact with the GUI to configure inputs, run model steps in order, and view diagnostic output. Internally, the GUI executes a series of R scripts that implement the modeling workflow.
The primary intended users are land managers, analysts, and researchers who need spatially explicit carbon estimates with associated uncertainty, but who do not want to modify or write R code.
- Install R and the required packages:
yaml,terra,geoR,spBayes. - Launch the GUI from the repository root with
Rscript src/gui.R. - Load
src/config.yamlorsrc/config_example.yaml. - Set
site,data_dir, andoutput_dir. - Run Step 0, then Step 1, then Step 2, then Step 3.
For most users, the GUI is the only interface that needs to be used.
The application uses two primary data sources:
- Field plot measurements of carbon
- A gridded raster covariate covering the full analysis area
Using these inputs, the application:
- Fits a regression model relating plot carbon measurements to the raster covariate.
- Evaluates spatial structure remaining in the residuals.
- Fits a Bayesian spatial regression model that accounts for this spatial structure.
- Predicts carbon continuously across the landscape.
- Produces uncertainty estimates associated with those predictions.
Outputs are intended to support mapping, reporting, and area-based summaries such as mean or total carbon within management units.
- R (version 4.0 or newer recommended)
- Internet access (for first-time package installation)
- A working Tcl/Tk installation for the GUI
Verify R is installed:
R --version
If you see an error such as:
Error in library(yaml) : there is no package called ‘yaml’
you must install the workflow packages:
tcltk, yaml, terra, geoR, spBayes
tcltk usually ships with R; the others typically need installation.
Install all non-base packages with:
Rscript -e "install.packages(c('yaml','terra','geoR','spBayes'), repos='https://cloud.r-project.org')"
Create a personal R library directory:
mkdir -p ~/R/x86_64-pc-linux-gnu-library
Install required packages:
Rscript -e "install.packages(c('yaml','terra','geoR','spBayes'), repos='https://cloud.r-project.org', lib='~/R/x86_64-pc-linux-gnu-library')"
Set the user library for your session:
export R_LIBS_USER=~/R/x86_64-pc-linux-gnu-library
Then launch the GUI from the repository root:
Rscript src/gui.R
For bash:
echo 'export R_LIBS_USER=~/R/x86_64-pc-linux-gnu-library' >> ~/.bashrc
source ~/.bashrc
For zsh:
echo 'export R_LIBS_USER=~/R/x86_64-pc-linux-gnu-library' >> ~/.zshrc
source ~/.zshrc
After this, simply run from the repository root:
Rscript src/gui.R
sudo Rscript -e "install.packages(c('yaml','terra','geoR','spBayes'), repos='https://cloud.r-project.org')"
Check R library paths:
Rscript -e "print(.libPaths())"
Your user library should appear first.
The application is run through a graphical user interface.
The GUI is the primary entry point for users.
Users launch the GUI and interact with buttons and controls to:
- Configure model inputs
- Run model steps
- View diagnostics
The GUI manages configuration, execution, and display of results.
Behind the scenes, the GUI runs a sequence of R scripts that implement the workflow. These scripts are not meant to be run directly by most users, although advanced users may do so if desired.
Launch the GUI from the repository root:
Rscript src/gui.R
On Windows, you can also start the GUI from an R console with:
source("src/gui.R")The modeling workflow consists of four sequential steps.
- Validates input files and directory structure
- Verifies that the boundary, plots, and raster share the same CRS
- Verifies that the raster covers the boundary extent, with optional tolerance-based warning behavior
- Loads spatial data (boundary, plots, raster)
- Extracts raster values at plot locations
- Fits a non-spatial linear regression model
- Computes a semivariogram of residuals
- Produces a diagnostic plot showing residual spatial structure
- Fits a Bayesian spatial regression model using MCMC
- Estimates regression coefficients, spatial variance, nugget variance, and spatial range
- Produces diagnostic plots of MCMC behavior
- Uses the fitted model to predict carbon across the raster grid
- Produces
pred.tif, a per-pixel output containing mean and uncertainty layers - Produces
pred-joint.tif, a joint predictive output that is better suited to uncertainty-aware aggregation - Generates both products from the same button click in the GUI
Each step depends on outputs from the previous step and is coordinated by the GUI.
The repository contains:
src/gui.R– launches the current graphical user interfacesrc/main_gui.R– older GUI implementation retained for referencesrc/step0.Rthroughsrc/step3.R– scripts implementing the modeling workflowsrc/mod.R– shared utility and modeling functionssrc/config.yaml– default configuration file controlling model settings and pathsREADME.md– this file
Most application code resides under src/.
Input data must be organized in a site-specific directory structure.
Each site directory must include:
- A boundary polygon shapefile defining the analysis extent
- A plots shapefile containing point locations and a field named
Total.Carbrepresenting measured carbon - A single-band raster file used as the predictor covariate
The GUI expects data_dir to be the parent directory that contains site folders, and site to name the specific folder to use.
For example, if:
data_dir: /path/to/datasite: black-mountains
then the program will look for:
/path/to/data/black-mountains/bnd/bnd.shp
/path/to/data/black-mountains/plots/plots.shp
/path/to/data/black-mountains/carbon-map.tif
Requirements:
- All spatial data must use a common coordinate reference system (CRS).
- The raster should cover the boundary polygon.
- By default, Step 0 can be configured either to stop on raster coverage mismatch or to log a warning and continue when the mismatch is small and expected.
- The GUI checks both CRS consistency and raster coverage during Step 0 and reports readable messages if they fail.
Practical notes:
- The plots shapefile must contain a field named
Total.Carb. - The raster is expected to be single-band.
- Small extent mismatches can occur because of pixel alignment or rounding; these can be handled with
raster.coverage.tolerance.
Model settings are controlled through a YAML configuration file.
The GUI includes:
- A YAML editor
- Quick fields for
site,data_dir, andoutput_dir Load Template,Save, andSave Asactions- Step buttons that run the workflow in order and stream log output into the GUI
For most first runs, only these values usually need to change:
sitedata_diroutput_dirn.samplesn.threads
Recommended first-pass workflow:
- Start with
src/config_example.yamlif you want a portable template, orsrc/config.yamlif you want to edit the default runtime config. - Change
site,data_dir, andoutput_dir. - Run Step 0 to validate paths and spatial inputs.
- Run Step 1 to inspect the semivariogram and logged nugget/sill values.
- Adjust advanced Step 2 settings only if needed.
- Run Step 2 and then Step 3.
Raster coverage behavior can also be controlled from YAML:
strict.raster.coverage- whentrue, Step 0 stops if the raster extent does not cover the boundary extentraster.coverage.tolerance- non-negative tolerance in the raster CRS units that allows small edge mismatches
Example:
strict.raster.coverage: false
raster.coverage.tolerance: 10This setting is useful when the raster and boundary differ by only a few map units because of extent alignment or rounding, but you still want the workflow to continue.
The variogram-related variance settings are more advanced. A common workflow is:
- Run Step 0 to validate inputs.
- Run Step 1 to inspect the variogram and estimated nugget/sill.
- Adjust advanced settings if needed.
- Run Step 2 and Step 3.
The configuration file specifies:
- Site name
- Paths to input data and output directories
- MCMC settings (number of samples, thinning)
- Prior bounds and tuning parameters for the spatial model
- Raster coverage validation behavior for Step 0
The GUI allows users to load, edit, and save the configuration file.
Model outputs are written to a site-specific directory under the configured output location.
Typical outputs include:
semivariogram.pngfrom Step 1chainImg.pngfrom Step 2m.1.RData, the fitted spatial model from Step 2pred.tif, the per-pixel prediction output from Step 3m.1.pred.RData, serialized per-pixel predictive samplespred-joint.tif, the joint prediction output from Step 3m.1.pred.joint.RData, serialized joint predictive samples
pred.tif is intended for mapping and per-pixel summaries. pred-joint.tif is intended for aggregation workflows where uncertainty over areas, polygons, or totals matters.
The application produces two types of predictions:
Non-joint predictions
- Suitable for per-pixel mapping and visualization
- Easier to use for standard raster display workflows
Joint predictions
- Preserve spatial covariance
- Better suited for uncertainty-aware aggregation
- Required when computing uncertainty for area-based summaries such as polygon means or totals
Joint predictions should be used when reporting uncertainty for aggregated quantities.
This application is intended for regional and landscape-scale carbon analysis and decision support.
It is not a substitute for local field inventories and should be used with appropriate domain knowledge, validation, and interpretation.
Results should be evaluated in the context of:
- Data quality
- Model assumptions
- Spatial scale
- Uncertainty
Questions, issues, and suggestions should be submitted through the project’s issue tracker or directed to the repository maintainer.