<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/interactive-panel-eda-with-3-lines-of-code/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://joachim-gassen.github.io/2018/12/interactive-panel-eda-with-3-lines-of-code/"> An Accounting and Data Science Nerd's Corner</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Exploratory data analysis is important, everybody knows that. With R, it is also easy. Below you see three lines of code that allow you to interactively explore the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Preston_curve" target="_blank">Preston Curve</a>, the prominent association of country level real income per capita with life expectancy.</p>
<pre><code>install.packages(&quot;ExPanDaR&quot;)
library(ExPanDaR)
wb &lt;- read.csv(&quot;https://joachim-gassen.github.io/data/wb_condensed.csv&quot;)
ExPanD(wb, cs_id = &quot;country&quot;, ts_id = &quot;year&quot;)</code></pre>
<p>After running these three lines of code (OK, four if you have to install the ExPanDaR package first), a shiny window will open, allowing you to explore a country-year panel of World Bank data and looking something like this. </p>
<div>
<img alt="Screenshot of ExPanD window" src="https://i1.wp.com/joachim-gassen.github.io/images/expand_startup.jpg?w=456&ssl=1" data-recalc-dims="1">
<p>Screenshot of ExPanD window</p>
</div>
<p>If you do not have access to R, you can also access a version <a rel="nofollow" href="https://jgassen.shinyapps.io/expand_preston" target="_blank">online</a>. Explore away! When you scroll down, you will notice several issues:</p>
<ul>
<li>While <code>gdp_capita</code> and <code>lifeexpectancy</code> are available for all observations, a lot of the additional variables presented in the data set are only available for a subset of country-year observations.</li>
<li><code>gdp_capita</code> is heavily right skewed, meaning that many countries have low real income per capital while few countries have high levels. By looking at the extreme observations table, you can identify the usual suspects.</li>
<li>Not surprisingly, income is unevenly distributed across geographic regions. This also holds for life expectancy.</li>
<li><code>gdp_capita</code> grows over time and its distribution widens. <code>lifeexpectancy</code> also grows but, luckily, its distribution does not widen over time.</li>
<li>The scatter plot of <code>gdp_capita</code> and <code>lifeexpectancy</code> constitutes the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Preston_curve" target="_blank">Preston Curve</a>, which was first documented in the <a rel="nofollow" href="https://www.tandfonline.com/doi/abs/10.1080/00324728.1975.10410201" target="_blank">seventies</a>. </li>
</ul>
<div>
<img alt="Preston Curve, as displayed by ExPanD" src="https://i2.wp.com/joachim-gassen.github.io/images/expand_preston_scatter.jpg?w=456&ssl=1" data-recalc-dims="1">
<p>Preston Curve, as displayed by ExPanD</p>
</div>
<p>The regression results below the scatter plot document the positive association between real income per capita and life expectancy. But, when you look at the scatter plot carefully and hover over it with your mouse to identify observations, you will likely notice that countries seem to have their unique development patterns. This calls for the inclusion of country-level fixed effects in the regression. </p>
<div>
<img alt="Regression results with country fixed effects" src="https://i1.wp.com/joachim-gassen.github.io/images/expand_preston_regression_cfe.jpg?w=456&ssl=1" data-recalc-dims="1">
<p>Regression results with country fixed effects</p>
</div>
<p>The association of real income with life expectancy is still there. But will it continue to hold when we allow life expectancy to follow a world-wide time trend? As medical advancements affect multiple countries more or less concurrently, this seems important to test. Let’s throw in some year fixed effects as well.</p>
<div>
<img alt="Regression results with country fixed effects" src="https://i0.wp.com/joachim-gassen.github.io/images/expand_preston_regression_cyfe.jpg?w=456&ssl=1" data-recalc-dims="1">
<p>Regression results with country fixed effects</p>
</div>
<p>Uhuh. Now the association of real income per capita and life expectancy has turned negative! This indicates that higher real income per capita is associated with lower life expectancy after allowing for unobserved time and country heterogeneity. What do we learn from this exercise?</p>
<ol>
<li>Correlations do not imply causality.</li>
<li>If we allow for the possibility that life expectancy changes over time because of global changes (like, e.g. medical discoveries, wars and conflicts) and that countries come from different starting grounds (e.g., because of their geographic location), then we cannot observe a robust association of real income per capita with life expectancy.</li>
<li>Whether real income per capital has a causal effect on life expectancy cannot be assessed by a simplistic analysis like the one presented here and the Preston curve presenting the striking correlation can be easily miss-understood to indicate such a causation.</li>
</ol>
<p>All this can be reached with three lines of R and some clicks. If you are interested to explore the Preston curve and the functions behind my ExPanDaR package in somewhat more detail, you can take a look at this <a rel="nofollow" href="https://www.kaggle.com/jogassen/explore-world-bank-panel-data-with-r" target="_blank">kaggle kernel</a>.</p>
<p>But hey, you might ask, I do not work with panel data, so why should I care? Well, while I developed the ExPanDaR package for panel data, you can squeeze it to use it on plain cross-sectional data. See below for a classic example:</p>
<pre><code>library(ExPanDaR)
df &lt;- mtcars
df$cs_id &lt;- row.names(df)
df$ts_id &lt;-1
ExPanD(df, cs_id =&quot;cs_id&quot;, ts_id = &quot;ts_id&quot;,
       components = c(trend_graph = FALSE, quantile_trend_graph = FALSE))</code></pre>
<p>If you want to dive into the ExPanDaR package a little it more, <a rel="nofollow" href="https://github.com/joachim-gassen/ExPanDaR" target="_blank">here</a> is its github page. It also contains the code to generate the data used here as well as the additional steps to generate the <a rel="nofollow" href="https://jgassen.shinyapps.io/expand_preston" target="_blank">online</a> version, including data definitions and a customized landing text for the ExPanD app.</p>
<p>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="https://joachim-gassen.github.io/2018/12/interactive-panel-eda-with-3-lines-of-code/"> An Accounting and Data Science Nerd's Corner</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/rcZ0BD5PZ1s" height="1"></a></div>