<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/dplyr-and-the-design-effect-in-survey-samples/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://hagutierrezro.blogspot.com/2017/11/dplyr-and-design-effect-in-survey.html"> Data Literacy - The blog of Andrés Gutiérrez</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://psirusteam.updog.co/2017/11/21/dplyr-and-the-design/" target="_blank">Blogdown entry here</a>.</p>
<p>For those guys like me who are not such <code>R</code> geeks, this trick could be of interest. The package <code>dplyr</code> can be very useful when it comes to data manipulation and you can extract valuable information from a data frame. For example, when using if you want to count how many humans have a particular hair color, you can run the following piece of code:</p>
<pre>library(<span>dplyr</span>)<br><br><span>starwars</span> %<span>></span>% filter(<span>species</span> <span>=</span><span>=</span> <span>&quot;Human&quot;</span>) %<span>></span>%<br>  group_by(<span>hair_color</span>) %<span>></span>%<br>  summarise(<span>n</span> <span>=</span> n())</pre>
<table>
<thead>
<tr>
<th align="left">hair_color</th>
<th align="right">n</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">auburn</td>
<td align="right">1</td>
</tr>
<tr>
<td align="left">auburn, grey</td>
<td align="right">1</td>
</tr>
<tr>
<td align="left">auburn, white</td>
<td align="right">1</td>
</tr>
<tr>
<td align="left">black</td>
<td align="right">8</td>
</tr>
<tr>
<td align="left">blond</td>
<td align="right">3</td>
</tr>
<tr>
<td align="left">brown</td>
<td align="right">14</td>
</tr>
<tr>
<td align="left">brown, grey</td>
<td align="right">1</td>
</tr>
<tr>
<td align="left">grey</td>
<td align="right">1</td>
</tr>
<tr>
<td align="left">none</td>
<td align="right">3</td>
</tr>
<tr>
<td align="left">white</td>
<td align="right">2</td>
</tr>
</tbody>
</table>
<p>As a result the former query gives you a data frame and you can use it to make another query. For example, if you want to know the average number of individuals in the data frame you can use the <code>summarise</code> twice:</p>
<pre>library(<span>dplyr</span>)<br><br><span>starwars</span> %<span>></span>% filter(<span>species</span> <span>=</span><span>=</span> <span>&quot;Human&quot;</span>) %<span>></span>%<br>  group_by(<span>hair_color</span>) %<span>></span>%<br>  summarise(<span>n</span> <span>=</span> n()) %<span>></span>%<br>  summarise(<span>x.b</span> <span>=</span> mean(<span>n</span>))</pre>
<table>
<thead>
<tr>
<th align="right">x.b</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">3.5</td>
</tr>
</tbody>
</table>
<p>Now, turning our attention to statistics, it is known that, when dealing with sample surveys, one measure of interest is the design effect defined as</p>
<p>$Deff \approx 1 + (\bar{m} – 1)\rho$</p>
<p>where $\bar{m}$ is the average cluster size and $\rho$ is the intraclass correlation coefficient. If you are dealing with survey data and you want to figure out the value of $\bar{m}$ and $\rho$, you can use <code>dplyr</code>. Let’s use the <code>Lucy</code> data of the <code>samplesize4surveys</code> package to show how you can do it.</p>
<pre>library(<span>samplesize4surveys</span>)<br>data(<span>Lucy</span>)<br><br><span>m</span> <span>&lt;-</span> <span>Lucy</span> %<span>></span>% group_by(<span>Zone</span>) %<span>></span>%<br>  summarise(<span>n</span> <span>=</span> n()) %<span>></span>%<br>  summarise(<span>m</span> <span>=</span> mean(<span>n</span>))<br><br><span>rho</span> <span>&lt;-</span> ICC(<span>y</span> <span>=</span> <span>Lucy</span><span>$</span><span>Taxes</span>, <span>cl</span> <span>=</span> <span>Lucy</span><span>$</span><span>Zone</span>)<span>$</span><span>ICC</span><br><br><span>DEFF</span> <span>&lt;-</span> <span>1</span> <span>+</span> (as.integer(<span>m</span>) <span>-</span> <span>1</span>) <span>*</span> <span>rho</span><br><span>DEFF</span><br></pre>
	<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://hagutierrezro.blogspot.com/2017/11/dplyr-and-design-effect-in-survey.html"> Data Literacy - The blog of Andrés Gutiérrez</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/RYcNutmVS7c" height="1"></a></div>