<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/generate-correlation-matrices-with-complex-survey-data-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/JacobLongR/~3/U6ScnRx5Rb4/"> R on Jacob Long</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <code>survey</code> package is one of R’s best tools for those working in the<br>
social sciences. For many, it saves you from needing to use commercial<br>
software for research that uses survey data. However, it lacks one<br>
function that many academic researchers often need to report in<br>
publications: correlations. The <code>svycor</code> function in <code>jtools</code> (<a target="_blank" rel="nofollow" href="https://www.jacob-long.com/project/jtools">more info</a>) helps to<br>
fill that gap. </p>
<p>An initial note, however, is necessary. The basic method behind this<br>
feature comes from a <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/34418822/pearson-correlation-coefficient-in-rs-survey-package#41031088">response to a<br>
question</a><br>
about calculating correlations with the <code>survey</code> package written by<br>
Thomas Lumley, the <code>survey</code> package author—he has not seen (to my knowledge) or endorsed this function. All that is good about this<br>
function should be attributed to Dr. Lumley; all that is wrong with it<br>
should be attributed to me (Jacob).</p>
<p>With that said, let’s look at an example. First, we need to get a<br>
<code>survey.design</code> object. This one is built into the <code>survey</code> package.</p>
<div>
<pre><span></span><span>library</span><span>(</span>survey<span>)</span>
data<span>(</span>api<span>)</span>
dstrat <span>&lt;-</span> svydesign<span>(</span>id <span>=</span> <span>~</span><span>1</span><span>,</span>strata <span>=</span> <span>~</span>stype<span>,</span> weights <span>=</span> <span>~</span>pw<span>,</span> data <span>=</span> apistrat<span>,</span> fpc<span>=~</span>fpc<span>)</span>
</pre>
</div>
<h2>Basic use</h2>
<p>The necessary arguments are no different than when using <code>svyvar</code>.<br>
Specify, using an equation, which variables (and from which design) to<br>
include. It doesn’t matter which side of the equation the variables are<br>
on.</p>
<div>
<pre><span></span>svycor<span>(</span><span>~</span>api00 <span>+</span> api99<span>,</span> design <span>=</span> dstrat<span>)</span>
</pre>
</div>
<pre>    
##       api00 api99
## api00  1.00  0.98
## api99  0.98  1.00
</pre>
<p>You can specify with the <code>digits =</code> argument how many digits past the<br>
decimal point should be printed.</p>
<div>
<pre><span></span>svycor<span>(</span><span>~</span>api00 <span>+</span> api99<span>,</span> design <span>=</span> dstrat<span>,</span> digits <span>=</span> <span>4</span><span>)</span>
</pre>
</div>
<pre>
##        api00  api99
## api00 1.0000 0.9759
## api99 0.9759 1.0000
</pre>
<p>Any other arguments that you would normally pass to <code>svyvar</code> will be<br>
used as well, though in some cases it may not affect the output.</p>
<h2>Statistical significance tests</h2>
<p>One thing that <code>survey</code> won’t do for you is give you <em>p</em> values for the<br>
null hypothesis that <i>r</i> = 0. While at first blush finding the <em>p</em> value<br>
might seem like a simple procedure, complex surveys will almost<br>
always violate the important distributional assumptions that go along with<br>
simple hypothesis tests of the correlation coefficient. There is not a<br>
clear consensus on the appropriate way to conduct hypothesis tests in<br>
this context, due in part to the fact that most analyses of complex<br>
surveys occurs in the context of multiple regression rather than simple bivariate cases.</p>
<p>If <code>sig.stats = TRUE</code>, then <code>svycor</code> will use the <code>wtd.cor</code> function<br>
from the <code>weights</code> package to conduct hypothesis tests. The <em>p</em> values<br>
are derived from a bootstrap procedure in which the weights define<br>
sampling probability. The <code>bootn =</code> argument is given to <code>wtd.cor</code> to<br>
define the number of simulations to run. This can significantly increase<br>
the running time for large samples and/or large numbers of bootstraps.<br>
The <code>mean1</code> argument tells <code>wtd.cor</code> whether it should treat your sample size<br>
as the number of observations in the survey design (the number of rows<br>
in the data frame) or the sum of the weights. Usually, the former is<br>
desired, so the default value of <code>mean1</code> is <code>TRUE</code>.</p>
<div>
<pre><span></span>svycor<span>(</span><span>~</span>api00 <span>+</span> api99<span>,</span> design <span>=</span> dstrat<span>,</span> digits <span>=</span> <span>4</span><span>,</span> sig.stats <span>=</span> <span>TRUE</span><span>,</span> bootn <span>=</span> <span>2000</span><span>,</span> mean1 <span>=</span> <span>TRUE</span><span>)</span>
</pre>
</div>
<pre>
##       api00   api99  
## api00 1       0.9759*
## api99 0.9759* 1
</pre>
<p>When using <code>sig.stats = TRUE</code>, the correlation parameter estimates come<br>
from the bootstrap procedure rather than the simpler method based<br>
on the survey-weighted covariance matrix when <code>sig.stats = FALSE</code>.</p>
<p>By saving the output of the function, you can extract non-rounded<br>
coefficients, <em>p</em> values, and standard errors.</p>
<div>
<pre><span></span><span>c</span> <span>&lt;-</span> svycor<span>(</span><span>~</span>api00 <span>+</span> api99<span>,</span> design <span>=</span> dstrat<span>,</span> digits <span>=</span> <span>4</span><span>,</span> sig.stats <span>=</span> <span>TRUE</span><span>,</span> bootn <span>=</span> <span>2000</span><span>,</span> mean1 <span>=</span> <span>TRUE</span><span>)</span>

<span>c</span><span>$</span>cors
</pre>
</div>
<pre>
##           api00     api99
## api00 1.0000000 0.9759047
## api99 0.9759047 1.0000000
</pre>
<div>
<pre><span></span><span>c</span><span>$</span>p.values
</pre>
</div>
<pre>
##       api00 api99
## api00     0     0
## api99     0     0
</pre>
<div>
<pre><span></span><span>c</span><span>$</span>std.err
</pre>
</div>
<pre>
##             api00       api99
## api00 0.000000000 0.003467371
## api99 0.003467371 0.000000000
</pre>
<h2>Technical details</h2>
<p>The heavy lifting behind the scenes is done by <code>svyvar</code>, which from its<br>
output you may not realize also calculates covariance.</p>
<div>
<pre><span></span>svyvar<span>(</span><span>~</span>api00 <span>+</span> api99<span>,</span> design <span>=</span> dstrat<span>)</span>
</pre>
</div>
<pre>
##       variance     SE
## api00    15191 1255.7
## api99    16518 1318.4
</pre>
<p>But if you save the <code>svyvar</code> object, you can see that there’s more than<br>
meets the eye.</p>
<div>
<pre><span></span>var <span>&lt;-</span> svyvar<span>(</span><span>~</span>api00 <span>+</span> api99<span>,</span> design <span>=</span> dstrat<span>)</span>
var <span>&lt;-</span> <span>as.matrix</span><span>(</span>var<span>)</span>
var
</pre>
</div>
<pre>
##          api00    api99
## api00 15190.59 15458.83
## api99 15458.83 16518.24
## attr(,&quot;var&quot;)
##         api00   api00   api99   api99
## api00 1576883 1580654 1580654 1561998
## api00 1580654 1630856 1630856 1657352
## api99 1580654 1630856 1630856 1657352
## api99 1561998 1657352 1657352 1738266
## attr(,&quot;statistic&quot;)
## [1] &quot;variance&quot;
</pre>
<p>Once we know that, it’s just a matter of using R’s <code>cov2cor</code> function<br>
and cleaning up the output.</p>
<div>
<pre><span></span>cor <span>&lt;-</span> cov2cor<span>(</span>var<span>)</span>
cor
</pre>
</div>
<pre>
##           api00     api99
## api00 1.0000000 0.9759047
## api99 0.9759047 1.0000000
## attr(,&quot;var&quot;)
##         api00   api00   api99   api99
## api00 1576883 1580654 1580654 1561998
## api00 1580654 1630856 1630856 1657352
## api99 1580654 1630856 1630856 1657352
## api99 1561998 1657352 1657352 1738266
## attr(,&quot;statistic&quot;)
## [1] &quot;variance&quot;
</pre>
<p>Now to get rid of that covariance matrix…</p>
<div>
<pre><span></span>cor <span>&lt;-</span> cor<span>[</span><span>1</span><span>:</span><span>nrow</span><span>(</span>cor<span>),</span> <span>1</span><span>:</span><span>nrow</span><span>(</span>cor<span>)]</span>
cor
</pre>
</div>
<pre>
##           api00     api99
## api00 1.0000000 0.9759047
## api99 0.9759047 1.0000000
</pre>
<p><code>svycor</code> has its own print method, so you won’t see so many digits past<br>
the decimal point. You can extract the un-rounded matrix, however.</p>
<div>
<pre><span></span>out <span>&lt;-</span> svycor<span>(</span><span>~</span>api99 <span>+</span> api00<span>,</span> design <span>=</span> dstrat<span>)</span>
out<span>$</span>cors
</pre>
</div>
<pre>
##           api99     api00
## api99 1.0000000 0.9759047
## api00 0.9759047 1.0000000
</pre>
<h2>Suggestions welcome</h2>
<p>Whether you see a problem with the method, found a bug, or have a suggestion for an enhancement, I’d like to hear it. Bug reports <a target="_blank" rel="nofollow" href="https://github.com/jacob-long/jtools">on Github</a> are probably the best way to do that, but I’ll keep an eye out for comments here as well.</p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/JacobLongR/~4/U6ScnRx5Rb4"></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://feedproxy.google.com/~r/JacobLongR/~3/U6ScnRx5Rb4/"> R on Jacob Long</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/_16YzBmqzNI"></a></div>