# 3. AOA in Parallel

#### Marvin Ludwig

#### 2023-03-28

Source:`vignettes/cast03-AOA-parallel.Rmd`

`cast03-AOA-parallel.Rmd`

Estimating the Area of Applicability (AOA) can be computationally intensive, depending on the amount of training data used for a model as well as the amount of new data the AOA has to be computed. This vignette goes over the possibility to (partly) compute the AOA in parallel. We will use the same data setup as the vignette “Area of applicability of spatial prediction models”. Please have a look there for a general introduction to the AOA and the details about the example data generation.

## Generate Example Data

```
library(CAST)
library(caret)
library(terra)
#library(sp)
library(sf)
library(viridis)
library(latticeExtra)
library(gridExtra)
```

```
generate_random_response <- function(raster, predictornames =
names(raster), seed = sample(seq(1000), 1)){
operands_1 = c("+", "-", "*", "/")
operands_2 = c("^1","^2")
expression <- paste(as.character(predictornames, sep=""))
# assign random power to predictors
set.seed(seed)
expression <- paste(expression,
sample(operands_2, length(predictornames),
replace = TRUE),
sep = "")
# assign random math function between predictors (expect after the last one)
set.seed(seed)
expression[-length(expression)] <- paste(expression[-
length(expression)],
sample(operands_1,
length(predictornames)-1, replace = TRUE),
sep = " ")
print(paste0(expression, collapse = " "))
# collapse
e = paste0("raster$", expression, collapse = " ")
response = eval(parse(text = e))
names(response) <- "response"
return(response)
}
```

See the Vignette on the Area of Applicability for more details how the data are generated.

```
predictors <- rast(system.file("extdata","bioclim.grd",package="CAST"))
response <- generate_random_response (predictors, seed = 10)
```

`## [1] "bio2^1 * bio5^1 + bio10^2 - bio13^2 / bio14^2 / bio19^1"`

```
mask <- predictors[[1]]
values(mask)[!is.na(values(mask))] <- 1
mask <- st_as_sf(as.polygons(mask))
set.seed(15)
samplepoints <- clustered_sample(mask,75,15,radius=25000)
trainDat <- extract(predictors,samplepoints,na.rm=TRUE)
trainDat$response <- extract(response,samplepoints,na.rm=FALSE)$response
trainDat <- data.frame(trainDat,samplepoints)
trainDat <- na.omit(trainDat)
```

```
set.seed(10)
model_random <- train(trainDat[,names(predictors)],
trainDat$response,
method="rf",
importance=TRUE,
trControl = trainControl(method="cv"))
prediction_random <- predict(predictors,model_random,na.rm=TRUE)
```

## Parallel AOA by dividing the new data

For better performances, it is recommended to compute the AOA in two
steps. First, the DI of training data and the resulting DI threshold is
computed from the model or training data with the function
`trainDI`

. The result from trainDI is usually the first step
of the `aoa`

function, however it can be skipped by providing
the trainDI object in the function call. This makes it possible to
compute the AOA on multiple raster tiles at once (e.g. on different
cores). This is especially useful for very large prediction areas,
e.g. in global mapping.

```
## DI of 75 observation
## Predictors: bio2 bio5 bio10 bio13 bio14 bio19
##
## AOA Threshold: 0.1825833
```

`saveRDS(model_random_trainDI, "path/to/file")`

If you have a large raster, you divide it into multiple smaller tiles and apply the trainDI object afterwards to each tile.

```
r1 = crop(predictors, c(0,7,42.33333,54.83333))
r2 = crop(predictors, c(7,14,42.33333,54.83333))
r3 = crop(predictors, c(14,21,42.33333,54.83333))
plot(r1[[1]],main = "Tile 1")
plot(r2[[1]],main = "Tile 2")
plot(r3[[1]],main = "Tile 3")
```

Use the `trainDI`

argument in the `aoa`

function to specify, that you want to use a previously computed trainDI
object.

```
aoa_r1 = aoa(newdata = r1, trainDI = model_random_trainDI)
plot(r1[[1]], main = "Tile 1: Predictors")
plot(aoa_r1$DI, main = "Tile 1: DI")
plot(aoa_r1$AOA, main = "Tile 1: AOA")
```

You can now run the aoa function in parallel on the different tiles!
Of course you can use for favorite parallel backend for this task, here
we use mclapply from the `parallel`

package.

```
library(parallel)
tiles_aoa = mclapply(list(r1, r2, r3), function(tile){
aoa(newdata = tile, trainDI = model_random_trainDI)
}, mc.cores = 3)
```

```
plot(tiles_aoa[[1]]$AOA, main = "Tile 1")
plot(tiles_aoa[[2]]$AOA, main = "Tile 2")
plot(tiles_aoa[[3]]$AOA, main = "Tile 3")
```

For larger tasks it might be useful to save the tiles to you hard-drive and load them one by one to avoid filling up your RAM.

```
# Simple Example Code for raster tiles on the hard drive
tiles = list.files("path/to/tiles", full.names = TRUE)
tiles_aoa = mclapply(tiles, function(tile){
current = terra::rast(tile)
aoa(newdata = current, trainDI = model_random_trainDI)
}, mc.cores = 3)
```