Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
We’ve got an exciting week ahead of us at Business Science: we’re launching our first ever Business Science Demo Week. Every day this week we are demoing an R package: tidyquant
(Monday), timetk
(Tuesday), sweep
(Wednesday), tibbletime
(Thursday) and h2o
(Friday)! That’s five packages in five days! We’ll give you intel on what you need to know about these packages to go from zero to hero. First up is tidyquant
, our flagship package that’s useful for financial and time series analysis. Here we go!
tidyquant: What’s It Used For?
Six reasons to use tidyquant
:
-
Getting web data from Yahoo! Finance, FRED Database, Quandl and more
-
Tidy application of financial and time series functions from
xts
,zoo
,quantmod
,TTR
andPerformanceAnalytics
-
Graphing: Beautiful themes and financial geoms (e.g.
geom_ma
) -
Aggregating portfolios
-
Financial performance analysis and portfolio attribution metrics
-
Great base for financial and time series analysis: The tidyquant stack automatically loads the
tidyverse
and various time series / financial packages behind the scenes making it a great starting point for any financial or time series analysis
We’ll go over first two points in this demo. See the documentation for other topics.
Load Libraries
If you don’t have it installed, install tidyquant
.
Load the tidyquant
package, which is all you need for the demo.
Getting data: tq_get
Use tq_get()
to get data from the web. There’s a bunch of API’s it connects to including Yahoo! Finance, FRED Economic Database, Quandl, and more!
Getting Stock Prices (Yahoo! Finance)
Pass a vector of stock symbols to tq_get()
setting get = "stock.prices"
. You can add from
and to
arguments to bound the daily stock prices to a window.
We can plot the results very easily using ggplot2
. We’ll use tidyquant
themes (theme_tq()
and scale_color_tq()
) for nice financial / business-ready graphs.
Getting Economic Data (FRED)
This example comes from Deputy Chief Economist at Freddie Mac, Leonard Kieffer’s recent article A (TIDYQUANT)UM OF SOLACE. We’ll use tq_get()
with the argument get = "economic.data"
to retrieve data from the FRED economic database.
Pass a vector of FRED symbols to tq_get()
.
Like the financial data, we can plot the results very easily using ggplot2
. We’ll again use tidyquant
themes (theme_tq()
and scale_color_tq()
) for nice financial / business-ready graphs.
Mutating Data: tq_transmute and tq_mutate
The tq_transmute()
and tq_mutate()
functions are used to apply xts
, zoo
, and quantmod
functions in a “tidy” way. We’ll jump right into how to use, but see below for Available Functions that are integrated into tidyquant
.
tq_transmute
The difference between tq_transmute()
and tq_mutate()
is that tq_transmute()
returns a new data frame whereas tq_mutate()
grows the existing data frame width-wise (i.e. adds columns). The tq_transmute()
function is most useful when periodicity changes the number of rows in the data.
Periodicity change with tq_transmute
Here’s an example of changing the periodicity from daily to monthly. You need to use tq_transmute()
for this operation because the number of rows changes.
A simple reason you might want to perform a periodicity change is to reduce the amount of data. A couple notes:
- We use
theme_tq()
andscale_color_tq()
to create beautiful ggplots that are “business ready” - If you need to change periodicity and do other time-based operations, pay attention on Thursday when we demo the new
tibbletime
package…tibbletime
takes time-based operations such as period changes to another level!!!
Before transformation – Too much data
After transformation – Easy to understand
Much clearer when changed to a monthly scale via tq_transmute()
.
tq_mutate
The tq_mutate()
function returns the existing data frame column-binded with the output of the xts-based operation. Because of this, tq_mutate()
is most useful when the number of columns returned is more than one (dplyr::mutate()
doesn’t work in these situations).
Lags with tq_mutate
An example of this is with lag.xts
. Typically we want more than one lag, which is where tq_mutate()
shines. We’ll get the first five lags plus the original data.
Rolling functions with tq_mutate
Another example is applying a rolling function via the xts-based roll.apply()
. Let’s apply the quantile()
function to get rolling quantiles. We’ll specify the following arguments for each function:
tq_mutate
arguments:
select = adjusted
, We only want the adjusted column to be kept. We can leave blank or select a different subset of columns to be returned in addition to the output.mutate_fun = rollapply
, This is the xts-based function that will be applied in a “tidy” way (to groups).
rollapply
arguments:
width = 5
, The width argument tellsrollapply
how many periods to use in the window calculation.by.column = FALSE
, Therollapply()
function defaults to applying functions to each column separately, however we want it to consider all columns in the operation.FUN = quantile
: Thequantile()
function is applied in a rolling fashion.
quantile
arguments:
probs = c(0, 0.025, ...)
, These are the probabilities that will be returned.na.rm = TRUE
, Instructsquantile
to removeNA
values if present.
Available Functions
We’ve just briefly touched on the xts-based functions that are integrated with tq_transmute
and tq_mutate
. There are a lot more xts-based functions that can be applied in a “tidy” way! Use tq_transmute_fun_options()
to investigate other available functions.
Next Steps
We’ve only scratched the surface of tidyquant
. There’s so much more to learn including tq_portfolio
, tq_performance
, charting capabilities, and more. Here are a few resources to help you along the way:
- Business Science Software
- tidyquant documentation
- tidyquant GitHub Page
- Business Science Insights Blog
Announcements
We have a busy couple of weeks. In addition to Demo Week, we have:
DataTalk
On Thursday, October 26 at 7PM EST, Matt will be giving a FREE LIVE #DataTalk on Machine Learning for Recruitment and Reducing Employee Attrition. You can sign up for a reminder at the Experian Data Lab website.
#MachineLearning for Reducing Employee Attrition @BizScienchttps://t.co/vlxmjWzKCL#ML #AI #HR #IoTT #IoT #DL #BigData #Tech #Cloud #Jobs pic.twitter.com/dF5Znf10Sk
— Experian DataLab (@ExperianDataLab) October 18, 2017
EARL
On Friday, November 3rd, Matt will be presenting at the EARL Conference on HR Analytics: Using Machine Learning to Predict Employee Turnover.
?Hey #rstats. I'll be presenting @earlconf on #MachineLearning applications in #HumanResources. Get 15% off tickets: https://t.co/b6JUQ6BSTl
— Matt Dancho (@mdancho84) October 11, 2017
Courses
Based on recent demand, we are considering offering application-specific machine learning courses for Data Scientists. The content will be business problems similar to our popular articles:
-
HR Analytics: Using Machine Learning to Predict Employee Turnover
-
Sales Analytics: How To Use Machine Learning to Predict and Optimize Product Backorders
The student will learn from Business Science how to implement cutting edge data science to solve business problems. Please let us know if you are interested. You can leave comments as to what you would like to see at the bottom of the post in Disqus.
About Business Science
Business Science specializes in “ROI-driven data science”. Our focus is machine learning and data science in business applications. We help businesses that seek to add this competitive advantage but may not have the resources currently to implement predictive analytics. Business Science works with clients primarily in small to medium size businesses, guiding these organizations in expanding predictive analytics while executing on ROI generating projects. Visit the Business Science website or contact us to learn more!
Follow Business Science on Social Media
- @bizScienc is on twitter!
- Check us out on Facebook page!
- Check us out on LinkedIn!
- Sign up for our insights blog to stay updated!
- If you like our software, star our GitHub packages!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.