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_dataPrepFit 1.3.0.9011 (development version)

* `spreadCovariates` takes two more values, `"climate"` (the intercept and the spread climate variable(s) only) and `"fuel"` (the intercept, `youngAge`, fuel and non-forest classes), beside `"all"` (default) and `"none"`, for an ANOVA of what drives spread-fit quality. The covariate tables are still made. `spreadInteractions = "climate:fuel"` stops with `"fuel"` and `"climate"` (it needs both). The default is unchanged.
* `coreFractionByYear()` and `addCoreFraction()` moved to fireSenseUtils (exported there, with their tests), so `fireSense_dataPrepPredict` gives the covariates it predicts the same `coreFraction`; this module calls `fireSenseUtils::addCoreFraction()`, with the same results. `prepIgnitionFitData` keys on `addCoreFraction()`, `coreFractionByYear()` and `inELFCore()`, so its cached result is recomputed once. Needs fireSenseUtils (>= 0.2.3.9183).
* `fitFileSuffix` is removed: an experiment names its ledger file in full (`spreadFitFilename`). With `"latest"`, the SpreadFit ledger is read with the run's escape definition (`fireSenseUtils::readSpreadFitLedger(escapeDefinition =)`), so a stored fit made under another, or with another fuel scale, is not used. Needs fireSenseUtils >= 0.2.3.9179.
* New parameter `.plots` (default `"screen"`) for the historical fire maps of the fit data. It was used (`anyPlotting(Par$.plots)`) but not declared, so it was `NULL` (no plots) unless `.globals` set it. `NA` or `NULL` for none.
Expand Down
35 changes: 23 additions & 12 deletions R/spreadFormula.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ spreadFormula <- function(RHS, intercept = FALSE) {
paste0("~ ", if (isTRUE(intercept)) "1" else "0", " + ", RHS)
}

## the values `spreadCovariates` takes: "all" (the default), "none" (the null model), and "fuel" or "climate" (one group of
## "all"'s covariates, for an ANOVA of what drives spread-fit quality)
spreadCovariatesValues <- c("all", "none", "fuel", "climate")

## the one value `spreadInteractions` takes: each spread climate variable x each fuel covariate
spreadInteractionClimateFuel <- "climate:fuel"

Expand All @@ -27,31 +31,37 @@ spreadInteractionClimateFuel <- "climate:fuel"
#' climate first, after all the main effects. Without interactions the string is what it always was, so the cache keys
#' that hold it do not move.
#'
#' @param spreadCovariates the module's `spreadCovariates`: `"none"` gives `character()`, the null model.
#' @param spreadCovariates the module's `spreadCovariates`: `"none"` gives `character()`, the null model; `"climate"`
#' keeps the spread climate variable(s) only; `"fuel"` keeps everything else (`youngAge`, fuel, non-forest: the
#' vegetation state), so each of the two is a subset of `"all"`'s terms.
#' @param spreadVars character; the spread climate variable(s).
#' @param vegColsForRHS character; the other covariates (not `youngAge` or the climate variables).
#' @param spreadInteractions the module's `spreadInteractions`; `NULL` or `character(0)` for none.
#' @return a character string, or `character()` for the null model.
spreadRHS <- function(spreadCovariates, spreadVars, vegColsForRHS, spreadInteractions = character()) {
if (identical(spreadCovariates, "none")) return(character())
fuel <- if (spreadInteractionClimateFuel %in% spreadInteractions) fireSenseUtils::spreadFuelCols(vegColsForRHS)
paste(c(paste0(spreadVars, collapse = " + "), fireSenseUtils::youngAgeTxt,
paste0(vegColsForRHS, collapse = " + "),
climateTerms <- if (!identical(spreadCovariates, "fuel")) paste0(spreadVars, collapse = " + ")
fuelTerms <- if (!identical(spreadCovariates, "climate"))
c(fireSenseUtils::youngAgeTxt, paste0(vegColsForRHS, collapse = " + "))
paste(c(climateTerms, fuelTerms,
paste(rep(spreadVars, each = length(fuel)), rep(fuel, times = length(spreadVars)), sep = ":")),
collapse = " + ")
}

#' Check the `spreadCovariates` parameter
#'
#' `"all"` (the formula has the covariates) or `"none"` (the null model, `~ 1`), which has to have an
#' intercept: without one it would have no coefficient at all.
#' `"all"` (the formula has the covariates), `"none"` (the null model, `~ 1`), `"climate"` (the climate variable(s)
#' only) or `"fuel"` (`youngAge`, fuel and non-forest only). `"none"` has to have an intercept: without one it would
#' have no coefficient at all.
#'
#' @param spreadCovariates the module's `spreadCovariates`.
#' @param spreadIntercept the module's `spreadIntercept`.
#' @return `NULL`, invisibly; stops on a value that is not allowed.
checkSpreadCovariates <- function(spreadCovariates, spreadIntercept) {
if (!is.character(spreadCovariates) || length(spreadCovariates) != 1L || !spreadCovariates %in% c("all", "none"))
stop("fireSense_dataPrepFit: parameter 'spreadCovariates' must be \"all\" or \"none\"; got: ",
if (!is.character(spreadCovariates) || length(spreadCovariates) != 1L || !spreadCovariates %in% spreadCovariatesValues)
stop("fireSense_dataPrepFit: parameter 'spreadCovariates' must be one of ",
paste0("\"", spreadCovariatesValues, "\"", collapse = ", "), "; got: ",
paste(format(spreadCovariates), collapse = ", "))
if (identical(spreadCovariates, "none") && !isTRUE(spreadIntercept))
stop("fireSense_dataPrepFit: spreadCovariates = \"none\" fits the formula `~ 1`, which needs an intercept: ",
Expand All @@ -62,8 +72,8 @@ checkSpreadCovariates <- function(spreadCovariates, spreadIntercept) {
#' Check the `spreadInteractions` parameter
#'
#' `character(0)` (none; `NULL` too) or `"climate:fuel"` (an interaction of each spread climate variable with each fuel
#' covariate). Interactions are terms of the covariates, so they need covariates: `spreadCovariates = "none"` (the
#' null model) has none.
#' covariate). Interactions are terms of the covariates, so they need both climate and fuel: `spreadCovariates = "none"` (the
#' null model), `"fuel"` and `"climate"` lack one or both.
#'
#' @param spreadInteractions the module's `spreadInteractions`.
#' @param spreadCovariates the module's `spreadCovariates`.
Expand All @@ -72,8 +82,9 @@ checkSpreadInteractions <- function(spreadInteractions, spreadCovariates) {
if (length(spreadInteractions) && (!is.character(spreadInteractions) || !all(spreadInteractions %in% spreadInteractionClimateFuel)))
stop("fireSense_dataPrepFit: parameter 'spreadInteractions' must be character(0) or \"", spreadInteractionClimateFuel, "\"; got: ",
paste(format(spreadInteractions), collapse = ", "))
if (length(spreadInteractions) && identical(spreadCovariates, "none"))
stop("fireSense_dataPrepFit: spreadInteractions = \"", spreadInteractionClimateFuel, "\" needs covariates: ",
"the null model (spreadCovariates = \"none\") has none")
if (length(spreadInteractions) && !identical(spreadCovariates, "all"))
stop("fireSense_dataPrepFit: spreadInteractions = \"", spreadInteractionClimateFuel, "\" needs covariates (both climate and fuel): ",
"spreadCovariates = \"", spreadCovariates, "\" lacks ",
switch(spreadCovariates, none = "both (the null model)", fuel = "climate", climate = "fuel"))
invisible(NULL)
}
10 changes: 7 additions & 3 deletions fireSense_dataPrepFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ defineModule(sim, list(
defineParameter("spreadCovariates", "character", "all", NA, NA,
paste("`\"all\"` (the default): the spread formula has the covariates (climate, `youngAge`, fuel,",
"non-forest). `\"none\"`: the null model, the formula `~ 1` (an intercept and no covariate), the",
"baseline that a spread fit with covariates has to beat. The covariate tables are still made, so",
"everything downstream finds its inputs. Needs `spreadIntercept = TRUE`. Ignored if",
"baseline that a spread fit with covariates has to beat. `\"climate\"`: the intercept and the spread climate",
"variable(s) only. `\"fuel\"`: the intercept and everything else `\"all\"` has: `youngAge`, fuel biomass classes and",
"non-forest classes (vegetation state goes with fuel). With `\"none\"`, `\"climate\"` and `\"fuel\"` (arms of an ANOVA",
"of spread-fit quality) the covariate tables are still made, so",
"everything downstream finds its inputs. `\"fuel\"` has no climate covariate: `fireSense_spreadFit` stops with",
"`spreadLink = \"climateOutside\"` or `spreadClimate = \"activation\"`. Needs `spreadIntercept = TRUE`. Ignored if",
"`sim$fireSense_spreadFormula` is supplied. A hindcast of a null fit sets it too (`.globals`).")),
defineParameter("spreadInteractions", "character", character(0), NA, NA,
paste("Interaction terms of the spread formula. `character(0)` (the default): none, and the formula",
Expand All @@ -192,7 +196,7 @@ defineModule(sim, list(
"(bounded symmetrically, no sign constraint). Each is the product of the two covariates after each is",
"rescaled to 0-1 and centred (0 where `youngAge` cleared the fuel), so each main effect is the slope at the mean of the other covariate,",
"(`fireSenseUtils::spreadProbFromIntegerCovs()`), in the fit and in `fireSense_spreadPredict`.",
"Needs covariates (not `spreadCovariates = \"none\"`). Ignored if `sim$fireSense_spreadFormula` is supplied.",
"Needs climate and fuel covariates (`spreadCovariates = \"all\"`). Ignored if `sim$fireSense_spreadFormula` is supplied.",
"Needs a `fireSense_spreadFit` and a `fireSense_spreadPredict` that know interactions.")),
defineParameter("spreadIntercept", "logical", TRUE, NA, NA,
paste("If `TRUE`, the spread formula has an intercept (`~ 1 + ...`) and `fireSense_spreadFit` fits",
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-spreadIntercept.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,32 @@ test_that("prepare_SpreadFit() with spreadCovariates 'none' sets the formula '~
expect_identical(formulaFor("all", FALSE), "~ 0 + CMD + youngAge + fuelA") # byte-identical, as before
expect_identical(formulaFor("all", TRUE), "~ 1 + CMD + youngAge + fuelA")
})

## `spreadCovariates` "climate" and "fuel": the two halves of "all", for an ANOVA of spread-fit quality
test_that("spreadCovariates 'climate' and 'fuel' split the terms of 'all': climate vs youngAge, fuel and non-forest", {
veg <- c("dom_agb_Pice_mar", "dom_agb_Pinu_con", "nfLCC_1_2")
expect_identical(spreadRHS("all", "CMD", veg), "CMD + youngAge + dom_agb_Pice_mar + dom_agb_Pinu_con + nfLCC_1_2")
expect_identical(spreadRHS("climate", "CMD", veg), "CMD")
expect_identical(spreadRHS("fuel", "CMD", veg), "youngAge + dom_agb_Pice_mar + dom_agb_Pinu_con + nfLCC_1_2")
expect_identical(spreadRHS("climate", c("CMD", "Tmax"), veg), "CMD + Tmax")
expect_identical(spreadFormula(spreadRHS("climate", "CMD", veg), TRUE), "~ 1 + CMD")
expect_identical(spreadFormula(spreadRHS("fuel", "CMD", veg), TRUE),
"~ 1 + youngAge + dom_agb_Pice_mar + dom_agb_Pinu_con + nfLCC_1_2")
expect_identical(spreadFormula(spreadRHS("none", "CMD", veg), TRUE), "~ 1")
expect_null(checkSpreadCovariates("fuel", TRUE))
expect_null(checkSpreadCovariates("climate", FALSE))
expect_error(checkSpreadCovariates("climate ", TRUE), "must be one of")
})

test_that("prepare_SpreadFit() sets the formula of each spreadCovariates value", {
expect_identical(formulaFor("climate", TRUE), "~ 1 + CMD")
expect_identical(formulaFor("fuel", TRUE), "~ 1 + youngAge + fuelA")
expect_identical(formulaFor("fuel", FALSE), "~ 0 + youngAge + fuelA")
})

test_that("spreadInteractions 'climate:fuel' stops with 'fuel' and 'climate', which lack one of the two", {
expect_error(checkSpreadInteractions("climate:fuel", "fuel"), "needs covariates.*lacks climate")
expect_error(checkSpreadInteractions("climate:fuel", "climate"), "needs covariates.*lacks fuel")
expect_error(checkSpreadInteractions("climate:fuel", "none"), "needs covariates")
expect_null(checkSpreadInteractions(character(0), "fuel"))
})
Loading