15.6 Prediction

# predicting using the best hyperparameter combination
autotuner_rf$predict(task)
## <PredictionRegr> for 84 observations:
##     row_ids      truth   response
##           1 -1.0818612 -1.0692597
##           2 -0.9554438 -1.0373188
##           3 -0.9207629 -1.0092878
## ---                              
##          82  0.8258654  0.6573228
##          83  0.8257684  0.8004869
##          84  0.8226045  0.8495434

The predict method will apply the model to all observations used in the modeling. Given a multilayer SpatRaster containing rasters named as the predictors used in the modeling, terra::predict() will also make spatial distribution maps, i.e., predict to new data.

pred_raster = terra::predict(ep, model = autotuner_rf, fun = predict)
plot(pred_raster)

vegetation bands prediced via NMDS

Manually making predictions
newdata = as.data.frame(as.matrix(ep))
colSums(is.na(newdata))  # 0 NAs
# but assuming there were 0s results in a more generic approach
ind = rowSums(is.na(newdata)) == 0
tmp = autotuner_rf$predict_newdata(newdata = newdata[ind, ], task = task)
newdata[ind, "pred"] = data.table::as.data.table(tmp)[["response"]]
pred_2 = ep$dem
# now fill the raster with the predicted values
pred_2[] = newdata$pred
# check if terra and our manual prediction is the same
all(values(pred - pred_2) == 0)