From 33ed4ec000d426601818db66cf5b7c0f8e026989 Mon Sep 17 00:00:00 2001 From: Peder <pbac@dtu.dk> Date: Fri, 10 Jul 2020 10:28:08 +0200 Subject: [PATCH] Changed ones() to one() --- .Rbuildignore | 2 +- .gitignore | 12 +----------- R/bspline.R | 2 +- R/forecastmodel.R | 2 +- R/input_class.R | 2 +- R/lm_fit.R | 4 ++-- R/lm_optim.R | 2 +- R/lm_predict.R | 2 +- R/long_format.R | 2 +- R/ones.R | 12 ++++++------ R/plot_ts.R | 2 +- R/rls_fit.R | 2 +- R/rls_optim.R | 2 +- R/rls_predict.R | 2 +- R/rls_prm.R | 2 +- R/rls_summary.R | 2 +- misc-R/temp.R | 2 +- tests/testthat/test-rls-heat-load.R | 2 +- vignettes/forecast-evaluation.Rmd | 6 +++--- vignettes/make.R | 16 +++++++++------- vignettes/online-updating.Rmd | 6 +++--- vignettes/setup-and-use-model.Rmd | 24 ++++++++++++------------ vignettes/setup-data.Rmd | 4 ++-- 23 files changed, 53 insertions(+), 61 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 4a7a006..7a419b7 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -2,7 +2,7 @@ ^\.Rproj\.user$ ^make.R ^data/all -^vignettes/tmp-output +^tmp ^vignettes/make.R ^vignettes/shared-init.Rmd ^vignettes/cache diff --git a/.gitignore b/.gitignore index 64b396e..8c94d92 100644 --- a/.gitignore +++ b/.gitignore @@ -21,15 +21,5 @@ misc-R/*cache* vignettes/*cache* vignettes/*genfig* vignettes/*_files* -vignettes/tmp-output/ -vignettes/setup-data_cache/ -vignettes/solar-forecasting_cache-rls/ -vignettes/building-heat-load-forecasting_cache/ -vignettes/onlineforecasting_pdf_source/onlineforecasting\.tex - -vignettes/onlineforecasting_pdf_source/*cache* -vignettes/onlineforecasting_pdf_source/*genfig* -vignettes/onlineforecasting_pdf_source/onlineforecasting-tikzDictionary -vignettes/onlineforecasting_pdf_source/onlineforecasting.log -vignettes/onlineforecasting_pdf_source/onlineforecasting.pdf +tmp/ diff --git a/R/bspline.R b/R/bspline.R index 56eeb30..0fe03e3 100644 --- a/R/bspline.R +++ b/R/bspline.R @@ -17,7 +17,7 @@ #' #' @param X data.frame (as part of data.list) with horizons as columns named \code{kxx} (i.e. one for each horizon) #' @param Boundary.knots The value is NA: then the boundaries are set to the range of each horizons (columns in X). See \code{?splines::bs} -#' @param intercept Default value is TRUE: in an onlineforecast model there is no intercept per defauls (set by \code{ones()}. See \code{?splines::bs} +#' @param intercept Default value is TRUE: in an onlineforecast model there is no intercept per defauls (set by \code{one()}. See \code{?splines::bs} #' @param df See \code{?splines::bs} #' @param knots See \code{?splines::bs} #' @param degree See \code{?splines::bs} diff --git a/R/forecastmodel.R b/R/forecastmodel.R index e33b37e..562058a 100644 --- a/R/forecastmodel.R +++ b/R/forecastmodel.R @@ -133,7 +133,7 @@ forecastmodel <- R6::R6Class("forecastmodel", public = list( # Keep the prm self$prm <- prm - # Find if any opt parameters, first the ones with "__" hence for the inputs + # Find if any opt parameters, first the one with "__" hence for the inputs pinputs <- prm[grep("__",nams(prm))] # If none found for inputs, then the rest must be for regression if (length(pinputs) == 0 & length(prm) > 0) { diff --git a/R/input_class.R b/R/input_class.R index 3c54a81..3d86867 100644 --- a/R/input_class.R +++ b/R/input_class.R @@ -8,7 +8,7 @@ input_class <- R6::R6Class( state_L = list(), state_i = integer(1), ## The model in which it is included (reference to the R6 forecastmodel object), its needed here, - ## since transformation functions (like AR, ones) need to access information about the model (like kseq) + ## since transformation functions (like AR, one) need to access information about the model (like kseq) model = NA, ## methods diff --git a/R/lm_fit.R b/R/lm_fit.R index 5996426..33adff3 100644 --- a/R/lm_fit.R +++ b/R/lm_fit.R @@ -38,7 +38,7 @@ #' model <- forecastmodel$new() #' model$output <- "y" #' model$add_inputs(Ta = "Ta", -#' mu = "ones()") +#' mu = "one()") #' model$add_regprm("rls_prm(lambda=0.99)") #' #' # Before fitting the model, define which points to include in the evaluation of the score function @@ -79,7 +79,7 @@ lm_fit <- function(prm=NA, model, data, scorefun = NA, returnanalysis = TRUE, pr # - If scorefun is given, e.g. rmse() then the value of this is returned if(printout){ - # Should here actually only print the ones that were found and changed? + # Should here actually only print the one that were found and changed? cat("----------------\n") if(is.na(prm[1])){ cat("prm=NA, so current parameters are used.\n") diff --git a/R/lm_optim.R b/R/lm_optim.R index 2b7486a..ad6a796 100644 --- a/R/lm_optim.R +++ b/R/lm_optim.R @@ -28,7 +28,7 @@ #' # Define a simple model #' model <- forecastmodel$new() #' model$add_inputs(Ta = "lp(Ta, a1=0.9)", -#' mu = "ones()") +#' mu = "one()") #' # Before fitting the model, define which points to include in the evaluation of the score function #' D$scoreperiod <- in_range("2010-12-20", D$t) #' # And the sequence of horizons to fit for diff --git a/R/lm_predict.R b/R/lm_predict.R index 8e5531c..6cef48d 100644 --- a/R/lm_predict.R +++ b/R/lm_predict.R @@ -14,7 +14,7 @@ #' D$y <- D$heatload #' # Define a model #' model <- forecastmodel$new() -#' model$add_inputs(Ta = "lp(Ta, a1=0.7)", mu = "ones()") +#' model$add_inputs(Ta = "lp(Ta, a1=0.7)", mu = "one()") #' #' # Before fitting the model, define which points to include in the evaluation of the score function #' D$scoreperiod <- in_range("2010-12-20", D$t) diff --git a/R/long_format.R b/R/long_format.R index 838c50b..3cf381c 100644 --- a/R/long_format.R +++ b/R/long_format.R @@ -21,7 +21,7 @@ #' # Define a model #' model <- forecastmodel$new() #' model$add_inputs(Ta = "Ta", -#' mu = "ones()") +#' mu = "one()") #' model$add_regprm("rls_prm(lambda=0.99)") #' model$kseq <- 1:6 #' # Fit it diff --git a/R/ones.R b/R/ones.R index e079be5..b2353b0 100644 --- a/R/ones.R +++ b/R/ones.R @@ -2,7 +2,7 @@ #library(devtools) #document() #load_all(as.package("../../onlineforecast")) -#?ones +#?one #' Returns a data.frame of ones which can be used in forecast model inputs #' @@ -12,26 +12,26 @@ #' #' @title Create ones for model input intercept #' @return A data.frame of ones -#' @name ones +#' @name one #' @examples #' #' # A model #' model <- forecastmodel$new() #' # Use the function in the input definition -#' model$add_inputs(mu = "ones()") +#' model$add_inputs(mu = "one()") #' # Set the forecast horizons #' model$kseq <- 1:4 -#' # During the transformation stage the ones will be generated for the horizons +#' # During the transformation stage the one will be generated for the horizons #' model$transform_data(subset(Dbuilding, 1:7)) #' #' @export -ones <- function(){ +one <- function(){ # To find kseq, get the model (remember it is call per reference, so don't change it without cloning) model <- get("self", parent.env(parent.frame(4))) # Get the data to find the all the names with k in data data <- get("data", parent.env(parent.frame())) n <- length(data$t) - # Generate the matrix of ones and return it as a data.frame + # Generate the matrix of one and return it as a data.frame as.data.frame(matrix(1, nrow=n, ncol=length(model$kseq), dimnames=list(NULL, pst("k",model$kseq)))) } diff --git a/R/plot_ts.R b/R/plot_ts.R index c030cbc..97d059b 100644 --- a/R/plot_ts.R +++ b/R/plot_ts.R @@ -466,7 +466,7 @@ plot_ts_series <- function(data, pattern, iplot = 1, #' model <- forecastmodel$new() #' model$output = "heatload" #' model$add_inputs(Ta = "Ta", -#' mu = "ones()") +#' mu = "one()") #' model$add_regprm("rls_prm(lambda=0.9)") #' model$kseq <- c(3,18) #' fit1 <- rls_fit(NA, model, D, returnanalysis = TRUE) diff --git a/R/rls_fit.R b/R/rls_fit.R index 79805ed..ed9d62d 100644 --- a/R/rls_fit.R +++ b/R/rls_fit.R @@ -54,7 +54,7 @@ #' model <- forecastmodel$new() #' model$output <- "y" #' model$add_inputs(Ta = "Ta", -#' mu = "ones()") +#' mu = "one()") #' model$add_regprm("rls_prm(lambda=0.99)") #' #' # Before fitting the model, define which points to include in the evaluation of the score function diff --git a/R/rls_optim.R b/R/rls_optim.R index 09f2cec..f8ba8af 100644 --- a/R/rls_optim.R +++ b/R/rls_optim.R @@ -27,7 +27,7 @@ #' D$y <- D$heatload #' # Define a simple model #' model <- forecastmodel$new() -#' model$add_inputs(Ta = "Ta", mu = "ones()") +#' model$add_inputs(Ta = "Ta", mu = "one()") #' model$add_regprm("rls_prm(lambda=0.99)") #' #' # Before fitting the model, define which points to include in the evaluation of the score function diff --git a/R/rls_predict.R b/R/rls_predict.R index 4f3855d..4726cef 100644 --- a/R/rls_predict.R +++ b/R/rls_predict.R @@ -13,7 +13,7 @@ #' D$y <- D$heatload #' # Define a simple model #' model <- forecastmodel$new() -#' model$add_inputs(Ta = "Ta", mu = "ones()") +#' model$add_inputs(Ta = "Ta", mu = "one()") #' model$add_regprm("rls_prm(lambda=0.99)") #' #' # Before fitting the model, define which points to include in the evaluation of the score function diff --git a/R/rls_prm.R b/R/rls_prm.R index 389c58e..4c9347c 100644 --- a/R/rls_prm.R +++ b/R/rls_prm.R @@ -18,7 +18,7 @@ #' D$scoreperiod <- in_range("2010-12-20", D$t) #' # Define a simple model #' model <- forecastmodel$new() -#' model$add_inputs(Ta = "Ta", mu = "ones()") +#' model$add_inputs(Ta = "Ta", mu = "one()") #' model$kseq <- 1:6 #' #' # Here the expression which sets the parameters is defined diff --git a/R/rls_summary.R b/R/rls_summary.R index 65f81e5..414e4e1 100644 --- a/R/rls_summary.R +++ b/R/rls_summary.R @@ -47,7 +47,7 @@ #' # Define a model #' model <- forecastmodel$new() #' model$add_inputs(Ta = "Ta", -#' mu = "ones()") +#' mu = "one()") #' model$add_regprm("rls_prm(lambda=0.99)") #' model$kseq <- 1:6 #' # Fit it diff --git a/misc-R/temp.R b/misc-R/temp.R index e13dd69..4f0ba53 100644 --- a/misc-R/temp.R +++ b/misc-R/temp.R @@ -8,7 +8,7 @@ D <- subset(Dbuilding, c("2010-12-15", "2011-01-01")) D$y <- D$heatload # Define a model model <- forecastmodel$new() -model$add_inputs(Ta = "lp(Ta, a1=0.7)", mu = "ones()") +model$add_inputs(Ta = "lp(Ta, a1=0.7)", mu = "one()") # Before fitting the model, define which points to include in the evaluation of the score function D$scoreperiod <- in_range("2010-12-20", D$t) diff --git a/tests/testthat/test-rls-heat-load.R b/tests/testthat/test-rls-heat-load.R index fdf9835..054105f 100644 --- a/tests/testthat/test-rls-heat-load.R +++ b/tests/testthat/test-rls-heat-load.R @@ -23,7 +23,7 @@ test_that("run", { model$add_inputs(Ta = "lp(Ta, a1=0.9)", I = "lp(I, a1=0.7)", mu_tday = "fs(tday/24, nharmonics=10)", - mu = "ones()") + mu = "one()") model$add_regprm("rls_prm(lambda=0.9)") ## ------------------------------------------------------------------------ diff --git a/vignettes/forecast-evaluation.Rmd b/vignettes/forecast-evaluation.Rmd index b8483cd..33bf041 100644 --- a/vignettes/forecast-evaluation.Rmd +++ b/vignettes/forecast-evaluation.Rmd @@ -45,9 +45,9 @@ knitr::opts_chunk$set( comment = "## ", prompt = FALSE, cache = TRUE, - cache.path = paste0("tmp-output/tmp-",vignettename,"/"), + cache.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.align="center", - fig.path = paste0("tmp-output/tmp-",vignettename,"/"), + fig.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.height = figheight, fig.width = figwidth, out.width = "100%" @@ -166,7 +166,7 @@ model <- forecastmodel$new() model$output = "y" model$add_inputs(Ta = "lp(Ta, a1=0.9)", I = "lp(I, a1=0.9)", - mu = "ones()") + mu = "one()") model$add_prmbounds(Ta__a1 = c(0.8, 0.9, 0.99), I__a1 = c(0.6, 0.9, 0.99), lambda = c(0.9, 0.99, 0.9999)) diff --git a/vignettes/make.R b/vignettes/make.R index 62a89ce..698cef4 100644 --- a/vignettes/make.R +++ b/vignettes/make.R @@ -4,8 +4,11 @@ library(knitr) library(rmarkdown) # Put the files in this dir (ignored in the git) -dirnam <- "tmp-output/" +dirnam <- "../tmp/vignettes/" +dir.create("../tmp") dir.create(dirnam) +file.remove(dir("cache", full.names=TRUE)) +file.remove("cache") makeit <- function(nam, openit=FALSE, clean=TRUE){ namrmd <- paste0(nam,".Rmd") @@ -15,19 +18,18 @@ makeit <- function(nam, openit=FALSE, clean=TRUE){ if(openit){ system(paste0("chromium-browser ",dirnam,nam,".html &")) } } -file.remove(dir("tmp-output/tmp-setup-data/", full.names=TRUE)) +# +file.remove(dir(paste0(dirnam,"tmp-setup-data/"), full.names=TRUE)) makeit("setup-data", openit=FALSE) # -file.remove(dir("cache", full.names=TRUE)) -file.remove("cache") -file.remove(dir("tmp-output/tmp-setup-and-use-model/", full.names=TRUE)) +file.remove(dir(paste0(dirnam,"tmp-setup-and-use-model/"), full.names=TRUE)) makeit("setup-and-use-model", openit=FALSE, clean=TRUE) # -file.remove(dir("tmp-output/tmp-forecast-evaluation/", full.names=TRUE)) +file.remove(dir(paste0(dirnam,"tmp-output/tmp-forecast-evaluation/"), full.names=TRUE)) makeit("forecast-evaluation", openit=FALSE) # Finish and include it!! -## file.remove(dir("tmp-output/tmp-online-updating/", full.names=TRUE)) +## file.remove(dir(paste0(dirnam,"tmp-output/tmp-online-updating/"), full.names=TRUE)) ## makeit("online-updating", openit=FALSE) diff --git a/vignettes/online-updating.Rmd b/vignettes/online-updating.Rmd index 6011a3e..3f5a3f1 100644 --- a/vignettes/online-updating.Rmd +++ b/vignettes/online-updating.Rmd @@ -49,9 +49,9 @@ knitr::opts_chunk$set( comment = "## ", prompt = FALSE, cache = TRUE, - cache.path = paste0("tmp-output/tmp-",vignettename,"/"), + cache.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.align="center", - fig.path = paste0("tmp-output/tmp-",vignettename,"/"), + fig.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.height = figheight, fig.width = figwidth, out.width = "100%" @@ -110,7 +110,7 @@ D$trainperiod <- in_range(D$t[1], D$t, "2011-02-01") model <- forecastmodel$new() model$output = "heatload" model$add_inputs(Ta = "lp(Ta, a1=0.9)", - mu = "ones()") + mu = "one()") model$add_regprm("rls_prm(lambda=0.9)") model$add_prmbounds(Ta__a1 = c(0.5, 0.9, 0.9999), lambda = c(0.9, 0.99, 0.9999)) diff --git a/vignettes/setup-and-use-model.Rmd b/vignettes/setup-and-use-model.Rmd index 890123b..035a912 100644 --- a/vignettes/setup-and-use-model.Rmd +++ b/vignettes/setup-and-use-model.Rmd @@ -49,9 +49,9 @@ knitr::opts_chunk$set( comment = "## ", prompt = FALSE, cache = TRUE, - cache.path = paste0("tmp-output/tmp-",vignettename,"/"), + cache.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.align="center", - fig.path = paste0("tmp-output/tmp-",vignettename,"/"), + fig.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.height = figheight, fig.width = figwidth, out.width = "100%" @@ -142,7 +142,7 @@ model <- forecastmodel$new() model$output = "heatload" # Inputs (transformation step) model$add_inputs(Ta = "Ta", - mu = "ones()") + mu = "one()") # Regression step parameters model$add_regprm("rls_prm(lambda=0.9)") # Optimization bounds for parameters @@ -170,7 +170,7 @@ The model inputs are defined by: ```{r} # Inputs (transformation step) model$add_inputs(Ta = "Ta", - mu = "ones()") + mu = "one()") ``` So this is really where the structure of the model is specified. The inputs are given a name (`Ta` and `mu`), which each are set as an R expression (given as a @@ -279,11 +279,11 @@ legend("topright", c("Observations",pst("Predictions (",min(model$kseq)," to ",m ## Input transformations The inputs can be transformations of the variables in the data, i.e. `D` in this -example. The function `ones()` generate a forecast matrix of 1 for the needed +example. The function `one()` generate a forecast matrix of 1 for the needed horizons. It cannot be called directly: ```{r, eval=FALSE} # This will give error -ones() +one() ``` (the code above was not executed) @@ -293,18 +293,18 @@ however we can see the result of the evaluation by: datatr <- model$transform_data(D) # See what came out summary(datatr) -# In particular for the mu = "ones()" +# In particular for the mu = "one()" head(datatr$mu) ``` If we wanted to debug we could: ```{r, eval=FALSE} # Set to debug -#debug(ones) -# Run the input transformation now and it will stop in ones() +#debug(one) +# Run the input transformation now and it will stop in one() datatr <- model$transform_data(D) # Set to undebug -#undebug(ones) +#undebug(one) ``` (the code above was not executed). @@ -323,7 +323,7 @@ the new transformation: model <- forecastmodel$new() model$output = "heatload" model$add_inputs(Ta = "lp(Ta, a1=0.9)", - mu = "ones()") + mu = "one()") model$add_regprm("rls_prm(lambda=0.9)") model$add_prmbounds(Ta__a1 = c(0.5, 0.9, 0.9999), lambda = c(0.9, 0.99, 0.9999)) @@ -359,7 +359,7 @@ Ta forecast is really smoothed, which models a system with a time constant There are quite a few functions available for input transformations: -- `ones()` generates an matrix of ones (for including an intercept). +- `one()` generates an matrix of ones (for including an intercept). - `fs()` generate Fourier series for modelling harmonic functions. - `bspline()` wraps the `bs()` function for generating base splines. - `AR()` generates auto-regressive model inputs. diff --git a/vignettes/setup-data.Rmd b/vignettes/setup-data.Rmd index 3629605..c8950db 100644 --- a/vignettes/setup-data.Rmd +++ b/vignettes/setup-data.Rmd @@ -50,9 +50,9 @@ knitr::opts_chunk$set( comment = "## ", prompt = FALSE, cache = TRUE, - cache.path = paste0("tmp-output/tmp-",vignettename,"/"), + cache.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.align="center", - fig.path = paste0("tmp-output/tmp-",vignettename,"/"), + fig.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"), fig.height = figheight, fig.width = figwidth, out.width = "100%" -- GitLab