Guest User

Untitled

a guest
Jun 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. choose_model <- function(x,h,end_train,start_test){
  2. library(forecast)
  3. library(forecastHybrid)
  4. library(tidyverse)
  5.  
  6. #train data
  7.  
  8. x_train <- window(x, end = end_train )
  9.  
  10. x_test <- window(x, start = start_test)
  11.  
  12. h1=length(x_test)
  13.  
  14. #model1
  15.  
  16. stlf(x_train,method="arima",s.window= 12, h=h1)-> fc_stlf
  17.  
  18. #model2
  19. auto.arima(x_train, stepwise = FALSE, approximation = FALSE)%>%forecast(h=h1) -> fc_arima
  20.  
  21. #model3
  22. set.seed(12345)#for nnetar model
  23. nnetar(x_train)%>%forecast(h=h1) -> fc_nnetar
  24.  
  25. #model4
  26. snaive(x_train,h=h)->fc_snaive
  27.  
  28. #model5
  29. hybridModel(x_train,models = "anst",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE)%>%forecast(h=h1) -> fc_hy
  30.  
  31. #model6
  32. hybridModel(x_train,models = "an",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE)%>%forecast(h=h1) -> fc_hy_2
  33.  
  34. #model7
  35.  
  36. ets(x_train)%>%forecast(h=h1)->fc_ets
  37.  
  38. #model8
  39.  
  40. ses(x_train, h=h1)->fc_ses
  41.  
  42. #model9
  43.  
  44. holt(x_train, h=h1)->fc_holt
  45.  
  46. #model10
  47.  
  48. hw(x_train,seasonal = "additive", h=h1)->fc_hw_ad
  49.  
  50. #model11
  51.  
  52. hw(x_train,seasonal = "multiplicative", h=h1)->fc_hw_mul
  53.  
  54. #model12
  55.  
  56. hw(x_train,seasonal = "additive",damped = TRUE, h=h1)->fc_hw_ad_dam
  57.  
  58.  
  59. #model13
  60. hw(x_train,seasonal = "multiplicative",damped = TRUE, h=h1)->fc_hw_mul_dam
  61.  
  62. #accuracy
  63.  
  64.  
  65. model1 <- accuracy(fc_stlf$mean,x_test)[2]
  66.  
  67. model2 <- accuracy(fc_arima$mean,x_test)[2]
  68.  
  69. model3 <- accuracy(fc_nnetar$mean,x_test)[2]
  70.  
  71. model4 <- accuracy(fc_snaive$mean,x_test)[2]
  72.  
  73. model5 <- accuracy(fc_hy$mean,x_test)[2]
  74.  
  75. model6 <- accuracy(fc_hy_2$mean,x_test)[2]
  76.  
  77. model7 <- accuracy(fc_ets$mean,x_test)[2]
  78.  
  79. model8 <- accuracy(fc_ses$mean,x_test)[2]
  80.  
  81. model9 <- accuracy(fc_holt$mean,x_test)[2]
  82.  
  83. model10 <- accuracy(fc_hw_ad$mean,x_test)[2]
  84.  
  85. model11 <- accuracy(fc_hw_mul$mean,x_test)[2]
  86.  
  87. model12 <- accuracy(fc_hw_ad_dam$mean,x_test)[2]
  88.  
  89. model13 <- accuracy(fc_hw_mul_dam$mean,x_test)[2]
  90.  
  91. best_model <- min(c(model1,model2,model3,model4,model5,model6,model7,model8,model9,model10,model11,model12,model13))
  92.  
  93.  
  94. if(best_model==model1){
  95.  
  96. return(stlf(x,method="arima",s.window= 20, h=h)$mean)
  97.  
  98. }
  99. if(best_model==model2){
  100.  
  101. return(forecast(auto.arima(x, stepwise = FALSE, approximation = FALSE),h=h )$mean)
  102.  
  103.  
  104. }
  105. if(best_model==model3){
  106.  
  107. set.seed(12345)
  108. return(forecast(nnetar(x),h=h)$mean)
  109.  
  110.  
  111. }
  112. if(best_model==model4){
  113.  
  114. return(snaive(x,h=h)$mean)
  115.  
  116. }
  117.  
  118. if(best_model==model5){
  119.  
  120. return(forecast(hybridModel(x,models = "anst",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE ),h=h)$mean)
  121.  
  122. }
  123.  
  124. if(best_model==model6){
  125.  
  126. return(forecast(hybridModel(x,models = "an",weights = c("equal"),errorMethod = c("RMSE", "MAE", "MASE"),verbose=FALSE),h=h)$mean)
  127.  
  128. }
  129.  
  130. if(best_model==model7){
  131.  
  132. return(forecast(ets(x),h=h)$mean)
  133.  
  134. }
  135.  
  136. if(best_model==model8){
  137. return(ses(x,h=h)$mean)
  138. }
  139. if(best_model==model9){
  140. return(holt(x, h=h)$mean)
  141. }
  142.  
  143. if(best_model==model10){
  144. return(hw(x,seasonal = "additive", h=h)$mean)
  145. }
  146. if(best_model==model11){
  147. return(hw(x,seasonal = "multiplicative", h=h)$mean)
  148. }
  149. if(best_model==model12){
  150. return(hw(x,seasonal = "additive",damped = TRUE, h=h)$mean)
  151. }
  152. if(best_model==model13){
  153. return(hw(x,seasonal = "multiplicative",damped = TRUE, h=h)$mean)
  154. }
  155. }
  156.  
  157.  
  158. choose_model_monthly(my_data,7,c(2018,01),c(2018,02))
Add Comment
Please, Sign In to add comment