<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/time-series-analysis-in-r-part-3-getting-data-from-quandl/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/time-series-analysis-in-r-part-3-getting-data-from-quandl/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is part 3 of a multi-part guide on working with time series data in R. You can find the previous parts here: <a rel="nofollow" href="https://datascienceplus.com/time-series-analysis-in-r-part-1-the-time-series-object/" target="_blank">Part 1</a>, <a rel="nofollow" href="https://datascienceplus.com/time-series-analysis-in-r-part-2-time-series-transformations/" target="_blank">Part 2</a>.</p>
<p>Generated data like that used in Parts 1 and 2 is great for sake of example, but not very interesting to work with. So let’s get some real-world data that we can work with for the rest of this tutorial. There are countless sources of time series data that we can use including some that are already included in R and some of its packages. We’ll use some of this data in examples. But I’d like to expand our horizons a bit.</p>
<p><a rel="nofollow" href="https://www.quandl.com/" target="_blank">Quandl</a> has a great warehouse of financial and economic data, some of which is free. We can use the Quandl R package to obtain data using the API. If you do not have the package installed in R, you can do so using:</p>
<pre>install.packages('Quandl')</pre>
<p>You can browse the site for a series of interest and get its API code. Below is an example of using the Quandl R package to get housing price index data. This data originally comes from the Yale Department of Economics and is featured in Robert Shiller’s book “Irrational Exuberance”. We use the <code>Quandl</code> function and pass it the code of the series we want. We also specify “ts” for the <code>type</code> argument so that the data is imported as an R <code>ts</code> object. We can also specify start and end dates for the series. This particular data series goes all the way back to 1890. That is far more than we need so I specify that I want data starting in January of 1990. I do not supply a value for the <code>end_date</code> argument because I want the most recent data available. You can find this data on the web <a rel="nofollow" href="https://www.quandl.com/data/YALE/NHPI-Historical-Housing-Market-Data-Nominal-Home-Price-Index" target="_blank">here</a>.</p>
<pre>library(Quandl)
hpidata &lt;- Quandl(&quot;YALE/NHPI&quot;, type=&quot;ts&quot;, start_date=&quot;1990-01-01&quot;)
plot.ts(hpidata, main = &quot;Robert Shiller's Nominal Home Price Index&quot;)</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_1.png?ssl=1" target="_blank"><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_1.png?w=450&ssl=1" alt="" class="wp-image-8903" data-recalc-dims="1"></a></p>
<p>While we are here, let’s grab some additional data series for later use. Below, I get data on US GDP and US personal income, and the University of Michigan Consumer Survey on selling conditions for houses. Again I obtained the relevant codes by browsing the <code>Quandl</code> website. The data are located on the web <a rel="nofollow" href="https://www.quandl.com/data/FRED/GDP-Gross-Domestic-Product" target="_blank">here</a>, <a rel="nofollow" href="https://www.quandl.com/data/FRED/PINCOME-Personal-Income" target="_blank">here</a>, and <a rel="nofollow" href="https://www.quandl.com/data/UMICH/SOC43-University-of-Michigan-Consumer-Survey-Selling-Conditions-for-Houses" target="_blank">here</a>.</p>
<pre>gdpdata &lt;- Quandl(&quot;FRED/GDP&quot;, type=&quot;ts&quot;, start_date=&quot;1990-01-01&quot;)
pidata &lt;- Quandl(&quot;FRED/PINCOME&quot;, type=&quot;ts&quot;, start_date=&quot;1990-01-01&quot;)
umdata &lt;- Quandl(&quot;UMICH/SOC43&quot;, type=&quot;ts&quot;)[, 1]
plot.ts(cbind(gdpdata, pidata),  main=&quot;US GPD and Personal Income, billions $&quot;)</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_2.png?ssl=1" target="_blank"><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_2.png?w=450&ssl=1" alt="" class="wp-image-8904" data-recalc-dims="1"></a></p>
<pre>plot.ts(umdata, main = &quot;University of Michigan Consumer Survey, Selling Conditions for Houses&quot;)</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_3.png?ssl=1" target="_blank"><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_3.png?w=450&ssl=1" alt="" class="wp-image-8905" data-recalc-dims="1"></a></p>
<p>The Quandl API also has some basic options for data preprocessing. The US GDP data is in quarterly frequency, but assume we want annual data. We can use the <code>collapse</code> argument to collapse the data to a lower frequency. Here we covert the data to annual as we import it.</p>
<pre>gdpdata_ann &lt;- Quandl(&quot;FRED/GDP&quot;, type=&quot;ts&quot;, start_date=&quot;1990-01-01&quot;, collapse=&quot;annual&quot;)
frequency(gdpdata_ann)
<em>[1] 1</em></pre>
<p>We can also transform our data on the fly as its imported. The <code>Quandl</code> function has a argument <code>transform</code> that allows us to specify the type of data transformation we want to perform. There are five options – “diff“, ”rdiff“, ”normalize“, ”cumul“, ”rdiff_from“. Specifying the <code>transform</code> argument as”diff” returns the simple difference, “rdiff” yields the percentage change, “normalize” gives an index where each value is that value divided by the first period value and multiplied by 100, “cumul” gives the cumulative sum, and “rdiff_from” gives each value as the percent difference between itself and the last value in the series. For more details on these transformations, check the API documentation <a rel="nofollow" href="https://docs.quandl.com/v1.0/docs/parameters-2" target="_blank">here</a>.</p>
<p>For example, here we get the data in percent change form:</p>
<pre>gdpdata_pc &lt;- Quandl(&quot;FRED/GDP&quot;, type=&quot;ts&quot;, start_date=&quot;1990-01-01&quot;, transform=&quot;rdiff&quot;)
plot.ts(gdpdata_pc * 100, ylab= &quot;% change&quot;, main=&quot;US Gross Domestic Product, % change&quot;)</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_4.png?ssl=1" target="_blank"><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/tsguide3_4.png?w=450&ssl=1" alt="" class="wp-image-8906" data-recalc-dims="1"></a></p>
<p>You can find additional documentation on using the Quandl R package <a rel="nofollow" href="https://docs.quandl.com/docs/time-series-1" target="_blank">here</a>. I’d also encourage you to check out the vast amount of free data that is available on the site. The API allows a maximum of 50 calls per day from anonymous users. You can sign up for an account and get your own API key, which will allow you to make as many calls to the API as you like (within reason of course).</p>
<p>In Part 4, we will discuss visualization of time series data. We’ll go beyond the base R plotting functions we’ve used up until now and learn to create better-looking and more functional plots.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/pulling-data-out-of-census-spreadsheets-using-r/" title="Permanent Link toPulling Data Out of Census Spreadsheets Using R" target="_blank">Pulling Data Out of Census Spreadsheets Using R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/extracting-tables-from-pdfs-in-r-using-the-tabulizer-package/" title="Permanent Link toExtracting Tables from PDFs in R using the Tabulizer Package" target="_blank">Extracting Tables from PDFs in R using the Tabulizer Package</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/extract-twitter-data-automatically-using-scheduler-r-package/" title="Permanent Link toExtract Twitter Data Automatically using Scheduler R package" target="_blank">Extract Twitter Data Automatically using Scheduler R package</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/introduction-to-time-series-with-json-data/" title="Permanent Link toAn Introduction to Time Series with JSON Data" target="_blank">An Introduction to Time Series with JSON Data</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/get-your-data-into-r/" title="Permanent Link toGet Your Data into R: Import Data from SPSS, Stata, SAS, CSV or TXT" target="_blank">Get Your Data into R: Import Data from SPSS, Stata, SAS, CSV or TXT</a></li>
</ol></strong>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/time-series-analysis-in-r-part-3-getting-data-from-quandl/"> R Programming – DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/JPlHfV5LAkQ" height="1"></a></div>