-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indian_rain_analysis_2.R
191 lines (138 loc) · 4.8 KB
/
Indian_rain_analysis_2.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Loading the
load_lb <- function()
{
suppressPackageStartupMessages(library(doMC))
registerDoMC(cores = 8)
suppressPackageStartupMessages(library(readxl))
suppressPackageStartupMessages(library(tidyr))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(caret))
suppressPackageStartupMessages(library(rpart))
suppressPackageStartupMessages(library(tree))
suppressPackageStartupMessages(library(MASS))
suppressPackageStartupMessages(require(xgboost))
require(forecast)
library(lubridate)
}
load_lb()
# Import the data file
df <- fread("E:\\Study\\R Projects\\Common files\\Apple_data.csv",
sep = ",")
head(df)
tail(df)
glimpse(df)
# Formating the columns and keeping only required columns for analysis
library(lubridate)
df %>%
mutate(date = as.Date(date)) %>%
dplyr::select(date, close) %>%
mutate(Year = year(date),
Month = month(date),
Day = day(date))-> df1
glimpse(df1)
ggplot(df1[df1$Year > 2017,],aes(x = date, y = close, color = date)) +
geom_line(show.legend = FALSE) +
labs(title = "Closing stock price trend - Apple")
## Naive forecasting - Trend data
# Most recently observed value, benchmarking
Apl_naive <- naive(df1[df1$Year > 2017,"close"], h = 10)
summary(Apl_naive)
autoplot(Apl_naive) +
labs(x = "Data Points", y = NULL)
# Confidence interval looks wide
## Seasonal naive approach
rain_sn <- snaive(df.ts, 24)
summary(rain_sn)
autoplot(rain_sn)+
labs(x = "year", y = NULL)
## Checking residuals - 2017 onwards
checkresiduals(Apl_naive)
## first plot: residuals show no trend -> white noise
## bottom left: One lag exceeds the threshold
## Residuals are almost normally distributed
## Lj test: p > 0.05 -> fail to reject the null hypothesis that is is purely due to noise
checkresiduals(rain_sn)
## first plot: residuals show trend
## bottom left: One lag exceeds the threshold
## Residuals are almost normally distributed, but with high kurtosis
## Lj test: p < 0.05 -> reject the null hypothesis that is is purely due to noise
# Training and test sets
ap_ts <- ts(df1[df1$Year > 2017,"close"])
ap_train <- subset(ap_ts, end = 85)
ap_test <- subset(ap_ts, start = 86, end = length(ap_ts))
naive_ap <- naive(ap_train, h = 5)
mean_ap <- meanf(ap_train, h =5)
accuracy(naive_ap, ap_test)
accuracy(mean_ap, ap_test)
err <- tsCV(ap_ts,
forecastfunction = naive,
h = 1) # 1-step ahead
mean(err^2, na.rm = TRUE)
# for seasonal features
rain_tr <- window(df.ts, end = c(2013,12))
rain_te <- window(df.ts, start = c(2014,1))
r_sn <- snaive(rain_tr, h = length(rain_te))
r_mean <- meanf(rain_tr, h = length(rain_te))
accuracy(r_sn, rain_te)
accuracy(r_mean, rain_te)
err <- tsCV(df.ts,
forecastfunction = snaive,
h = 1)
mean(err^2, na.rm = TRUE)
library(zoo)
df1 %>%
filter(Year > 2010) %>%
select(date, close) %>%
mutate(ma00 = rollmean(close, k = 13, fill = NA),
ma01 = rollmean(close, k = 25, fill = NA),
ma02 = rollmean(close, k = 49, fill = NA),
ma03 = rollmean(close, k = 97, fill = NA)) -> df_ap
head(df_ap)
df_ap %>%
gather(metric, value, close:ma03) %>%
ggplot(aes(date, value, color = metric))+
geom_line()
df_ap %>%
gather(metric, value, ma00:ma03) %>%
group_by(metric) %>%
summarise(MSE = mean((close - value)^2, na.rm = TRUE),
MAPE = mean(abs((close - value)/close),na.rm = TRUE))
# trailing moving average
df1 %>%
filter(Year > 2010) %>%
dplyr::select(date, close) %>%
mutate(ma_trail = rollmean(close, k = 12, fill = NA, align = "right")) %>%
gather(metric, value, -date) %>%
ggplot(aes(date, value, color = metric)) +
geom_line()
# Simple expotential smoothing
ses_ap <- ses(ap_train, alpha = 0.2, h = 5)
autoplot(ses_ap)
ap_diff <- diff(ap_train)
autoplot(ap_diff)
ses_ap_diff <- ses(ap_diff, alpha = 0.2, h = 5)
autoplot(ses_ap_diff)
# Holt's method
holt_app <- holt(ap_train, h = 5)
autoplot(holt_app)
holt_app$model
accuracy(holt_app, ap_test)[2,5]
# tuning the 'beta'value
beta <- seq(0.0001, 0.1, by = 0.001)
RMSE <- NA
for (i in seq_along(beta)){
fit <- holt(ap_train, beta = beta[i], h = 5)
RMSE[i] <- accuracy(fit, ap_test)[2,2]
}
beta.fit <- data.frame(beta, RMSE)
beta.min <- filter(beta.fit, RMSE == min(RMSE))
beta.fit %>%
ggplot(aes(beta, RMSE))+
geom_line() +
geom_point(data = beta.min, aes(beta, RMSE), size = 2, color="blue")
# refit the model
holt_app <- holt(ap_train, h = 5, beta = beta.min[1,1])
autoplot(holt_app)
accuracy(holt_app, ap_test) # MAPE increased
# Holt-Winters seasonal method
autoplot(decompose(df.ts))