Helper function to summarize a matrix or data.frame of predictive draws
Arguments
- draws
A
matrix,data.frame, or data.table::data.table of predictive draws.- id_fields
(default NULL) Only considered for data.frame-like
draws. What identifier fields in the data should be kept in the summary table and not included among the draw fields?- draw_fields
(default NULL) Only considered for data.frame-like
draws. What fields represent actual draws, as opposed to identifier fields or other metadata like population? IfNULL, the default, automatically determines the draw fields as all columns not included in theid_fields.- ui_width
(
numeric, default 0.95) Size of the uncertainty interval width when calculating the upper and lower summary rasters- na.rm
(
logical, default TRUE) Should NA values be removed when calculating summaries across draws?
Value
A data.table::data.table containing at least the following fields:
The
id_fields, if passed"mean": Mean across predictive draws
"lower": Lower bound of the (X%) uncertainty interval
"upper": Upper bound of the (X%) uncertainty interval
"ui_width": "upper" - "lower"
Examples
# Summarize a draws matrix
draws_matrix <- matrix(rnorm(200), nrow = 10)
summary_table_a <- summarize_draws(draws_matrix)
head(summary_table_a)
#> mean lower upper ui_width
#> <num> <num> <num> <num>
#> 1: 0.09737227 -1.696132 1.984732 3.680864
#> 2: 0.02254290 -1.279289 1.330979 2.610268
#> 3: 0.24014468 -1.499205 1.975478 3.474683
#> 4: 0.04991584 -1.476200 1.577184 3.053384
#> 5: 0.09614328 -2.135038 1.829265 3.964303
#> 6: 0.49469713 -1.441610 2.456848 3.898458
# Summarize a draws data.table with location IDs
draws_table <- matrix(c(1:10, rnorm(200)), nrow = 10) |>
data.table::as.data.table() |>
data.table::setnames(c('location_id', paste0('draw_', 1:20)))
summary_table_b <- summarize_draws(draws_table, id_fields = 'location_id')
head(summary_table_b)
#> location_id mean lower upper ui_width
#> <num> <num> <num> <num> <num>
#> 1: 1 -0.02270941 -1.034371 1.411232 2.445603
#> 2: 2 -0.37443064 -2.083516 1.721388 3.804904
#> 3: 3 0.31381573 -1.106188 1.667994 2.774182
#> 4: 4 -0.03861082 -1.697354 1.770278 3.467632
#> 5: 5 0.12432379 -1.465282 1.878477 3.343759
#> 6: 6 0.31366739 -1.823267 1.959878 3.783145
