library(zoo); library (forecast); library (magrittr); library (knitr); library(dplyr); library(ggplot2);
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
ttdata <- read.csv("/Users/normanreitter/RCoding/Trouble Ticket Counts.csv", header=TRUE, sep=",", stringsAsFactors = default.stringsAsFactors())
# ttdata %>% kable()
Using zoo library
tt.ts <- ts(ttdata$Trouble.Ticket...Coun, start = c(ttdata$Period [1],1), freq=12)
# tt.ts %>% kable()
tt.ts.lm <- tslm(tt.ts ~ trend +I(trend^2))
tt.ts.lm
##
## Call:
## tslm(formula = tt.ts ~ trend + I(trend^2))
##
## Coefficients:
## (Intercept) trend I(trend^2)
## 4689.9764 3.2078 -0.2436
Letting R choose the best model
ttets.best <- ets(tt.ts, restrict = FALSE, allow.multiplicative.trend = TRUE)
ttets.best
## ETS(M,N,M)
##
## Call:
## ets(y = tt.ts, restrict = FALSE, allow.multiplicative.trend = TRUE)
##
## Smoothing parameters:
## alpha = 0.3673
## gamma = 1e-04
##
## Initial states:
## l = 4675.7565
## s=0.8535 0.8953 1.0017 1.038 1.1609 1.2034
## 1.09 1.0089 0.9347 0.9585 0.8554 0.9997
##
## sigma: 0.0861
##
## AIC AICc BIC
## 1091.088 1100.688 1123.933
ttets.prediction <- forecast(ttets.best, h=12, level = c(0.2, 0.4, 0.6, 0.8))
plot(forecast(ttets.best))
plot(ttets.best)
saveRDS(ttets.prediction, "ttets.prediction")
tt.r <- readRDS("ttets.prediction")
tt.r %>% lapply(class)
## $model
## [1] "ets"
##
## $mean
## [1] "ts"
##
## $level
## [1] "numeric"
##
## $x
## [1] "ts"
##
## $upper
## [1] "mts" "ts" "matrix"
##
## $lower
## [1] "mts" "ts" "matrix"
##
## $fitted
## [1] "ts"
##
## $method
## [1] "character"
##
## $series
## [1] "character"
##
## $residuals
## [1] "ts"
# we want to save out $mean, $upper, $lower vectors from forecast class
Calculated “centered” and “trailing” moving averages over the time series
ma.tt.ts.trailing <- rollmean(tt.ts, k=12, alight = "right")
ma.tt.ts <- ma(tt.ts, order = 12)
Writing to three files - one for predicted values, one for centered moving average, a third for trailing moving average.
Note: I did not combine these files as the rows are different lengths
write.table(tt.r,file = "ttpredictionout.txt", append = FALSE, quote = FALSE, sep = " ", na = "NA", row.names = FALSE)
write.table(ma.tt.ts, file = "ttmacentered.txt", append = FALSE, quote = FALSE, sep = " ", na = "NA", row.names = FALSE)
write.table(ma.tt.ts.trailing, file = "ttmatrailing.txt", append = FALSE, quote = FALSE, sep = " ", na = "NA", row.names = FALSE)
Reads in a file and produces and output file with estimated forecast values over “h” time periods
library(zoo); library (forecast); library (magrittr); library (knitr); library(dplyr); library(ggplot2);
ttdata <- read.csv("/Users/normanreitter/RCoding/Trouble Ticket Counts.csv", header=TRUE, sep=",", stringsAsFactors = default.stringsAsFactors())
tt.ts <- ts(ttdata$Trouble.Ticket...Coun, start = c(ttdata$Period [1],1), freq=12)
ttets.best <- ets(tt.ts, restrict = FALSE, allow.multiplicative.trend = TRUE)
ttets.prediction <- forecast(ttets.best, h=12, level = c(0.2, 0.4, 0.6, 0.8))
ma.tt.ts.trailing <- rollmean(tt.ts, k=12, alight = "right")
ma.tt.ts <- ma(tt.ts, order = 12)
write.table(ttets.prediction,file = "ttpredictionout1.txt", append = FALSE, quote = FALSE, sep = " ", na = "NA", row.names = FALSE)
write.table(ma.tt.ts, file = "ttmacentered.txt", append = FALSE, quote = FALSE, sep = " ", na = "NA", row.names = FALSE)
write.table(ma.tt.ts.trailing, file = "ttmatrailing.txt", append = FALSE, quote = FALSE, sep = " ", na = "NA", row.names = FALSE)