Skip to content

Commit

Permalink
added univariate fable model
Browse files Browse the repository at this point in the history
  • Loading branch information
kvelleby committed Nov 13, 2024
1 parent 459ee22 commit c5f448a
Show file tree
Hide file tree
Showing 44 changed files with 615 additions and 1,218 deletions.
42 changes: 35 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
# Generated by roxygen2: do not edit by hand

export(DeterministicModel)
export(EndogenousSimulator)
export(EndogenousSystem)
export(ExogenModel)
export(LinearRegressionModel)
export(Model)
export(StochasticStaticModel)
S3method(predict,deterministic)
S3method(predict,exogen)
S3method(predict,linear)
S3method(predict,parametric_distribution)
S3method(predict,univariate_fable)
export(bootstraplm)
export(build_model)
export(create_distribution_object)
export(create_panel_frame)
export(deterministicmodel)
export(dt_ls)
export(exogenmodel)
export(fit_parametric_distribution_model)
export(get_accuracy)
export(get_execution_order)
export(get_independent_models)
export(get_sepi)
export(getpi)
export(linearmodel)
export(new_endogenmodel)
export(parametric_distribution_model)
export(parse_formula)
export(plotsim)
export(prepare_simulation_data)
export(process_dependent_models)
export(process_independent_models)
export(pt_ls)
export(qt_ls)
export(rt_ls)
export(select_col_per_row)
export(setup_simulator)
export(sim_to_dist)
export(simulate_endogenr)
export(univariate_fable_model)
export(update_dependency_graph)
importFrom(data.table,":=")
importFrom(data.table,.BY)
importFrom(data.table,.GRP)
Expand Down
2 changes: 1 addition & 1 deletion R/systemsim.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ simulate_endogenr <- function(nsim, simulator_setup, parallel = FALSE, ncores =
horizon = simulator_setup$horizon,
execution_order = simulator_setup$execution_order,
future.seed = TRUE,
future.packages = "dplyr")
future.packages = c("dplyr", "endogenr"))
future::plan(old_plan)
} else{
simulation_results <- lapply(1:nsim, simulate,
Expand Down
58 changes: 58 additions & 0 deletions R/univariate_fable_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' Univariate model from fable
#'
#' @param formula
#' @param data
#' @param ...
#'
#' @return
#' @export
#'
#' @examples
univariate_fable_model <- function(formula = NULL, data = NULL, ...){
model <- new_endogenmodel(formula)

model$fitted <- df |> fabletools::model(fable::ARIMA(formula))


class(model) <- c("univariate_fable", class(model))
model$independent <- TRUE

outcome <- parse_formula(model)$outcome
return(model)
}

#' Predict univariate fable model
#'
#' @param model
#' @param horizon
#' @param test_start
#' @param data
#'
#' @return
#' @export
#'
#' @examples
predict.univariate_fable <- function(model, horizon, test_start, data){
# Get index and key variables from tsibble
idx <- tsibble::index_var(data)
grp <- tsibble::key_vars(data)

forecast <- model$fitted |> fabletools::forecast(h = paste(horizon, "years")) |>
dplyr::mutate(sample = distributional::generate(!!rlang::sym(model$outcome), 1) |> unlist()) |>
dplyr::select(dplyr::all_of(c(grp, idx, "sample"))) |>
tsibble::tsibble(key = grp, index = grp)

# Create prediction data frame with only necessary columns
pred_data <- data |>
dplyr::filter(!!rlang::sym(idx) >= test_start) |>
dplyr::select(
!!!rlang::syms(grp),
!!rlang::sym(idx),
!!rlang::sym(model$outcome)
) |>
tsibble::tsibble(key = grp, index = grp)

pred_data <- dplyr::left_join(pred_data, forecast)

return(pred_data)
}
123 changes: 10 additions & 113 deletions man/DeterministicModel.Rd

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

Loading

0 comments on commit c5f448a

Please sign in to comment.