Skip to content

Commit

Permalink
Issue #981: Convert as_forecast_ to s3 (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
seabbs authored Jan 13, 2025
1 parent 1629541 commit dbe5da6
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 52 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ S3method("[",forecast)
S3method("[<-",forecast)
S3method("[[<-",forecast)
S3method(`[`,scores)
S3method(as_forecast_binary,default)
S3method(as_forecast_nominal,default)
S3method(as_forecast_ordinal,default)
S3method(as_forecast_point,default)
S3method(as_forecast_point,forecast_quantile)
S3method(as_forecast_quantile,default)
S3method(as_forecast_quantile,forecast_sample)
S3method(as_forecast_sample,default)
S3method(assert_forecast,default)
S3method(assert_forecast,forecast_binary)
S3method(assert_forecast,forecast_nominal)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Minor spelling / mathematical updates to Scoring rule vignette. (#969)
## Package updates

- A bug was fixed where `crps_sample()` could fail in edge cases.
- All `as_forecast_<type>()` functions now have S3 methods for converting from another forecast type to the respective forecast type.
- Implemented a new forecast class, `forecast_ordinal` with appropriate metrics. Ordinal forecasts are a form of categorical forecasts. The main difference between ordinal and nominal forecasts is that the outcome is ordered, rather than unordered.
- Refactored the way that columns get internally renamed in `as_forecast_<type>()` functions (#980)

Expand Down
22 changes: 15 additions & 7 deletions R/class-forecast-binary.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@
#'
#' See the [example_binary] data set for an example.
#' @inheritSection forecast_types Forecast unit
#' @param ... Unused
#' @returns A `forecast` object of class `forecast_binary`
#' @family functions to create forecast objects
#' @importFrom cli cli_warn
#' @keywords as_forecast
#' @export
#' @keywords as_forecast transform
#' @examples
#' as_forecast_binary(
#' example_binary,
#' predicted = "predicted",
#' forecast_unit = c("model", "target_type", "target_end_date",
#' "horizon", "location")
#' )
as_forecast_binary <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL) {
as_forecast_binary <- function(data, ...) {
UseMethod("as_forecast_binary")
}

#' @rdname as_forecast_binary
#' @export
#' @method as_forecast_binary default
#' @importFrom cli cli_warn
as_forecast_binary.default <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL,
...) {
data <- as_forecast_generic(
data,
forecast_unit,
Expand All @@ -45,7 +54,6 @@ as_forecast_binary <- function(data,
return(data)
}


#' @export
#' @rdname assert_forecast
#' @importFrom cli cli_abort
Expand Down
31 changes: 21 additions & 10 deletions R/class-forecast-nominal.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#'
#' # Required input
#'
#' The input needs to be a data.frame or similar with the following columns:
#' The input needs to be a data.frame or similar for the default method
#' with the following columns:
#' - `observed`: Column with observed values of type `factor` with N levels,
#' where N is the number of possible outcomes.
#' The levels of the factor represent the possible outcomes that
Expand All @@ -26,12 +27,10 @@
#'
#' See the [example_nominal] data set for an example.
#' @inheritSection forecast_types Forecast unit
#' @param predicted_label (optional) Name of the column in `data` that denotes
#' the outcome to which a predicted probability corresponds to.
#' This column will be renamed to "predicted_label".
#' @param ... Unused
#' @returns A `forecast` object of class `forecast_nominal`
#' @family functions to create forecast objects
#' @keywords as_forecast
#' @keywords as_forecast transform
#' @export
#' @examples
#' as_forecast_nominal(
Expand All @@ -40,11 +39,23 @@
#' forecast_unit = c("model", "target_type", "target_end_date",
#' "horizon", "location")
#' )
as_forecast_nominal <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL,
predicted_label = NULL) {
as_forecast_nominal <- function(data, ...) {
UseMethod("as_forecast_nominal")
}

#' @rdname as_forecast_nominal
#' @param predicted_label (optional) Name of the column in `data` that denotes
#' the outcome to which a predicted probability corresponds to.
#' This column will be renamed to "predicted_label".
#' @export
#' @method as_forecast_nominal default
#' @importFrom cli cli_warn
as_forecast_nominal.default <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL,
predicted_label = NULL,
...) {
data <- as_forecast_generic(
data,
forecast_unit,
Expand Down
33 changes: 22 additions & 11 deletions R/class-forecast-ordinal.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#'
#' # Required input
#'
#' The input needs to be a data.frame or similar with the following columns:
#' The input needs to be a data.frame or similar for the default method
#' with the following columns:
#' - `observed`: Column with observed values of type `factor` with N ordered
#' levels, where N is the number of possible outcomes.
#' The levels of the factor represent the possible outcomes that
Expand All @@ -26,25 +27,35 @@
#'
#' See the [example_ordinal] data set for an example.
#' @inheritSection forecast_types Forecast unit
#' @param predicted_label (optional) Name of the column in `data` that denotes
#' the outcome to which a predicted probability corresponds to.
#' This column will be renamed to "predicted_label".
#' @returns A `forecast` object of class `forecast_ordinal`
#' @param ... Unused
#' @family functions to create forecast objects
#' @keywords as_forecast
#' @returns A `forecast` object of class `forecast_ordinal`
#' @export
#' @keywords as_forecast transform
#' @examples
#' as_forecast_ordinal(
#' na.omit(example_ordinal),
#' predicted = "predicted",
#' forecast_unit = c("model", "target_type", "target_end_date",
#' "horizon", "location")
#' )
as_forecast_ordinal <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL,
predicted_label = NULL) {
as_forecast_ordinal <- function(data, ...) {
UseMethod("as_forecast_ordinal")
}

#' @rdname as_forecast_ordinal
#' @param predicted_label (optional) Name of the column in `data` that denotes
#' the outcome to which a predicted probability corresponds to.
#' This column will be renamed to "predicted_label".
#' @export
#' @method as_forecast_ordinal default
#' @importFrom cli cli_warn
as_forecast_ordinal.default <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL,
predicted_label = NULL,
...) {
data <- as_forecast_generic(
data,
forecast_unit,
Expand Down
4 changes: 3 additions & 1 deletion R/class-forecast-point.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#' @details
#' # Required input
#'
#' The input needs to be a data.frame or similar with the following columns:
#' The input needs to be a data.frame or similar for the default method
#' with the following columns:
#' - `observed`: Column of type `numeric` with observed values.
#' - `predicted`: Column of type `numeric` with predicted values.
#'
Expand All @@ -24,6 +25,7 @@ as_forecast_point <- function(data, ...) {

#' @rdname as_forecast_point
#' @export
#' @method as_forecast_point default
#' @importFrom cli cli_warn
as_forecast_point.default <- function(data,
forecast_unit = NULL,
Expand Down
4 changes: 3 additions & 1 deletion R/class-forecast-quantile.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#' @details
#' # Required input
#'
#' The input needs to be a data.frame or similar with the following columns:
#' The input needs to be a data.frame or similar for the default method
#' with the following columns:
#' - `observed`: Column of type `numeric` with observed values.
#' - `predicted`: Column of type `numeric` with predicted values. Predicted
#' values represent quantiles of the predictive distribution.
Expand Down Expand Up @@ -39,6 +40,7 @@ as_forecast_quantile <- function(data, ...) {
#' the quantile level of the predicted values. This column will be renamed to
#' "quantile_level". Only applicable to quantile-based forecasts.
#' @export
#' @method as_forecast_quantile default
#' @importFrom cli cli_warn
as_forecast_quantile.default <- function(data,
forecast_unit = NULL,
Expand Down
28 changes: 19 additions & 9 deletions R/class-forecast-sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#' @details
#' # Required input
#'
#' The input needs to be a data.frame or similar with the following columns:
#' The input needs to be a data.frame or similar for the default method
#' with the following columns:
#' - `observed`: Column of type `numeric` with observed values.
#' - `predicted`: Column of type `numeric` with predicted values. Predicted
#' values represent random samples from the predictive distribution.
Expand All @@ -17,18 +18,27 @@
#' See the [example_sample_continuous] and [example_sample_discrete] data set
#' for an example
#' @inheritSection forecast_types Forecast unit
#' @param ... Unused
#' @family functions to create forecast objects
#' @returns A `forecast` object of class `forecast_sample`
#' @export
#' @keywords as_forecast transform
as_forecast_sample <- function(data, ...) {
UseMethod("as_forecast_sample")
}


#' @rdname as_forecast_sample
#' @param sample_id (optional) Name of the column in `data` that contains the
#' sample id. This column will be renamed to "sample_id".
#' @export
#' @returns A `forecast` object of class `forecast_sample`
#' @family functions to create forecast objects
#' @importFrom cli cli_warn
#' @keywords as_forecast
as_forecast_sample <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL,
sample_id = NULL) {
as_forecast_sample.default <- function(data,
forecast_unit = NULL,
observed = NULL,
predicted = NULL,
sample_id = NULL,
...) {
data <- as_forecast_generic(
data,
forecast_unit,
Expand Down
11 changes: 9 additions & 2 deletions man/as_forecast_binary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions man/as_forecast_nominal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions man/as_forecast_ordinal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/as_forecast_point.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dbe5da6

Please sign in to comment.