-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgold_ARIMA.Rmd
More file actions
101 lines (80 loc) · 2.01 KB
/
gold_ARIMA.Rmd
File metadata and controls
101 lines (80 loc) · 2.01 KB
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
---
title: "gold_ARIMA"
author: "MCM2022"
date: "2/19/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(ggplot2)
library(tseries)
library(forecast)
theme_set(theme_minimal())
set.seed(123)
```
```{r}
# Load the data & clean the data
bitcoin_data <- read.csv("data/BCHAIN-MKPRU.csv") %>%
mutate(Date = as.Date(Date, "%m/%d/%y")) %>%
na.omit()
gold_data <- read.csv("data/LBMA-GOLD.csv") %>%
mutate(Date = as.Date(Date, "%m/%d/%y")) %>%
na.omit()
```
```{r}
value <- gold_data$USD..PM.[1:5]
model_arima <- function(result, listIn)
{
temp = result
for (i in 6:length(listIn))
{
train_data <- listIn[1:i]
fitARIMA <- auto.arima(diff(log(train_data)), trace = FALSE)
predARIMA <- forecast(fitARIMA, h = 1, level = c(99))
pred <- as.numeric(predARIMA$mean) * (10^5) / (10^5)
new_value <- 2 ^ pred * listIn[i:i]
temp = c(temp, new_value)
print(i)
print(new_value)
}
return(temp)
}
value <- model_arima(value, gold_data$USD..PM.)
```
```{r}
gold_result <- gold_data %>%
cbind(value)
ggplot(data = gold_result) +
geom_line(mapping = aes(x = Date,
y = USD..PM.,
color = "red")) +
geom_line(mapping = aes(x = Date,
y = value,
color = "blue")) +
theme_minimal()
bitcoin_result
```
```{r}
gold_test <- gold_result %>%
filter(Date >= "2017-12-20",
Date <= "2018-12-20")
ggplot(data = gold_test) +
geom_line(mapping = aes(x = Date,
y = USD..PM.,
color = "red")) +
geom_line(mapping = aes(x = Date,
y = value,
color = "blue")) +
theme_minimal()
gold_result %>%
mutate(dif = abs(USD..PM. - value))
```
```{r}
gold_final <- gold_result %>%
filter(Date > "2016-09-15") %>%
select(Date, value)
write.csv(gold_final, "prediction/gold_ARIMA_result.csv", row.names = FALSE)
```