Skip to content
Snippets Groups Projects
ggplot-advanced.html 212 KiB
Newer Older
  • Learn to ignore specific revisions
  • bpkleer's avatar
    bpkleer committed
    <span id="cb40-14"><a href="#cb40-14" aria-hidden="true" tabindex="-1"></a>fakeDfbyDistrict<span class="sc">$</span>pred_se <span class="ot">&lt;-</span> predFakeDfbyDistrict<span class="sc">$</span>se.fit</span></code></pre></div>
    
    bpkleer's avatar
    bpkleer committed
    <p>Anschließend können wir das ganze dann mit <code>ggplot</code> plotten:</p>
    <div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(fakeDfbyDistrict,</span>
    <span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a>       <span class="fu">aes</span>(<span class="at">x =</span> trstprt, </span>
    <span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a>           <span class="at">y =</span> pred,</span>
    <span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a>           <span class="at">color =</span> district,</span>
    <span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a>           <span class="at">shape =</span> district</span>
    
    bpkleer's avatar
    bpkleer committed
    <span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a>           )</span>
    <span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></a>       ) <span class="sc">+</span></span>
    <span id="cb41-8"><a href="#cb41-8" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_line</span>() <span class="sc">+</span></span>
    <span id="cb41-9"><a href="#cb41-9" aria-hidden="true" tabindex="-1"></a>  <span class="fu">ylab</span>(<span class="st">&quot;Predicted value of Trust in Parliament&quot;</span>) <span class="sc">+</span></span>
    <span id="cb41-10"><a href="#cb41-10" aria-hidden="true" tabindex="-1"></a>  <span class="fu">xlab</span>(<span class="st">&quot;Trust in Parties&quot;</span>) <span class="sc">+</span></span>
    <span id="cb41-11"><a href="#cb41-11" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">&quot;Linear relationship between trstprl and trstprt (others constant)&quot;</span>,</span>
    <span id="cb41-12"><a href="#cb41-12" aria-hidden="true" tabindex="-1"></a>       <span class="at">color =</span> <span class="st">&quot;Distrikte&quot;</span></span>
    <span id="cb41-13"><a href="#cb41-13" aria-hidden="true" tabindex="-1"></a>       ) </span></code></pre></div>
    
    bpkleer's avatar
    bpkleer committed
    <p><img src="ggplot-advanced_files/figure-html/ggplot-districts-1.png" width="576" style="display: block; margin: auto;" /> Somit haben wir die Regressionslinie zwischen <code>trstprl</code> und <code>trstprt</code> unter Konstanthalten von Alter, Zufriedenheit mit der Demokratie für Personen nach Distrikten dargestellt.</p>
    <p>Oftmals möchte man aber eine Linie besonders hervorheben und zum Beispiel die Datenpunkte des hervorgehobenen Distrikts einzeichnen. Prüfe den Code zu dem vorherigen und überlege, was an welchen Stellen geändert wurde!</p>
    <div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>fakeDistrict12 <span class="ot">&lt;-</span> fakeDfbyDistrict <span class="sc">%&gt;%</span> </span>
    <span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">filter</span>(district <span class="sc">==</span> <span class="st">&quot;Distrikt 12&quot;</span>) <span class="sc">%&gt;%</span> </span>
    <span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">select</span>(<span class="sc">-</span>district)</span>
    <span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a></span>
    <span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a>district12 <span class="ot">&lt;-</span> pss <span class="sc">%&gt;%</span> </span>
    <span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a>  <span class="fu">filter</span>(district <span class="sc">==</span> <span class="st">&quot;Distrikt 12&quot;</span>) <span class="sc">%&gt;%</span> </span>
    <span id="cb42-7"><a href="#cb42-7" aria-hidden="true" tabindex="-1"></a>  <span class="fu">select</span>(<span class="sc">-</span>district)</span>
    <span id="cb42-8"><a href="#cb42-8" aria-hidden="true" tabindex="-1"></a></span>
    <span id="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(fakeDistrict12,</span>
    <span id="cb42-10"><a href="#cb42-10" aria-hidden="true" tabindex="-1"></a>       <span class="fu">aes</span>(<span class="at">x =</span> trstprt, </span>
    <span id="cb42-11"><a href="#cb42-11" aria-hidden="true" tabindex="-1"></a>           <span class="at">y =</span> pred</span>
    
    bpkleer's avatar
    bpkleer committed
    <span id="cb42-12"><a href="#cb42-12" aria-hidden="true" tabindex="-1"></a>           )</span>
    <span id="cb42-13"><a href="#cb42-13" aria-hidden="true" tabindex="-1"></a>       ) <span class="sc">+</span></span>
    <span id="cb42-14"><a href="#cb42-14" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_line</span>(<span class="at">color =</span> <span class="st">&quot;tomato&quot;</span>) <span class="sc">+</span> </span>
    <span id="cb42-15"><a href="#cb42-15" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>(<span class="at">data =</span> district12,</span>
    <span id="cb42-16"><a href="#cb42-16" aria-hidden="true" tabindex="-1"></a>             <span class="fu">aes</span>(<span class="at">y =</span> trstprl),</span>
    <span id="cb42-17"><a href="#cb42-17" aria-hidden="true" tabindex="-1"></a>             <span class="at">color =</span> <span class="st">&quot;tomato&quot;</span>,</span>
    <span id="cb42-18"><a href="#cb42-18" aria-hidden="true" tabindex="-1"></a>             <span class="at">position =</span> <span class="st">&quot;jitter&quot;</span>,</span>
    <span id="cb42-19"><a href="#cb42-19" aria-hidden="true" tabindex="-1"></a>             <span class="at">size =</span> <span class="fl">0.7</span>,</span>
    <span id="cb42-20"><a href="#cb42-20" aria-hidden="true" tabindex="-1"></a>             <span class="at">alpha =</span> <span class="fl">0.5</span></span>
    <span id="cb42-21"><a href="#cb42-21" aria-hidden="true" tabindex="-1"></a>             ) <span class="sc">+</span> </span>
    <span id="cb42-22"><a href="#cb42-22" aria-hidden="true" tabindex="-1"></a>  <span class="fu">ylab</span>(<span class="st">&quot;Predicted value of Trust in Parliament&quot;</span>) <span class="sc">+</span></span>
    <span id="cb42-23"><a href="#cb42-23" aria-hidden="true" tabindex="-1"></a>  <span class="fu">xlab</span>(<span class="st">&quot;Trust in Parties&quot;</span>) <span class="sc">+</span></span>
    <span id="cb42-24"><a href="#cb42-24" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">&quot;Linear relationship between trstprl and trstprt (others constant)&quot;</span>,</span>
    <span id="cb42-25"><a href="#cb42-25" aria-hidden="true" tabindex="-1"></a>       <span class="at">lty =</span> <span class="st">&quot;Distrikte&quot;</span>,</span>
    <span id="cb42-26"><a href="#cb42-26" aria-hidden="true" tabindex="-1"></a>       <span class="at">caption =</span> <span class="st">&quot;Highlighted District 12.&quot;</span></span>
    <span id="cb42-27"><a href="#cb42-27" aria-hidden="true" tabindex="-1"></a>       )</span></code></pre></div>
    
    bpkleer's avatar
    bpkleer committed
    <p><img src="ggplot-advanced_files/figure-html/ggplot-districts2-1.png" width="576" style="display: block; margin: auto;" /></p>
    <p>Wir könnten zusätzlich auch noch die anderen Datenpunkte in das Bild einfügen. Man sollte aber immer aufpassen, dass ein Plot nicht zu unübersichtlich wird.</p>
    <div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>districts <span class="ot">&lt;-</span> pss <span class="sc">%&gt;%</span> </span>
    <span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">filter</span>(district <span class="sc">!=</span> <span class="st">&quot;Distrikt 12&quot;</span>) <span class="sc">%&gt;%</span> </span>
    <span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">select</span>(<span class="sc">-</span>district)</span>
    <span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a></span>
    <span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(fakeDistrict12,</span>
    <span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a>       <span class="fu">aes</span>(<span class="at">x =</span> trstprt, </span>
    <span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a>           <span class="at">y =</span> pred</span>
    
    bpkleer's avatar
    bpkleer committed
    <span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a>           )</span>
    <span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a>       ) <span class="sc">+</span></span>
    <span id="cb43-10"><a href="#cb43-10" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_line</span>(<span class="at">color =</span> <span class="st">&quot;tomato&quot;</span>) <span class="sc">+</span> </span>
    <span id="cb43-11"><a href="#cb43-11" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>(<span class="at">data =</span> district12,</span>
    <span id="cb43-12"><a href="#cb43-12" aria-hidden="true" tabindex="-1"></a>             <span class="fu">aes</span>(<span class="at">y =</span> trstprl),</span>
    <span id="cb43-13"><a href="#cb43-13" aria-hidden="true" tabindex="-1"></a>             <span class="at">color =</span> <span class="st">&quot;tomato&quot;</span>,</span>
    <span id="cb43-14"><a href="#cb43-14" aria-hidden="true" tabindex="-1"></a>             <span class="at">position =</span> <span class="st">&quot;jitter&quot;</span>,</span>
    <span id="cb43-15"><a href="#cb43-15" aria-hidden="true" tabindex="-1"></a>             <span class="at">size =</span> <span class="fl">0.7</span>,</span>
    <span id="cb43-16"><a href="#cb43-16" aria-hidden="true" tabindex="-1"></a>             <span class="at">alpha =</span> <span class="fl">0.85</span></span>
    <span id="cb43-17"><a href="#cb43-17" aria-hidden="true" tabindex="-1"></a>             ) <span class="sc">+</span> </span>
    <span id="cb43-18"><a href="#cb43-18" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>(<span class="at">data =</span> districts,</span>
    <span id="cb43-19"><a href="#cb43-19" aria-hidden="true" tabindex="-1"></a>             <span class="fu">aes</span>(<span class="at">y =</span> trstprl),</span>
    <span id="cb43-20"><a href="#cb43-20" aria-hidden="true" tabindex="-1"></a>             <span class="at">color =</span> <span class="st">&quot;darkgray&quot;</span>,</span>
    <span id="cb43-21"><a href="#cb43-21" aria-hidden="true" tabindex="-1"></a>             <span class="at">position =</span> <span class="st">&quot;jitter&quot;</span>,</span>
    <span id="cb43-22"><a href="#cb43-22" aria-hidden="true" tabindex="-1"></a>             <span class="at">size =</span> <span class="fl">0.7</span>,</span>
    <span id="cb43-23"><a href="#cb43-23" aria-hidden="true" tabindex="-1"></a>             <span class="at">alpha =</span> <span class="fl">0.3</span></span>
    <span id="cb43-24"><a href="#cb43-24" aria-hidden="true" tabindex="-1"></a>             ) <span class="sc">+</span> </span>
    <span id="cb43-25"><a href="#cb43-25" aria-hidden="true" tabindex="-1"></a>  <span class="fu">ylab</span>(<span class="st">&quot;Predicted value of Trust in Parliament&quot;</span>) <span class="sc">+</span></span>
    <span id="cb43-26"><a href="#cb43-26" aria-hidden="true" tabindex="-1"></a>  <span class="fu">xlab</span>(<span class="st">&quot;Trust in Parties&quot;</span>) <span class="sc">+</span></span>
    <span id="cb43-27"><a href="#cb43-27" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">&quot;Linear relationship between trstprl and trstprt (others constant)&quot;</span>,</span>
    <span id="cb43-28"><a href="#cb43-28" aria-hidden="true" tabindex="-1"></a>       <span class="at">lty =</span> <span class="st">&quot;Distrikte&quot;</span>,</span>
    <span id="cb43-29"><a href="#cb43-29" aria-hidden="true" tabindex="-1"></a>       <span class="at">caption =</span> <span class="st">&quot;Highlighted District 12.&quot;</span></span>
    <span id="cb43-30"><a href="#cb43-30" aria-hidden="true" tabindex="-1"></a>       ) <span class="sc">+</span></span>
    <span id="cb43-31"><a href="#cb43-31" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">&quot;darkgray&quot;</span>, </span>
    <span id="cb43-32"><a href="#cb43-32" aria-hidden="true" tabindex="-1"></a>                                <span class="st">&quot;darkgray&quot;</span>, </span>
    <span id="cb43-33"><a href="#cb43-33" aria-hidden="true" tabindex="-1"></a>                                <span class="st">&quot;darkgray&quot;</span>, </span>
    <span id="cb43-34"><a href="#cb43-34" aria-hidden="true" tabindex="-1"></a>                                <span class="st">&quot;darkgray&quot;</span>, </span>
    <span id="cb43-35"><a href="#cb43-35" aria-hidden="true" tabindex="-1"></a>                                <span class="st">&quot;tomato&quot;</span></span>
    <span id="cb43-36"><a href="#cb43-36" aria-hidden="true" tabindex="-1"></a>                                )</span>
    <span id="cb43-37"><a href="#cb43-37" aria-hidden="true" tabindex="-1"></a>                     ) <span class="sc">+</span></span>
    <span id="cb43-38"><a href="#cb43-38" aria-hidden="true" tabindex="-1"></a>  <span class="fu">guides</span>(<span class="at">color =</span> <span class="st">&quot;none&quot;</span>)</span></code></pre></div>
    
    bpkleer's avatar
    bpkleer committed
    <p><img src="ggplot-advanced_files/figure-html/ggplot-districts3-1.png" width="576" style="display: block; margin: auto;" /></p>
    <div id="koeffizienten-darstellen" class="section level3">
    <h3>Koeffizienten darstellen</h3>
    <p>Wenn wir aber das Modell grafisch darstellen wollen, also die Koeffizienten (anstatt oder zusätzlich zu einer Tabelle), hilft das <em>package</em> <code>dotwhisker</code>. Mithilfe dieses <em>packages</em> können Objekte aus <code>lm()</code>-Funktionen direkt geplottet werden.</p>
    <p>Zuerst installieren und laden wir das Paket:</p>
    <div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;dotwhisker&quot;</span>)</span>
    <span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a></span>
    <span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(<span class="st">&quot;dotwhisker&quot;</span>)</span></code></pre></div>
    <p>Anschließend ruft man die Funktion <code>dwplot()</code> auf, die die Koeffizienten grafisch darstellt. Dies ist ebenfalls ein <code>ggplot</code>, so dass man es im Anschluss beliebig bearbeiten kann.</p>
    <div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dwplot</span>(model3)</span></code></pre></div>
    <p><img src="ggplot-advanced_files/figure-html/coefplot-1.png" width="576" style="display: block; margin: auto;" /></p>
    <p>Das sieht noch etwas unschön aus und wir bearbeiten dies nun in <code>ggplot</code>: Wir fügen bei <strong>0</strong> eine Linie ein (Signifikanz), wir ändern die Achsenbeschriftung auf der y-Achese und wir ändern die Skala auf der x-Achse und fügen Titel ein.</p>
    <div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dwplot</span>(model3) <span class="sc">+</span></span>
    <span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_vline</span>(<span class="at">xintercept =</span> <span class="dv">0</span>,</span>
    
    bpkleer's avatar
    bpkleer committed
    <span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a>             <span class="at">linetype =</span> <span class="st">&quot;dashed&quot;</span></span>
    <span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a>             ) <span class="sc">+</span></span>
    <span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_y_discrete</span>(<span class="at">labels =</span> <span class="fu">rev</span>(<span class="fu">c</span>(<span class="st">&quot;Trust Parties&quot;</span>, </span>
    <span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a>                                  <span class="st">&quot;Age&quot;</span>, </span>
    <span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a>                                  <span class="st">&quot;Satisfaction w/ Democracy&quot;</span>,</span>
    <span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a>                                  <span class="st">&quot;District 5&quot;</span>, </span>
    <span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a>                                  <span class="st">&quot;District 7&quot;</span>, </span>
    <span id="cb46-10"><a href="#cb46-10" aria-hidden="true" tabindex="-1"></a>                                  <span class="st">&quot;District 10&quot;</span>, </span>
    <span id="cb46-11"><a href="#cb46-11" aria-hidden="true" tabindex="-1"></a>                                  <span class="st">&quot;District 12&quot;</span></span>
    <span id="cb46-12"><a href="#cb46-12" aria-hidden="true" tabindex="-1"></a>                                  )</span>
    <span id="cb46-13"><a href="#cb46-13" aria-hidden="true" tabindex="-1"></a>                                )</span>
    <span id="cb46-14"><a href="#cb46-14" aria-hidden="true" tabindex="-1"></a>                   ) <span class="sc">+</span> </span>
    <span id="cb46-15"><a href="#cb46-15" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">seq</span>(<span class="sc">-</span><span class="dv">2</span>, </span>
    <span id="cb46-16"><a href="#cb46-16" aria-hidden="true" tabindex="-1"></a>                                  <span class="dv">1</span>,</span>
    <span id="cb46-17"><a href="#cb46-17" aria-hidden="true" tabindex="-1"></a>                                  <span class="fl">0.2</span></span>
    <span id="cb46-18"><a href="#cb46-18" aria-hidden="true" tabindex="-1"></a>                                  )</span>
    <span id="cb46-19"><a href="#cb46-19" aria-hidden="true" tabindex="-1"></a>                     ) <span class="sc">+</span></span>
    <span id="cb46-20"><a href="#cb46-20" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">&quot;Lin. Regression on Trust in Parliament (ref: District 1)&quot;</span>,</span>
    <span id="cb46-21"><a href="#cb46-21" aria-hidden="true" tabindex="-1"></a>       <span class="at">caption =</span> <span class="st">&quot;Data: Panem Social Survey.&quot;</span></span>
    <span id="cb46-22"><a href="#cb46-22" aria-hidden="true" tabindex="-1"></a>       )</span></code></pre></div>
    <p><img src="ggplot-advanced_files/figure-html/coefplot2-1.png" width="576" style="display: block; margin: auto;" /></p>
    <div id="das-wars" class="section level4">
    <h4>Das war’s!</h4>
    
    bpkleer's avatar
    bpkleer committed
    <p>Übungsaufgaben findet ihr als <code>task ggplot advanced</code> in der Cloud.</p>
    
    bpkleer's avatar
    bpkleer committed
    </div>
    
    bpkleer's avatar
    bpkleer committed
    </div>
    </div>
    
    <br><br>
    <hr />
    <center>
    <p style="font-size:10px">
      Copyright: <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a>
    <br/>Benedikt Philipp Kleer, Justus-Liebig-Universität Gießen.
    </p>
    </center>
    
    
    </div>
    </div>
    
    </div>
    
    <script>
    
    // add bootstrap table styles to pandoc tables
    function bootstrapStylePandocTables() {
      $('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
    }
    $(document).ready(function () {
      bootstrapStylePandocTables();
    });
    
    
    </script>
    
    <!-- tabsets -->
    
    <script>
    $(document).ready(function () {
      window.buildTabsets("TOC");
    });
    
    $(document).ready(function () {
      $('.tabset-dropdown > .nav-tabs > li').click(function () {
        $(this).parent().toggleClass('nav-tabs-open');
      });
    });
    </script>
    
    <!-- code folding -->
    
    <script>
    $(document).ready(function ()  {
    
        // temporarily add toc-ignore selector to headers for the consistency with Pandoc
        $('.unlisted.unnumbered').addClass('toc-ignore')
    
        // move toc-ignore selectors from section div to header
        $('div.section.toc-ignore')
            .removeClass('toc-ignore')
            .children('h1,h2,h3,h4,h5').addClass('toc-ignore');
    
        // establish options
        var options = {
          selectors: "h1,h2,h3",
          theme: "bootstrap3",
          context: '.toc-content',
          hashGenerator: function (text) {
            return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
          },
          ignoreSelector: ".toc-ignore",
          scrollTo: 0
        };
        options.showAndHide = true;
        options.smoothScroll = true;
    
        // tocify
        var toc = $("#TOC").tocify(options).data("toc-tocify");
    });
    </script>
    
    <!-- dynamically load mathjax for compatibility with self-contained -->
    <script>
      (function () {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
        document.getElementsByTagName("head")[0].appendChild(script);
      })();
    </script>
    
    </body>
    </html>