Generate Spatiotemporal Predictions from Temporal Models
Source:R/generate_spatiotemporal_predictions.R
generate_spatiotemporal_predictions.RdGenerates temporally explicit habitat suitability predictions by projecting fitted models onto environmental raster stacks matching each time period. Accepts output from any of the four TemporalModelR modeling functions. Produces per-time-step assessment metrics showing how predictions and test point coverage vary over time.
Arguments
- partition_result
List or character. Output from
spatiotemporal_partitionor path to an.rdsfile containing that output.- model_result
List or character. Output from any of
build_temporal_hv,build_temporal_glm,build_temporal_gam, orbuild_temporal_rf, or a path to an.rdsfile. Model type is detected automatically from themodel_typefield.- pseudoabsence_result
List, character, or
NULL. Optional. Output fromgenerate_absencesor path to an.rdsfile. When supplied for presence/absence models (GLM, GAM, RF), the held-out pseudoabsence test points for each fold are filtered to the current time step and used alongside presence test points to compute per-timestep TN, FP, Specificity, and TSS. These columns are added to the timestep metrics table when pseudoabsences are available. Ignored for hypervolume models. Default isNULL.- raster_dir
Character. Directory containing environmental raster files (
.tif), typically the output ofraster_alignorscale_rasters. File names must follow the patterns supplied invariable_patterns, with any time placeholder substituted for the corresponding value fromtime_cols.- variable_patterns
Named character vector mapping clean variable names to raster filename patterns. For time-varying variables include the time placeholder in the pattern (e.g.
"forest_cover" = "forest_cover_YEAR"); for static variables omit it (e.g."elevation" = "elevation"). Time placeholders must match entries intime_cols.- time_cols
Character. Name of the column(s) containing year or time step values in the occurrence data. Must match
time_colsused inspatiotemporal_partitionand the time placeholders used invariable_patterns.- time_steps
Vector, data frame, or matrix of time periods for which to generate predictions.
- output_dir
Character. Directory to write prediction rasters and the assessment metrics CSV. Default is
file.path(tempdir(), "Predictions").- overwrite
Logical. If
TRUE, overwrites existing output files. IfFALSE(default), existing files are skipped.- verbose
Logical. If
TRUE(default), prints progress messages during processing. Includes per-time-step prediction progress.
Value
Invisibly returns a list containing:
timestep_metrics: data frame of per-fold per-time-step metrics. Columns always present:Fold, the time column(s),Pct_Suitable,N_Pres,TP,FN,Sensitivity,CBP. Whenpseudoabsence_resultis supplied for parametric models, additionally:N_Abs,TN,FP,Specificity,TSS. Saved asTimestep_Assessment_Metrics.csv.overall_summary: data frame of pooled metrics per fold. Columns:Fold,N_Timesteps,Mean_Pct_Suitable,Total_TP,Total_FN,Overall_Sensitivity,Overall_CBP. When pseudoabsences available: additionallyTotal_TN,Total_FP,Overall_Specificity,Overall_TSS.prediction_files: character vector of paths to saved prediction rasters.model_type: character string recording the model type used.
Details
G-space predictions are produced as rasters for each time-step and fold of an input model.
Per-timestep metrics are computed per fold per time step by extracting
raster predictions at the held-out presence points that fall within that
time step. Pct_Suitable records the proportion of the study area
predicted suitable. Sensitivity shows how consistently test points are
captured. CBP (cumulative binomial probability) tests whether the
observed number of correctly predicted test points is better than expected
by random placement: under the null, each point has Pct_Suitable
probability of falling in suitable area, so
dbinom(TP, N_Test_Pts, Pct_Suitable) gives the probability of
observing exactly TP correct by chance. Small values indicate
predictions are better than random.
See also
Preprocessing: scale_rasters,
spatiotemporal_partition
Modeling: build_temporal_hv,
build_temporal_glm, build_temporal_gam,
build_temporal_rf
Post-processing: summarize_raster_outputs,
plot_model_assessment
Examples
data(tmr_partition, package = "TemporalModelR")
data(tmr_glm, package = "TemporalModelR")
data(tmr_absences, package = "TemporalModelR")
scl_dir <- system.file("extdata/rasters_scaled",
package = "TemporalModelR")
time_steps <- expand.grid(
year = 1:15,
season = "Spring",
stringsAsFactors = FALSE
)
generate_spatiotemporal_predictions(
partition_result = tmr_partition,
model_result = tmr_glm,
pseudoabsence_result = tmr_absences,
raster_dir = scl_dir,
variable_patterns = c(
"elevation" = "elevation",
"forest_cover" = "forest_cover_YEAR",
"prseas" = "prseas_YEAR_SEASON"
),
time_cols = c("year", "season"),
time_steps = time_steps,
output_dir = tempdir(),
overwrite = TRUE,
verbose = FALSE
)