-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime Series Playground.R
125 lines (103 loc) · 3.23 KB
/
Time Series Playground.R
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
# Install packages
install.packages("fpp2")
install.packages("ggplot2")
install.packages("forecast")
install.packages("ggpubr")
install.packages("zoo")
install.packages("smooth")
install.packages("MLmetrics")
install.packages("scales")
## Import libraries
library(fpp2)
library(ggplot2)
library(forecast)
library(ggpubr)
library(zoo)
library(smooth)
library(MLmetrics)
library(scales)
# Time series of quarterly data
y <- ts(rnorm(n=60,mean=10,sd=1), start=2000, frequency=4)
y
autoplot(y,color="black")+
ggtitle("Example Time Series Plot Quarterly Data") +
xlab("Year") +
ylab("Data")
# Time series of monthly data
y <- ts(rnorm(n=180,mean=10,sd=1), start=2000, frequency=12)
y
autoplot(y,color="black")+
ggtitle("Example Time Series Plot Monthly Data") +
xlab("Year") +
ylab("Data")
# Stacked time series
x <- ts(rnorm(n=20,mean=0,sd=1), start=2000, frequency=4)
y <- ts(rnorm(n=20,mean=0,sd=1), start=2000, frequency=4)
plot_x <- autoplot(x,color="blue")+
ggtitle("Example Time Series Plot with X-Data") +
xlab("Year") +
ylab("Data")
plot_y <- autoplot(y,color="red")+
ggtitle("Example Time Series Plot with Y-Data") +
xlab("Year") +
ylab("Data")
example_dataset <- cbind(x,y)
autoplot(example_dataset)+
ggtitle("Example Time Series Plot X-Y Data") +
xlab("Year") +
ylab("Data") +
scale_color_manual(values = c("black", "red")) +
theme_bw()
# Separated panel
ggarrange(plot_x, plot_y, ncol=2, nrow=1)
# Using ggplot
# WIP
## Modelling with naive method
# Set training data from 1992 to 2007
beer2 <- window(ausbeer,start=1992,end=c(2007,4))
# Plot some forecasts
autoplot(beer2) +
autolayer(naive(beer2, h=11),
series="Naïve", PI=FALSE) +
autolayer(snaive(beer2, h=11),
series="Seasonal naïve", PI=FALSE) +
ggtitle("Forecasts for quarterly beer production") +
xlab("Year") + ylab("Megalitres") +
guides(colour=guide_legend(title="Forecast"))
## Modelling with moving average method
model_ma <- sma(goog200,order=10,h=40)
plot(forecast(model_ma,interval = FALSE),
legend=FALSE,
main="Google stock forecasting with MA-10",
xlab="Day",
ylab="Closing Price (US$)")
## Modelling with time series regression model
# WIP
## Evaluation model
data.train <- window(ausbeer, start=1980, end=c(2000,4))
data.test <- window(ausbeer, start=2001, end=c(2004,4))
data.train
data.test
naive_forecast <- naive(data.train, h=16)
snaive_forecast <- snaive(data.train, h=16)
tslm_model <- tslm(data.train ~ trend+season)
tslm_forecast <- forecast(tslm_model, h=16)
prediction <- naive_forecast$mean
evaluation <- rbind(RMSE(prediction,data.test),
MAPE(prediction,data.test))
rownames(evaluation) <- c("RMSE","MAPE")
colnames(evaluation) <- c("Value (Naive)")
evaluation
prediction <- snaive_forecast$mean
evaluation <- rbind(RMSE(prediction,data.test),
MAPE(prediction,data.test))
rownames(evaluation) <- c("RMSE","MAPE")
colnames(evaluation) <- c("Value (SNaive)")
evaluation
prediction <- tslm_forecast$mean
evaluation <- rbind(RMSE(prediction,data.test),
MAPE(prediction,data.test))
rownames(evaluation) <- c("RMSE","MAPE")
colnames(evaluation) <- c("Value (TSLM)")
evaluation
evaluation