Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #981: Convert as_forecast_ to s3 #982

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Changes from 1 commit
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
Next Next commit
convert everywhere to s3
  • Loading branch information
seabbs committed Jan 13, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit f817fcff03f2a2359d71217763378a9f306cb94a
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
@@ -40,7 +44,6 @@ export(add_relative_skill)
export(ae_median_quantile)
export(ae_median_sample)
export(as_forecast_binary)
export(as_forecast_nominal)
export(as_forecast_ordinal)
export(as_forecast_point)
export(as_forecast_quantile)
22 changes: 15 additions & 7 deletions R/class-forecast-binary.R
Original file line number Diff line number Diff line change
@@ -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,
@@ -45,7 +54,6 @@ as_forecast_binary <- function(data,
return(data)
}


#' @export
#' @rdname assert_forecast
#' @importFrom cli cli_abort
33 changes: 22 additions & 11 deletions R/class-forecast-nominal.R
Original file line number Diff line number Diff line change
@@ -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
@@ -26,25 +27,35 @@
#'
#' 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
#' @export
#' @keywords as_forecast transform
#' @export4
#' @examples
#' as_forecast_nominal(
#' na.omit(example_nominal),
#' predicted = "predicted",
#' 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,
33 changes: 22 additions & 11 deletions R/class-forecast-ordinal.R
Original file line number Diff line number Diff line change
@@ -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
@@ -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,
4 changes: 3 additions & 1 deletion R/class-forecast-point.R
Original file line number Diff line number Diff line change
@@ -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.
#'
@@ -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,
4 changes: 3 additions & 1 deletion R/class-forecast-quantile.R
Original file line number Diff line number Diff line change
@@ -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.
@@ -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,
28 changes: 19 additions & 9 deletions R/class-forecast-sample.R
Original file line number Diff line number Diff line change
@@ -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.
@@ -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,
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
Loading