Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# fireSense_ignitionPredict (development version)

* An escape fit with a year climate term (`modelList$yearClimate` of fireSense_ignitionFit's new `escapeYearClimate`) has its expected escapes multiplied by it, from the same year mean of the covariates as the ignitions' term (`yearClimateMultiplierOf()` now takes either fit). A logistic term (fireSense_ignitionFit's `yearClimateShape = "logistic"`) is applied the same way, from the stored fit, through `fireSenseUtils::yearClimateMultiplier()`. A fit without the term is unchanged. Needs fireSenseUtils (>= 0.2.3.9195).
* A fit with a year climate term (`modelList$yearClimate` of fireSense_ignitionFit's new `ignitionYearClimate`) has its expected ignitions multiplied by `exp(beta * z)`, `z` this year's standardised mean of the climate covariate over the ELF's pixels (`fireSenseUtils::yearClimateMultiplier()`; the fit and the prediction take the same unweighted mean over the rows with the covariate and core, `fireSenseUtils::yearClimateMeans()`, in the predict taken before `na.omit()` so a pixel with an NA in another column still counts, and the multiplier averages 1 over the fit years). The covariates must carry `coreFraction` (added by fireSense_dataPrepPredict, in the same way as for the fit): without it a fit that has the term stops with a message. Escapes are untouched; fits without the term are unchanged. Needs fireSenseUtils (>= 0.2.3.9183).
* The yearly ignition and escape probability map (`.plots = "png"`) is bigger. Its png is now 10 x 8 inches at 144 dpi (1440 x 1152 pixels), as fireSense_burn's; it was R's `png()` default of 480 x 480, which looked small in `SpaDES.shiny::shine()`.
* The yearly map shows the escape probability beside the ignition probability (`IgnitionEscapeProbability_yr<year>`, was `IgnitionProbability_yr<year>`), both layers of `fireSense_IgAndEscapeProbRas`, and their product, the expected number of escaped fires starting in each pixel that year (`expectedEscapes`).
Expand Down
19 changes: 10 additions & 9 deletions fireSense_ignitionPredict.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defineModule(sim, list(
"data.table", "magrittr", "terra", "xgboost (>= 3.0.0)",
"PredictiveEcology/SpaDES.core@development (>= 3.0.4)",
"PredictiveEcology/reproducible@development",
"PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9184)" # 9184: yearClimateMeans(covariates, variable) unweighted, yearClimateMultiplier() norm; 9182: yearClimateColumn(), yearClimateMultiplier(); 9177: storedFitProblem(); 9168: foldModels(), foldMeanPrediction()
"PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9195)" # 9195: fireSenseUtils#264 (yearClimateMultiplier() reads the logistic term, logisticYearCurve()); TODO change if another fireSenseUtils PR merges before #264; 9194: annualYearWeights(), standardiseYearClimate() (#263); 9184: yearClimateMeans(covariates, variable) unweighted, yearClimateMultiplier() norm; 9182: yearClimateColumn(), yearClimateMultiplier(); 9177: storedFitProblem(); 9168: foldModels(), foldMeanPrediction()
),
loadOrder = list(after = "fireSense_dataPrepPredict"),
parameters = bindrows(
Expand Down Expand Up @@ -145,8 +145,8 @@ doEvent.fireSense_ignitionPredict <- function(sim, eventTime, eventType, debug =
#' Averages the per-fold predictions of the ignition models and of the escape models, for every coarse
#' pixel with covariates (`fireSenseUtils::foldMeanPrediction()`), and gives each pixel its fits' year-effect sd
#' (`yearSDOf()`) and, when they have one, their fitted effect of this year (`yearEffectOf()`). A fit with a year
#' climate term (`modelList$yearClimate`, `fireSense_ignitionFit`'s `ignitionYearClimate`) has its expected ignitions
#' multiplied by `yearClimateMultiplierOf()`; escapes have none. Nothing is
#' climate term (`modelList$yearClimate`, `fireSense_ignitionFit`'s `ignitionYearClimate` and `escapeYearClimate`) has its
#' expected ignitions, or escapes, multiplied by `yearClimateMultiplierOf()`, the same year's mean for both. Nothing is
#' drawn: `fireSense_burn` draws ignitions and escapes from these means,
#' with the distribution named in `fireSense_IgAndEscapeDistribution`.
#'
Expand Down Expand Up @@ -188,8 +188,9 @@ IgnitionPredictRun <- function(sim) {
# own mean and sd would make every year look average.
covsIgn <- if (rescaleVars) scaleAsFit(covsHere, f$ign$scaleData, "fireSense_IgnitionFitted") else covsHere
covsEsc <- if (rescaleVars) scaleAsFit(covsHere, f$esc$scaleData, "fireSense_EscapeFitted") else covsHere
predsIgns[f$rows] <- fireSenseUtils::foldMeanPrediction(f$ign, covsIgn) * yearClimateMultiplierOf(f$ign, if (!is.null(f$ign$modelList$yearClimate)) igCovAll[f$allRows])
predsEscs[f$rows] <- fireSenseUtils::foldMeanPrediction(f$esc, covsEsc)
## `covariates` of `yearClimateMultiplierOf()` is read only by a fit with a term (lazy), the same year mean for both
predsIgns[f$rows] <- fireSenseUtils::foldMeanPrediction(f$ign, covsIgn) * yearClimateMultiplierOf(f$ign, igCovAll[f$allRows])
predsEscs[f$rows] <- fireSenseUtils::foldMeanPrediction(f$esc, covsEsc) * yearClimateMultiplierOf(f$esc, igCovAll[f$allRows])
sdIgns[f$rows] <- yearSDOf(f$ign)
sdEscs[f$rows] <- yearSDOf(f$esc)
effIgns[f$rows] <- yearEffectOf(f$ign, year)
Expand Down Expand Up @@ -332,16 +333,16 @@ yearSDOf <- function(fit) {
if (is.null(sd)) 0 else sd
}

#' The year climate multiplier of a fit's expected ignitions
#' The year climate multiplier of a fit's expected ignitions or escapes
#'
#' `fireSense_ignitionFit` stores `modelList$yearClimate` (`list(variable, center, scale, beta, norm)`) with a year
#' climate term; the multiplier is `fireSenseUtils::yearClimateMultiplier()` of this year's mean of the raw covariate
#' over the fit's pixels, `fireSenseUtils::yearClimateMeans()`, the same definition the fit used (unweighted mean over
#' over the fit's pixels, `fireSenseUtils::yearClimateMeans()`, the same definition the fit used (the ignition covariates' means, for an escape fit too) (unweighted mean over
#' the rows with the variable and with core: it needs the `coreFraction` column `fireSense_dataPrepPredict` adds). A fit without the term (older fits) gives 1, and so does a year with no
#' usable row, with a warning. Stops if the covariates hold more than one year, or lack the variable or `coreFraction`.
#'
#' @param fit A `fireSense_IgnitionFitted`.
#' @param covariates The raw (not standardised) covariates of the fit's pixels, one year, before `na.omit()` (`f$allRows` of `IgnitionPredictRun()`); not used by a fit without the term.
#' @param fit A `fireSense_IgnitionFitted` or `fireSense_EscapeFitted`.
#' @param covariates The raw (not standardised) covariates of the fit's pixels, one year, before `na.omit()` (`f$allRows` of `IgnitionPredictRun()`); not used (not evaluated) by a fit without the term.
#' @return One number.
yearClimateMultiplierOf <- function(fit, covariates) {
yc <- fit$modelList$yearClimate
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test-run-values.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,38 @@ test_that("a year with no usable covariate gives a multiplier of 1 and a warning
toyIgCovariates()[integer(0), , drop = FALSE]), "it is 1")
expect_identical(m, 1)
})

## the year climate term of the escapes (fireSense_ignitionFit's `escapeYearClimate`), with the same year mean
test_that("an escape fit's year climate term scales the expected escapes by the same z, ignitions untouched", {
dry <- twoFolds(covs = toyIgCovariates(MDC = c(100, 120, 140, 160) + 20)) # z = 1
dry$fireSense_EscapeFitted$modelList$yearClimate <- modifyList(toyClimate, list(beta = 0.3, norm = 2))
sim <- toyIgRun(dry)
expect_equal(probVals(sim)[, "escapeProb"], rep(1, 4) * exp(0.3) / 2)
expect_equal(probVals(sim)[, "ignitionProb"], c(0, 3, 3, 0))
## both terms at once: each fit's own beta, one z
both <- withYearClimate(dry, toyClimate)
simB <- toyIgRun(both)
expect_equal(probVals(simB)[, "ignitionProb"], c(0, 3, 3, 0) * exp(0.5))
expect_equal(probVals(simB)[, "escapeProb"], rep(1, 4) * exp(0.3) / 2)
## a logistic escape term, from the stored fit: S-curve through z = 0, floor 1, upper level `ratio`
lgs <- list(variable = "MDC", center = 130, scale = 20, shape = "logistic", k = 2, ratio = 4, norm = 2)
dry$fireSense_EscapeFitted$modelList$yearClimate <- lgs
expect_equal(probVals(toyIgRun(dry))[, "escapeProb"], rep(1, 4) * (1 + 3 * stats::plogis(2)) / 2)
## far beyond the fit range it saturates at the upper level over norm
vdry <- twoFolds(covs = toyIgCovariates(MDC = c(100, 120, 140, 160) + 2000))
vdry$fireSense_EscapeFitted$modelList$yearClimate <- lgs
expect_equal(probVals(toyIgRun(vdry))[, "escapeProb"], rep(1, 4) * 4 / 2)
## the wet side is the floor
wet <- twoFolds(covs = toyIgCovariates(MDC = c(100, 120, 140, 160) - 2000))
wet$fireSense_EscapeFitted$modelList$yearClimate <- lgs
expect_equal(probVals(toyIgRun(wet))[, "escapeProb"], rep(1, 4) * 1 / 2)
## no term (the default): escapes are untouched
expect_equal(probVals(toyIgRun(twoFolds(covs = toyIgCovariates(MDC = c(100, 120, 140, 160) + 20))))[, "escapeProb"], rep(1, 4))
})

test_that("an escape fit with a term and covariates without coreFraction stops", {
noCore <- toyIgCovariates(); data.table::set(noCore, NULL, "coreFraction", NULL)
ins <- twoFolds(covs = noCore)
ins$fireSense_EscapeFitted$modelList$yearClimate <- toyClimate
expect_error(toyIgRun(ins), "no `coreFraction` column")
})