Background

I am writing this short article to clear air on whether a change in one or all of raster layers used in building a model will prevent prediction using the trained model.

If a model is trained using say stacked raster layers, say four in total, if values of one raster is changed, the same model will still perform prediction.

Most important is to ensure that the names of the rasters used in training the model are maintained even if values are changed.

Here, I will use data that comes with sdm package to enable ease of reproducibility.

Loading data

file <- system.file("external/species.shp", package = "sdm")
species <- shapefile(file)
path <- system.file("external", package = "sdm")
lst <- list.files(path = path, pattern = 'asc$', full.names = T)
preds <- stack(lst)

Creating sdmData object

sdm_data <- sdmData(formula = Occurrence ~., train = species, predictors = preds)
## Loading required package: dismo
## Loading required package: gbm
## Loaded gbm 2.1.8
## Loading required package: tree
## Loading required package: mda
## Loading required package: class
## Loaded mda 0.5-2
## Loading required package: mgcv
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:raster':
## 
##     getData
## This is mgcv 1.8-38. For overview type 'help("mgcv-package")'.
## Loading required package: glmnet
## Loading required package: Matrix
## Loaded glmnet 4.1-3
## Loading required package: earth
## Loading required package: Formula
## Loading required package: plotmo
## Loading required package: plotrix
## Loading required package: TeachingDemos
## Loading required package: rJava
## Loading required package: RSNNS
## Loading required package: Rcpp
## Loading required package: ranger
## Loading required package: randomForest
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:ranger':
## 
##     importance
## Loading required package: rpart
## Loading required package: kernlab
## 
## Attaching package: 'kernlab'
## The following objects are masked from 'package:raster':
## 
##     buffer, rotated
## Warning in proj4string(train): CRS object has comment, which is lost in output; in tests, see
## https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

## Warning in proj4string(train): CRS object has comment, which is lost in output; in tests, see
## https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

Creating the model

sdm_model <- sdm(Occurrence ~., data = sdm_data, methods = c('rf', 'svm'), replication = 'boot', n = 3)
## Loading required package: parallel

Makiing prediction on the original data

prediction_original <- predict(sdm_model, preds)
## Warning in .rasterFromRasterFile(grdfile, band = band, objecttype, ...): size of
## values file does not match the number of cells (given the data type)

## Warning in .rasterFromRasterFile(grdfile, band = band, objecttype, ...): size of
## values file does not match the number of cells (given the data type)
plot(prediction_original[[2]])

Changing one of the rasters in preds stack

elevation <- preds[['elevation']]
elevation_doubled <- elevation * 2 

Feeding the new raster back to preds stack

preds[['elevation']] <- elevation_doubled
preds_2 <- preds # Preds with one raster values doubled

Making prediction with one changed raster in the stack

prediction_changed <- predict(sdm_model, preds_2) 
## Warning in .rasterFromRasterFile(grdfile, band = band, objecttype, ...): size of
## values file does not match the number of cells (given the data type)

## Warning in .rasterFromRasterFile(grdfile, band = band, objecttype, ...): size of
## values file does not match the number of cells (given the data type)
plot(prediction_changed[[2]])

Ploting the two outputs

par(mfrow = c(2,1))
plot(prediction_original[[2]])
plot(prediction_changed[[2]])

Allaire, JJ, Yihui Xie, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, Hadley Wickham, Joe Cheng, Winston Chang, and Richard Iannone. 2021. Rmarkdown: Dynamic Documents for r. https://CRAN.R-project.org/package=rmarkdown.
Bivand, Roger S., Edzer Pebesma, and Virgilio Gomez-Rubio. 2013. Applied Spatial Data Analysis with R, Second Edition. Springer, NY. https://asdar-book.org/.
Hijmans, Robert J. 2021. Raster: Geographic Data Analysis and Modeling. https://rspatial.org/raster.
Naimi, Babak, and Miguel B. Araujo. 2016. “Sdm: A Reproducible and Extensible r Platform for Species Distribution Modelling.” Ecography 39: 368–75. https://doi.org/10.1111/ecog.01881.
———. 2021. Sdm: Species Distribution Modelling. https://www.biogeoinformatics.org.
Pebesma, Edzer J., and Roger S. Bivand. 2005. “Classes and Methods for Spatial Data in R.” R News 5 (2): 9–13. https://CRAN.R-project.org/doc/Rnews/.
Pebesma, Edzer, and Roger Bivand. 2021. Sp: Classes and Methods for Spatial Data. https://CRAN.R-project.org/package=sp.
R Core Team. 2021. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Xie, Yihui. 2014. “Knitr: A Comprehensive Tool for Reproducible Research in R.” In Implementing Reproducible Computational Research, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC. http://www.crcpress.com/product/isbn/9781466561595.
———. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.
———. 2021. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://yihui.org/knitr/.
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. R Markdown: The Definitive Guide. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown.
Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. R Markdown Cookbook. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown-cookbook.