diff --git a/ARIMA_2_1_2_simulation.Rmd b/ARIMA_2_1_2_simulation.Rmd new file mode 100644 index 0000000..0ea161d --- /dev/null +++ b/ARIMA_2_1_2_simulation.Rmd @@ -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))) +``` + diff --git a/Introduction to tidyquant.Rmd b/Introduction to tidyquant.Rmd new file mode 100644 index 0000000..29c2265 --- /dev/null +++ b/Introduction to tidyquant.Rmd @@ -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]) +}) + +``` + + + + diff --git a/Time_Series_Analysis.Rmd b/Time_Series_Analysis.Rmd new file mode 100644 index 0000000..53865c1 --- /dev/null +++ b/Time_Series_Analysis.Rmd @@ -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() +``` diff --git a/Time_Series_Simulation_And_Fitting.Rmd b/Time_Series_Simulation_And_Fitting.Rmd new file mode 100644 index 0000000..cc23e1e --- /dev/null +++ b/Time_Series_Simulation_And_Fitting.Rmd @@ -0,0 +1,70 @@ +--- +title: "Time Series Simulation and Fitting" +output: html_notebook +--- + +The xts package is called Extensible Time Series. +Load needed Libraries +```{r} +library(glue) +library(xts) +``` +Set parameters for the simulation. +```{r} +l=10000 +``` +Look at LC_TIME. +```{r} +Sys.getlocale("LC_TIME") +``` +A time series contains two pieces, a `Index` containing a date index and `Data`. +Use `read.zoo` to read a file from the zoo package. The file was downloaded from +`https://raw.githubusercontent.com/cran/zoo/master/vignettes/demo1.txt` +to my project directory `data`. +The format uses the standard date formating codes. +%d +: Numeric day: 13 +%b +: Three diget month: Feb +%Y: Four digit year: 2005 +sep +: Separator in the text file between the *Time* index and the *Data* value. +```{r} +z <- read.zoo(file.path("data","demo1.txt"),sep = "|", format="%d %b %Y") +glue("The class of z is {class(z)}") +str(z) +``` + + +```{r} +dates <-xts::timeBasedSeq('20060204//m',length=l) +``` + +Simulate a time series that is observed every day instead of every month using '2006//d'. +```{r} +parameters <- list( model=list(ar=c(0,0,0,0.8), ma=c(0.5)), n=l) +y <- ts(do.call(arima.sim,parameters),frequency=4) +fit_y <- arima(y, order=c(0,0,1), seasonal=list(order=c(1,0,0)), include.mean=FALSE) +c(coef(fit_y), sigma2 = fit_y$sigma2) +x <-xts(y,order.by = dates,descr="Simulated xts object") +colnames(x) <- c("rate") +attr(x,'frequency') + +# Fit without the frequency. Seasonal adjustment will be wrong. +fit_auto <- auto.arima(x) +broom::tidy(fit_auto) +sweep::sw_tidy(fit_auto) +sweep::sw_tidy(x) + +attr(x,'frequency') <-4 +attr(x,'frequency') + +fit_x <- arima(x, order=c(0,0,1), seasonal=list(order=c(1,0,0)),include.mean=FALSE) +c(coef(fit_x), sigma2 = fit_x$sigma2) + +fit_auto <- auto.arima(x) +broom::tidy(fit_auto) +install.packages('tsdecomp') +library(tsdecomp) +roots.allocation(fit_auto) +``` diff --git a/lecture_1_time_series.Rmd b/lecture_1_time_series.Rmd new file mode 100644 index 0000000..db205e8 --- /dev/null +++ b/lecture_1_time_series.Rmd @@ -0,0 +1,224 @@ +--- +title: "Time Series Analysis" +subtitle: Session 1 +output: + html_document: + df_print: paged +--- + +This R notebook provides code to accompany the first *Time Series* lecture. + +Attach libraries. These packages are referenced below. +```{r} +library(devtools) +library(zoo) +library(timeSeries) +library(forecast) +library(tidyquant) +library(tidyverse) +library(plotly) +``` + +Reference +: See *Time Series Data* on the Sollers Site. + +# Time Series Analysis +## Time Series Data. +Time series data represent observations of an outcome measured over time. We will start by +reviewing facilities in R to get time series data and then to plot that data. +Later we will learn how to model, predict and forcast time series data. + +We will use examples and note what technique each example demonostrates. +## Gas prices. +The package `tidyquant` has some useful facilities for getting data. It tries to provide +a link between time series analysis packages and tidyverse packages. + +In addition to `tidyquant`, `tidyverse` is a fundemental set of packages. +The tidyverse is a name given to a set of packages and the concept of keeping data organized in +a table of one row per observation. Data analysis requires keeping the data in a useful +structure and the tidyverse standard is an attempt to make that happen. + +Here the `tidyquant::tq_get` function is used to get the Natual Gas Index(NGI). +The data will be extracted and plotted. Note the class if the returned object. + +```{r} +ngi <- tq_get("NGI",complete_cases = TRUE) +class(ngi) +``` +Take a look at the last few observations of the data frame. +```{r} +tail(ngi) +``` +Note that there is a date field as well as other measures. The format is supposed to look like a table +you would see in a spreadsheet program. It is `tidy`. + +```{r} +class(ngi) +``` + +Next we can create a typical time series plot. We will start with a static plot. +The function `plot` adjusts for the class of the object being plotted. +It is a good start. + +```{r} +plot(ngi) +``` +We can do better with the plot by constructing our own. +Here is a fairly popular plotting function `ggplot2`. + +`ggplot` uses aesthetic (aes), which defines how the data is mapped to the grid. +Then geometry specifies how the data is displayed. +```{r} +p <- ngi %>% + ggplot() + + aes(x=date,y=open) + + geom_point() + + labs(title = "Natural Gas Index", subtitle = "adjusted", + x = "Date",y = "Open Price") +p +``` +There is another package that will take the output of `ggplot` and add tool tips. +The package `plotly` adds some interactive abilities to the plot. +`plotly` does a lot of other stuff and is probably better than ggplot. +`plotly` translates `ggplot` syntax because `ggplot` is so popular, but +it also has its own plot layering syntax. + +Here a ggplot object is converted to a plotly object. Notice that you can hover over the +points and there is a tool bar. +```{r} +ggplotly(p) +``` +Here is a data set of rain in LA. There is a data function that can be use to load data sets. +Package writers can include data using the R Data package. +```{r} +library(TSA) +data(larain) +``` + +`larain` is a time series object. +A time series object has a date index and a data column. +Even in the more advanced representations of a time series, +there is a date index and data. Here there is only one column of data, +in `tidyquant` there can be many columns of data. +Keeping the date or time index associated with the data is what makes +the time series structure unique. This also makes it more difficult to +work with than other `data.frame` objects. +```{r} +class(larain) +``` +The index is the date part. +```{r} +index(larain) +``` +The data can be extracted using `coredata` +```{r} +coredata(larain) +``` +One important property of a time series is frequency. +A time series has a frequency. For example if the data is monthly and there is some periodicity, +then the frequency will be 12. The larain data is yearly and the frequency is 1. +```{r} +frequency(larain) +``` + +There is a plot method for this data. +```{r} +plot(larain,main="LA Rain Data") +``` + +The function actually called is `plot.zoo`. `plot` is a generic function that looks for the +right method to fit the class of the data. +```{r} +plot.zoo(larain,main="LA Rain") +``` +Base r has a `ts` function. `larain` is a ts object. +There is also a package called `zoo`. Most more advanced time series objects +extend `zoo`. Extend means the take the methods of zoo and add to them. +```{r} +larain_z <- zoo(larain) +class(larain_z) +``` +```{r} +print(larain_z) +``` +```{r} +plot(larain_z,main="LA Rain") +``` +The zoo package alows you to do much more with the time series data +```{r} +methods(class="ts") +``` +```{r} +methods(class="zoo") +``` +You can see that zoo adds a lot including date functions like `as.POSIXct` and the +ability to handle multiple columns and conversion to a data frame using `as.data.frame`. + +```{r} +methods(class="yearmon") +``` +If you need a `data.frame` for things like ploting, you can use `fortify`. +The fortify function will separate the date index and the core data so the +date can be plotted on the x axis and the core data on the y-axis. +```{r} +larain_f <- fortify(larain) +head(larain_f) +``` +The result is a `data.frame`. +```{r} +class(larain_f) +``` +```{r} +larain_f %>% + ggplot() + + aes(x=x,y=y) + + geom_point() + + labs(title = "LA Rain") +``` + +There is an extension of the `zoo` package called `xts` for extensible time series. + +```{r} +larain_zoo <- zoo(larain) +class(larain_zoo) +``` +Here is what it looks like. +```{r} +head(larain_zoo) +``` +There is a `as.data.frame` method in zoo used to convert to a data frame. +```{r} +as.data.frame(larain_zoo) +``` +There is also a fortify method. +Note that the fortify method produces a column withe the date index whereas +`as.data.frame` does not. +```{r} +larain_zoo_fortify <- fortify(larain_zoo) +larain_zoo_fortify +``` +If is possible to modify the names of the result +```{r} +names(larain_zoo_fortify) <- c("Date","rain") +head(larain_zoo_fortify) +``` +```{r} +larain_zoo_fortify %>% + ggplot() + + aes(x=Date,y=rain) + + geom_point() + + labs(title = "LA Rain") +``` +There is a package called `forecast` that provides some modeling abilities. +```{r} +methods(class="forecast") +``` + +```{r} +forecast(larain_zoo) +``` +```{r} +fit <- auto.arima(larain) +forecast(fit) +``` + diff --git a/practice_1.Rmd b/practice_1.Rmd new file mode 100644 index 0000000..54a815d --- /dev/null +++ b/practice_1.Rmd @@ -0,0 +1,85 @@ +--- +title: "R Notebook" +output: html_notebook +--- + +```{r} +source('library.R') +``` + +```{r} +tq_get_options() +``` +```{r} +from <- today() - years(1) +AAPL <- tq_get("AAPL", get = "stock.prices", from = from) +AAPL +``` +```{r} +# Get of list of the tq_mutate() options +tq_mutate_fun_options() %>% + select(zoo) %>% + str() +``` +```{r} +AAPL %>% + tq_mutate(ohlc_fun = Cl, mutate_fun = SMA, n = 15) %>% + rename(SMA.15 = SMA) %>% + tq_mutate(ohlc_fun = Cl, mutate_fun = SMA, n = 50) %>% + rename(SMA.50 = SMA) +``` +```{r} +AAPL %>% + tq_mutate_xy(x = close, mutate_fun = SMA, n = 15) %>% + rename(SMA.15 = SMA) %>% + tq_mutate_xy(x = close, mutate_fun = SMA, n = 50) %>% + rename(SMA.50 = SMA) +``` +```{r} +AAPL %>% + mutate(SMA.15 = SMA(close,n=10)) +``` + + +```{r} + +``` + + + + +```{r} +1 + 4 +``` + + +```{r} +aapl_stock_prices<- tq_get("AAPL") +``` +```{r} +mult_stocks <- tq_get(c("FB","AMZN"), +get = "stock.prices", +from = "2016-01-01", +to = "2017-01-01") +``` + + +```{r} +acf(mult_stocks$open) +``` +```{r} +pacf(mult_stocks$open ) +``` +```{r} +stats::Box.test(mult_stocks$open) +``` + + + +```{r} +arima(mult_stocks$open,order = (1 0 2)) +``` + + + + diff --git a/practice_2.Rmd b/practice_2.Rmd new file mode 100644 index 0000000..099a811 --- /dev/null +++ b/practice_2.Rmd @@ -0,0 +1,34 @@ +--- +title: "R Notebook" +output: html_notebook +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set( + echo = FALSE, + message = FALSE, + warning = FALSE +) +``` +```{r} +source('library.R') +``` +```{r} +library(tidyverse) +``` + + +```{r} +count = 1000 +average <- 14 +theta_1 <- 0.7 +theta_2 <- 0.5 +omega <- rnorm(count) + + +test <- tibble(date = seq.Date(from = today("EST") - count +1, to = today("EST"), "days"), + average = average, + omega = rnorm) + +``` + diff --git a/practice_3.Rmd b/practice_3.Rmd new file mode 100644 index 0000000..7243eb9 --- /dev/null +++ b/practice_3.Rmd @@ -0,0 +1,80 @@ +--- +title: "R Notebook" +output: html_notebook +--- +```{r} +#install_github("vqv/ggbiplot") +library(ggbiplot) +data(iris) +pca.obj <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE) +str(pca.obj) +P <- ggbiplot(pca.obj, + obs.scale = 1, + var.scale=1, + ellipse=T, + circle=F, + varname.size=3, + var.axes=T, + groups=iris$Species, #no need for coloring, I'm making the points invisible + alpha=0) #invisible points, I add them below +P$layers <- c(geom_point(aes(color=iris$Species), cex=5), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot) +png(filename="test.png", height=600, width=600) +print(#or ggsave() + P +) +dev.off() +``` +```{r} +library(magrittr) +``` + +```{r} +#introduce NAs +iris$Sepal.Length[sample(1:150, 5)] <- NA +iris$Sepal.Width[sample(1:150, 5)] <- NA +iris$Petal.Length[sample(1:150, 5)] <- NA +iris$Petal.Width[sample(1:150, 5)] <- NA +#pca.obj2 <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE) #cannot use prcomp with NAs +#source("https://bioconductor.org/biocLite.R") +#biocLite("pcaMethods") +library(pcaMethods) +pca.obj2 <- pcaMethods::pca(iris[,1:4], method="nipals", nPcs=3, center=TRUE, scale.=TRUE) +class(pca.obj2) #class pcaRes pcaMethods +pca.obj2 +str(pca.obj2) +ggbiplot(pca.obj2) +pca.obj2 %>% na.omit() %>% ggbiplot() +``` +```{r} +library(pcaMethods) +``` +```{r} +data("metaboliteDataComplete") +mdc <- scale(metaboliteDataComplete, center=TRUE, scale=FALSE) +length(mdc) +class(mdc) +cond <- runif(length(mdc)) < 0.05 +mdcOut <- mdc +mdcOut[cond] <- 10 +``` +```{r} +resSvd <- pca(mdc, method = "svd", nPcs = 5, center = FALSE) +resSvdOut <- pca(mdcOut, method = "svd", nPcs = 5, center = FALSE) +resRobSvd <- pca(mdcOut, method = "robustPca", nPcs = 5, center = FALSE) +``` + +```{r} +par(mfrow=c(2,2)) +plot(loadings(resSvd)[,1], loadings(resSvdOut)[,1], +xlab = "Loading 1 SVD", ylab = "Loading 1 SVD with outliers") +plot(loadings(resSvd)[,1], loadings(resRobSvd)[,1], +xlab = "Loading 1 SVD", ylab = "Loading 1 robustSVD with outliers") +plot(loadings(resSvd)[,1], loadings(resPPCA)[,1], +xlab = "Loading 1 SVD", ylab = "Loading 1 PPCA with outliers = NA") +plot(loadings(resRobSvd)[,1], loadings(resPPCA)[,1], +xlab = "Loading 1 roubst SVD with outliers", ylab = "Loading 1 svdImpute with outliers = NA") + +``` + + + diff --git a/tidyquant_Tutorial.Rmd b/tidyquant_Tutorial.Rmd new file mode 100644 index 0000000..ce83927 --- /dev/null +++ b/tidyquant_Tutorial.Rmd @@ -0,0 +1,87 @@ +--- +title: "tidyquant Tutorial Workthrough" +subtitle: Work through the tidyquant Tutorial +output: + html_document: + df_print: paged +--- + +This is a work through of the tutorial at +source: https://quantdev.ssri.psu.edu/sites/qdev/files/tidyquant_tutorial_Gray.html . + +# Install needed Libraries +```{r} +library(tidyquant) +library(ggplot2) +``` + +Download data using tq_get + +```{r} +google <- tq_get(x = "GOOG") +head(google) +``` +```{r} +syse <- tq_exchange("NYSE") +``` + +```{r} +head(syse) +``` +```{r} +nyse <- syse +``` + +```{r} +nyse$symbol +``` +```{r} +nyse$symbol[1] +``` + +```{r} +full <- tq_get(x = nyse$symbol[1], get = "stock.prices") %>% + add_column(symbol = nyse$symbol[1]) +``` + +```{r} +for (s in nyse$symbol[2:20]){ + single <- try(tq_get(x = s, get = "stock.prices") %>% + add_column(symbol = s)) + full <- try(rbind(full, single)) +} +``` + +```{r} +head(full) +``` + +```{r} +str(full) +``` +```{r} +unique(full$symbol) +``` + +```{r} +ggplot(data = full) + + aes(x = date, y = adjusted, color = symbol) + + geom_line(aes(group = symbol)) + + theme_classic() + + theme(legend.position = "none") + + ylab("Adjusted Price") + + ggtitle("10 Years of NYSE Stock Prices") +``` + + + +```{r} +getwd() +write.csv(full, file = "nyse_stock_prices.csv") +``` + + + + + +