From 524c37a9dcf2aa7b690e7658ad0ecbd9bd18e6ee Mon Sep 17 00:00:00 2001 From: Mitchell Date: Fri, 10 Jul 2020 22:29:50 +1000 Subject: [PATCH] Added methods for "NULL" model Related: #234 --- NAMESPACE | 9 +++++++++ R/model_null.R | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index bdb33327..604332d4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -65,10 +65,12 @@ S3method(features,tbl_ts) S3method(features_all,tbl_ts) S3method(features_at,tbl_ts) S3method(features_if,tbl_ts) +S3method(fitted,"NULL") S3method(fitted,mdl_df) S3method(fitted,mdl_ts) S3method(fitted,model_combination) S3method(fitted,null_mdl) +S3method(forecast,"NULL") S3method(forecast,fbl_ts) S3method(forecast,lst_btmup_mdl) S3method(forecast,lst_mdl) @@ -83,6 +85,7 @@ S3method(format,lst_mdl) S3method(format,mdl_ts) S3method(fortify,fbl_ts) S3method(gather,mdl_df) +S3method(generate,"NULL") S3method(generate,mdl_df) S3method(generate,mdl_ts) S3method(generate,model_combination) @@ -91,6 +94,7 @@ S3method(get_frequencies,"NULL") S3method(get_frequencies,Period) S3method(get_frequencies,character) S3method(get_frequencies,numeric) +S3method(glance,"NULL") S3method(glance,mdl_df) S3method(glance,mdl_ts) S3method(glance,null_mdl) @@ -119,15 +123,18 @@ S3method(quantile,fcdist) S3method(rbind,dcmp_ts) S3method(rbind,fbl_ts) S3method(reconcile,mdl_df) +S3method(refit,"NULL") S3method(refit,lst_mdl) S3method(refit,mdl_df) S3method(refit,mdl_ts) S3method(refit,null_mdl) S3method(rep,fcdist) +S3method(report,"NULL") S3method(report,mdl_df) S3method(report,mdl_ts) S3method(report,model_combination) S3method(report,null_mdl) +S3method(residuals,"NULL") S3method(residuals,mdl_df) S3method(residuals,mdl_ts) S3method(residuals,null_mdl) @@ -139,12 +146,14 @@ S3method(response_vars,mdl_df) S3method(select,fbl_ts) S3method(select,grouped_fbl) S3method(select,mdl_df) +S3method(stream,"NULL") S3method(stream,lst_mdl) S3method(stream,mdl_df) S3method(stream,mdl_ts) S3method(stream,null_mdl) S3method(summarise,fbl_ts) S3method(summarise,grouped_fbl) +S3method(tidy,"NULL") S3method(tidy,mdl_df) S3method(tidy,mdl_ts) S3method(tidy,null_mdl) diff --git a/R/model_null.R b/R/model_null.R index 74295cb5..a89f6c21 100644 --- a/R/model_null.R +++ b/R/model_null.R @@ -25,7 +25,7 @@ null_model <- function(formula, ...){ is_null_model <- function(x){ if(is_model(x)) return(is_null_model(x[["fit"]])) if(inherits(x, "lst_mdl")) return(map_lgl(x, is_null_model)) - inherits(x, "null_mdl") + is.null(x) || inherits(x, "null_mdl") } #' @export @@ -34,51 +34,80 @@ forecast.null_mdl <- function(object, new_data, ...){ construct_fc(rep(NA_real_, h), rep(0, h), dist_unknown(h)) } +#' @export +forecast.NULL <- forecast.null_mdl + #' @export generate.null_mdl <- function(x, new_data, ...){ mutate(new_data, .sim = NA_real_) } +#' @export +generate.NULL <- generate.null_mdl #' @export stream.null_mdl <- function(object, new_data, ...){ object$n <- object$n + NROW(new_data) object } +#' @export +stream.NULL <- function(object, new_data, ...) { + NULL +} #' @export refit.null_mdl <- function(object, new_data, ...){ object$n <- NROW(new_data) object } +#' @export +refit.NULL <- function(object, new_data, ...) { + NULL +} #' @export residuals.null_mdl <- function(object, ...){ matrix(NA_real_, nrow = object$n, ncol = length(object$vars), dimnames = list(NULL, object$vars)) } +#' @export +residuals.NULL <- function(object, new_data, ...) { + NA_real_ +} #' @export fitted.null_mdl <- function(object, ...){ matrix(NA_real_, nrow = object$n, ncol = length(object$vars), dimnames = list(NULL, object$vars)) } +#' @export +fitted.NULL <- function(object, new_data, ...) { + NA_real_ +} #' @export glance.null_mdl <- function(x, ...){ tibble() } +#' @export +glance.NULL <- glance.null_mdl #' @export tidy.null_mdl <- function(x, ...){ tibble(term = character(), estimate = numeric()) } +#' @export +tidy.NULL <- tidy.null_mdl #' @export report.null_mdl <- function(object, ...){ cat("NULL model") } +#' @export +report.NULL <- report.null_mdl #' @export model_sum.null_mdl <- function(x){ "NULL model" -} \ No newline at end of file +} +#' @export +report.NULL <- report.null_mdl \ No newline at end of file