-
Notifications
You must be signed in to change notification settings - Fork 1
/
Decompositional_Methods.Rmd
161 lines (128 loc) · 3.3 KB
/
Decompositional_Methods.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
title: "Decompositional Methods for Indian TV Ratings"
output:
html_document:
df_print: paged
---
```{r}
library(ggplot2)
library(forecast)
library(tis)
# Read CSV into R
setwd("/Users/davidleonardi/Projects/Indian_TV_Time_Series")
curDir <- getwd()
```
Load Data.
```{r}
df <- read.csv(file="./data/ActualRatings_weeklyGRP.csv", header=TRUE, sep=",", stringsAsFactors=FALSE)
# remove extra column
df <- df[ , !(names(df) %in% c("X"))]
# convert string date to R POSIXct format
df$GRPRatingsDate <- as.POSIXct(as.Date(sapply(df$GRPRatingsDate, function(x) strsplit(x, " ")[[1]][1]), "%d-%B-%Y"))
```
Plot Data.
```{r}
ggplot(data=df, aes(x=GRPRatingsDate, y=GRP)) +
geom_line()+
geom_point()+
labs(title="Weekly Ratings 2007 - 2009", y="GRP", x="GRP Ratings Date")
```
Split data to training and testing data.
```{r}
df_train <- df[which(df$GRPRatingsDate <= "2008-10-26"),]
df_test <- df[which(df$GRPRatingsDate > "2008-10-26"),]
head(df_train, 5)
```
```{r}
head(df_test, 5)
```
### Decompose the data
#### Using decompose method
```{r}
timeseries_train = `^`(df_train$GRP, 0.7)
ts_train = ts(timeseries_train, frequency = 12)
decompose_train_additive = decompose(ts_train, "additive")
plot(decompose_train_additive)
```
```{r}
decompose_train_multiplicative = decompose(ts_train, "multiplicative")
plot(decompose_train_multiplicative)
```
#### Using STL
```{r}
stl_train = stl(ts_train, s.window="periodic")
seasonal_stl_train <- stl_train$time.series[,1]
trend_stl_train <- stl_train$time.series[,2]
random_stl_train <- stl_train$time.series[,3]
plot(as.ts(seasonal_stl_train))
```
```{r}
plot(trend_stl_train)
```
```{r}
plot(random_stl_train)
```
```{r}
plot(stl_train, main="Seasonal Decomposition of Time Series by Loess (STL)")
```
```{r}
ts_train_actual <- ts(df_train$GRP, frequency = 12)
ts_train_predicted <- ts(`^`(stl_train$time.series[,1]+stl_train$time.series[,2], 1/0.7), frequency = 12)
```
```{r}
forecast_stl <- forecast(stl_train, h=21)
plot(forecast_stl)
```
```{r}
unclass(forecast_stl)
```
```{r}
ts_test_actual <- ts(df_test$GRP, frequency = 12)
ts_test_predicted <- ts(`^`(forecast_stl$mean, 1/0.7), frequency = 12)
```
```{r}
train_mape <- Reduce(`+`, unclass(abs((ts_train_actual-ts_train_predicted)/ts_train_actual) * 100)) / length(ts_train_actual)
cat("Train MAPE:",train_mape,"%")
```
```{r}
train_mae <- Reduce(`+`, unclass(abs(ts_train_actual-ts_train_predicted))) / length(ts_train_actual)
cat("Test MAE:",train_mae)
```
```{r}
test_mape <- Reduce(`+`, unclass(abs((ts_test_actual-ts_test_predicted)/ts_test_actual) * 100)) / length(ts_test_actual)
cat("Test MAPE:",test_mape,"%")
```
```{r}
test_mae <- Reduce(`+`, unclass(abs(ts_test_actual-ts_test_predicted))) / length(ts_test_actual)
cat("Test MAE:",test_mae)
```
```{r}
new_df <- ts(df_train$GRP, start = c(2007,23), frequency = 52)
new_df
```
```{r}
decompose_df <- tslm(new_df ~ trend + fourier(new_df, 2))
decompose_df
```
```{r}
trend <- coef(decompose_df)[1] + coef(decompose_df)['trend'] * seq_along(new_df)
components <- cbind(
time.series = cbind(
data = new_df,
seasonal = new_df - trend - residuals(decompose_df),
trend = trend,
remainder = residuals(decompose_df)
)
)
plot(components)
```
```{r}
forecast_stl <- forecast(components, h=21)
plot(forecast_stl)
```
```{r}
stl_train
```
```{r}
components
```