Skip to content

Commit

Permalink
update chi
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyDoorn committed Oct 14, 2024
1 parent 25dd0e3 commit d1a0242
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 88 deletions.
73 changes: 45 additions & 28 deletions SSR/2024/16. chi_squared_test/16. chi_squared_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ <h3 class="smalller anchored" data-anchor-id="contingency-table">Contingency tab
<section id="chi2-distribution" class="level2 smaller subsection">
<h2 class="smaller subsection anchored" data-anchor-id="chi2-distribution"><span class="math inline">\(\chi^2\)</span> distribution</h2>
<p>The <span class="math inline">\(\chi^2\)</span> distribution describes the test statistic under the assumption of <span class="math inline">\(H_0\)</span>, given the degrees of freedom.</p>
<p><span class="math inline">\(df = (r - 1) (c - 1)\)</span> where <span class="math inline">\(r\)</span> is the number of rows and <span class="math inline">\(c\)</span> the amount of columns.</p>
<p><span class="math inline">\(df = (r - 1) (c - 1)\)</span> where <span class="math inline">\(r\)</span> is the number of rows and <span class="math inline">\(c\)</span> the number of columns.</p>
<div class="cell" data-output-location="slide">
<div class="cell-output-display">
<p><img src="16.-chi_squared_test_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid" width="672"></p>
Expand All @@ -237,8 +237,8 @@ <h2 class="smaller anchored" data-anchor-id="data">Data</h2>
<div class="cell">
<div class="cell-output-display">

<div class="datatables html-widget html-fill-item" id="htmlwidget-139bfbd3af67eee112fb" style="width:100%;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-139bfbd3af67eee112fb">{"x":{"filter":"none","vertical":false,"data":[["1","2","3","4","5"],["Dogs","Cats","Other","Dogs","Other"],["Live","Digital","Digital","Live","Digital"],["Introvert","Introvert","Extrovert","Extrovert","Introvert"]],"container":"<table class=\"display\">\n <thead>\n <tr>\n <th> <\/th>\n <th>Animal<\/th>\n <th>Lecture<\/th>\n <th>Personality<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"columnDefs":[{"orderable":false,"targets":0},{"name":" ","targets":0},{"name":"Animal","targets":1},{"name":"Lecture","targets":2},{"name":"Personality","targets":3}],"order":[],"autoWidth":false,"orderClasses":false}},"evals":[],"jsHooks":[]}</script>
<div class="datatables html-widget html-fill-item" id="htmlwidget-42a344916eb606e6e716" style="width:100%;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-42a344916eb606e6e716">{"x":{"filter":"none","vertical":false,"data":[["1","2","3","4","5"],["Dogs","Cats","Other","Dogs","Other"],["Live","Digital","Digital","Live","Digital"],["Introvert","Introvert","Extrovert","Extrovert","Introvert"]],"container":"<table class=\"display\">\n <thead>\n <tr>\n <th> <\/th>\n <th>Animal<\/th>\n <th>Lecture<\/th>\n <th>Personality<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"columnDefs":[{"orderable":false,"targets":0},{"name":" ","targets":0},{"name":"Animal","targets":1},{"name":"Lecture","targets":2},{"name":"Personality","targets":3}],"order":[],"autoWidth":false,"orderClasses":false}},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</small><p><small></small></p>
Expand Down Expand Up @@ -281,41 +281,58 @@ <h2 class="subsection anchored" data-anchor-id="calculating-the-model">Calculati
<h2 class="anchored" data-anchor-id="calculating-the-model-1">Calculating the model</h2>
<p><span class="math inline">\(\text{model}_{ij} = E_{ij} = \frac{\text{row total}_i \times \text{column total}_j}{n }\)</span></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>modelPredictions <span class="ot">&lt;-</span> <span class="fu">matrix</span>( <span class="fu">c</span>((totExt <span class="sc">*</span> totDig) <span class="sc">/</span> n,</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> (totExt <span class="sc">*</span> totLiv) <span class="sc">/</span> n,</span>
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>modelPredictions <span class="ot">&lt;-</span> <span class="fu">matrix</span>( <span class="fu">c</span>((totExt <span class="sc">*</span> totDig) <span class="sc">/</span> n,</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> (totExt <span class="sc">*</span> totLiv) <span class="sc">/</span> n,</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> (totInt <span class="sc">*</span> totDig) <span class="sc">/</span> n,</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> (totInt <span class="sc">*</span> totLiv) <span class="sc">/</span> n), <span class="dv">2</span>, <span class="dv">2</span>, <span class="at">byrow=</span>T</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>modelPredictions</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> (totInt <span class="sc">*</span> totLiv) <span class="sc">/</span> n), <span class="dv">2</span>, <span class="dv">2</span>, </span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="at">byrow=</span><span class="cn">FALSE</span>, <span class="at">dimnames =</span> <span class="fu">dimnames</span>(observed)</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a>modelPredictions</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2]
[1,] 1.2 0.8
[2,] 1.8 1.2</code></pre>
<pre><code> Personality
Lecture Extrovert Introvert
Digital 1.2 1.8
Live 0.8 1.2</code></pre>
</div>
</div>
<p><span class="math inline">\(\text{model}_{ij} = \begin{pmatrix} 1.2 &amp; 0.8 \\ 1.8 &amp; 1.2 \\ \end{pmatrix}\)</span></p>
<p><span class="math inline">\(\text{model}_{ij} = \begin{pmatrix} 1.2 &amp; 1.8 \\ 0.8 &amp; 1.2 \\ \end{pmatrix}\)</span></p>
</section>
<section id="error-observed---model" class="level2 subsection">
<h2 class="subsection anchored" data-anchor-id="error-observed---model">Error = Observed - Model</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>observed <span class="sc">-</span> modelPredictions</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>observed</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Personality
Lecture Extrovert Introvert
Digital 1 2
Live 1 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>modelPredictions</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Personality
Lecture Extrovert Introvert
Digital 1.2 1.8
Live 0.8 1.2</code></pre>
</div>
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>observed <span class="sc">-</span> modelPredictions</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Personality
Lecture Extrovert Introvert
Digital -0.2 1.2
Live -0.8 -0.2</code></pre>
Digital -0.2 0.2
Live 0.2 -0.2</code></pre>
</div>
</div>
</section>
<section id="calculating-chi2-1" class="level2">
<h2 class="anchored" data-anchor-id="calculating-chi2-1">Calculating <span class="math inline">\(\chi^2\)</span></h2>
<p><span class="math inline">\(\chi^2 = \sum \frac{(\text{observed}_{ij} - \text{model}_{ij})^2}{\text{model}_{ij}}\)</span></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate chi squared</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>chi.squared <span class="ot">&lt;-</span> <span class="fu">sum</span>((observed <span class="sc">-</span> modelPredictions)<span class="sc">^</span><span class="dv">2</span> <span class="sc">/</span> modelPredictions)</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>chi.squared</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate chi squared</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>chi.squared <span class="ot">&lt;-</span> <span class="fu">sum</span>((observed <span class="sc">-</span> modelPredictions)<span class="sc">^</span><span class="dv">2</span> <span class="sc">/</span> modelPredictions)</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>chi.squared</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.222222</code></pre>
<pre><code>[1] 0.1388889</code></pre>
</div>
</div>
</section>
Expand Down Expand Up @@ -344,13 +361,13 @@ <h2 class="subsection anchored" data-anchor-id="yatess-correction">Yates’s cor
<p>For 2 x 2 contingency tables, Yates’s correction is to prevent overestimation of statistical significance for small data (at least one cell of the table has an expected count smaller than 5). Unfortunately, Yates’s correction may tend to overcorrect. This can result in an overly conservative result.</p>
<p><span class="math inline">\(\chi^2 = \sum \frac{ ( | \text{observed}_{ij} - \text{model}_{ij} | - .5)^2}{\text{model}_{ij}}\)</span></p>
<div class="cell" data-output-location="slide">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate Yates's corrected chi squared</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>chi.squared.yates <span class="ot">&lt;-</span> <span class="fu">sum</span>((<span class="fu">abs</span>(observed <span class="sc">-</span> modelPredictions) <span class="sc">-</span> .<span class="dv">5</span>)<span class="sc">^</span><span class="dv">2</span> <span class="sc">/</span> modelPredictions)</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>chi.squared.yates</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate Yates's corrected chi squared</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>chi.squared.yates <span class="ot">&lt;-</span> <span class="fu">sum</span>((<span class="fu">abs</span>(observed <span class="sc">-</span> modelPredictions) <span class="sc">-</span> .<span class="dv">5</span>)<span class="sc">^</span><span class="dv">2</span> <span class="sc">/</span> modelPredictions)</span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>chi.squared.yates</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8125</code></pre>
<pre><code>[1] 0.3125</code></pre>
</div>
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co"># visualize::visualize.chisq(chi.squared.yates, df, section='upper')</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="co"># visualize::visualize.chisq(chi.squared.yates, df, section='upper')</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<!-- ## Likelihood ratio {.subsection} -->
<!-- Alternative to Pearson's $\chi^2$. -->
Expand All @@ -366,20 +383,20 @@ <h2 class="subsection anchored" data-anchor-id="yatess-correction">Yates’s cor
<h2 class="subsection anchored" data-anchor-id="standardized-residuals">Standardized residuals</h2>
<p><span class="math inline">\(\text{standardized residuals} = \frac{ \text{observed}_{ij} - \text{model}_{ij} }{ \sqrt{ \text{model}_{ij} } }\)</span></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>(observed <span class="sc">-</span> modelPredictions) <span class="sc">/</span> <span class="fu">sqrt</span>(modelPredictions)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>(observed <span class="sc">-</span> modelPredictions) <span class="sc">/</span> <span class="fu">sqrt</span>(modelPredictions)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Personality
Lecture Extrovert Introvert
Digital -0.1825742 1.3416408
Live -0.5962848 -0.1825742</code></pre>
Digital -0.1825742 0.1490712
Live 0.2236068 -0.1825742</code></pre>
</div>
</div>
</section>
<section id="effect-size" class="level2 subsection">
<h2 class="subsection anchored" data-anchor-id="effect-size">Effect size</h2>
<p>Odds ratio based on the observed values</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>odds <span class="ot">&lt;-</span> <span class="fu">round</span>( observed, <span class="dv">2</span>); odds</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>odds <span class="ot">&lt;-</span> <span class="fu">round</span>( observed, <span class="dv">2</span>); odds</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Personality
Lecture Extrovert Introvert
Expand Down
Loading

0 comments on commit d1a0242

Please sign in to comment.