diff --git a/R/cache_name.R b/R/cache_name.R index bdffef06a549afd4898a2de0cc8c9c5f39ac2930..f3dd52288ce06b037daeb842bb5410bb417f70f1 100644 --- a/R/cache_name.R +++ b/R/cache_name.R @@ -83,13 +83,16 @@ cache_name <- function(..., cachedir = "cache"){ # Get the name, definition and arguments of the function from which cache_name was called funname <- strsplit(deparse(sys.calls()[[sys.nframe()-1]]), "\\(")[[1]][1] # Find the function in the nearest environment in the stack (i.e. parent calls) - for(i in rev(sys.parents())){ - if(funname %in% ls(parent.frame(i+1))){ - val <- mget(funname, parent.frame(i+1)) - break - } - } - fundef <- digest::digest(attr(eval(val[[funname]]), "srcref")) + ## for(i in rev(sys.parents())){ + ## browser() + ## if(funname %in% ls(parent.frame(i+1))){ + ## val <- mget(funname, parent.frame(i+1)) + ## break + ## } + ## } + ## fundef <- digest::digest(attr(eval(val[[funname]]), "srcref")) + # Somehow the above stopped working, don't know why! just take it, this should do the same I guess + fundef <- digest::digest(get(funname)) # if no arguments were given, then use the arguments function from which cache_name was called if(length(list(...)) == 0){ funargs <- digest::digest(as.list( match.call(definition = sys.function( -1 ), call = sys.call(-1)))[-1]) diff --git a/R/lm_optim.R b/R/lm_optim.R index a49cdb6337f69173af7beba04d963e04542392f4..2c6bd686541cb148113291a2e31f95690b43b4e3 100644 --- a/R/lm_optim.R +++ b/R/lm_optim.R @@ -15,7 +15,7 @@ #' @param kseq The horizons to fit for (if not set, then model$kseq is used) #' @param scorefun The function to be score used for calculating the score to be optimized. #' @param cachedir A character specifying the path (and prefix) of the cache file name. If set to \code{""}, then no cache will be loaded or written. See \url{https://onlineforecasting.org/vignettes/nice-tricks.html} for examples. -#' @param cacheload A logical controlling whether to load the cache if it exists. +#' @param cachererun A logical controlling whether to run the optimization even if the cache exists. #' @param printout A logical determining if the score function is printed out in each iteration of the optimization. #' @param method The method argument for \code{\link{optim}}. #' @param ... Additional parameters to \code{\link{optim}} @@ -57,7 +57,7 @@ #' #' @importFrom stats optim #' @export -lm_optim <- function(model, data, kseq = NA, scorefun = rmse, cachedir="", cacheload=FALSE, printout=TRUE, method="L-BFGS-B", ...){ +lm_optim <- function(model, data, kseq = NA, scorefun = rmse, cachedir="", cachererun=FALSE, printout=TRUE, method="L-BFGS-B", ...){ ## Take the parameters bounds from the parameter bounds set in the model init <- model$get_prmbounds("init") lower <- model$get_prmbounds("lower") @@ -81,7 +81,7 @@ lm_optim <- function(model, data, kseq = NA, scorefun = rmse, cachedir="", cache cnm <- cache_name(lm_fit, lm_optim, m$outputrange, m$regprm, m$transform_data(data), data[[m$output]], scorefun, init, lower, upper, cachedir = cachedir) # Load the cached result if it exists - if(file.exists(cnm) & !cacheload){ + if(file.exists(cnm) & !cachererun){ res <- readRDS(cnm) # Set the optimized parameters into the model model$insert_prm(res$par) diff --git a/R/rls_optim.R b/R/rls_optim.R index c01cc778eae0dad634bcc0337752e92c63e75fb5..7c60e1daf98d5cb8d4834d9c91368f0066a680e1 100644 --- a/R/rls_optim.R +++ b/R/rls_optim.R @@ -13,7 +13,7 @@ #' \code{cachedir} argument (relative to the current working directory). #' E.g. \code{rls_optim(model, D, cachedir="cache")} will write a file in the folder 'cache', such that #' next time the same call is carried out, then the file is read instead of running the optimization again. -#' See the example in url{https://onlineforecasting.org/vignettes/nice-tricks.html}. +#' See the example in \url{https://onlineforecasting.org/vignettes/nice-tricks.html}. #' #' @title Optimize parameters for onlineforecast model fitted with RLS #' @param model The onlineforecast model, including inputs, output, kseq, p @@ -21,7 +21,7 @@ #' @param kseq The horizons to fit for (if not set, then model$kseq is used) #' @param scorefun The function to be score used for calculating the score to be optimized. #' @param cachedir A character specifying the path (and prefix) of the cache file name. If set to \code{""}, then no cache will be loaded or written. See \url{https://onlineforecasting.org/vignettes/nice-tricks.html} for examples. -#' @param cacheload A logical controlling whether to load the cache if it exists. +#' @param cachererun A logical controlling whether to run the optimization even if the cache exists. #' @param printout A logical determining if the score function is printed out in each iteration of the optimization. #' @param method The method argument for \code{\link{optim}}. #' @param ... Additional parameters to \code{\link{optim}} @@ -61,7 +61,7 @@ #' #' #' @export -rls_optim <- function(model, data, kseq = NA, scorefun = rmse, cachedir="", cacheload=FALSE, printout=TRUE, method="L-BFGS-B", ...){ +rls_optim <- function(model, data, kseq = NA, scorefun = rmse, cachedir="", cachererun=FALSE, printout=TRUE, method="L-BFGS-B", ...){ # Take the parameters bounds from the parameter bounds set in the model init <- model$get_prmbounds("init") lower <- model$get_prmbounds("lower") @@ -86,7 +86,7 @@ rls_optim <- function(model, data, kseq = NA, scorefun = rmse, cachedir="", cach # This is maybe smarter, don't have to calculate the transformation of the data: cnm <- cache_name(m$regprm, getse(m$inputs, nms="expr"), m$output, m$prmbounds, m$kseq, data, objfun, init, lower, upper, cachedir = cachedir) cnm <- cache_name(rls_fit, rls_optim, m$outputrange, m$regprm, m$transform_data(data), data[[m$output]], scorefun, init, lower, upper, kseq, cachedir = cachedir) # Load the cached result if it exists - if(file.exists(cnm) & !cacheload){ + if(file.exists(cnm) & !cachererun){ res <- readRDS(cnm) # Set the optimized parameters into the model model$insert_prm(res$par) diff --git a/make.R b/make.R index c49a0f463b7e418cfb68f49944d2246c266375b5..70cc8b3c56bf3aaf8fdf5846758e0f2018511a50 100644 --- a/make.R +++ b/make.R @@ -60,7 +60,7 @@ library(roxygen2) # ---------------------------------------------------------------- # Build the package document() -build(".", vignettes=TRUE) +build(".", vignettes=FALSE) # Install it install.packages("../onlineforecast_0.9.4.tar.gz")