522 lines
45 KiB
HTML
522 lines
45 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||
|
||
<meta charset="utf-8">
|
||
<meta name="generator" content="quarto-1.1.251">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||
|
||
<meta name="author" content="Emanuel Rodriguez">
|
||
|
||
<title>Chapter 3 Beta-Binomial Bayesian Model Notes</title>
|
||
<style>
|
||
code{white-space: pre-wrap;}
|
||
span.smallcaps{font-variant: small-caps;}
|
||
div.columns{display: flex; gap: min(4vw, 1.5em);}
|
||
div.column{flex: auto; overflow-x: auto;}
|
||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||
ul.task-list{list-style: none;}
|
||
ul.task-list li input[type="checkbox"] {
|
||
width: 0.8em;
|
||
margin: 0 0.8em 0.2em -1.6em;
|
||
vertical-align: middle;
|
||
}
|
||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||
.sourceCode { overflow: visible; }
|
||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||
div.sourceCode { margin: 1em 0; }
|
||
pre.sourceCode { margin: 0; }
|
||
@media screen {
|
||
div.sourceCode { overflow: auto; }
|
||
}
|
||
@media print {
|
||
pre > code.sourceCode { white-space: pre-wrap; }
|
||
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||
}
|
||
pre.numberSource code
|
||
{ counter-reset: source-line 0; }
|
||
pre.numberSource code > span
|
||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||
pre.numberSource code > span > a:first-child::before
|
||
{ content: counter(source-line);
|
||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||
border: none; display: inline-block;
|
||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||
-khtml-user-select: none; -moz-user-select: none;
|
||
-ms-user-select: none; user-select: none;
|
||
padding: 0 4px; width: 4em;
|
||
color: #aaaaaa;
|
||
}
|
||
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
||
div.sourceCode
|
||
{ }
|
||
@media screen {
|
||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||
}
|
||
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
||
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
||
code span.at { color: #7d9029; } /* Attribute */
|
||
code span.bn { color: #40a070; } /* BaseN */
|
||
code span.bu { color: #008000; } /* BuiltIn */
|
||
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
||
code span.ch { color: #4070a0; } /* Char */
|
||
code span.cn { color: #880000; } /* Constant */
|
||
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
||
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
||
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
||
code span.dt { color: #902000; } /* DataType */
|
||
code span.dv { color: #40a070; } /* DecVal */
|
||
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
||
code span.ex { } /* Extension */
|
||
code span.fl { color: #40a070; } /* Float */
|
||
code span.fu { color: #06287e; } /* Function */
|
||
code span.im { color: #008000; font-weight: bold; } /* Import */
|
||
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
||
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
||
code span.op { color: #666666; } /* Operator */
|
||
code span.ot { color: #007020; } /* Other */
|
||
code span.pp { color: #bc7a00; } /* Preprocessor */
|
||
code span.sc { color: #4070a0; } /* SpecialChar */
|
||
code span.ss { color: #bb6688; } /* SpecialString */
|
||
code span.st { color: #4070a0; } /* String */
|
||
code span.va { color: #19177c; } /* Variable */
|
||
code span.vs { color: #4070a0; } /* VerbatimString */
|
||
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
||
</style>
|
||
|
||
|
||
<script src="ch3-beta-binomial_files/libs/clipboard/clipboard.min.js"></script>
|
||
<script src="ch3-beta-binomial_files/libs/quarto-html/quarto.js"></script>
|
||
<script src="ch3-beta-binomial_files/libs/quarto-html/popper.min.js"></script>
|
||
<script src="ch3-beta-binomial_files/libs/quarto-html/tippy.umd.min.js"></script>
|
||
<script src="ch3-beta-binomial_files/libs/quarto-html/anchor.min.js"></script>
|
||
<link href="ch3-beta-binomial_files/libs/quarto-html/tippy.css" rel="stylesheet">
|
||
<link href="ch3-beta-binomial_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
|
||
<script src="ch3-beta-binomial_files/libs/bootstrap/bootstrap.min.js"></script>
|
||
<link href="ch3-beta-binomial_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||
<link href="ch3-beta-binomial_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
|
||
|
||
<script>window.backupDefine = window.define; window.define = undefined;</script><script src="https://cdn.jsdelivr.net/npm/katex@0.15.1/dist/katex.min.js"></script>
|
||
<script>document.addEventListener("DOMContentLoaded", function () {
|
||
var mathElements = document.getElementsByClassName("math");
|
||
var macros = [];
|
||
for (var i = 0; i < mathElements.length; i++) {
|
||
var texText = mathElements[i].firstChild;
|
||
if (mathElements[i].tagName == "SPAN") {
|
||
katex.render(texText.data, mathElements[i], {
|
||
displayMode: mathElements[i].classList.contains('display'),
|
||
throwOnError: false,
|
||
macros: macros,
|
||
fleqn: false
|
||
});
|
||
}}});
|
||
</script>
|
||
<script>window.define = window.backupDefine; window.backupDefine = undefined;</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.15.1/dist/katex.min.css">
|
||
|
||
<link rel="stylesheet" href="styles.css">
|
||
</head>
|
||
|
||
<body class="fullcontent">
|
||
|
||
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
|
||
|
||
<main class="content" id="quarto-document-content">
|
||
|
||
<header id="title-block-header" class="quarto-title-block default">
|
||
<div class="quarto-title">
|
||
<h1 class="title">Chapter 3 Beta-Binomial Bayesian Model Notes</h1>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="quarto-title-meta">
|
||
|
||
<div>
|
||
<div class="quarto-title-meta-heading">Author</div>
|
||
<div class="quarto-title-meta-contents">
|
||
<p>Emanuel Rodriguez </p>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
|
||
</header>
|
||
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(bayesrules)</span>
|
||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
|
||
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(patchwork)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>The chapter is set up with an example of polling results. We are put into the scenario where we are managig the campaing for a candidate. We know that on average her support based on recent polls is around 45%. In the next few sections we’ll work through our Bayesian framework and incorporate a new tool the <strong>Beta-Binomial</strong> model. This model will take develop a continuous prior, as opposed to the discrete one’s we’ve been working with so far.</p>
|
||
<section id="the-beta-prior" class="level2">
|
||
<h2 class="anchored" data-anchor-id="the-beta-prior">The Beta prior</h2>
|
||
<div class="callout-note callout callout-style-default no-icon callout-captioned">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon no-icon"></i>
|
||
</div>
|
||
<div class="callout-caption-container flex-fill">
|
||
Probability Density Function
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>Let <span class="math inline">\pi</span> be a continuous random variable with probability density function (pdf) <span class="math inline">f(\pi)</span>. Then <span class="math inline">f(\pi)</span> has the following properties:</p>
|
||
<ol type="1">
|
||
<li><span class="math inline">f(\pi) \geq 0</span></li>
|
||
<li><span class="math inline">\int_{\pi}f(\pi)d\pi = 1</span> (this is analogous to <span class="math inline">\sum</span> in the case of pmfs)</li>
|
||
<li><span class="math inline">P(a < \pi < b) = \int_a^bf(\pi)d\pi</span> when <span class="math inline">a\leq b</span></li>
|
||
</ol>
|
||
</div>
|
||
</div>
|
||
<div class="callout-tip callout callout-style-default callout-captioned">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon"></i>
|
||
</div>
|
||
<div class="callout-caption-container flex-fill">
|
||
Tip
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>a quick note on (1) above. Note that it does not place a restriction on <span class="math inline">f(\pi)</span> being less than 1. This means that we can’t interpret values of <span class="math inline">f</span> as probabilities, we can however use to interpret plausability of two different events, the greater the value of <span class="math inline">f</span> the more plausible. To calculate probabilities using <span class="math inline">f</span> we must determine the area under the curve it defines, as shown in (3).</p>
|
||
</div>
|
||
</div>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">seq</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="at">by =</span> .<span class="dv">05</span>)</span>
|
||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>y1 <span class="ot"><-</span> <span class="fu">dbeta</span>(<span class="at">x=</span>x, <span class="dv">5</span>, <span class="dv">5</span>)</span>
|
||
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>y2 <span class="ot"><-</span> <span class="fu">dbeta</span>(<span class="at">x=</span>x, <span class="dv">5</span>, <span class="dv">1</span>)</span>
|
||
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>y3 <span class="ot"><-</span> <span class="fu">dbeta</span>(<span class="at">x=</span>x, <span class="dv">1</span>, <span class="dv">5</span>)</span>
|
||
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>d <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
|
||
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> x, </span>
|
||
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(5, 1)</span><span class="st">`</span><span class="ot">=</span>y2, </span>
|
||
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(5, 5)</span><span class="st">`</span><span class="ot">=</span>y1, </span>
|
||
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(1, 5)</span><span class="st">`</span><span class="ot">=</span>y3</span>
|
||
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|></span></span>
|
||
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"beta_shape"</span>, <span class="at">values_to=</span><span class="st">"beta"</span>, </span>
|
||
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a> <span class="sc">-</span>x) <span class="sc">|></span></span>
|
||
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">beta_shape=</span><span class="fu">factor</span>(beta_shape, </span>
|
||
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a> <span class="at">levels=</span><span class="fu">c</span>(<span class="st">"beta(5, 1)"</span>,</span>
|
||
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a> <span class="st">"beta(5, 5)"</span>, </span>
|
||
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a> <span class="st">"beta(1, 5)"</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data=</span>d, <span class="fu">aes</span>(x, beta)) <span class="sc">+</span> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
|
||
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>() <span class="sc">+</span> </span>
|
||
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(beta_shape))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<div id="fig-beta-shapes" class="quarto-figure quarto-figure-center anchored">
|
||
<figure class="figure">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/fig-beta-shapes-1.png" class="img-fluid figure-img" width="672"></p>
|
||
<p></p><figcaption class="figure-caption">Figure 1: The basic shapes of beta based on the hyperparameters</figcaption><p></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>In general the shape of the beta distribution is skewed-left when <span class="math inline">\alpha > \beta</span>, symmetrical when <span class="math inline">\alpha = \beta</span> and skewed-right when <span class="math inline">\alpha < \beta</span>, see <a href="#fig-beta-shapes">Figure 1</a>.</p>
|
||
<div class="callout-note callout callout-style-default no-icon callout-captioned">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon no-icon"></i>
|
||
</div>
|
||
<div class="callout-caption-container flex-fill">
|
||
The Standard Uniform
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>When <span class="math inline">\pi</span> can take equally take on any value between 0 and 1, we can model <span class="math inline">\pi</span> using the standard uniform model.</p>
|
||
<p><span class="math display">\pi \sim Unif(0, 1)</span></p>
|
||
<p>the pdf of <span class="math inline">Unif(0, 1)</span> is <span class="math inline">f(\pi) = 1</span></p>
|
||
<p>Note that <span class="math inline">Unif(0, 1)</span> is just a special case of the Beta with hyperparameters <span class="math inline">\alpha = \beta = 1</span>, see <a href="#fig-std-unif-as-beta">Figure 2</a></p>
|
||
</div>
|
||
</div>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>std_unif <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
|
||
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> x, <span class="st">`</span><span class="at">beta(1, 1)</span><span class="st">`</span><span class="ot">=</span><span class="fu">dbeta</span>(x, <span class="dv">1</span>, <span class="dv">1</span>)</span>
|
||
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>)</span>
|
||
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data=</span>std_unif, <span class="fu">aes</span>(x, <span class="st">`</span><span class="at">beta(1, 1)</span><span class="st">`</span>)) <span class="sc">+</span> </span>
|
||
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span> </span>
|
||
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<div id="fig-std-unif-as-beta" class="quarto-figure quarto-figure-center anchored">
|
||
<figure class="figure">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/fig-std-unif-as-beta-1.png" class="img-fluid figure-img" width="672"></p>
|
||
<p></p><figcaption class="figure-caption">Figure 2: The standard uniform is a special case of the beta distrubtion with a = b = 1</figcaption><p></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<section id="mean-and-mode-of-the-beta" class="level3">
|
||
<h3 class="anchored" data-anchor-id="mean-and-mode-of-the-beta">Mean and Mode of the Beta</h3>
|
||
<p>The mean and mode are both measures of centrality. The mean is average value the mode is the most “common”, in the case of pmf this is just the value that occurs the most in the pdf its the max value.</p>
|
||
<p>The formulations of these for the beta are:</p>
|
||
<p><span class="math display">E(\pi) = \frac{\alpha}{\alpha + \beta}</span> <span class="math display">\text{Mode}(\pi) = \frac{\alpha - 1}{\alpha + \beta -2}\;\; \text{when} \;\;\alpha,\beta > 1</span></p>
|
||
<p>When can also measure the variability of <span class="math inline">\pi</span>. Take <a href="#fig-beta-vars">Figure 3</a> we can see the variability of <span class="math inline">\pi</span> differ based on the values <span class="math inline">\alpha, \beta</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>beta_variances <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
|
||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> x, </span>
|
||
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(5, 5)</span><span class="st">`</span><span class="ot">=</span><span class="fu">dbeta</span>(x, <span class="dv">5</span>, <span class="dv">5</span>), </span>
|
||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(20, 20)</span><span class="st">`</span><span class="ot">=</span><span class="fu">dbeta</span>(x, <span class="dv">20</span>, <span class="dv">20</span>)</span>
|
||
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|></span></span>
|
||
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"beta_shape"</span>, <span class="at">values_to =</span> <span class="st">"beta"</span>, <span class="sc">-</span>x)</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><span class="fu">ggplot</span>(<span class="at">data=</span>beta_variances, <span class="fu">aes</span>(x, beta)) <span class="sc">+</span> </span>
|
||
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span> </span>
|
||
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>() <span class="sc">+</span> </span>
|
||
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(beta_shape))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<div id="fig-beta-vars" class="quarto-figure quarto-figure-center anchored">
|
||
<figure class="figure">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/fig-beta-vars-1.png" class="img-fluid figure-img" width="672"></p>
|
||
<p></p><figcaption class="figure-caption">Figure 3: Two symmetrical shapes of beta with different variance</figcaption><p></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>We can formulate the variance of <span class="math inline">Beta(\alpha, \beta)</span> with</p>
|
||
<p><span class="math display">Var(\pi) = \frac{\alpha\beta}{(\alpha + \beta)^2(\alpha+\beta+1)}</span></p>
|
||
<p>it follows that</p>
|
||
<p><span class="math display">SD = \sqrt{Var(\pi)}</span></p>
|
||
</section>
|
||
<section id="tuning-the-beta-prior" class="level3">
|
||
<h3 class="anchored" data-anchor-id="tuning-the-beta-prior">Tuning the beta prior</h3>
|
||
<p>Now that we know we can use the beta model to represent <span class="math inline">\pi</span> for <span class="math inline">\pi \in [0, 1]</span>, what we need to try and do is tune the model so that it best represents our prior. Our options of course are chaning values of <span class="math inline">\alpha</span> and <span class="math inline">\beta</span>. We can make use of the fact that we know that on average the candidate is polling at around 45%. That is we know that</p>
|
||
<p><span class="math display">E(\pi) \approx .45 \approx \frac{\alpha}{\alpha + \beta}</span></p>
|
||
<p><em>so I tried to work through the algebra myself, but was unable to do so, the book gets to the following conclusion</em></p>
|
||
<p><span class="math display">\alpha = \frac{9}{11}\beta</span></p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>p <span class="ot"><-</span> <span class="fu">plot_beta</span>(<span class="dv">9</span>, <span class="dv">11</span>) <span class="sc">+</span> </span>
|
||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title=</span><span class="st">"Beta(9,11)"</span>) <span class="sc">+</span> </span>
|
||
<span id="cb6-3"><a href="#cb6-3" 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="dv">0</span>, <span class="dv">1</span>, <span class="at">by=</span>.<span class="dv">1</span>))</span>
|
||
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">plot_beta</span>(<span class="dv">27</span>, <span class="dv">33</span>) <span class="sc">+</span> </span>
|
||
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title=</span><span class="st">"Beta(27,33)"</span>) <span class="sc">+</span> </span>
|
||
<span id="cb6-6"><a href="#cb6-6" 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="dv">0</span>, <span class="dv">1</span>, <span class="at">by=</span>.<span class="dv">1</span>))</span>
|
||
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>p3 <span class="ot"><-</span> <span class="fu">plot_beta</span>(<span class="dv">45</span>, <span class="dv">55</span>) <span class="sc">+</span> </span>
|
||
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title=</span><span class="st">"Beta(45,55)"</span>) <span class="sc">+</span> </span>
|
||
<span id="cb6-9"><a href="#cb6-9" 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="dv">0</span>, <span class="dv">1</span>, <span class="at">by=</span>.<span class="dv">1</span>))</span>
|
||
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a>p <span class="sc">/</span> p2 <span class="sc">/</span> p3</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<p>when compared to the original polling values, we can see that the closest of these is <span class="math inline">Beta(45, 55)</span></p>
|
||
<p>we can get the pdf, and then mean, mode and variance of the pdf.</p>
|
||
<p><span class="math display">f(\pi) = \frac{\Gamma(100)}{\Gamma(45)\Gamma(55)}\pi^{44}(1-\pi)^{54}</span></p>
|
||
<p><span class="math display">E(\pi) = \frac{45}{45 + 55} = .4500</span> <span class="math display">\text{Mode}(\pi) = \frac{45 - 1}{45 + 55 - 2} = .449</span> <span class="math display"> SD(\pi) = .05</span></p>
|
||
<div class="cell" data-fig.caption="the prior distrubtions with Y = 30 hihglighted">
|
||
<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>rb1 <span class="ot"><-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">2</span>)</span>
|
||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>rb2 <span class="ot"><-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">3</span>)</span>
|
||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>rb3 <span class="ot"><-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">5</span>)</span>
|
||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>rb4 <span class="ot"><-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">6</span>)</span>
|
||
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a>rb5 <span class="ot"><-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">7</span>)</span>
|
||
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>rb6 <span class="ot"><-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">8</span>)</span>
|
||
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a>results <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
|
||
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .2)</span><span class="st">`</span> <span class="ot">=</span> rb1,</span>
|
||
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .3)</span><span class="st">`</span> <span class="ot">=</span> rb2,</span>
|
||
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .5)</span><span class="st">`</span> <span class="ot">=</span> rb3,</span>
|
||
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .6)</span><span class="st">`</span> <span class="ot">=</span> rb4,</span>
|
||
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .7)</span><span class="st">`</span> <span class="ot">=</span> rb5,</span>
|
||
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .8)</span><span class="st">`</span> <span class="ot">=</span> rb6,</span>
|
||
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|></span></span>
|
||
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"bin_model"</span>, <span class="at">values_to =</span> <span class="st">"value"</span>, </span>
|
||
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .2)</span><span class="st">`</span><span class="sc">:</span><span class="st">`</span><span class="at">Bin(50, .8)</span><span class="st">`</span>) <span class="sc">|></span></span>
|
||
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">is_30 =</span> value <span class="sc">==</span> <span class="dv">30</span>)</span>
|
||
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a>results <span class="sc">|></span></span>
|
||
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(value, <span class="at">fill=</span>is_30)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"count"</span>) <span class="sc">+</span></span>
|
||
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"grey30"</span>, <span class="st">"darkblue"</span>), <span class="at">guide=</span><span class="st">"none"</span>) <span class="sc">+</span> </span>
|
||
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(bin_model))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<div id="fig-betas-for-poll" class="quarto-figure quarto-figure-center anchored">
|
||
<figure class="figure">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/fig-betas-for-poll-1.png" class="img-fluid figure-img" width="672"></p>
|
||
<p></p><figcaption class="figure-caption">Figure 4: <strong>?(caption)</strong></figcaption><p></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>After the finish the poll, we see that the actual value of <span class="math inline">Y</span> is 30. The vertical line in <a href="#fig-betas-for-poll">Figure 4</a></p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>results <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
|
||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="at">ys =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">50</span>, </span>
|
||
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .1)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">1</span>),</span>
|
||
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .2)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">2</span>),</span>
|
||
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .3)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">3</span>),</span>
|
||
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .4)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">4</span>),</span>
|
||
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .5)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">5</span>),</span>
|
||
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .6)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">6</span>),</span>
|
||
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .7)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">7</span>),</span>
|
||
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .8)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">8</span>),</span>
|
||
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .9)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">9</span>),</span>
|
||
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|></span></span>
|
||
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"beta_model"</span>, <span class="at">values_to=</span><span class="st">"value"</span>, <span class="sc">-</span>ys) <span class="sc">|></span></span>
|
||
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
|
||
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a> <span class="at">is_30 =</span> ys <span class="sc">==</span> <span class="dv">30</span>, </span>
|
||
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a> <span class="at">pie =</span> <span class="fu">rep</span>(<span class="fu">c</span>(.<span class="dv">1</span>, .<span class="dv">2</span>, .<span class="dv">3</span>, .<span class="dv">4</span>, .<span class="dv">5</span>, .<span class="dv">6</span>, .<span class="dv">7</span>, .<span class="dv">8</span>, .<span class="dv">9</span>), <span class="dv">50</span>))</span>
|
||
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a>results <span class="sc">|></span> <span class="fu">ggplot</span>(<span class="fu">aes</span>(ys, value, <span class="at">fill =</span> is_30)) <span class="sc">+</span> </span>
|
||
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>() <span class="sc">+</span> </span>
|
||
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"darkgray"</span>, <span class="st">"darkblue"</span>), <span class="at">guide=</span><span class="st">"none"</span>) <span class="sc">+</span> </span>
|
||
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(beta_model))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<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>results <span class="sc">|></span></span>
|
||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(is_30) <span class="sc">|></span></span>
|
||
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(pie, value)) <span class="sc">+</span> <span class="fu">geom_col</span>() <span class="sc">+</span> </span>
|
||
<span id="cb9-4"><a href="#cb9-4" 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="dv">1</span>, .<span class="dv">9</span>, <span class="at">by =</span> .<span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summarize_beta_binomial</span>(<span class="at">alpha=</span><span class="dv">45</span>, <span class="at">beta=</span><span class="dv">55</span>, <span class="at">n=</span><span class="dv">50</span>, <span class="at">y=</span><span class="dv">30</span>)</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> model alpha beta mean mode var sd
|
||
1 prior 45 55 0.45 0.4489796 0.002450495 0.04950248
|
||
2 posterior 75 75 0.50 0.5000000 0.001655629 0.04068942</code></pre>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="simulation" class="level2">
|
||
<h2 class="anchored" data-anchor-id="simulation">Simulation</h2>
|
||
<p>In this simulation we first generate 10,000 random values from a beta distribution with parameter <span class="math inline">\alpha = 45, \beta = 55</span>. Recall, that this is what we believe to be support based on recent polls for the candidate, and therefore our prior. We use each one of these are the <code>prob</code> value (the <span class="math inline">\pi</span> value) into 10,000 more random this time from the binomial distribution with <span class="math inline">n = 50</span>. This binomial distribution is the model for <span class="math inline">Y</span> conditioned on <span class="math inline">\pi</span>.</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>sim <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
|
||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="at">pi =</span> <span class="fu">rbeta</span>(<span class="dv">10000</span>, <span class="dv">45</span>, <span class="dv">55</span>)</span>
|
||
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|></span></span>
|
||
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">y =</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> pi))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<div class="cell">
|
||
<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>sim <span class="sc">|></span></span>
|
||
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(pi, y)) <span class="sc">+</span> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">color =</span> (y <span class="sc">==</span> <span class="dv">30</span>))) <span class="sc">+</span> </span>
|
||
<span id="cb13-3"><a href="#cb13-3" 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">"darkgrey"</span>, <span class="st">"navyblue"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<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>sim <span class="sc">|></span></span>
|
||
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(y <span class="sc">==</span> <span class="dv">30</span>) <span class="sc">|></span> <span class="co"># the posterior</span></span>
|
||
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(pi)) <span class="sc">+</span> <span class="fu">geom_density</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
</main>
|
||
<!-- /main column -->
|
||
<script id="quarto-html-after-body" type="application/javascript">
|
||
window.document.addEventListener("DOMContentLoaded", function (event) {
|
||
const toggleBodyColorMode = (bsSheetEl) => {
|
||
const mode = bsSheetEl.getAttribute("data-mode");
|
||
const bodyEl = window.document.querySelector("body");
|
||
if (mode === "dark") {
|
||
bodyEl.classList.add("quarto-dark");
|
||
bodyEl.classList.remove("quarto-light");
|
||
} else {
|
||
bodyEl.classList.add("quarto-light");
|
||
bodyEl.classList.remove("quarto-dark");
|
||
}
|
||
}
|
||
const toggleBodyColorPrimary = () => {
|
||
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
|
||
if (bsSheetEl) {
|
||
toggleBodyColorMode(bsSheetEl);
|
||
}
|
||
}
|
||
toggleBodyColorPrimary();
|
||
const icon = "";
|
||
const anchorJS = new window.AnchorJS();
|
||
anchorJS.options = {
|
||
placement: 'right',
|
||
icon: icon
|
||
};
|
||
anchorJS.add('.anchored');
|
||
const clipboard = new window.ClipboardJS('.code-copy-button', {
|
||
target: function(trigger) {
|
||
return trigger.previousElementSibling;
|
||
}
|
||
});
|
||
clipboard.on('success', function(e) {
|
||
// button target
|
||
const button = e.trigger;
|
||
// don't keep focus
|
||
button.blur();
|
||
// flash "checked"
|
||
button.classList.add('code-copy-button-checked');
|
||
var currentTitle = button.getAttribute("title");
|
||
button.setAttribute("title", "Copied!");
|
||
setTimeout(function() {
|
||
button.setAttribute("title", currentTitle);
|
||
button.classList.remove('code-copy-button-checked');
|
||
}, 1000);
|
||
// clear code selection
|
||
e.clearSelection();
|
||
});
|
||
function tippyHover(el, contentFn) {
|
||
const config = {
|
||
allowHTML: true,
|
||
content: contentFn,
|
||
maxWidth: 500,
|
||
delay: 100,
|
||
arrow: false,
|
||
appendTo: function(el) {
|
||
return el.parentElement;
|
||
},
|
||
interactive: true,
|
||
interactiveBorder: 10,
|
||
theme: 'quarto',
|
||
placement: 'bottom-start'
|
||
};
|
||
window.tippy(el, config);
|
||
}
|
||
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
|
||
for (var i=0; i<noterefs.length; i++) {
|
||
const ref = noterefs[i];
|
||
tippyHover(ref, function() {
|
||
// use id or data attribute instead here
|
||
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
|
||
try { href = new URL(href).hash; } catch {}
|
||
const id = href.replace(/^#\/?/, "");
|
||
const note = window.document.getElementById(id);
|
||
return note.innerHTML;
|
||
});
|
||
}
|
||
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
|
||
for (var i=0; i<bibliorefs.length; i++) {
|
||
const ref = bibliorefs[i];
|
||
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
|
||
tippyHover(ref, function() {
|
||
var popup = window.document.createElement('div');
|
||
cites.forEach(function(cite) {
|
||
var citeDiv = window.document.createElement('div');
|
||
citeDiv.classList.add('hanging-indent');
|
||
citeDiv.classList.add('csl-entry');
|
||
var biblioDiv = window.document.getElementById('ref-' + cite);
|
||
if (biblioDiv) {
|
||
citeDiv.innerHTML = biblioDiv.innerHTML;
|
||
}
|
||
popup.appendChild(citeDiv);
|
||
});
|
||
return popup.innerHTML;
|
||
});
|
||
}
|
||
});
|
||
</script>
|
||
</div> <!-- /content -->
|
||
|
||
|
||
|
||
</body></html> |