library(data.table) library(magrittr) library(stringr) library(yardstick) # library(hts) library(ggplot2) library(forecast) library(ggplot2) library(forecast) library(stringr)
reg_random_string <- rpois(1,1000) experiment_random_string <- rpois(1,1000) reg_random_string <- rpois(1,1000) experiment_random_string <- rpois(1,1000) PROJECT_NAME <- str_glue("Forecasting Examples {reg_random_string}") EXPERIMENT_NAME <- str_glue("Forecasting Example {experiment_random_string}") WORKSPACE <- "Stefan_Petrov" # library(vertaReticulateClient) HOST = "dev.verta.ai" Sys.setenv(VERTA_EMAIL= "...", VERTA_DEV_KEY = "...") library(vertaReticulateClient) init_verta(HOST)
proj <- set_verta_project(name=PROJECT_NAME) experiment <- set_verta_experiment(name=EXPERIMENT_NAME)
We'll use the WWWusage dataset by Durbin and Koopman, containing the number of users.
run <- set_experiment_run(name=EXPERIMENT_NAME,desc = "Forecasting Experiment 2") # train-test WWWusageTrain <- WWWusage[1:floor(length(WWWusage)/2+0.01)] WWWusageTest <- WWWusage[(length(WWWusageTrain)+1):length(WWWusage)] # we'll create an auto-arima model WWWusageFit <- auto.arima(WWWusageTrain)
log_run_hyperparameters(run,hyperparams = list(dataset_name ="WWWusage", seasonality = NULL, trainSetLength = length(WWWusageTrain) ),overwrite = T) # accuracy(WWWusageFit) # run_log_metric(run,key = "rsquared",value = rsq) autoplot(forecast(WWWusageFit,h=20))
The verta deployment object requires the model object to have a
predict
method attached. The forecast
method is not completely
appropriate if we would like like to give them model a new chunk of data
and predict the next period without re-fitting.
To that end, we use a 2-step process:
WWWusageFit <- list(model=WWWusageFit) class(WWWusageFit) <- "newForecaster"
# setup a predict.Arima object # this will return the expectation for the first period after. Adjust as needed to predict multiple steps. # by convention, the deployed verta prediction endpoints return a single value, but you can return multiple values, the full data frame with the quantiles required, etc # the predict method assumes we get a data frame, so # the data we send to this method will be a dataframe, # ordered by date, with the 'value' column as value predict.newForecaster <- function(mdl,newdata,colname="value"){ if(is(newdata,"data.frame")){ newdata= newdata[[colname]] } modI <- mdl[["model"]] # print(class(modI)) # print(modI) mod <- Arima(y=newdata,model = modI) fct_obj <- forecast(mod) # print("class of forecasting object") # print(class(fct_obj)) as.data.frame(fct_obj)[1,1] }
We can call the predict method with some random data to verify:
ll <- predict(WWWusageFit,newdata=data.frame(value=rnorm(10))) ll
Additional functions we would like to wrap, besides the model object, should be kept in a separate file.
NB- later fold this into whatever we upload
save(list=c("predict.newForecaster"),file = "additional_objects.RData")
save_model_data_2(file_to_save_at = "fctArtifact.RData", modFile = WWWusageFit, formula_obj = "mpg~.", original_df = WWWusageTrain, required_packages = c("forecast"), additional_objects = list("predict.newForecaster"=predict.newForecaster) )
first_model_object <- list( model_file = WWWusageFit, formula_obj = "mpg~.", original_df = WWWusageTrain, required_packages=c("forecast"), additional_objects=list( "predict.newForecaster"=predict.newForecaster ) ) saveRDS(first_model_object,"WWWUsage_object.RData")
# run$log_model(model = "WWWUsage_object.RData",overwrite = T)# # TODO- add# run_log run$log_model(model = "fctArtifact.RData",overwrite = T)# # TODO- add# run_log
run$download_model("./usage_object.RData") # run$download_model("./dl_model/") downloaded_model <- readRDS("usage_object.RData")# this downloaded_model # this is what it should be
createDockerContextZip( "usage_object.RData", required_packages = "forecast" #, #additional_objects_file_location="additional_objects.RData" )
buildDocker(location = "docker_folder_GAOEI2889F/",tag = "verta-forecast:latest")
Test with
POST http://localhost:8000/score Content-Type: application/json [{"value":1},{"value":2},{"value":3},{"value":4},{"value":5},{"value":6},{"value":7},{"value":8}]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.