forked from quaesito/time-series-forecast-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
106 lines (106 loc) · 3.94 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
#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))
library(tidyverse)
library(fpp)
library(forecast)
library(backtest)
library(quantmod)
library(lubridate)
library(dplyr)
Sale_of_fish <- read.csv("Fish dataset.csv",header=T) #data source=FranceAgriMer/VISIOMer
setwd("C:/Users/miche/OneDrive - HKUST Connect/Data Science/Data-Science-Private-Consulting/FR-P20201130-Time_Series_Forecast")
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
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
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 te
adj$logr <- log(lag(adj$sales)) - log(adj$sales)
head(adj)
narm <- function (x) {
x[is.na(x)] <- 1
return(x)
}
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)
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
View(Sale_of_fish)
View(adj)
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 th
View(adj)
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
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
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