Code INLA-SPDE to explain changes of mesic vegetation
This project uses R and the INLA package to prepare and model spatial data of mesic vegetation conditions across four different years (2004, 2009, 2014, 2019). It includes raster data extraction, covariate standardization, and mesh generation for spatial modeling using the SPDE approach. This analysis supports understanding the spatial patterns of mesic vegetation in relation to precipitation, temperature, and land tenure.
You can download the images in those following links.
Precipitation and temperature: https://code.earthengine.google.com/6649b7d5bd1fb2088b7ede64ba9d3b16 Mesic vegetation proportion: https://code.earthengine.google.com/a36833e5d2d99b1002108096e8de0193 Land Tenure: https://code.earthengine.google.com/3add7ddef9fb84b11706ec5871e6c379
Make sure to install and load the following packages:
install.packages("INLA", repos=c(getOption("repos"),
INLA="https://inla.r-inla-download.org/R/stable"),
dependencies=TRUE)
install.packages("rgdal") # required for raster and spatial data handling
# Also use install.packages() for: raster, sp, terra, dplyr, sf-
Set working directory
Set the file path to your local directory where all raster datasets are stored. -
Load raster datasets
Load mesic vegetation rasters for four years and a land tenure raster. -
Sample pixels randomly
Randomly select 500 spatial points from the 2004 mesic raster to extract values from all rasters at the same locations for consistency. -
Extract covariates
- Extract standardized precipitation and temperature values for each year.
- Extract land tenure values for the sampled locations.
-
Normalize climate variables
Precipitation and temperature values are z-score normalized:
[ x_{\text{norm}} = \frac{x - \mu}{2\sigma} ] -
Clean and merge
All year-specific data frames are merged into a single dataset for modeling.
-
Mesh creation
The mesh is created usinginla.mesh.2d()based on the convex hull of the sampled coordinates.
Parameters:max.edge: controls triangle sizes near boundaries vs. interiorsoffset: buffer zone for outer meshcutoff: removes duplicate/close points to reduce mesh complexity
-
SPDE model
- A projection matrix (
A) is created to map data points to the mesh. - The Matérn covariance model is defined via
inla.spde2.matern()to capture spatial correlation.
- A projection matrix (
This script sets up the data and spatial model input needed to run spatial regression or INLA-based spatial-temporal models. Once the mesh and projection matrix are created, you can move on to define the full INLA model using inla().
Place the following in your working directory:
WRP-040029-YYYY08.tif(mesic raster for each year)TenureRasterExport.tif(land tenure)029ppt_YYYY_08.tif(precip and temp stacked raster for each year underppt_exports/)
- All rasters must be projected into the same CRS (coordinate reference system).
- The script includes comments to help you adjust mesh resolution and spatial extent.
- Make sure you have a working internet connection when installing INLA or older versions of
rgdal.