Skip to content
Closed
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_spreadPredict (development version)

- A fitted fuel covariate that this run's covariates lack (the species or fuel class is absent from the vegetation) is added as no biomass (`logMinB(0)`, as `fireSenseUtils::fuelCovsToCoarse()` fills it) before the covariates are rescaled, instead of stopping with "have no covariate". Climate, non-forest and other missing covariates still stop. Uses the new `fireSenseUtils::addAbsentSpeciesCols()` (`Depends-on: PredictiveEcology/fireSenseUtils`; set the `reqdPkgs` floor after it merges).
- A ledger row with an interaction coefficient (`a:b`, e.g. `CMD:dom_agb_Pice_mar`, from a `fireSense_spreadFit` fitted with `fireSense_dataPrepFit`'s `spreadInteractions = "climate:fuel"`) predicts with it: `spreadProbOneELF()` needs the covariates `a` and `b` (and their ranges in `covMinMax_spread`), and `fireSenseUtils::spreadProbFromIntegerCovs()` forms their product after each is rescaled and centred with the row's `covCentre_spread` entry for it (0 where `youngAge` cleared the fuel), and multiplies it by the coefficient, as the fit does. A row without interactions predicts exactly as before. Needs fireSenseUtils >= 0.2.3.9177 (#242).

- With `paramSets`: the cache of a parameter set's surface no longer reads all the values of `flammableRTM` and `rasterToMatchLargeELF` every replicate (a new raster object or a change at 1000 sampled cells is a change) and returns the stored surface without copying it (a changed stored surface is recomputed). On a 20-million-cell raster a replicate that reuses a surface takes 0.35 s instead of 0.9 s, most of it SpaDES. `paramSets` stops if the ledger rows have the simulated ELF twice, or if a file of ledger rows has none for it.
Expand Down
14 changes: 9 additions & 5 deletions fireSense_spreadPredict.R
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,15 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp
params[randomEffects] <- NULL
}

## the intercept, if the fit has one, is a coefficient with no covariate: it multiplies a column of 1s
hasIntercept <- fireSenseUtils::spreadInterceptTxt %in% names(params)
fitted <- setdiff(names(params), c(unlist(fireSenseUtils::logisticParamNames), fireSenseUtils::spreadInterceptTxt))
## an interaction (`CMD:fuel`) is made from its two covariates by spreadProbFromIntegerCovs(), as in the fit
needed <- fireSenseUtils::spreadSourceCols(fitted)
## a fuel class the vegetation lacks has no biomass: logMinB(0), as fireSenseUtils::fuelCovsToCoarse() fills
## it. Set here, with the other fuel columns, so a linear-fuel fit's log is undone for it too.
covs <- fireSenseUtils::addAbsentSpeciesCols(covs, needed, value = fireSenseUtils::logMinB(0))

## Fuel biomass arrives logged (fireSenseUtils::logMinB()). A fit made on LINEAR fuel biomass has
## fireSenseUtils::fuelLinearRange, c(0, 1e4), as that covariate's covMinMax_spread, and its
## coefficients only mean anything for biomass / 1e4: undo the log with the function the fit used.
Expand All @@ -521,11 +530,6 @@ spreadProbOneELF <- function(covs, params, covMinMax, yr, maxFireSpread, lowerSp

## every fitted coefficient needs a covariate: without one the logistic gets no covariates and
## fails later in rowMeans(), with no hint of the cause
## the intercept, if the fit has one, is a coefficient with no covariate: it multiplies a column of 1s
hasIntercept <- fireSenseUtils::spreadInterceptTxt %in% names(params)
fitted <- setdiff(names(params), c(unlist(fireSenseUtils::logisticParamNames), fireSenseUtils::spreadInterceptTxt))
## an interaction (`CMD:fuel`) is made from its two covariates by spreadProbFromIntegerCovs(), as in the fit
needed <- fireSenseUtils::spreadSourceCols(fitted)
noCov <- setdiff(needed, names(covs))
if (length(noCov)) {
stop(
Expand Down
11 changes: 9 additions & 2 deletions tests/testthat/test-spreadInteractions.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ test_that("with an intercept and centres, the interaction is the product of the
expect_equal(predVals(toyRun(ins))[c(1, 2, 3, 4, 6, 7, 8)], handLogistic3(x), tolerance = 1e-7)
})

test_that("a fitted interaction whose covariate is missing stops, naming it", {
test_that("a fitted interaction whose fuel covariate is absent is formed from no biomass; another missing covariate stops, naming it", {
covs <- toyCovariates()
withFuel <- data.table::copy(covs)
data.table::set(withFuel, j = "fuelA", value = fireSenseUtils::logMinB(rep(0, NROW(covs))))
data.table::set(covs, j = "fuelA", value = NULL)
ins <- insX()
ins$fireSense_SpreadCovariates <- covs
expect_error(toyRun(ins), "fuelA")
ref <- insX()
ref$fireSense_SpreadCovariates <- withFuel
expect_equal(suppressMessages(predVals(toyRun(ins))), predVals(toyRun(ref)))
data.table::set(covs, j = "youngAge", value = NULL)
ins$fireSense_SpreadCovariates <- covs
expect_error(toyRun(ins), "youngAge")
})

test_that("the prediction is the fit's spreadProb for the same covariates, with fuel cleared where youngAge is 1", {
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-youngAgeExclusivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,27 @@ test_that("a young pixel's covariates entering the link are youngAge = 1 and all
## x = 0.75 + 0 + 0.5 + 0 = 1.25
expect_equal(v[3], handLogistic3(1.25), tolerance = 1e-7)
})

test_that("a fuel class the vegetation lacks predicts as a fuel class with no biomass, not a 'no covariate' stop", {
withFuel <- toyCovariates()
withFuel$fuelA <- fireSenseUtils::logMinB(rep(0, NROW(withFuel))) # no biomass, as the covariates would have it
covs <- withFuel
covs$fuelA <- NULL # the species is not in the vegetation
formula <- "~ MDC + youngAge + fuelA - 1"
for (range in list(c(0, 1e4), c(0, 10))) { # linear-fuel fit, and a log-scale fit
run <- function(cv) {
ins <- toyInputs(covs = cv, params = toyParams(), formula = formula)
ins$covMinMax_spread$fuelA <- range
predVals(toyRun(ins))
}
expect_equal(suppressMessages(run(covs)), run(withFuel))
}
## a fuel class the fit never saw is a renamed class, not an absent species: the stop stays
other <- covs
other$fuelB <- fireSenseUtils::logMinB(rep(0, NROW(other)))
ins <- toyInputs(covs = other, params = toyParams(), formula = formula)
expect_error(toyRun(ins), "fuelA")
## a missing climate covariate is still an error
ins <- toyInputs(covs = covs, params = toyParams(CMDsp = 1.5), formula = "~ MDC + CMDsp - 1")
expect_error(toyRun(ins))
})
Loading