15.6 Prediction
# predicting using the best hyperparameter combination
$predict(task) autotuner_rf
## <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.
= terra::predict(ep, model = autotuner_rf, fun = predict)
pred_raster plot(pred_raster)
Manually making predictions
= as.data.frame(as.matrix(ep))
newdata colSums(is.na(newdata)) # 0 NAs
# but assuming there were 0s results in a more generic approach
= rowSums(is.na(newdata)) == 0
ind = autotuner_rf$predict_newdata(newdata = newdata[ind, ], task = task)
tmp "pred"] = data.table::as.data.table(tmp)[["response"]]
newdata[ind, = ep$dem
pred_2 # now fill the raster with the predicted values
= newdata$pred
pred_2[] # check if terra and our manual prediction is the same
all(values(pred - pred_2) == 0)