Skip to content

Commit

Permalink
Fix conditional execution of vignette code
Browse files Browse the repository at this point in the history
  • Loading branch information
nehill197 committed Aug 24, 2024
1 parent 77329cf commit d8dba5f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions vignettes/beta-binomial-deviance-residuals.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ knitr::opts_chunk$set(
library(extras)
library(tidyr)
installed_suggests <- requireNamespace("tidyr", "ggplot2", "viridis", "scales")
```

```{r eval = installed_suggests, include = FALSE}
library(tidyr)
library(ggplot2)
library(viridis)
library(scales)
```


### Beta-binomial distribution

The beta-binomial distribution is useful when we wish to incorporate additional variation into the probability parameter of the binomial distribution, $p$.
Expand Down Expand Up @@ -72,7 +79,7 @@ Both of these approaches have issues.
The problem with the first approach is that likelihood profile of the beta-binomial becomes increasingly dispersed relative to the binomial as $\theta$ increases (Fig. 1).
Consequently, increasing $\theta$ increases the deviance even if the expected value of the fitted model is the same as the saturated binomial model.

```{r eval = requireNamespace("tidyr", "ggplot2", "viridis", "scales"), echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 1: Beta-binomial likelihood profile for $x = 1$ and $n = 5$, for different values of $\\theta$. $\\theta = 0$ corresponds to the binomial case."}
```{r eval = installed_suggests, echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 1: Beta-binomial likelihood profile for $x = 1$ and $n = 5$, for different values of $\\theta$. $\\theta = 0$ corresponds to the binomial case."}
n_samp <- 1000
x <- 1
size <- 5
Expand Down Expand Up @@ -140,7 +147,7 @@ We will refer to the optimized $p$ value for the $i^{th}$ data point as $p_i^*$,
We use $p^*$ to calculate the saturated log-likelihood that produces deviances that are (a) strictly positive, and (b) relative to the value of $\theta$.
The value of $p_i^*$ for the example case with $x = 1$, $n = 5$, and $\theta = 0.5$ is shown in Figure 2.

```{r eval = requireNamespace("ggplot2", "viridis", "scales"), echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 2: Beta-binomial likelihood profile for $x = 1$, $n = 5$, and $\\theta = 0.5$. The dashed vertical line shows the likelihood at the expected value of the beta-binomial distribution ($n \\cdot p$) where $p = \\frac{x}{n} = \\frac{1}{5} = 0.2$. As you can see, this is not the $p$ for which the likelihood is maximized. The solid vertical line shows the likelihood at its maximum point. In this case, $p^* = 0.26$."}
```{r eval = installed_suggests, echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 2: Beta-binomial likelihood profile for $x = 1$, $n = 5$, and $\\theta = 0.5$. The dashed vertical line shows the likelihood at the expected value of the beta-binomial distribution ($n \\cdot p$) where $p = \\frac{x}{n} = \\frac{1}{5} = 0.2$. As you can see, this is not the $p$ for which the likelihood is maximized. The solid vertical line shows the likelihood at its maximum point. In this case, $p^* = 0.26$."}
n_samp <- 1000
x <- 1
size <- 5
Expand Down Expand Up @@ -182,7 +189,7 @@ The *deviance residual* is the signed squared root of the unit deviance,
$$r_i = \text{sign}(x_i - (n p_i^*)) \sqrt{d_i}.$$
The following plot shows histograms of deviance residuals for 10,000 randomly generated data points with $n = 50$ and various values of $p$ and $\theta$.

```{r eval = requireNamespace("tidyr", "ggplot2", "scales"), echo = FALSE, fig.height = 8, fig.width = 7, fig.cap = "Fig. 4: Histograms of deviance residuals for 10,000 beta-binomial data points simulated with $n = 50$, $p = \\{0.3, 0.5, 0.9 \\}$, and $\\theta = \\{0.0, 0.1, 0.5, 1.0 \\}$. The percentages within each panel (i.e., each combination of $p$ and $\\theta$) sum to 100%."}
```{r eval = installed_suggests, echo = FALSE, fig.height = 8, fig.width = 7, fig.cap = "Fig. 4: Histograms of deviance residuals for 10,000 beta-binomial data points simulated with $n = 50$, $p = \\{0.3, 0.5, 0.9 \\}$, and $\\theta = \\{0.0, 0.1, 0.5, 1.0 \\}$. The percentages within each panel (i.e., each combination of $p$ and $\\theta$) sum to 100%."}
n_samp <- 10000
size <- 50
prob <- c(0.3, 0.5, 0.9)
Expand Down

0 comments on commit d8dba5f

Please sign in to comment.