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.
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)
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
sdm_model <- sdm(Occurrence ~., data = sdm_data, methods = c('rf', 'svm'), replication = 'boot', n = 3)
## Loading required package: parallel
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]])
elevation <- preds[['elevation']]
elevation_doubled <- elevation * 2
preds[['elevation']] <- elevation_doubled
preds_2 <- preds # Preds with one raster values doubled
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]])
par(mfrow = c(2,1))
plot(prediction_original[[2]])
plot(prediction_changed[[2]])