Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. fsscore<-function(model, numPeriodsToForecast) {
  2. if (numPeriodsToForecast<=0)
  3. {
  4. print("ERROR: forecast doesn't have any unknow value as time attribute present in training data")
  5. return(data.frame(NA))
  6. }
  7. else
  8. {
  9.  
  10. forecastedData <- forecast(model, h=numPeriodsToForecast)
  11.  
  12. output <- data.frame(forecastperiod=seq(1:numPeriodsToForecast),forecast=as.numeric(forecastedData$mean)
  13.  
  14. )
  15. #print(fcst80)
  16. fcstLo<-data.frame(forecastedData$lower)
  17. fcstUp<-data.frame(forecastedData$upper)
  18. names(fcstLo)<-forecastedData$level
  19. names(fcstUp)<-forecastedData$level
  20.  
  21. output80 <- data.frame(
  22. lower80=as.numeric(fcstLo[["80"]]),
  23. upper80=as.numeric(fcstUp[["80"]]))
  24. output95 <- data.frame(
  25. lower95=as.numeric(fcstLo[["95"]]),
  26. upper95=as.numeric(fcstUp[["95"]]))
  27.  
  28. #return(list(output,data.frame(model.frame())))
  29. return(list(output,output80,output95, saveModel(model)))
  30. }
  31. }
  32. fstrain<-function(dataset1, freq, valcol, fcst)
  33. {
  34.  
  35. orig_names <- names(dataset1)
  36. seasonality<-freq
  37. datacol <- which((orig_names %in% valcol))
  38. if (length(datacol)>=2)
  39. {
  40. print("ERROR: please use a single column for forecasting")
  41. return(data.frame(NA))
  42. }
  43. #labels <- as.numeric(dataset1[,which((orig_names %in% valcol))[1]])
  44. labels <- as.numeric(dataset1[,datacol])
  45. timeseries <- ts(labels,frequency=seasonality)
  46.  
  47.  
  48. if(fcst=="arima"){
  49.  
  50. model <- auto.arima(timeseries)
  51.  
  52. }
  53. else if (fcst == "stl") {
  54. model <- stl(timeseries, s.window="periodic")
  55. }
  56. else if (fcst == "stl+arima") {
  57. model <- stlf(train.ts, method = "arima", s.window = "periodic")
  58.  
  59. }
  60. else {
  61. model <- ets(timeseries)
  62. }
  63.  
  64. return(model)
  65.  
  66.  
  67. }
  68.  
  69. saveModel<-function(model)
  70. {
  71. m1<-data.frame(payload = as.integer(serialize(model, connection = NULL)))
  72. #m2<-model.frame(model)
  73. #print(m2)
  74. return(m1)
  75. }
  76.  
  77. retrieveModel<-function(ml1)
  78. {
  79.  
  80. return(unserialize(as.raw(ml1$payload)))
  81. }
  82.  
  83. fs<-function(dataset1, valcol, numPeriodsToForecast, freq, fcst){
  84. library(forecast)
  85. model<-fstrain(dataset1, freq, valcol, fcst)
  86.  
  87. if (length(model)>1){
  88. #numPeriodsToForecast <- ceiling(max(dataset2$time)) - ceiling(max(dataset1$time))
  89. #numPeriodsToForecast <- max(numPeriodsToForecast, 0)
  90. #numPeriodsToForecast <- min(length(dataset2$time), numPeriodsToForecast)
  91. #dataset3 <- subset(dataset2$time, dataset2$time>max(dataset1$time))
  92. pred<-fsscore(model, numPeriodsToForecast)
  93. if (length(pred)<1){
  94. print("Error: Not Applicable predictions")
  95. return(pred)
  96. }
  97. return(pred)
  98. }
  99. else {
  100. print("Error: Not Applicable model")
  101. return(model)
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement