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)

* 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 used the exposure-weighted mean over the core, here it is the plain mean). Escapes are untouched; fits without the term are unchanged. Needs fireSenseUtils (>= 0.2.3.9182).
* 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`).
* New parameter `.plots` (default `"screen"`): the ignition probability map is plotted each `run` with `Plots(types = .plots)`; `NA` or `NULL` for none. The plot was gated on `.saveInterval` instead, so `.plots` (e.g. `"png"` from `.globals`) drew and saved nothing. `.saveInterval`, used for nothing else, is removed.
Expand Down
24 changes: 21 additions & 3 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.9177)" # 9177: storedFitProblem(); 9168: foldModels(), foldMeanPrediction()
"PredictiveEcology/fireSenseUtils@development (>= 0.2.3.9182)" # 9182: yearClimateColumn(), yearClimateMultiplier(); 9177: storedFitProblem(); 9168: foldModels(), foldMeanPrediction()
),
loadOrder = list(after = "fireSense_dataPrepPredict"),
parameters = bindrows(
Expand Down Expand Up @@ -144,7 +144,9 @@ 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()`). Nothing is
#' (`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
#' drawn: `fireSense_burn` draws ignitions and escapes from these means,
#' with the distribution named in `fireSense_IgAndEscapeDistribution`.
#'
Expand Down Expand Up @@ -177,7 +179,7 @@ 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)
predsIgns[f$rows] <- fireSenseUtils::foldMeanPrediction(f$ign, covsIgn) * yearClimateMultiplierOf(f$ign, covsHere)
predsEscs[f$rows] <- fireSenseUtils::foldMeanPrediction(f$esc, covsEsc)
sdIgns[f$rows] <- yearSDOf(f$ign)
sdEscs[f$rows] <- yearSDOf(f$esc)
Expand Down Expand Up @@ -321,6 +323,22 @@ yearSDOf <- function(fit) {
if (is.null(sd)) 0 else sd
}

#' The year climate multiplier of a fit's expected ignitions
#'
#' `fireSense_ignitionFit` stores `modelList$yearClimate` (`list(variable, center, scale, beta)`) with a year climate
#' term; the multiplier is `fireSenseUtils::yearClimateMultiplier()` of this year's mean of the raw covariate over
#' the fit's pixels. The fit standardised with the exposure-weighted mean over the core of its ELF; here it is the
#' plain mean over the ELF's pixels. A fit without the term (older fits) gives 1.
#'
#' @param fit A `fireSense_IgnitionFitted`.
#' @param covariates The raw (not standardised) covariates of the fit's pixels.
#' @return One number.
yearClimateMultiplierOf <- function(fit, covariates) {
yc <- fit$modelList$yearClimate
if (is.null(yc)) return(1)
fireSenseUtils::yearClimateMultiplier(yc, mean(covariates[[fireSenseUtils::yearClimateColumn(yc$variable, names(covariates))]]))
}

#' A fit's fitted year effect of one year
#'
#' `fireSense_ignitionFit` stores, with both processes fitted, `modelList$yearEffects`: one row per fit year,
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-run-values.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,28 @@ test_that("a fit's fitted year effect of this year becomes a layer; without one
expect_identical(names(toyIgRun(ins)$fireSense_IgAndEscapeProbRas),
c("ignitionProb", "escapeProb", "ignitionYearSD", "escapeYearSD"))
})

## the year climate term of the ignitions (fireSense_ignitionFit's `ignitionYearClimate`)
withYearClimate <- function(ins, yc) {
ins$fireSense_IgnitionFitted$modelList$yearClimate <- yc
ins
}
toyClimate <- list(variable = "MDC", center = 130, scale = 20, beta = 0.5)

test_that("a fit without a year climate term gives a multiplier of 1", {
expect_identical(yearClimateMultiplierOf(twoFolds()$fireSense_IgnitionFitted, toyIgCovariates()), 1)
expect_equal(probVals(toyIgRun(twoFolds()))[, "ignitionProb"], c(0, 3, 3, 0))
})

test_that("a drier year raises the expected ignitions by exp(beta * z), escapes untouched", {
## the mean MDC of the toy year is 130 = the fit's centre: no change
expect_equal(probVals(toyIgRun(withYearClimate(twoFolds(), toyClimate)))[, "ignitionProb"], c(0, 3, 3, 0))
## 20 drier: z = 1
dry <- twoFolds(covs = toyIgCovariates(MDC = c(100, 120, 140, 160) + 20))
sim <- toyIgRun(withYearClimate(dry, toyClimate))
expect_equal(probVals(sim)[, "ignitionProb"], c(0, 3, 3, 0) * exp(0.5))
expect_equal(probVals(sim)[, "escapeProb"], rep(1, 4))
## beta 0 is no term
sim0 <- toyIgRun(withYearClimate(dry, modifyList(toyClimate, list(beta = 0))))
expect_equal(probVals(sim0)[, "ignitionProb"], c(0, 3, 3, 0))
})