Skip to content

Commit

Permalink
results='hide', message=FALSE, warning=FALSE
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdamico committed Nov 27, 2023
1 parent 2e32661 commit c4ed2d6
Show file tree
Hide file tree
Showing 22 changed files with 2,772 additions and 2,786 deletions.

Large diffs are not rendered by default.

496 changes: 241 additions & 255 deletions docs/1.6-survey-of-consumer-finances-scf.html

Large diffs are not rendered by default.

64 changes: 32 additions & 32 deletions docs/2.2-replication-based-variance-estimation.html
Original file line number Diff line number Diff line change
Expand Up @@ -270,34 +270,34 @@ <h2><span class="header-section-number">2.2</span> Replication-Based Variance Es
<p>The function <code>bootVar</code> from the <code>laeken</code> library <span class="citation">(<label for="tufte-mn-12" class="margin-toggle">&#8853;</label><input type="checkbox" id="tufte-mn-12" class="margin-toggle">Alfons and Templ 2013<span class="marginnote">Alfons, Andreas, and Matthias Templ. 2013. <span>“Estimation of Social Exclusion Indicators from Complex Surveys: The <span>R</span> Package <span class="nocase">laeken</span>.”</span> <em>Journal of Statistical Software</em> 54 (15): 1–25. <a href="http://www.jstatsoft.org/v54/i15/">http://www.jstatsoft.org/v54/i15/</a>.</span>)</span>, also uses the bootstrap method to estimate variances.</p>
<div id="replication-design-example" class="section level3" number="2.2.1">
<h3><span class="header-section-number">2.2.1</span> Replication Design Example</h3>
<div class="sourceCode" id="cb94"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb94-1"><a href="2.2-replication-based-variance-estimation.html#cb94-1" tabindex="-1"></a><span class="co"># load libraries</span></span>
<span id="cb94-2"><a href="2.2-replication-based-variance-estimation.html#cb94-2" tabindex="-1"></a><span class="fu">library</span>( survey )</span>
<span id="cb94-3"><a href="2.2-replication-based-variance-estimation.html#cb94-3" tabindex="-1"></a><span class="fu">library</span>( convey )</span>
<span id="cb94-4"><a href="2.2-replication-based-variance-estimation.html#cb94-4" tabindex="-1"></a><span class="fu">library</span>( laeken ) <span class="co"># for the dataset</span></span>
<span id="cb94-5"><a href="2.2-replication-based-variance-estimation.html#cb94-5" tabindex="-1"></a></span>
<span id="cb94-6"><a href="2.2-replication-based-variance-estimation.html#cb94-6" tabindex="-1"></a><span class="co"># get laeken eusilc data</span></span>
<span id="cb94-7"><a href="2.2-replication-based-variance-estimation.html#cb94-7" tabindex="-1"></a><span class="fu">data</span>( eusilc ) ; <span class="fu">names</span>( eusilc ) <span class="ot">&lt;-</span> <span class="fu">tolower</span>( <span class="fu">names</span>( eusilc ) )</span>
<span id="cb94-8"><a href="2.2-replication-based-variance-estimation.html#cb94-8" tabindex="-1"></a></span>
<span id="cb94-9"><a href="2.2-replication-based-variance-estimation.html#cb94-9" tabindex="-1"></a><span class="co"># survey design object for TSL/ifluence function variance estimation</span></span>
<span id="cb94-10"><a href="2.2-replication-based-variance-estimation.html#cb94-10" tabindex="-1"></a>des_eusilc <span class="ot">&lt;-</span> <span class="fu">svydesign</span>( <span class="at">ids =</span> <span class="sc">~</span>rb030 , <span class="at">strata =</span> <span class="sc">~</span>db040 , <span class="at">weights =</span> <span class="sc">~</span>rb050 , <span class="at">data =</span> eusilc )</span>
<span id="cb94-11"><a href="2.2-replication-based-variance-estimation.html#cb94-11" tabindex="-1"></a>des_eusilc <span class="ot">&lt;-</span> <span class="fu">convey_prep</span>( des_eusilc )</span>
<span id="cb94-12"><a href="2.2-replication-based-variance-estimation.html#cb94-12" tabindex="-1"></a></span>
<span id="cb94-13"><a href="2.2-replication-based-variance-estimation.html#cb94-13" tabindex="-1"></a><span class="co"># influence function SE estimate for the gini index</span></span>
<span id="cb94-14"><a href="2.2-replication-based-variance-estimation.html#cb94-14" tabindex="-1"></a>convey<span class="sc">:::</span><span class="fu">svygini.survey.design</span>( <span class="sc">~</span>eqincome , <span class="at">design =</span> des_eusilc )</span></code></pre></div>
<div class="sourceCode" id="cb89"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb89-1"><a href="2.2-replication-based-variance-estimation.html#cb89-1" tabindex="-1"></a><span class="co"># load libraries</span></span>
<span id="cb89-2"><a href="2.2-replication-based-variance-estimation.html#cb89-2" tabindex="-1"></a><span class="fu">library</span>( survey )</span>
<span id="cb89-3"><a href="2.2-replication-based-variance-estimation.html#cb89-3" tabindex="-1"></a><span class="fu">library</span>( convey )</span>
<span id="cb89-4"><a href="2.2-replication-based-variance-estimation.html#cb89-4" tabindex="-1"></a><span class="fu">library</span>( laeken ) <span class="co"># for the dataset</span></span>
<span id="cb89-5"><a href="2.2-replication-based-variance-estimation.html#cb89-5" tabindex="-1"></a></span>
<span id="cb89-6"><a href="2.2-replication-based-variance-estimation.html#cb89-6" tabindex="-1"></a><span class="co"># get laeken eusilc data</span></span>
<span id="cb89-7"><a href="2.2-replication-based-variance-estimation.html#cb89-7" tabindex="-1"></a><span class="fu">data</span>( eusilc ) ; <span class="fu">names</span>( eusilc ) <span class="ot">&lt;-</span> <span class="fu">tolower</span>( <span class="fu">names</span>( eusilc ) )</span>
<span id="cb89-8"><a href="2.2-replication-based-variance-estimation.html#cb89-8" tabindex="-1"></a></span>
<span id="cb89-9"><a href="2.2-replication-based-variance-estimation.html#cb89-9" tabindex="-1"></a><span class="co"># survey design object for TSL/ifluence function variance estimation</span></span>
<span id="cb89-10"><a href="2.2-replication-based-variance-estimation.html#cb89-10" tabindex="-1"></a>des_eusilc <span class="ot">&lt;-</span> <span class="fu">svydesign</span>( <span class="at">ids =</span> <span class="sc">~</span>rb030 , <span class="at">strata =</span> <span class="sc">~</span>db040 , <span class="at">weights =</span> <span class="sc">~</span>rb050 , <span class="at">data =</span> eusilc )</span>
<span id="cb89-11"><a href="2.2-replication-based-variance-estimation.html#cb89-11" tabindex="-1"></a>des_eusilc <span class="ot">&lt;-</span> <span class="fu">convey_prep</span>( des_eusilc )</span>
<span id="cb89-12"><a href="2.2-replication-based-variance-estimation.html#cb89-12" tabindex="-1"></a></span>
<span id="cb89-13"><a href="2.2-replication-based-variance-estimation.html#cb89-13" tabindex="-1"></a><span class="co"># influence function SE estimate for the gini index</span></span>
<span id="cb89-14"><a href="2.2-replication-based-variance-estimation.html#cb89-14" tabindex="-1"></a>convey<span class="sc">:::</span><span class="fu">svygini.survey.design</span>( <span class="sc">~</span>eqincome , <span class="at">design =</span> des_eusilc )</span></code></pre></div>
<pre><code>## gini SE
## eqincome 0.26497 0.0019</code></pre>
<div class="sourceCode" id="cb96"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb96-1"><a href="2.2-replication-based-variance-estimation.html#cb96-1" tabindex="-1"></a><span class="co"># create survey design object for replicate-based variance estimation</span></span>
<span id="cb96-2"><a href="2.2-replication-based-variance-estimation.html#cb96-2" tabindex="-1"></a>des_eusilc_rep <span class="ot">&lt;-</span> <span class="fu">as.svrepdesign</span>( des_eusilc , <span class="at">type =</span> <span class="st">&quot;bootstrap&quot;</span> )</span>
<span id="cb96-3"><a href="2.2-replication-based-variance-estimation.html#cb96-3" tabindex="-1"></a>des_eusilc_rep <span class="ot">&lt;-</span> <span class="fu">convey_prep</span>( des_eusilc_rep )</span>
<span id="cb96-4"><a href="2.2-replication-based-variance-estimation.html#cb96-4" tabindex="-1"></a></span>
<span id="cb96-5"><a href="2.2-replication-based-variance-estimation.html#cb96-5" tabindex="-1"></a><span class="co"># replicate-based (bootstrao) SE estimate for the gini index</span></span>
<span id="cb96-6"><a href="2.2-replication-based-variance-estimation.html#cb96-6" tabindex="-1"></a><span class="co"># with option to keep replicates</span></span>
<span id="cb96-7"><a href="2.2-replication-based-variance-estimation.html#cb96-7" tabindex="-1"></a>( gini.repstat <span class="ot">&lt;-</span> convey<span class="sc">:::</span><span class="fu">svygini.svyrep.design</span>( <span class="sc">~</span>eqincome , <span class="at">design =</span> des_eusilc_rep , <span class="at">return.replicates =</span> <span class="cn">TRUE</span> ) )</span></code></pre></div>
<div class="sourceCode" id="cb91"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb91-1"><a href="2.2-replication-based-variance-estimation.html#cb91-1" tabindex="-1"></a><span class="co"># create survey design object for replicate-based variance estimation</span></span>
<span id="cb91-2"><a href="2.2-replication-based-variance-estimation.html#cb91-2" tabindex="-1"></a>des_eusilc_rep <span class="ot">&lt;-</span> <span class="fu">as.svrepdesign</span>( des_eusilc , <span class="at">type =</span> <span class="st">&quot;bootstrap&quot;</span> )</span>
<span id="cb91-3"><a href="2.2-replication-based-variance-estimation.html#cb91-3" tabindex="-1"></a>des_eusilc_rep <span class="ot">&lt;-</span> <span class="fu">convey_prep</span>( des_eusilc_rep )</span>
<span id="cb91-4"><a href="2.2-replication-based-variance-estimation.html#cb91-4" tabindex="-1"></a></span>
<span id="cb91-5"><a href="2.2-replication-based-variance-estimation.html#cb91-5" tabindex="-1"></a><span class="co"># replicate-based (bootstrao) SE estimate for the gini index</span></span>
<span id="cb91-6"><a href="2.2-replication-based-variance-estimation.html#cb91-6" tabindex="-1"></a><span class="co"># with option to keep replicates</span></span>
<span id="cb91-7"><a href="2.2-replication-based-variance-estimation.html#cb91-7" tabindex="-1"></a>( gini.repstat <span class="ot">&lt;-</span> convey<span class="sc">:::</span><span class="fu">svygini.svyrep.design</span>( <span class="sc">~</span>eqincome , <span class="at">design =</span> des_eusilc_rep , <span class="at">return.replicates =</span> <span class="cn">TRUE</span> ) )</span></code></pre></div>
<pre><code>## gini SE
## eqincome 0.26497 0.0022</code></pre>
<p>To understand how that variance is estimated, we can look at the replicates:</p>
<div class="sourceCode" id="cb98"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb98-1"><a href="2.2-replication-based-variance-estimation.html#cb98-1" tabindex="-1"></a><span class="co"># collect gini bootstrap replicates</span></span>
<span id="cb98-2"><a href="2.2-replication-based-variance-estimation.html#cb98-2" tabindex="-1"></a>( gini.reps <span class="ot">&lt;-</span> gini.repstat<span class="sc">$</span>replicates )</span></code></pre></div>
<div class="sourceCode" id="cb93"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb93-1"><a href="2.2-replication-based-variance-estimation.html#cb93-1" tabindex="-1"></a><span class="co"># collect gini bootstrap replicates</span></span>
<span id="cb93-2"><a href="2.2-replication-based-variance-estimation.html#cb93-2" tabindex="-1"></a>( gini.reps <span class="ot">&lt;-</span> gini.repstat<span class="sc">$</span>replicates )</span></code></pre></div>
<pre><code>## [1] 0.2628816 0.2653672 0.2663951 0.2646311 0.2660021 0.2648396 0.2633737
## [8] 0.2611656 0.2613233 0.2647174 0.2638256 0.2621616 0.2646830 0.2587950
## [15] 0.2642947 0.2651559 0.2663231 0.2673018 0.2687169 0.2671058 0.2654078
Expand All @@ -314,16 +314,16 @@ <h3><span class="header-section-number">2.2.1</span> Replication Design Example<
## attr(,&quot;mse&quot;)
## [1] FALSE</code></pre>
<p>These are resampling (bootstrap) replicates. With them, we can look at the variance of these replicates to get an estimate the gini estimator’s variance:</p>
<div class="sourceCode" id="cb100"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb100-1"><a href="2.2-replication-based-variance-estimation.html#cb100-1" tabindex="-1"></a><span class="co"># variance estimate</span></span>
<span id="cb100-2"><a href="2.2-replication-based-variance-estimation.html#cb100-2" tabindex="-1"></a>des.scale <span class="ot">&lt;-</span> des_eusilc_rep<span class="sc">$</span>scale</span>
<span id="cb100-3"><a href="2.2-replication-based-variance-estimation.html#cb100-3" tabindex="-1"></a>meantheta <span class="ot">&lt;-</span> <span class="fu">mean</span>( gini.reps )[[<span class="dv">1</span>]]</span>
<span id="cb100-4"><a href="2.2-replication-based-variance-estimation.html#cb100-4" tabindex="-1"></a>v <span class="ot">&lt;-</span> <span class="fu">sum</span>( ( gini.reps <span class="sc">-</span> meantheta )<span class="sc">^</span><span class="dv">2</span> ) <span class="sc">*</span> des.scale</span>
<span id="cb100-5"><a href="2.2-replication-based-variance-estimation.html#cb100-5" tabindex="-1"></a></span>
<span id="cb100-6"><a href="2.2-replication-based-variance-estimation.html#cb100-6" tabindex="-1"></a><span class="co"># SE estimate</span></span>
<span id="cb100-7"><a href="2.2-replication-based-variance-estimation.html#cb100-7" tabindex="-1"></a>( gini.se <span class="ot">&lt;-</span> ( <span class="fu">sqrt</span>( v ) ) )</span></code></pre></div>
<div class="sourceCode" id="cb95"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb95-1"><a href="2.2-replication-based-variance-estimation.html#cb95-1" tabindex="-1"></a><span class="co"># variance estimate</span></span>
<span id="cb95-2"><a href="2.2-replication-based-variance-estimation.html#cb95-2" tabindex="-1"></a>des.scale <span class="ot">&lt;-</span> des_eusilc_rep<span class="sc">$</span>scale</span>
<span id="cb95-3"><a href="2.2-replication-based-variance-estimation.html#cb95-3" tabindex="-1"></a>meantheta <span class="ot">&lt;-</span> <span class="fu">mean</span>( gini.reps )[[<span class="dv">1</span>]]</span>
<span id="cb95-4"><a href="2.2-replication-based-variance-estimation.html#cb95-4" tabindex="-1"></a>v <span class="ot">&lt;-</span> <span class="fu">sum</span>( ( gini.reps <span class="sc">-</span> meantheta )<span class="sc">^</span><span class="dv">2</span> ) <span class="sc">*</span> des.scale</span>
<span id="cb95-5"><a href="2.2-replication-based-variance-estimation.html#cb95-5" tabindex="-1"></a></span>
<span id="cb95-6"><a href="2.2-replication-based-variance-estimation.html#cb95-6" tabindex="-1"></a><span class="co"># SE estimate</span></span>
<span id="cb95-7"><a href="2.2-replication-based-variance-estimation.html#cb95-7" tabindex="-1"></a>( gini.se <span class="ot">&lt;-</span> ( <span class="fu">sqrt</span>( v ) ) )</span></code></pre></div>
<pre><code>## [1] 0.002226009</code></pre>
<div class="sourceCode" id="cb102"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb102-1"><a href="2.2-replication-based-variance-estimation.html#cb102-1" tabindex="-1"></a><span class="co"># compare estimates</span></span>
<span id="cb102-2"><a href="2.2-replication-based-variance-estimation.html#cb102-2" tabindex="-1"></a><span class="fu">identical</span>( gini.se , <span class="fu">SE</span>( gini.repstat )[[<span class="dv">1</span>]] )</span></code></pre></div>
<div class="sourceCode" id="cb97"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb97-1"><a href="2.2-replication-based-variance-estimation.html#cb97-1" tabindex="-1"></a><span class="co"># compare estimates</span></span>
<span id="cb97-2"><a href="2.2-replication-based-variance-estimation.html#cb97-2" tabindex="-1"></a><span class="fu">identical</span>( gini.se , <span class="fu">SE</span>( gini.repstat )[[<span class="dv">1</span>]] )</span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
</div>
</div>
Expand Down
Loading

0 comments on commit c4ed2d6

Please sign in to comment.