Skip to content

Commit

Permalink
added Rmd
Browse files Browse the repository at this point in the history
  • Loading branch information
harlananelson committed May 12, 2018
1 parent fedb927 commit d2144eb
Show file tree
Hide file tree
Showing 9 changed files with 952 additions and 0 deletions.
90 changes: 90 additions & 0 deletions ARIMA_2_1_2_simulation.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
title: "Simulate Time Series"
runtime: shiny
output: html_document
---

This is an interactive document. It interactively simmulates a Moving average order 2 model.
The same code that could simulate the series non interactively is also included.

Make sure you have the following packages installed on your system.
```{r,echo=FALSE,message=FALSE}
# load packages ####
# if you do not have these packages - install.packages('package name')
library(tidyquant)
library(forecast)
library(quantmod)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(lubridate)
library(xts)
library(shiny)
```

```{r,echo=FALSE}
inputPanel(
sliderInput("phi_1","Correlation in First MA component: ",min=0, max=0.99999,value=0.5,step = 0.01),
sliderInput("phi_2","Correlation in Second MA component: ",min=0, max=0.99999,value=0.5,step = 0.01)
)
```

```{r,echo=FALSE}
x <- reactive({
date <- seq.Date(as.Date("2001/01/01"),as.Date("2013/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ma=c(input$phi_1,input$phi_2)),n=l)
d <- data.frame(date=date,y=y)
x <- xts(d[,-1],order.by = d[,1])
})
```

```{r,echo=FALSE,message=FALSE}
renderPlot({
plot(x())
})
```
```{r,echo=FALSE}
renderPlot({
acf(x())
})
```
```{r,echo=FALSE}
renderPlot({
pacf(x())
})
```

```{r}
renderPrint({
fit_2 <- auto.arima(x())
broom::tidy(fit_2)
})
```


```{r}
renderPrint({
arima(x(),order = c(0,0,2))
})
```

```{r,echo=FALSE,include=FALSE,eval=FALSE}
date <- seq.Date(as.Date("2001/01/01"),as.Date("2013/01/01"),"days")
l <- length(date)
y <-arima.sim(model=list(ma=c(0.9)),n=l)
x <- data.frame(date=date,y=y)
x <- xts(x[,-1],order.by = x[,1])
```

```{r,echo=FALSE,message=FALSE,include=FALSE,eval=FALSE}
plot(x)
```

```{r,echo=FALSE,include=FALSE,eval=FALSE}
print(acf(x))
print(pacf(x) )
print(arima(x,order=c(0,0,1)))
```

87 changes: 87 additions & 0 deletions Introduction to tidyquant.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: "Introduction to tidyquant"
runtime: shiny
output: html_notebook
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE
)
```

```{r}
library(tidyverse)
library(magrittr)
library(tidyquant)
```


Copied from Matt Dancho

tidyquant is a package that integrates other R package resourses for collecting and analyzing financial
data into a tidy framework.

Three packages integrated
: xts, zoo, quantmod, TTR, PerfomanceAnalytics

## tq_index()
Financial index data can be collected using `tq_index`. A list of the indexes available can
be listed with `tq_index_options()`.

```{r,include=FALSE}
index_options <- tq_index_options()
inputPanel(
selectInput("index_options", label = "tq_index_options:",
choices = index_options, selected = index_options[1])
)
```

```{r,include=FALSE}
renderDataTable({
tq_index(input$index_options)
})
```


A list of the exchanges available through `tq_exchange()` can be listed using `tq_exchange_options()`

```{r}
exchange_options <- tq_exchange_options()
inputPanel(
selectInput("exchange_options", label = "tq_exchange_options:",
choices = exchange_options, selected = exchange_options[1])
)
actionButton("list_exchange","List Selected Exchange")
```

```{r,include=TRUE}
exchange_list <-eventReactive(input$list_exchange,{
tq_exchange(input$exchange_options)
})
exchange_options <-eventReactive(input$list_exchange,{
exchange_list() %>%
select(symbol,company) %$%
structure(as.character(symbol), names = as.character(company))
})
renderDataTable({ exchange_list() })
```

```{r,include=FALSE}
renderPrint({ exchange_options() })
```

```{r,include=FALSE}
renderUI({
selectInput("exchange_index", label = "Exchange Index:",
choices = exchange_options(), selected = exchange_options()[1])
})
```




195 changes: 195 additions & 0 deletions Time_Series_Analysis.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
---
title: "Time Series Analysis"
author: "Harlan A Nelson"
output:
html_document:
df_print: paged
---

This notebook compiles information from a variety of web sources regarding time series analysis.
This code is from the tidyquant tutorial.


There are a few packages related to time series analysis.
* zoo
* xts
* quantmod
* TTR
* PerformanceAnalytics
* tidyquant

This notebook starts with tidyquant
```{r}
library(tidyquant)
```

```{r}
tq_transmute_fun_options()
```
```{r}
data("FANG")
```

```{r}
FANG
```
```{r}
FANG_annual_returns <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
type = "arithmetic")
FANG_annual_returns
```

```{r}
FANG_annual_returns %>%
ggplot(aes(x = date, y = yearly.returns, fill = symbol)) +
geom_bar(stat = "identity") +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
scale_y_continuous(labels = scales::percent) +
labs(title = "FANG: Annual Returns",
subtitle = "Get annual returns quickly with tq_transmute!",
y = "Annual Returns", x = "") +
facet_wrap(~ symbol, ncol = 2) +
theme_tq() +
scale_fill_tq()
```


```{r}
FANG_daily_log_returns <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
type = "log",
col_rename = "monthly.returns")
```
```{r}
FANG_daily_log_returns %>%
ggplot() +
aes(x = monthly.returns, fill = symbol) +
geom_density(alpha = 0.5) +
labs(title = "FANG: Charting the Daily Log Returns",
x = "Monthly Returns",
y = "Density") +
theme_tq() +
scale_fill_tq() +
facet_wrap(~ symbol, ncol = 2)
```


```{r}
FANG %>%
group_by(symbol) %>%
tq_transmute(select = open:volume,
mutate_fun = to.period,
period = "months")
```
```{r}
FANG_daily <- FANG %>%
group_by(symbol)
FANG_daily %>%
ggplot() +
aes(x = date, y = adjusted, color = symbol) +
geom_line(size = 1) +
labs(title = "Daily Stock Prices",
x = "",
y = "Adjusted Proces",
color = "") +
facet_wrap( ~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
```
```{r}
FANG_monthly <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = to.period,
period = "months")
FANG_monthly %>%
ggplot() +
aes(x = date, y = adjusted, color = symbol) +
geom_line(size = 1) +
labs(title = "Monthly Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
```


```{r}
FANG_returns_monthly <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly")
```
```{r}
baseline_returns_monthly <- "XLK" %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly")
```

```{r}
returns_joined <- left_join(FANG_returns_monthly,
baseline_returns_monthly,
by = "date")
returns_joined
```


```{r}
FANG_rolling_corr <- returns_joined %>%
tq_transmute_xy(x = monthly.returns.x,
y = monthly.returns.y,
mutate_fun = runCor,
n = 6,
col_rename = "rolling.corr.6")
```

```{r}
FANG_rolling_corr %>%
ggplot() +
aes(x = date, y = rolling.corr.6, color = symbol) +
geom_hline(yintercept = 0, color = palette_light()[1]) +
geom_line(size = 1) +
labs(title = "FANG: Six Month Rolling Correlations to XLK",
x = "", y = "Correlation", color = "") +
facet_wrap(~ symbol, ncol = 2) +
theme_tq() +
scale_color_tq()
```
```{r}
names(returns_joined )
FANG_rolling_corr <- returns_joined %>%
tq_transmute_xy(x=monthly.returns.x,y=monthly.returns.y,
mutate_fun = runCor,
n = 6,
col_rename = "rolling.corr.6")
```

```{r}
FANG_rolling_corr %>%
ggplot() +
aes(x = date, y = rolling.corr.6, color = symbol) +
geom_hline(yintercept = 0, color = palette_light()[1]) +
geom_line(size = 1) +
labs(title = "FANG: Six Month Rolling Correlations to XLK",
x = "", y = "Correlation", color = "") +
facet_wrap(~ symbol, ncol = 2) +
theme_tq() +
scale_color_tq()
```
Loading

0 comments on commit d2144eb

Please sign in to comment.