<p>The <code>survey</code> package is one of R’s best tools for those working in the
social sciences. For many, it saves you from needing to use commercial
software for research that uses survey data. However, it lacks one
function that many academic researchers often need to report in
publications: correlations. The <code>svycor</code> function in <code>jtools</code> (<a href="https://www.jacob-long.com/project/jtools">more info</a>) helps to
fill that gap. </p>
<p>An initial note, however, is necessary. The basic method behind this
feature comes from a <a href="http://stackoverflow.com/questions/34418822/pearson-correlation-coefficient-in-rs-survey-package#41031088">response to a
question</a>
about calculating correlations with the <code>survey</code> package written by
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
function should be attributed to Dr. Lumley; all that is wrong with it
should be attributed to me (Jacob).</p>
<p>With that said, let’s look at an example. First, we need to get a
<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>.
Specify, using an equation, which variables (and from which design) to
include. It doesn’t matter which side of the equation the variables are
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
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
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 p values for the
null hypothesis that <i>r</i> = 0. While at first blush finding the p value
might seem like a simple procedure, complex surveys will almost
always violate the important distributional assumptions that go along with
simple hypothesis tests of the correlation coefficient. There is not a
clear consensus on the appropriate way to conduct hypothesis tests in
this context, due in part to the fact that most analyses of complex
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
from the <code>weights</code> package to conduct hypothesis tests. The p values
are derived from a bootstrap procedure in which the weights define
sampling probability. The <code>bootn =</code> argument is given to <code>wtd.cor</code> to
define the number of simulations to run. This can significantly increase
the running time for large samples and/or large numbers of bootstraps.
The <code>mean1</code> argument tells <code>wtd.cor</code> whether it should treat your sample size
as the number of observations in the survey design (the number of rows
in the data frame) or the sum of the weights. Usually, the former is
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
from the bootstrap procedure rather than the simpler method based
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
coefficients, p 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
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
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
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
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 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>