## ----output.lines=10---------------------------------------------------------------------------------------------- # Load the package and data library(onlineforecast) D <- subset(Dbuilding, c("2010-12-15", "2011-01-01")) D$y <- D$heatload # Define a simple model model <- forecastmodel$new() model$add_inputs(Ta = "Ta", mu = "one()") model$add_regprm("rls_prm(lambda=0.99)") # Period 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 model$kseq <- 1:6 # Add bounds for lambda (lower, init, upper) model$add_prmbounds(lambda = c(0.9, 0.98, 0.999)) ## ----eval=FALSE--------------------------------------------------------------------------------------------------- ## # Set debugging of the one() function ## debug(one) ## # Do the transformation, which will stop when entering one() ## model$transform_data(D) ## # Stop the debugging of one() ## undebug(one) ## ----message=FALSE------------------------------------------------------------------------------------------------ # The optimization optimization of lambda rls_optim(model, D)$par ## ----message=FALSE------------------------------------------------------------------------------------------------ tstart <- Sys.time() # Caching can be done by providing a path rls_optim(model, D, cachedir="cache")$par Sys.time() - tstart ## ----------------------------------------------------------------------------------------------------------------- # See the cache files dir("cache") ## ----message=FALSE------------------------------------------------------------------------------------------------ tstart <- Sys.time() # So running again the result is read from the file rls_optim(model, D, cachedir="cache")$par Sys.time() - tstart ## ----message=FALSE------------------------------------------------------------------------------------------------ # Change the lower bound for optimization model$add_prmbounds(lambda = c(0.89, 0.98, 0.999)) # New optimization results are calculated and cached val <- rls_optim(model, D, cachedir="cache") dir("cache") ## ----cache=FALSE-------------------------------------------------------------------------------------------------- # To delete the cache folder unlink("cache", recursive=TRUE) ## ----warning=FALSE------------------------------------------------------------------------------------------------ D <- Dbuilding # Use plotly library(plotly) # usely=TRUE L <- plot_ts(D, c("heatload","Ta"), kseq=c(1,24), usely=TRUE, xlab="Time", ylabs=c("Heat (kW)","Temperature (C)")) # or exact same L <- plotly_ts(D, c("heatload","Ta"), kseq=c(1,24), xlab="Time", ylabs=c("Heat (kW)","Temperature (C)")) # # From plotly the figures are returned and can be further manipulated # e.g. put the legend in the top by L[[length(L)]] <- L[[length(L)]] %>% layout(legend = list(x = 100, y = 0.98)) print(subplot(L, shareX=TRUE, nrows=length(L), titleY = TRUE)) #' \donttest{ #' D <- Dbuilding #' #' plotly_ts(D, c("heatload","Ta"), kseq=c(1,24)) #' plotly_ts(D, c("heatload","Ta$|Taobs$"), kseq=c(1,24)) #' } ## ----warning=FALSE------------------------------------------------------------------------------------------------ D$scoreperiod <- in_range("2010-12-22", D$t) model <- forecastmodel$new() model$output = "heatload" model$add_inputs(Ta = "Ta", mu = "one()") model$add_regprm("rls_prm(lambda=0.9)") model$kseq <- c(3,18) fit1 <- rls_fit(NA, model, D, returnanalysis = TRUE) # Plot it plot_ts(fit1) # Plot it with plotly plot_ts(fit1, usely=TRUE)