Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- choose_model <- function(x,h,end_train,start_test){
- library(forecast)
- library(forecastHybrid)
- library(tidyverse)
- #train data
- x_train <- window(x, end = end_train )
- x_test <- window(x, start = start_test)
- h1=length(x_test)
- #model1
- stlf(x_train,method="arima",s.window= 12, h=h1)-> fc_stlf
- #model2
- auto.arima(x_train, stepwise = FALSE, approximation = FALSE)%>%forecast(h=h1) -> fc_arima
- #model3
- set.seed(12345)#for nnetar model
- nnetar(x_train)%>%forecast(h=h1) -> fc_nnetar
- #model4
- snaive(x_train,h=h)->fc_snaive
- #model5
- hybridModel(x_train,models = "anst",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE)%>%forecast(h=h1) -> fc_hy
- #model6
- hybridModel(x_train,models = "an",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE)%>%forecast(h=h1) -> fc_hy_2
- #model7
- ets(x_train)%>%forecast(h=h1)->fc_ets
- #model8
- ses(x_train, h=h1)->fc_ses
- #model9
- holt(x_train, h=h1)->fc_holt
- #model10
- hw(x_train,seasonal = "additive", h=h1)->fc_hw_ad
- #model11
- hw(x_train,seasonal = "multiplicative", h=h1)->fc_hw_mul
- #model12
- hw(x_train,seasonal = "additive",damped = TRUE, h=h1)->fc_hw_ad_dam
- #model13
- hw(x_train,seasonal = "multiplicative",damped = TRUE, h=h1)->fc_hw_mul_dam
- #accuracy
- model1 <- accuracy(fc_stlf$mean,x_test)[2]
- model2 <- accuracy(fc_arima$mean,x_test)[2]
- model3 <- accuracy(fc_nnetar$mean,x_test)[2]
- model4 <- accuracy(fc_snaive$mean,x_test)[2]
- model5 <- accuracy(fc_hy$mean,x_test)[2]
- model6 <- accuracy(fc_hy_2$mean,x_test)[2]
- model7 <- accuracy(fc_ets$mean,x_test)[2]
- model8 <- accuracy(fc_ses$mean,x_test)[2]
- model9 <- accuracy(fc_holt$mean,x_test)[2]
- model10 <- accuracy(fc_hw_ad$mean,x_test)[2]
- model11 <- accuracy(fc_hw_mul$mean,x_test)[2]
- model12 <- accuracy(fc_hw_ad_dam$mean,x_test)[2]
- model13 <- accuracy(fc_hw_mul_dam$mean,x_test)[2]
- best_model <- min(c(model1,model2,model3,model4,model5,model6,model7,model8,model9,model10,model11,model12,model13))
- if(best_model==model1){
- return(stlf(x,method="arima",s.window= 20, h=h)$mean)
- }
- if(best_model==model2){
- return(forecast(auto.arima(x, stepwise = FALSE, approximation = FALSE),h=h )$mean)
- }
- if(best_model==model3){
- set.seed(12345)
- return(forecast(nnetar(x),h=h)$mean)
- }
- if(best_model==model4){
- return(snaive(x,h=h)$mean)
- }
- if(best_model==model5){
- return(forecast(hybridModel(x,models = "anst",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE ),h=h)$mean)
- }
- if(best_model==model6){
- return(forecast(hybridModel(x,models = "an",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE),h=h)$mean)
- }
- if(best_model==model7){
- return(forecast(ets(x),h=h)$mean)
- }
- if(best_model==model8){
- return(ses(x,h=h)$mean)
- }
- if(best_model==model9){
- return(holt(x, h=h)$mean)
- }
- if(best_model==model10){
- return(hw(x,seasonal = "additive", h=h)$mean)
- }
- if(best_model==model11){
- return(hw(x,seasonal = "multiplicative", h=h)$mean)
- }
- if(best_model==model12){
- return(hw(x,seasonal = "additive",damped = TRUE, h=h)$mean)
- }
- if(best_model==model13){
- return(hw(x,seasonal = "multiplicative",damped = TRUE, h=h)$mean)
- }
- }
- choose_model_monthly(my_data,7,c(2018,01),c(2018,02))
Add Comment
Please, Sign In to add comment