## ----setup, include=FALSE-----------------------------------------------------
library(httptest2)
.mockPaths("../tests/mocks")
start_vignette(dir = "../tests/mocks")

original_options <- options("NIXTLA_API_KEY"="dummy_api_key", digits=7)

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  fig.width = 7, 
  fig.height = 4
)

## -----------------------------------------------------------------------------
library(nixtlar)

## -----------------------------------------------------------------------------
df <- nixtlar::electricity

train_df <- df |> 
  dplyr::group_by(unique_id) |> 
  dplyr::slice(1:(dplyr::n()-24)) 

test_df <- df |> 
  dplyr::group_by(unique_id) |> 
  dplyr::slice_tail(n = 24) 

## -----------------------------------------------------------------------------
fc_zeroshot <- nixtlar::nixtla_client_forecast(train_df, h = 24)
fc_finetune <- nixtlar::nixtla_client_forecast(train_df, h = 24, finetune_steps = 100)  
fc_finetune_depth <- nixtlar::nixtla_client_forecast(train_df, h = 24, finetune_steps = 100, finetune_depth = 2) 

## -----------------------------------------------------------------------------
test_df$ds <- lubridate::ymd_hms(test_df$ds)

compute_mae <- function(forecast, test){
  res <- merge(test, forecast, by = c("unique_id", "ds")) |> 
    dplyr::mutate(abs_error = abs(y-TimeGPT))
  
  mae <- round(mean(res$abs_error), 2)
  return(mae)
}

print(paste0("MAE zero-shot model: ", compute_mae(test_df, fc_zeroshot)))
print(paste0("MAE fine-tune model: ", compute_mae(test_df, fc_finetune)))
print(paste0("MAE fine-tune model with depth: ", compute_mae(test_df, fc_finetune_depth)))

## ----include=FALSE------------------------------------------------------------
options(original_options)
end_vignette()

