-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.Rhistory
360 lines (360 loc) · 12.7 KB
/
.Rhistory
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#work with the number of sales from dataset
adj <- select(Sale_of_fish, -c("year", "month", "end.of.period", "value..E.", "unsoldes..Kg."))
install.packages("dplyr")
select
select
library(dplyr)
#import dataset
Sale_of_fish <- read.csv("Fish dataset.csv",header=T) #data source=FranceAgriMer/VISIOMer
#import dataset
Sale_of_fish <- read.csv("Fish dataset.csv",header=T) #data source=FranceAgriMer/VISIOMer
Fish
Fish
Fish
adj$sales <- as.numeric(gsub(' ', '', adj$sales))
#Analysis of Fish sales dataset
#Import important library for time series analysis in R
library(tidyverse)
library(fpp)
library(forecast)
library(backtest)
library(quantmod)
library(lubridate)
library(dplyr)
#import dataset
Sale_of_fish <- read.csv("Fish dataset.csv",header=T) #data source=FranceAgriMer/VISIOMer
head(Sale_of_fish) #See head of the dataset
tail(Sale_of_fish) #see tail of the dataset
#work with the number of sales from dataset
adj <- select(Sale_of_fish, -c("year", "month", "end.of.period", "value..E.", "unsoldes..Kg."))
adj$sales <- as.numeric(gsub(' ', '', adj$sales))
adj$start <- dmy(adj$start)##This tells you that the data series is in a time series format
adj$logr <- log(lag(adj$sales)) - log(adj$sales)
head(adj)
narm <- function (x) {
x[is.na(x)] <- 1
return(x)
}
adj$logr <- narm(adj$logr)
head(adj)
setwd("C:/Users/miche/OneDrive - HKUST Connect/Data Science/Data-Science-Private-Consulting/FR-P20201130-Time_Series_Forecast")
#Analysis of Fish sales dataset
#Import important library for time series analysis in R
library(tidyverse)
library(fpp)
library(forecast)
library(backtest)
library(quantmod)
library(lubridate)
library(dplyr)
#import dataset
Sale_of_fish <- read.csv("Fish dataset.csv",header=T) #data source=FranceAgriMer/VISIOMer
head(Sale_of_fish) #See head of the dataset
tail(Sale_of_fish) #see tail of the dataset
#work with the number of sales from dataset
adj <- select(Sale_of_fish, -c("year", "month", "end.of.period", "value..E.", "unsoldes..Kg."))
adj$sales <- as.numeric(gsub(' ', '', adj$sales))
adj$start <- dmy(adj$start)##This tells you that the data series is in a time series format
adj$logr <- log(lag(adj$sales)) - log(adj$sales)
head(adj)
narm <- function (x) {
x[is.na(x)] <- 1
return(x)
}
adj$logr <- narm(adj$logr)
head(adj)
max_Date <- max(adj$start)
min_Date <- min(adj$start)
test_ts <- ts(adj$logr, end=c(year(max_Date), month(max_Date)),start=c(year(min_Date), month(min_Date)),frequency=12)#freq 12 => Monthly data
logr <- adj
logr
dev.off() #to face margin problem error
dev.off() #to face margin problem error
plot(test_ts)
print(adf.test(test_ts)) # p-value < 0.05 indicates the TS is stationary
summary(m1)
summary(m1)
acf = acf(test_ts, main='ACF Plot', lag.max=100) # autocorrelation
pacf.logr = pacf(test_ts, main='PACF Plot', lag.max=100) # partial autocorrelation
accuracy(forecast(m1))
m1 <- auto.arima(test_ts, seasonal = TRUE)
summary(m1)
accuracy(forecast(m1))
source("C:/Users/miche/OneDrive - HKUST Connect/Data Science/Data-Science-Private-Consulting/FR-P20201130-Time_Series_Forecast/ts.R")
#Analysis for Yahoo finance data
#Import important library
library(quantmod)
library(fpp)
library(backtest)
#read yahoo finance datasets for last one year CA.PA
data <- getSymbols("CA.PA", from="2019-11-25",to = "2020-11-24" ,src="yahoo", auto.assign=F)
head(data)#see the head of dataset
tail(data) #see the tail of dataset
adj <- data[,5]
adj
dev.off() #to face margin problem error
plot(data[,6], main = "Adhusted Closing Price")
log <- periodReturn(adj,period = "daily", type = "log", leading = TRUE)
log <- 1+log
head(logr)
#differentiate
diff(log)
par(mfrow = c(2,1))
# both acf() and pacf() generates plots by default
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
#Augmented Dickey-Fuller(ADF) Test
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
#Estimate the coefficients Using Seasonal Arima model
m1 <- auto.arima(logr, seasonal = FALSE)
summary(m1)
#studying the residues
checkresiduals(m1) # p-value over .05 confirms no autocorrelations
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
summary(m1)
#Analysis for Yahoo finance data
#Import important library
library(quantmod)
library(fpp)
library(backtest)
#read yahoo finance datasets for last one year CA.PA
data <- getSymbols("CA.PA", from="2019-11-25",to = "2020-11-24" ,src="yahoo", auto.assign=F)
head(data)#see the head of dataset
tail(data) #see the tail of dataset
adj <- data[,5]
adj
dev.off() #to face margin problem error
plot(data[,6], main = "Adhusted Closing Price")
log <- periodReturn(adj,period = "daily", type = "log", leading = TRUE)
log <- 1+log
head(logr)
#differentiate
diff(log)
par(mfrow = c(2,1))
# both acf() and pacf() generates plots by default
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
#Augmented Dickey-Fuller(ADF) Test
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
#Estimate the coefficients Using Seasonal Arima model
m1 <- auto.arima(logr, seasonal = FALSE)
summary(m1)
#studying the residues
checkresiduals(m1) # p-value over .05 confirms no autocorrelations
#Analysis of Fish sales dataset
#Import important library for time series analysis in R
library(tidyverse)
library(fpp)
library(forecast)
library(backtest)
library(quantmod)
library(lubridate)
library(dplyr)
#import dataset
Sale_of_fish <- read.csv("Fish dataset.csv",header=T) #data source=FranceAgriMer/VISIOMer
head(Sale_of_fish) #See head of the dataset
tail(Sale_of_fish) #see tail of the dataset
#work with the number of sales from dataset
adj <- select(Sale_of_fish, -c("year", "month", "end.of.period", "value..E.", "unsoldes..Kg."))
adj$sales <- as.numeric(gsub(' ', '', adj$sales))
adj$start <- dmy(adj$start)##This tells you that the data series is in a time series format
adj$logr <- log(lag(adj$sales)) - log(adj$sales)
head(adj)
narm <- function (x) {
x[is.na(x)] <- 1
return(x)
}
adj$logr <- narm(adj$logr)
head(adj)
max_Date <- max(adj$start)
min_Date <- min(adj$start)
test_ts <- ts(adj$logr, end=c(year(max_Date), month(max_Date)),start=c(year(min_Date), month(min_Date)),frequency=12)#freq 12 => Monthly data
logr <- adj
logr
dev.off() #to face margin problem error
plot(test_ts)
plot(stl(test_ts,s.window = "periodic"))
# both acf() and pacf() generates plots by default
acf = acf(test_ts, main='ACF Plot', lag.max=100) # autocorrelation
pacf.logr = pacf(test_ts, main='PACF Plot', lag.max=100) # partial autocorrelation
#Augmented Dickey-Fuller(ADF) Test
print(adf.test(test_ts)) # p-value < 0.05 indicates the TS is stationary
#Estimate the coefficients Using Seasonal Arima model
m1 <- auto.arima(test_ts, seasonal = TRUE)
summary(m1)
#Check Accuracy
accuracy(forecast(m1))
#studying the residues
checkresiduals(m1) # p-value over .05 confirms no autocorrelations
#Analysis for Yahoo finance data
#Import important library
library(quantmod)
library(fpp)
library(backtest)
#read yahoo finance datasets for last one year CA.PA
data <- getSymbols("CA.PA", from="2019-11-25",to = "2020-11-24" ,src="yahoo", auto.assign=F)
head(data)#see the head of dataset
tail(data) #see the tail of dataset
adj <- data[,5]
adj
dev.off() #to face margin problem error
plot(data[,6], main = "Adhusted Closing Price")
log <- periodReturn(adj,period = "daily", type = "log", leading = TRUE)
log <- 1+log
head(logr)
#differentiate
diff(log)
par(mfrow = c(2,1))
# both acf() and pacf() generates plots by default
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
#Augmented Dickey-Fuller(ADF) Test
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
#Estimate the coefficients Using Seasonal Arima model
m1 <- auto.arima(logr, seasonal = FALSE)
summary(m1)
#studying the residues
checkresiduals(m1) # p-value over .05 confirms no autocorrelations
View(acf.logr)
View(adj)
View(log)
View(narm)
Analysis for Yahoo finance data
#Import important library
library(quantmod)
library(fpp)
library(backtest)
#read yahoo finance datasets for last one year CA.PA
data <- getSymbols("CA.PA", from="2019-11-25",to = "2020-11-24" ,src="yahoo", auto.assign=F)
head(data)#see the head of dataset
tail(data) #see the tail of dataset
adj <- data[,5]
adj
dev.off() #to face margin problem error
plot(data[,6], main = "Adhusted Closing Price")
log <- periodReturn(adj,period = "daily", type = "log", leading = TRUE)
log <- 1+log
head(logr)
head(log)
diff(log)
par(mfrow = c(2,1))
acf.log = acf(log, main='ACF Plot', lag.max=100)# autocorrelation
adj <- data[,5]
adj
adj$logr <- log(lag(adj$CA.PA.Volume)) - log(adj$CA.PA.Volume)
adj$logr <- log(lag(adj$Volume)) - log(adj$Volume)
adj[1]
adj$logr <- log(lag(adj$CA.PA.Volume)) - log(adj$CA.PA.Volume)
log(lag(adj$CA.PA.Volume))
log(adj$CA.PA.Volume)
log(lag(adj$CA.PA.Volume))
adj$CA.PA.Volume
lag(adj$CA.PA.Volume)
adj$logr <- log(stats::lag(adj$CA.PA.Volume)) - log(adj$CA.PA.Volume)
head(adj)
narm <- function (x) {
x[is.na(x)] <- 1
return(x)
}
both acf() and pacf() generates plots by default
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
#Augmented Dickey-Fuller(ADF) Test
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
m1 <- auto.arima(logr, seasonal = FALSE)
summary(m1)
logr <- adj
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
logr
head(adj)
narm <- function (x) {
x[is.na(x)] <- 1
return(x)
}
adj
logr <- adj
logr
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
logr
adj$logr <- log(stats::lag(adj$CA.PA.Volume)) - log(adj$CA.PA.Volume)
head(adj)
narm <- function (x) {
x[is.na(x)] <- 1
return(x)
}
logr <- adj
logr.head()
head(logr)
adj$logr <- log(stats::lag(adj$CA.PA.Volume)) - log(adj$CA.PA.Volume)
adj$logr <- narm(adj$logr)
logr <- adj
head(logr)
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
adj$logr
adj$logr <- log(stats::lag(adj$CA.PA.Volume)) - log(adj$CA.PA.Volume)
head(adj)
narf <- function (x) {
x[is.infinite(x)] <- 1
return(x)
}
adj$logr <- narf(adj$logr)
logr <- adj
head(logr)
# both acf() and pacf() generates plots by default
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
#Augmented Dickey-Fuller(ADF) Test
print(adf.test(logr)) # p-v
logr
adj$logr <- marm(narf(adj$logr))
adj$logr <- narm(narf(adj$logr))
logr <- adj
head(logr)
# both acf() and pacf() generates plots by default
acf.logr = acf(logr, main='ACF Plot', lag.max=100)# autocorrelation
pacf.logr = pacf(logr, main='PACF Plot', lag.max=100)# partial autocorrelation
#Augmented Dickey-Fuller(ADF) Test
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
#Estimate the coefficients Using Seasonal Arima model
m1 <- auto.arima(logr, seasonal = FALSE)
summary(m1)
logr
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
log <- periodReturn(adj,period = "daily", type = "log", leading = TRUE)
log <- 1+log
head(log)
adj$logr <- log(stats::lag(adj$CA.PA.Volume)) - log(adj$CA.PA.Volume)
diff(log)
par(mfrow = c(2,1))
narf <- function (x) {
x[is.infinite(x)] <- 1
return(x)
}
adj$logr <- narm(narf(adj$logr))
adj
logr <- adj
head(logr)
adj
logr <- adj
head(logr)
acf = acf(test_ts, main='ACF Plot', lag.max=100) # autocorrelation
pacf.logr = pacf(test_ts, main='PACF Plot', lag.max=100) # partial au
test_ts <- ts(adj$logr, end=c(year(max_Date), month(max_Date)),start=c(year(min_Date), month(min_Date)),frequency=12)#freq 12 => Monthly data
acf = acf(test_ts, main='ACF Plot', lag.max=100) # autocorrelation
pacf.logr = pacf(test_ts, main='PACF Plot', lag.max=100) # partial autocorrelation
# both acf() and pacf() generates plots by default
acf = acf(test_ts, main='ACF Plot', lag.max=100) # autocorrelation
pacf.logr = pacf(test_ts, main='PACF Plot', lag.max=100) # partial autocorrelation
print(adf.test(logr)) # p-value < 0.05 indicates the TS is stationary
print(adf.test(test_ts)) # p-value < 0.05 indicates the TS is stationary
test_ts <- ts(adj$logr, end=c(year(max_Date), month(max_Date)),start=c(year(min_Date), month(min_Date)),frequency=12)#freq 12 => Monthly data
print(adf.test(test_ts)) # p-value < 0.05 indicates the TS is stationary
m1 <- auto.arima(logr, seasonal = TRUE)
m1 <- auto.arima(test_ts, seasonal = TRUE)
summary(m1)
checkresiduals(m1) # p-value over .05 confirms no autocorrelations
summary(m1)