I have been trying to use the Box cox transformation inside an ARIMA model(Dynamic harmonic regression with Fourier terms).
But for some reason, I'm getting the below error
"Error in stop_vctrs()
:
! Can't recycle input of size 2 to size 36."
Could you please advise what im doing wrong in here?
I have first stored the lambda in the lambda_comp object using guerrero to automatically select lambda and used that within box_cox function from fable package inside the ARIMA model.
#Conditionally install packages
cond.install <- function(package.name){
options(repos = "http://cran.rstudio.com") #set repo
#check for package in library, if package is missing install
if(package.name%in%rownames(installed.packages())==FALSE) {
install.packages(package.name)}else{require(package.name, character.only = TRUE)}}
cond.install_vec <- Vectorize(cond.install, vectorize.args = "package.name")
##CONDITIONALLY INSTALL AND CALL REQUIRED LIBRARIES -------------------------------------------------------------------
packages<- c("dplyr","tsibble","fpp3","tidyverse","data.table",
"lubridate","openxlsx","tibble","norm","tseries",
"astsa","ISOweek","vars","ggfortify","stringr","xlsx")
cond.install_vec(packages)
lapply(packages, require, character.only = TRUE)
#Input data
Comp_1.df<-structure(list(Material = c("100251001", "100251001", "100251001",
"100251001", "100251001", "100251001", "100251001", "100251001",
"100251001", "100251001", "100251001", "100251001", "100251001",
"100251001", "100251001", "100251001", "100251001", "100251001",
"100251001", "100251001", "100251001", "100251001", "100251001",
"100251001", "100251001", "100251001", "100251001", "100251001",
"100251001", "100251001", "100251001", "100251001", "100251001",
"100251001", "100251001", "100251001", "1007291", "1007291",
"1007291", "1007291", "1007291", "1007291", "1007291", "1007291",
"1007291", "1007291", "1007291", "1007291", "1007291", "1007291",
"1007291", "1007291", "1007291", "1007291", "1007291", "1007291",
"1007291", "1007291", "1007291", "1007291", "1007291", "1007291",
"1007291", "1007291", "1007291", "1007291", "1007291", "1007291",
"1007291", "1007291", "1007291", "1007291"), Month = structure(c(18140,
18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414,
18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687,
18718, 18748, 18779, 18809, 18840, 18871, 18901, 18932, 18962,
18993, 19024, 19052, 19083, 19113, 19144, 19174, 19205, 18140,
18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414,
18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687,
18718, 18748, 18779, 18809, 18840, 18871, 18901, 18932, 18962,
18993, 19024, 19052, 19083, 19113, 19144, 19174, 19205), class = c("yearmonth",
"vctrs_vctr")), Value = c(966, 968, 656, 658, 1124, 1305, 929,
603, 886, 657, 1011, 942, 921, 1125, 902, 464, 893, 1374, 1534,
1778, 1746, 1851, 1965, 2386, 2501, 2255, 1864, 2026, 1900, 2006,
2143, 1515, 1860, 1805, 1712, 1722, 3, 2, 3, 4, 5, 5, 7, 8, 10,
7, 13, 6, 4, 11, 9, 15, 19, 10, 7, 12, 17, 1, 14, 10, 25, 9,
12, 3, 15, 12, 7, 2, 3, 11, 6, 10)), class = c("tbl_ts", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -72L), key = structure(list(
Material = c("100251001", "1007291"), .rows = structure(list(
1:36, 37:72), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE), index = structure("Month", ordered = TRUE), index2 = "Month", interval = structure(list(
year = 0, quarter = 0, month = 1, week = 0, day = 0, hour = 0,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")))
#Data Pre-processing
Comp_1.df$Month<- as.Date(Comp_1.df$Month)
Comp_1.df$Month<- as.yearmon(Comp_1.df$Month, "%Y %m")
Comp_1.df$Month<- yearmonth(Comp_1.df$Month)
Comp_1.df <- Comp_1.df %>%
as_tsibble(key = Material,index = Month)
#Data modeling
#Dynamic regression with fourier terms
lambda_Comp<-Comp_1.df %>% features(Value,features = guerrero) %>% pull(lambda_guerrero)
bestfit.Comp_1.AICc <- Inf
for(K in seq(6)){
fit.Comp.1 <- Comp_1.df %>%
model(ARIMA(box_cox(Value, lambda_Comp) ~ fourier(K = K), stepwise = FALSE, approximation = FALSE))
if(purrr::pluck(glance(fit.Comp.1), "AICc") < bestfit.Comp_1.AICc)
{
bestfit.Comp_1.AICc <- purrr::pluck(glance(fit.Comp.1), "AICc")
bestfit.Comp.1<- fit.Comp.1
bestK.Comp.1 <- K
}
}
bestK.Comp.1
glance(bestfit.Comp.1)