-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar_1_simulation_2.Rmd
185 lines (165 loc) · 4.04 KB
/
ar_1_simulation_2.Rmd
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
---
title: "R Notebook"
runtime: shiny
output: html_notebook
---
$x_t = \delta +\phi_1 x_{t-1} + \omega_t$
$\omega_t \sim N(0,\sigma^2)$
MA(1)
$x_t = \mu + \omega_t + \phi_1 \omega_{t-1}$
ARIMA Auto Regression Differences Moving average
Start by loading useful libraries.
xts seems to be the time series library many people are currently using.
https://cran.r-project.org/web/packages/xts/vignettes/xts.pdf
lubridate is the tidyverse package for dates.
quantmod lets you get financial data as well as do other things.
https://github.com/joshuaulrich/quantmod
tidyquant is the tidyverse frontend and makes quantmod easier.
google "Core Functions in tidyquant"
```{r}
# load packages ####
# if you do not have these packages - install.packages('package name')
library(tidyquant)
library(quantmod)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(lubridate)
library(xts)
```
It is possible to simulate a time series. This will help you recognize what they look like.
Here an AR(1) model with correlation of 0.9 is simulated.
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2013/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ar=c(0.99)),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
pacf(t)
acf(t)
```
How about a AR(2)
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2003/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ar=c(0.6,0.3)),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
pacf(t)
acf(t)
```
How about a AR(3)
Try changing the parameters so the add up to more than 1.
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2012/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ar=c(0.5,0.1,0.1)),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
acf(t)
pacf(t)
```
Here is a MA(1)
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2003/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ma=c(0.5)),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
pacf(t)
acf(t)
```
Here is a MA(2)
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2003/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ma=c(0.6,.5)),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
acf(t)
pacf(t)
```
Here is a MA(3)
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2003/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ma=c(0.6,.5,0.9)),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
acf(t)
pacf(t)
```
Here is an ARIMA(1,0,1)
Seems impossible to figure diagnose from the plots.
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2013/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ma=0.6,ar=.8),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
acf(t)
pacf(t)
```
Here is an ARIMA(2,0,2)
Seems impossible to figure diagnose from the plots.
```{r}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2003/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ma=c(0.6,0.2),ar=c(.8,.1)),n=l)
d <- data.frame(date=date,y=y)
t <- xts(d[,-1],order.by = d[,1])
plot(t)
acf(t)
pacf(t)
```
```{r}
install.packages('XLConnect')
install.packages('XLConnectJars')
install.packages('rJava')
library(XLConnectJars)
library(XLConnect)
tidyquant::tq_index_options()
tq_index("SP500")
```
```{r}
oil <- tidyquant::tq_get("DCOILWTICO", get = "economic.data")
oil
class(oil)
plot(oil)
oil %>% na.omit() %>% pacf
```
Plot using ggplot.
```{r}
oil %>% na.omit() %>%
ggplot(aes(x=date,y=price)) +
geom_point(shape=20)
```
Here is the LA Rain data.
```{r}
data(larain,package='TSA')
larain <- as.xts(larain)
names(larain)
acf(larain)
pacf(larain)
plot(larain)
```
Oil and gold.
https://blog.exploratory.io/introduction-to-tidyquant-quantitative-financial-analysis-for-tidyverse-habitats-e5f72a023ce2
```{r}
oil <- tq_get("DCOILWTICO", get = "economic.data")
names(oil)
dim(oil)
str(oil)
head(oil)
oil %>% na.omit() %>% pacf
gold <- tq_get("GOLDAMGBD228NLBM", get = "economic.data")
plot(gold)
gold %>% na.omit() %>% pacf
```