---
title: "R Notebook"
output: html_notebook
---
```{r}
library(forecast)
library(ggplot2)
library(tidyverse)
ccd_df <- read.csv("CallCenterData.csv")
ggplot(data=ccd_df, aes(x=Time_Period, y = Calls)) +geom_line()+geom_point()
```
```{r}
ccd_ts <- ts(data = ccd_df$Calls, start=c(2010, head(ccd_df$Time_Period,1)), frequency = 4)
autoplot(ccd_ts)
```
```{r}
ccd_decomp <- decompose(ccd_ts)
ccd_decomp
autoplot(ccd_decomp)
```
```{r}
smooth <- ses(ccd_ts) #exponential smoothing not appropriate for trend,seasonality in data - straight line as projection
autoplot(smooth)
```
```{r}
ccd_holt <- holt(ccd_ts) # models the smoothing & the trend
autoplot(ccd_holt)
```
```{r}
ccd_ts <- ts(data = ccd_df$Calls, start=c(2010),frequency = 4)
ccd_hw <- hw(ccd_ts, initial="optimal")
autoplot(ccd_hw)
# from stats package another version of hw
ccd_hwf <- HoltWinters(ccd_ts)
forecast <- predict(ccd_hwf, n.ahead = 4, prediction.interval = T, level = 0.95)
autoplot(forecast)
```
```{r}
#using the predict function on fitted values to predict future values. n.ahead is the number of steps ahead for which prediction is required error bounds at 95% confidence level
arima (data, nonseasonal_desc, seasonal_desc)
?arima
fitP <- arima(ccd_ts, order=c(1,0,0), list(order=c(2,1,0), period=4))
#Creating a fit variable according to the data samples
fore <- predict(fitP, n.ahead=24)
Upper_b <- fore$pred + 2*fore$se #upper confidence level
Lower_b <- fore$pred - 2*fore$se #lower confidence level
ts.plot(ccd_ts, fore$pred, Upper_b, Lower_b, col=c(1,2,4,4), lty = c(1,1,2,2))
#plotting time series data with legend
legend("topleft", c("Actual", "Forecast", "Error Bounds .05"), col=c("black","red","blue"), lty=c(1,1,2))
#using the predict function on fitted values to predict future values. n.ahead is the number of steps ahead for which prediction is required error bounds at 95% confidence level
```
```{r}
acf(ccd_ts)
pacf(ccd_ts)
```
```{r try_forcast}
# example dataset for the forecast package
class(AirPassengers)
autoplot(AirPassengers)
```
```{r}
sections <- decompose(AirPassengers)
autoplot(sections)
```
Consider moving averages
Moving average estimates future values at time t by averaging values of the time series within k periods of t. This method works best when the data does not contain any trend or cyclic patterns. The arima() function (ARIthmetic Moving Average) of forecast package is used for predicting future values.
```{r}
library(tseries)
moving_ave <- ma(AirPassengers,order=2)
autoplot(moving_ave)
```
```{r try_forecast}
fore_simple <-forecast(AirPassengers)
### forecast default values forecast(object, h = ifelse(frequency(object) > 1, 2 * frequency(object), 10), level = c(80, 95), fan = FALSE, robust = FALSE, lambda = NULL, biasadj = FALSE, find.frequency = FALSE, allow.multiplicative.trend = FALSE, model = NULL, ...)
# default techniqe for time series is exponential smoothing ets
autoplot(fore_simple)
```
```{r}
acf(AirPassengers)
pacf(AirPassengers)
fore_trend <-forecast(AirPassengers,allow.multiplicative.trend = TRUE )
autoplot(fore_trend)
```
```{r}
#using the predict function on fitted values to predict future values. n.ahead is the number of steps ahead for which prediction is required error bounds at 95% confidence level
fitP <- arima(AirPassengers, order=c(1,0,0), list(order=c(2,1,0), period=12))
#Creating a fit variable according to the dataset
fore <- predict(fitP, n.ahead=24)
Upper_b <- fore$pred + 2*fore$se #upper confidence level
Lower_b <- fore$pred - 2*fore$se #lower confidence level
ts.plot(AirPassengers, fore$pred, Upper_b, Lower_b, col=c(1,2,4,4), lty = c(1,1,2,2))
#plotting time series data with legend
legend("topleft", c("Actual", "Forecast", "Error Bounds .05"), col=c("black","red","blue"), lty=c(1,1,2))
```
Exponential smoothing weights the closest data point most heavily. It is appropriate if you have determined no seasonality in data
```{r}
exp_s <- ses(AirPassengers)
autoplot(exp_s)
# Is this an appropriate method for the AirPassengers data?
# a flat line for predictions?
```
```{r}
holt_pass <- holt(AirPassengers)
autoplot(holt_pass)
```
```{r}
library("expsmooth")
consumer_confidence <- ts(unemp.cci[,"cci"])
autoplot(consumer_confidence)
smooth_cci<- ses(consumer_confidence)
autoplot(smooth_cci)
# Does the projections seem reasonable?
```
Holt's method uses smoothing & trend functions to represent the data
```{r}
holt_cci <- holt(consumer_confidence)
autoplot(holt_cci)
```