24  Lab: Spatial ML – classification

Spatial prediction of binary response by random forest

24.1 Explore data

We will spatially predict landslide incidents in dataset lsl from R package spDataLarge (see excercieses session 1). The response is a binary variable lslpts. Further, five covariates derived from the elevation model are available.

TipTask 1

Investigate the dataset: Compute the percentage of each class. Is the dataset balanced?

TipTask 2

Create boxplots of response vs. covariates. Which relationships do you see?

24.2 Fit random forest model

TipTask 1

Fit a random forest model using the terrain information as covariates (without spatial coordinates x and y).

You can for example use package ranger. Use arguments importance = "permutation" to obtain permuted covariate importance and probability = T to obtain probability predictions for the next tasks.

TipTask 2

Evaluate covariate importance using the function importance(). Create a barplot for better visibility. Is the performance of the covariates as expected from the exploratory analysis?

24.3 Predictions and interpretation

Load the area to be predicted containing the covariates:

library(terra)
ta <- rast(system.file("raster/ta.tif", package = "spDataLarge")) 
TipTask 1

Create predictions for the entire extent of the ta.tif. Use predict() with the random forest object from the section above. Use as.data.frame() to transform the raster values into a data.frame. What information/values does the function predict() return?

TipTask 2

Create a SpatRaster object with the function rast() containing the predictions for the class TRUE. Plot the resulting map.

Hint You can get x and y coordinates for each pixel using as.data.frame(, xy = TRUE).

The predictions were now created for the entire extent. We only have observations in the central part. Therefore, we need crop the predictions by the study area to avoid spatial extrapolation.

Note: this could also have been done before predicting to increase computational efficiency.

Load the study area:

data("study_mask", package = "spDataLarge")
TipTask 3

Set the pixels outside of the study area to NA using the function mask() from terra.

24.4 Model interpretation (optional)

Inform yourself on accumulated local effects plots: Chapter 8.2, Molnar, 2024.

TipTask 1

Print accumulated local effects plots for all covariates using the function ALE() from the package ale. What is the interpretation of these plots?

Note: You will need to define a prediction function (pred_fun). Make sure this function returns a vector of the probabilities just for one class, see output of predict() above.

24.5 Spatial coordinates as covariates

a <- 30 # rotation angle
x*cos(a/180*pi) - y*sin(a/180*pi)
x*sin(a/180*pi) + y*cos(a/180*pi)
TipTask 1

Add spatial coordinate axis including rotation by e.g. 30 and 60 degrees as additional covariates. Fit the above random forest model again and compute predictions and create the map.

TipTask 2

Compare the models by comparing the out-of-bag (OOB) model fit, covariate importance and the output maps. Create a difference map with new.raster <- raster1 - raster2.

If you have done so above, also compute accumulated local effects plots.