<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/use-r-to-calculate-inflation-with-the-blscraper-package/"></div></div><div><a data-url="https://www.r-bloggers.com/use-r-to-calculate-inflation-with-the-blscraper-package/" data-counturl="https://www.r-bloggers.com/use-r-to-calculate-inflation-with-the-blscraper-package/" data-count="horizontal" data-text="Use R to Calculate Inflation with the blscrapeR Package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.datascienceriot.com/use-r-to-calculate-inflation-with-the-blscraper-package/kris/"> R Tricks – Data Science Riot!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The Consumer Price Index (CPI) is the main standard for tracking the inflation of the U.S. dollar. The various CPI measures are published monthly by the <a target="_blank" href="http://data.bls.gov/cgi-bin/surveymost?cu">Bureau of Labor Statistics</a>.</p>
<p>For this walk-through, we will be using the <a target="_blank" href="https://github.com/keberwein/blscrapeR">blcsrapeR package</a> to download our data from the BLS and perform the calculation. The blscrapeR package can be installed via CRAN. <code>install.packages(“blscrapeR”)</code></p>
<p><strong>CPI: Tracking Inflation</strong></p>
<p>Although there are many measures of inflation, the CPI’s “Consumer Price Index for All Urban Consumers: All Items” is normally the headline inflation rate one would hear about on the news (<a target="_blank" href="https://fred.stlouisfed.org/series/CPIAUCSL">see FRED</a>).</p>
<p>Getting these data from the blscrapeR package is easy enough:</p>
<pre>
library(blscrapeR)
df &lt;- bls_api(&quot;CUSR0000SA0&quot;)
head(df)
</pre>
<p>Due to the limitations of the API, we are only able to gather twenty years of data per request. However the formula for calculating inflation is based on the 1980 dollar, so the data from the API aren't sufficient.</p>
<p>The package includes a function that collects information form the CPI beginning at 1947 and calculates inflation. Since these data are not pulled directly from the API, the inflation_adjust() function does not count against your daily API call limit.</p>
<p>To find out the value of a 1995 dollar in 2015, we just make a simple function call. Note that in the results, 2016 represents an incomplete year at the time this document was created.</p>
<pre>
library(blscrapeR)
df &lt;- inflation_adjust(1995)
tail(df)</pre>
<p>Our result returns the inflation of a 1995 dollar over the past several years.<br>
<a target="_blank" href="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/10/inflation_terminal.png?ssl=1"><img sizes="(max-width: 351px) 100vw, 351px" alt="inflation_terminal" class="wp-image-979" src="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/10/inflation_terminal.png?resize=351%2C165&ssl=1" data-recalc-dims="1"></a></p>
<p>If we want to check our results, we can head over to the<a target="_blank" href="http://data.bls.gov/cgi-bin/cpicalc.pl"> CPI Inflation Calculator</a> on the BLS website.</p>
<p><strong>CPI: Tracking Escalation</strong></p>
<p>Another typical use of the CPI is to determine price escalation. This is especially common in escalation contracts. While there are many different ways one could calculate escalation below is a simple example. Note: the BLS recommends using non-seasonally adjusted data for escalation calculations.</p>
<p>Suppose we want the price escalation of $100 investment we made in January 2014 to February 2015:</p>
<pre>
library(blscrapeR)
df &lt;- bls_api(&quot;CUSR0000SA0&quot;,
              startyear = 2014, endyear = 2015)
head(df)
# Set base value.
base_value &lt;- 100
# Get CPI from base period (January 2014).
base_cpi &lt;- subset(df, year==2014 &amp; periodName==&quot;January&quot;, select = &quot;value&quot;)
# Get the CPI for the new period (February 2015).
new_cpi &lt;- subset(df, year==2015 &amp; periodName==&quot;February&quot;, select = &quot;value&quot;)
# Calculate the updated value of our $100 investment.
(base_value / base_cpi) * new_cpi
# Woops, looks like we lost a penny!
</pre>
<p><strong>Disclaimer:</strong> Escalation is normally formulated by lawyers and bankers, the author(s) of this package or this post are neither, so the above should only be considered a code example.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/use-r-to-calculate-inflation-with-the-blscraper-package/"></div></div><div><a data-url="https://www.r-bloggers.com/use-r-to-calculate-inflation-with-the-blscraper-package/" data-counturl="https://www.r-bloggers.com/use-r-to-calculate-inflation-with-the-blscraper-package/" data-count="vertical" data-text="Use R to Calculate Inflation with the blscrapeR Package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.datascienceriot.com/use-r-to-calculate-inflation-with-the-blscraper-package/kris/"> R Tricks – Data Science Riot!</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Bfhw7nv78dA"></a></div>