From 92a42cb54a260ce3f0014f639ed84a7606a8fc97 Mon Sep 17 00:00:00 2001 From: Jonathan Berrisch Date: Thu, 14 Dec 2023 17:21:10 +0100 Subject: [PATCH] Update online() post processing and class vignette --- NAMESPACE | 1 + NEWS.md | 1 + R/misc.R | 11 +++++++++++ R/online.R | 2 +- R/online_update.R | 2 +- inst/include/conline.h | 16 ++++++++++------ man/post_process_model.Rd | 20 ++++++++++++++++++++ src/conline_exports.cpp | 2 +- vignettes/class.Rmd | 28 +++++++++++++++++++++++++++- 9 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 man/post_process_model.Rd diff --git a/NAMESPACE b/NAMESPACE index 8e871c4..80e810b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ export(make_knots) export(online) export(oracle) export(penalty) +export(post_process_model) export(splines2_basis) export(tidy) import(Rcpp) diff --git a/NEWS.md b/NEWS.md index 5cb6a03..f3e0517 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,7 @@ profoc 1.3.0 * A new article on the usage of the `conline` C++ class was added. * Various functions are now exported to R to allow easier interaction with the `conline` C++ class. These functions are: `init_experts_list()`, `make_basis_mats` and `make_hat_mats` * The code of `online()` was simplified a bit by utilizing the new `init_experts_list()` function. +* Function `post_process_model()` was improved and is now exposed to be used in conjunction with the `conline` C++ class. * Move aggregation of timings from cppclock.R to clock.h. This make it faster, easier to maintain and simplifies the code (which will be used in python in the future as well). profoc 1.2.1 diff --git a/R/misc.R b/R/misc.R index b456b51..238ba94 100644 --- a/R/misc.R +++ b/R/misc.R @@ -105,6 +105,17 @@ val_or_def <- function(val, def) { } } + +#' Post Process Data from conline Class +#' +#' This function works in conjunction with the conline class. +#' After the main learning task, it takes the output of the +#' conline class and returns an object suitable for, visualization, +#' further, and deployment. +#' analysis. +#' @param model_instance An instance of conline. +#' @param names A named list with dimnames of `y` and `experts`. +#' @export post_process_model <- function(model_instance, names) { # Generate output model <- list( diff --git a/R/online.R b/R/online.R index 5f2fcd6..dee0daa 100644 --- a/R/online.R +++ b/R/online.R @@ -457,7 +457,7 @@ online <- function(y, experts, tau, model <- post_process_model(model_instance, names) - model_instance$teardown() + model_instance$get_times() rm(model_instance) return(model) diff --git a/R/online_update.R b/R/online_update.R index c6057ad..e26018a 100644 --- a/R/online_update.R +++ b/R/online_update.R @@ -74,7 +74,7 @@ update.online <- function(object, object <- post_process_model(model_instance, names = names) - model_instance$teardown() + model_instance$get_times() rm(model_instance) return(object) diff --git a/inst/include/conline.h b/inst/include/conline.h index ebcbfa8..f5fb8ba 100644 --- a/inst/include/conline.h +++ b/inst/include/conline.h @@ -25,10 +25,15 @@ class conline std::string method = "bewa"; std::map params; - std::map params_basis_pr; - std::map params_basis_mv; - std::map params_hat_pr; - std::map params_hat_mv; + std::map params_basis_pr = {{"", + arma::ones(0)}}; + std::map params_basis_mv = {{"", + arma::ones(0)}}; + std::map params_hat_pr = {{"", + arma::ones(0)}}; + std::map params_hat_mv = {{"", + arma::ones(0)}}; + double forget_past_performance = 0.0; bool allow_quantile_crossing = false; bool trace = true; @@ -104,8 +109,7 @@ class conline Rcpp::List &object, arma::mat &new_y, arma::field &new_experts); - Rcpp::List output(); - void teardown() + void get_times() { clock.stop(); }; diff --git a/man/post_process_model.Rd b/man/post_process_model.Rd new file mode 100644 index 0000000..8b2876f --- /dev/null +++ b/man/post_process_model.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/misc.R +\name{post_process_model} +\alias{post_process_model} +\title{Post Process Data from conline Class} +\usage{ +post_process_model(model_instance, names) +} +\arguments{ +\item{model_instance}{An instance of conline.} + +\item{names}{A named list with dimnames of \code{y} and \code{experts}.} +} +\description{ +This function works in conjunction with the conline class. +After the main learning task, it takes the output of the +conline class and returns an object suitable for, visualization, +further, and deployment. +analysis. +} diff --git a/src/conline_exports.cpp b/src/conline_exports.cpp index 57553ae..2cbb144 100644 --- a/src/conline_exports.cpp +++ b/src/conline_exports.cpp @@ -60,5 +60,5 @@ RCPP_MODULE(conlineEx) .method("getP", &conline::getP) .method("getK", &conline::getK) .method("getX", &conline::getX) - .method("teardown", &conline::teardown); + .method("get_times", &conline::get_times); } diff --git a/vignettes/class.Rmd b/vignettes/class.Rmd index 716e4b2..ba0abc0 100644 --- a/vignettes/class.Rmd +++ b/vignettes/class.Rmd @@ -155,5 +155,31 @@ The learning process fills the class objects. So we can inspect them using the ` head(model$weights[[T]][, , 1]) ``` -However, we can also use the post processing functions from `online()` to access the results. This will create output that is identical to the output of `online()`. +However, we can also use the post processing function of `online()` to access the results. This will create output that is identical to the output of `online()`: +```{r} +names <- list(y = dimnames(y)) +names$experts <- list( + 1:T, + paste("Marginal", 1:D), + tau, + paste("Expert", 1:N) +) + +output <- post_process_model(model, names) +``` + +We can now use `output` in `update()`, `plot()` and others. + +At this point, we do not need to keep the model in memory anymore. So we can delete it: + +```{r} +rm(model) +``` + +## Summary + +The C++ class `conline` allows you to gain fine grained control over the learning process. However, it is not as convenient as the `online()` wrapper. So you should only use it if you need to alter the default behavior. However, mixing up helper +functions from `online()` and the C++ class is possible. So you can compute your +combinations using the class interface while still being able to use `update()`, +`plot()` and others afterward. \ No newline at end of file