---
title: "ANLY 512: Data Visualization"
author: "Vinay Mariyappa"
Date: "`r Sys.Date()`"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
source_code: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
library(knitr)
library(ggplot2)
library(ggthemes)
# Time Series Plotting
library(ggplot2)
library(xts)
# Get AAPL, TWTR and MSFT stock data from Yahoo Finance
aapl_url <- "http://real-chart.finance.yahoo.com/table.csv?s=AAPL&a=07&b=24&c=2010&d=07&e=24&f=2015&g=d&ignore=.csv"
twtr_url <- "http://real-chart.finance.yahoo.com/table.csv?s=TWTR&a=07&b=24&c=2010&d=07&e=24&f=2015&g=d&ignore=.csv"
msft_url <- "http://real-chart.finance.yahoo.com/table.csv?s=MSFT&a=07&b=24&c=2010&d=07&e=24&f=2015&g=d&ignore=.csv"
yahoo.read <- function(url){
dat <- read.table(url,header=TRUE,sep=",")
df <- dat[,c(1,5)]
df$Date <- as.Date(as.character(df$Date))
return(df)}
aapl <- yahoo.read(aapl_url)
twtr <- yahoo.read(twtr_url)
msft <- yahoo.read(msft_url)
```
Dashboard
=======================================================================
Sidebar {.sidebar}
-----------------------------------------------------------------------
### Stock Analysis
This dashboard analyses stocks of Apple, Microsoft and Twitter for Future Investment Purpose.
Row {data-height=650}
-----------------------------------------------------------------------
### Time Series of Closing Stock Prices
```{r,warning=FALSE,message=FALSE}
ggplot(aapl,aes(Date,Close)) +
geom_line(aes(color="aapl")) +
geom_line(data=twtr,aes(color="twtr")) + geom_line(data=msft,aes(color="msft"))+
labs(color="Legend") +
scale_colour_manual("", breaks = c("aapl", "twtr","msft"),
values = c("blue", "red","yellow")) +
ggtitle("Closing Stock Prices: Apple, Twitter & Microsoft") +
theme(plot.title = element_text(lineheight=.7, face="bold"))+ theme_solarized(light = FALSE)
```
### Time Series Comparission of Stock Volume
```{r,warning=FALSE,message=FALSE}
yahoo1.read <- function(url){
dat <- read.table(url,header=TRUE,sep=",")
dm <- dat[,c(1,6)]
dm$Date <- as.Date(as.character(dm$Date))
return(dm)}
aapl <- yahoo1.read(aapl_url)
twtr <- yahoo1.read(twtr_url)
msft <- yahoo1.read(msft_url)
ggplot(aapl,aes(Date,Volume)) +
geom_line(aes(color="aapl")) +
geom_line(data=twtr,aes(color="twtr")) + geom_line(data=msft,aes(color="msft"))+
labs(color="Legend") +
scale_colour_manual("", breaks = c("aapl", "twtr","msft"),
values = c("blue", "black","red")) +
ggtitle("Stock Volume Comparission: Apple, Twitter & Microsoft") +
theme(plot.title = element_text(lineheight=.7, face="bold"))
```
Row {.tabset .tabset-fade}
------------------------------------------------------------------------------------
###APPL vs TWTR Spread
```{r,warning=FALSE,message=FALSE}
require(quantmod)
symbols <- c("AAPL", "TWTR","MSFT")
c<- getSymbols(symbols)
#define training set
startT <- "2016-01-01"
endT <- "2017-01-01"
rangeT <- paste(startT,"::",endT,sep ="")
tAAPL <- AAPL[,6][rangeT]
tTWTR <- TWTR[,6][rangeT]
tMSFT <- MSFT[,6][rangeT]
#define out of sample set
startO <- "2016-02-01"
endO <- "2017-12-01"
rangeO <- paste(startO,"::",endO,sep ="")
oAAPL <- AAPL[,6][rangeO]
oTWTR <- TWTR[,6][rangeO]
oMSFT <- MSFT[,6][rangeO]
#compute price differences on in-sample data
pdtAAPL <- diff(tAAPL)[-1]
pdtTWTR <- diff(tTWTR)[-1]
pdtMSFT <- diff(tMSFT)[-1]
#build the model
model <- lm(pdtAAPL ~ pdtTWTR - 1)
#extract the hedge ratio
hr <- as.numeric(model$coefficients[1])
spreadT <- tAAPL - hr * tTWTR
spreadT1 <- tAAPL - hr * tMSFT
spreadT2 <- tTWTR - hr * tMSFT
#compute statistics of the spread
meanT <- as.numeric(mean(spreadT,na.rm=TRUE))
sdT <- as.numeric(sd(spreadT,na.rm=TRUE))
upperThr <- meanT + 1 * sdT
lowerThr <- meanT - 1 * sdT
hist(spreadT, col = "blue", breaks = 100, main = "Spread Histogram (AAPL vs. TWTR)")
abline(v = meanT, col = "red", lwd = 2)
```
###APPL vs MSFT Spread
```{r,warning=FALSE,message=FALSE}
require(quantmod)
symbols <- c("AAPL", "TWTR","MSFT")
c<- getSymbols(symbols)
#define training set
startT <- "2016-01-01"
endT <- "2017-01-01"
rangeT <- paste(startT,"::",endT,sep ="")
tAAPL <- AAPL[,6][rangeT]
tTWTR <- TWTR[,6][rangeT]
tMSFT <- MSFT[,6][rangeT]
#define out of sample set
startO <- "2016-02-01"
endO <- "2017-12-01"
rangeO <- paste(startO,"::",endO,sep ="")
oAAPL <- AAPL[,6][rangeO]
oTWTR <- TWTR[,6][rangeO]
oMSFT <- MSFT[,6][rangeO]
#compute price differences on in-sample data
pdtAAPL <- diff(tAAPL)[-1]
pdtTWTR <- diff(tTWTR)[-1]
pdtMSFT <- diff(tMSFT)[-1]
#build the model
model <- lm(pdtAAPL ~ pdtMSFT - 1)
#extract the hedge ratio
hr <- as.numeric(model$coefficients[1])
spreadT <- tAAPL - hr * tMSFT
spreadT1 <- tAAPL - hr * tMSFT
spreadT2 <- tTWTR - hr * tMSFT
#compute statistics of the spread
meanT <- as.numeric(mean(spreadT,na.rm=TRUE))
sdT <- as.numeric(sd(spreadT,na.rm=TRUE))
upperThr <- meanT + 1 * sdT
lowerThr <- meanT - 1 * sdT
hist(spreadT, col = "blue", breaks = 100, main = "Spread Histogram (AAPL vs. MSFT)")
abline(v = meanT, col = "red", lwd = 2)
```
###TWTR vs MSFT Spread
```{r,warning=FALSE,message=FALSE}
require(quantmod)
symbols <- c("AAPL", "TWTR","MSFT")
c<- getSymbols(symbols)
#define training set
startT <- "2016-01-01"
endT <- "2017-01-01"
rangeT <- paste(startT,"::",endT,sep ="")
tAAPL <- AAPL[,6][rangeT]
tTWTR <- TWTR[,6][rangeT]
tMSFT <- MSFT[,6][rangeT]
#define out of sample set
startO <- "2016-02-01"
endO <- "2017-12-01"
rangeO <- paste(startO,"::",endO,sep ="")
oAAPL <- AAPL[,6][rangeO]
oTWTR <- TWTR[,6][rangeO]
oMSFT <- MSFT[,6][rangeO]
#compute price differences on in-sample data
pdtAAPL <- diff(tAAPL)[-1]
pdtTWTR <- diff(tTWTR)[-1]
pdtMSFT <- diff(tMSFT)[-1]
#build the model
model <- lm(pdtTWTR ~ pdtMSFT - 1)
#extract the hedge ratio
hr <- as.numeric(model$coefficients[1])
spreadT <- tTWTR - hr * tMSFT
spreadT1 <- tAAPL - hr * tMSFT
spreadT2 <- tTWTR - hr * tMSFT
#compute statistics of the spread
meanT <- as.numeric(mean(spreadT,na.rm=TRUE))
sdT <- as.numeric(sd(spreadT,na.rm=TRUE))
upperThr <- meanT + 1 * sdT
lowerThr <- meanT - 1 * sdT
hist(spreadT, col = "blue", breaks = 100, main = "Spread Histogram (TWTR vs. MSFT)")
abline(v = meanT, col = "red", lwd = 2)
```
Stock Comparission
=======================================================================
Sidebar {.sidebar}
-----------------------------------------------------------------------
### Correlation and Individual Analysis of Stocks
Row {data-height=700}
-----------------------------------------------------------------------
### Correlation Analysis of AAPL, TWTR and MSFT Stocks
```{r,echo=FALSE,warning=FALSE,message=FALSE}
library(quantmod)
c<- getSymbols(c("AAPL","TWTR","MSFT"))
library(PerformanceAnalytics)
Data <- cbind(diff(log(Cl(AAPL))),diff(log(Cl(TWTR))),diff(log(Cl(MSFT))))
chart.Correlation(Data)
```
Row {data-height=600}
-----------------------------------------------------------------------
### AAPL: Price and Volume
```{r,echo=FALSE,warning=FALSE,message=FALSE}
library(quantmod)
chartSeries(AAPL, subset='last 3 months')
```
### TWTR: Price and Volume
```{r,echo=FALSE,warning=FALSE,message=FALSE}
library(quantmod)
chartSeries(TWTR, subset='last 3 months')
```
### MSFT: Price and Volume
```{r,echo=FALSE,warning=FALSE,message=FALSE}
library(quantmod)
chartSeries(MSFT, subset='last 3 months')
```