Skip to content

Commit

Permalink
don't sort models (#23)
Browse files Browse the repository at this point in the history
* don't sort models

* Increment version number to 0.1.2.9000

* add news item

* update snapshot
  • Loading branch information
sbfnk authored Nov 6, 2024
1 parent fb21dcc commit 04095da
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: qrensemble
Title: Forecast ensembles using Quantile Regression Average (QRA)
Version: 0.1.2
Version: 0.1.2.9000
Authors@R:
c(person(given = "Sebastian",
family = "Funk",
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# qrensemble (development version)

- fixed a bug in model labelling
- fixed a bug where the `group` argument could not be empty

# qrensemble 0.1.2

- The package was renamed to qrensemble to avoid a conflict with the existing qra package on CRAN
Expand Down
11 changes: 8 additions & 3 deletions R/qra.r
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ qra_create_ensemble <- function(x, target, per_quantile_weights, intercept,
## create return tibbles
wtb <- CJ(
model = unique(x$model),
quantile = unique(x$quantile_level)
quantile = unique(x$quantile_level),
sorted = FALSE
)[, weight := weights]

itb <- data.table(
Expand Down Expand Up @@ -221,8 +222,12 @@ qra <- function(forecast, target, group = c(),
forecast <- as.data.table(forecast)
forecast <- forecast[!is.na(predicted)]

## first, split by group
ensemble <- split(forecast, by = group)
if (length(group) > 1) {
## first, split by group
ensemble <- split(forecast, by = group)
} else {
ensemble <- list(forecast)
}
## next, re-convert to forecast format
ensemble <- map(ensemble, as_forecast_quantile, forecast_unit = forecast_unit)
## next, split off target forecasts and check for completeness
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-qra.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ test_that("qra works", {
)
expect_snapshot(res)
})

test_that("qra produces sensible results in trivial cases", {
forecasts <- scoringutils::as_forecast_quantile(data.frame(
model = rep(c("model2", "model1"), each = 2),
observed = rep(1, 4),
predicted = rep(c(0, 1), each = 2),
quantile_level = rep(0.5, 4),
time = rep(c(0, 1), time = 2)
))
res <- qra(
forecasts,
target = c(time = 1)
)
expect_equal(attr(res, "weights")[model == "model1"]$weight, 1)
})

0 comments on commit 04095da

Please sign in to comment.