<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/scatter-plots-in-survey-sampling/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://hagutierrezro.blogspot.com/2017/11/scatter-plots-in-survey-sampling.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/post/scatter-plots-in-survey-sampling2/" target="_blank">You can find this post in Blogdown format by clicking here</a></p>
<p>When it comes to analyzing survey data, you have to take into account the stochastic structure of the sample that was selected to obtain the data. Plots and graphics should not be an exception. The main aim of such studies is to try to infer about how the behavior of the outcomes of interest in the finite population.</p>
<p>For example, you may want to know how many people are in poverty. How about counting the poor people in the sample? I know, it is not a good idea. You have to use the sampling weights to estimate the whole number of poor people in the population. The total error paradigm follows the same approach when estimating any parameter of the finite population: you have to use the sampling weights in order to obtain unbiased estimators.</p>
<p>The idea behind this paradigm is the representative principle. If a person <span>$k$</span> is included in the sample with a sampling weight <span>$w_k$</span>, she/he represents to himself and <span>$w_k-1$</span> remaining people. That way, you obtain design-unbiasedness.</p>
<p>I am not a big fan of using descriptive sample statistics to analyze survey data because it can mask the reality, and the fact is that person <span>$k$</span> is included in the sample, but he/she is not alone. Behind person <span>$k$</span> there are other people, not included in the sample, and you have to realize that.</p>
<p>So, let’s apply that principle to scatter plots. I am using the <code>Lucy</code> population from the <code>TeachingSampling</code> package to recreate my idea. The following code is used to draw a <span>$\pi PS$</span> sample.</p>
<pre>library(<span>TeachingSampling</span>)<br>data(<span>Lucy</span>)<br><span># The inclusion probability is proportional to Income </span><br><span># The selected sample of size n=400 </span><br><span>n</span> <span>&lt;-</span> <span>400</span><br>set.seed(<span>123</span>)<br><span>res</span> <span>&lt;-</span> S.piPS(<span>n</span>, <span>Lucy</span><span>$</span><span>Income</span>)<br><span>sam</span> <span>&lt;-</span> <span>res</span>[,<span>1</span>]<br><span># The sample is stored in an data.sample </span><br><span>data.sample</span> <span>&lt;-</span> <span>Lucy</span>[<span>sam</span>, ]<br>attach(<span>data.sample</span>)<br></pre>
<p>The sampling weights will be stored in the <code>data.sample</code> data in the column <code>wk</code>. They will be useful to reproduce our finite popultaion from the sample data.</p>
<pre><span># Pik.s is the inclusion probability of units in the sample</span><br><span>data.sample</span><span>$</span><span>Pik.s</span> <span>&lt;-</span> <span>res</span>[,<span>2</span>]<br><span># wk is the sampling weight</span><br><span>data.sample</span><span>$</span><span>wk</span> <span>&lt;-</span> <span>1</span><span>/</span><span>data.sample</span><span>$</span><span>Pik.s</span><br></pre>
<p>Now, let`s make a plot of the sampling data (with just 400 observations). I recall that this scenario is somehow misleading, because we want to know the behavior of the variables in the finite population.</p>
<pre>library(<span>ggplot2</span>)<br>ggplot(<span>data</span> <span>=</span> <span>data.sample</span>, aes(<span>x</span> <span>=</span> <span>Income</span>, <span>y</span> <span>=</span> <span>Employees</span>)) <span>+</span><br>geom_point()<br></pre>
<p><img border="0" src="https://i1.wp.com/psirusteam.updog.co/post/2017-11-27-scatter-plots-in-survey-sampling_files/figure-html/unnamed-chunk-3-1.png?w=450&ssl=1" alt="" title="" data-recalc-dims="1"></p>
<p>The first option that comes to mind is to include the sampling weights in the points of the scatter plot. However, this approach is not appealing to me, because it is not straightforward from this plot to visuzlize the entire finite population.</p>
<pre>ggplot(<span>data</span> <span>=</span> <span>data.sample</span>, aes(<span>x</span> <span>=</span> <span>Income</span>, <span>y</span> <span>=</span> <span>Employees</span>, <span>size</span> <span>=</span> <span>wk</span>)) <span>+ <br></span>geom_point()<br></pre>
<p><img border="0" src="https://i0.wp.com/psirusteam.updog.co/post/2017-11-27-scatter-plots-in-survey-sampling_files/figure-html/unnamed-chunk-4-1.png?w=450&ssl=1" alt="" title="" data-recalc-dims="1"></p>
<p>In order to make the finite population scatter plot from the survey sample, I will replicate the rows of the <code>data.sample</code> object as many times as the sampling weight <code>wk</code>. I am using the <code>mefa::rep</code> function to achieve this goal. So, the <code>newLucy</code> object is an intent to mimic the finite population by using the selected sample.</p>
<pre>library(<span>mefa</span>)<br><span>newLucy</span> <span>&lt;-</span> <span>NULL</span><br><br><span>for</span>(<span>i</span> <span>in</span> <span>1</span><span>:</span>nrow(<span>data.sample</span>)){<br><span>newLucy</span> <span>&lt;-</span> rbind(<span>newLucy</span>,<br>rep(<span>data.sample</span>[<span>i</span>, ],<br>round(<span>data.sample</span><span>$</span><span>wk</span>[<span>i</span>])))<br>}<br><br><span>newLucy</span> <span>&lt;-</span> as.data.frame(<span>newLucy</span>)<br></pre>
<p>Now, with the <code>newLucy</code> population, I will make a scatter plot. Now, as I am replicating the rows of the sample data, I will add a jitter to avoid overplotting of the points in the scatter plot. This way, this plot (with 2396 observations) looks as if it would come from the finite population.</p>
<pre>ggplot(<span>data</span> <span>=</span> <span>newLucy</span>, aes(<span>x</span> <span>=</span> <span>Income</span>, <span>y</span> <span>=</span> <span>Employees</span>)) <span>+</span><br>geom_point() <span>+</span> geom_jitter(<span>width</span> <span>=</span> <span>15</span>, <span>height</span> <span>=</span> <span>15</span>)<br></pre>
<p><img border="0" src="https://i0.wp.com/psirusteam.updog.co/post/2017-11-27-scatter-plots-in-survey-sampling_files/figure-html/unnamed-chunk-6-1.png?w=450&ssl=1" alt="" title="" data-recalc-dims="1"></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://hagutierrezro.blogspot.com/2017/11/scatter-plots-in-survey-sampling.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/pUztzw0XpRY" height="1"></a></div>