<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/a-new-package-hhi-for-quick-calculation-of-herfindahl-hirschman-index-scores/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/a-new-package-hhi-for-calculation-of-herfindahl-hirschman-index-scores/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The Herfindahl-Hirschman Index (HHI) is a widely used measure of concentration in a variety of fields including, business, economics, political science, finance, and many others. Though simple to calculate (summed squared market shares of firms/actors in a single market/space), calculation of the HHI can get onerous, especially as the number of firms/actors increases and the time period grows. Thus, I decided to write a package aimed at streamlining and simplifying calculation of HHI scores. The package, <em>hhi</em>, calculates the concentration of a market/space based on a supplied vector of values corresponding with shares of all individual firms/actors acting in that space. The package is available on <a rel="nofollow" href="https://cran.r-project.org/package=hhi" target="_blank">CRAN</a>.</p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code>The purpose of this blog post is to provide a quick overview of the package’s two key functions: <em>hhi</em> (calculation) and <em>plot_hhi</em> (visualization). </p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code><strong>Calculating HHI Scores</strong></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code>As the package is intended for simple, intuitive usage, the function requires only the name of the data frame and then the name of the variable corresponding with the market shares in quotation marks. With these placed directly in the command, calling the function <em>hhi</em> will generate the HHI score based on the values supplied, following the basic form,</p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code><img data-orig-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/HHI.png?fit=186%2C96" data-orig-size="186,96" data-medium-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/HHI.png?fit=186%2C96" data-attachment-id="480" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/HHI.png?resize=186%2C96" data-permalink="http://r-posts.com/a-new-package-hhi-for-calculation-of-herfindahl-hirschman-index-scores/hhi/" data-large-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/HHI.png?fit=186%2C96" alt="" data-image-title="HHI" width="186" data-comments-opened="1" class="wp-image-480" data-recalc-dims="1" height="96">where <em>MS</em> is the market share of each firm, <em>i</em>, operating in a single market. Summing across all squared market shares for all firms results in the measure of concentration in the given market, <em>HHI</em>. </p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code>Consider a simple application calculating the HHI for the men’s footwear market in the United States in 2017 (see and download the data file, “footwear.txt”, from my <a rel="nofollow" href="https://github.com/pdwaggoner/hhi" target="_blank">GitHub repo</a>). Using market share data for every men’s footwear company operating in the U.S. in 2017 from Euromonitor Passport, we can calculate this market’s HHI with the following code:</p>
<p><code><br>
</code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><br>
</code></p>
<pre> 
# First, install the &quot;hhi&quot; package, then load the library
install.packages(&quot;hhi&quot;)
library(hhi)

# Next, read in data: US Men's Footwear Company Market Shares, 2012-2017
footwear = read.table(&quot;.../footwear.txt&quot;)

# Now, call the &quot;hhi&quot; command to calculate HHI for 2017
hhi(footwear, &quot;ms.2017&quot;) # first the df, then the shares variable in quotes
</pre>
<p><code><br>
</code><code><code><br>
</code></code><code><code><br>
</code></code><code><code><br>
</code></code>Calling the function <em>hhi</em> gives us an HHI index value for men’s footwear in the U.S. in 2017 of 2009.25. You can corroborate this output manually by squaring each market share corresponding with each company in the data file in the year 2017, and then summing over each firm’s squared market share.</p>
<p>Often, the HHI is used as a measure of competition, with 10,000 equaling perfect monopoly (100^2) and 0.0 equaling perfect competition. As such, we can see that the U.S. men’s footwear industry in 2017 seems relatively competitive. Yet, to say anything substantive about the men’s U.S. footwear market, we really need a comparison of HHI scores for this market over time. This is where the second command comes in.</p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code><strong>Visualizing HHI Time Series</strong></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code>The second key function in the package, <em>plot_hhi</em>, is a plotting feature allowing for quick and simple visualization of a time series of HHI scores. Usage is similarly straightforward, requiring only the name of the data frame, the name of the variable corresponding with the <em>time</em> indicator in quotation marks, and then the name of the variable corresponding with the <em>market shares</em> also in quotation marks. The package leverages <em>ggplot2 </em>to provide a visual rendering of the supplied vector of HHI values over the specified range of time. The function supports any measure of time, such as, years, quarters, months, etc. Note that <em>plot_hhi</em> is a relatively inflexible function meant for quick visual rendering of a vector of HHI scores over a period of time. For bigger and formal projects, users are advised to generate original plots with other plotting functions and packages beyond <em>hhi</em> to allow for greater flexibility in customizing visual output according to specific needs.</p>
<p>Let’s return to our men’s U.S. footwear example to see how the function works in practice. First, we need to calculate the HHI scores for each year in the data file (2012-2017), and store those as objects to make a data frame of HHI scores corresponding to individual years. Then, we simply call the <em>plot_hhi</em> command and generate a simple, pleasing plot of HHI scores over time. This will give us a much better sense of how our 2017 HHI score above compares with other years in this market. See the code below, followed by the output.</p>
<pre># First, calculate and store HHI for each year in the data file (2012-2017)
hhi.12 = hhi(footwear, &quot;ms.2012&quot;)
hhi.13 = hhi(footwear, &quot;ms.2013&quot;)
hhi.14 = hhi(footwear, &quot;ms.2014&quot;)
hhi.15 = hhi(footwear, &quot;ms.2015&quot;)
hhi.16 = hhi(footwear, &quot;ms.2016&quot;)
hhi.17 = hhi(footwear, &quot;ms.2017&quot;)

# Combine and create df for plotting
hhi = rbind(hhi.12, hhi.13, hhi.14, hhi.15, hhi.16, hhi.17)

year = c(2012, 2013, 2014, 2015, 2016, 2017)

hhi.data = data.frame(year, hhi)

# Finally, generate HHI time series plot using the &quot;plot_hhi&quot; command
plot_hhi(hhi.data, &quot;year&quot;, &quot;hhi&quot;)
</pre>
<p></p>
<p>These lines of code will give us the following plot of HHI scores for each year in the data set.</p>
<p>
<img data-orig-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/footwear.HHI_-1.png?fit=761%2C761" data-orig-size="761,761" data-medium-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/footwear.HHI_-1.png?fit=300%2C300" data-attachment-id="483" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/footwear.HHI_-1.png?resize=300%2C300" data-permalink="http://r-posts.com/a-new-package-hhi-for-calculation-of-herfindahl-hirschman-index-scores/footwear-hhi-2/" data-large-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/04/footwear.HHI_-1.png?fit=450%2C450" alt="" data-image-title="footwear.HHI" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" class="wp-image-483" data-recalc-dims="1" height="300"><br>
Interestingly, the men’s U.S. footwear industry seems to be getting slightly less competitive (higher HHI scores) from 2012 to 2017, on average. To say anything substantive about this trend, though, would obviously require more sophisticated methods as well as a longer time series. Yet, the value of the <em>hhi</em> package is allowing for quick calculation and visualization of HHI scores over time. You can download the package from <a rel="nofollow" href="https://cran.r-project.org/package=hhi" target="_blank">CRAN</a> or directly from the package installation context in RStudio. And as always, if you have any questions or find any bugs requiring fixing, please feel free to <a rel="nofollow" href="http://www.philipdwaggoner.com/" target="_blank">contact me</a>.</p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code>As a final note, here are a few references for further reading on the HHI and its original calculation and intuition:</p>
<p><em>Herfindahl, Orris C. 1950. “Concentration in the steel industry.” Ph.D. dissertation, Columbia University.</em></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code><br>
<em>Hirschman, Albert O. 1945. “National power and structure of foreign trade.” Berkeley, CA: University of California Press.</em></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code><br>
<em>Rhoades, Stephen A. 1993. “The herfindahl-hirschman index.” Federal Reserve Bulletin 79: 188.</em></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code></p>
<p><code><br>
</code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code> <code><code><br>
</code></code>Thanks and enjoy!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-posts.com/a-new-package-hhi-for-calculation-of-herfindahl-hirschman-index-scores/"> R-posts.com</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/eKcjMfXQyqU" height="1"></a></div>