-
Notifications
You must be signed in to change notification settings - Fork 15
/
kde-comparison.Rmd
executable file
·102 lines (86 loc) · 2.95 KB
/
kde-comparison.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
title: "Bounded KDE for ggdist comparison"
author: "Matthew Kay"
date: "2023-04-21"
output:
github_document
---
```{r setup, warning = FALSE, message = FALSE}
library(ggplot2)
library(dplyr)
library(distributional)
library(ggdist)
library(ggtext)
library(posterior)
library(tidybayes)
library(ragg)
knitr::opts_chunk$set(dev = "ragg_png", fig.retina = 2)
```
```{r}
set.seed(1234)
kde_comparison_plot = function(dists, sample_size = 100) {
tibble(
x = dists,
x_name = format(x),
x_sample = dist_sample(generate(x, sample_size))
) |>
ggplot() +
stat_slab(aes(xdist = x), fill = "gray85", n = 2000) +
geom_rug(aes(x = x_sample), data = \(d) d |> transmute(x_name, x_sample = as_rvar(x_sample)) |> unnest_rvars(), alpha = 0.2) +
stat_slab(aes(xdist = x_sample), color = "gray25", fill = NA, density = density_unbounded(bandwidth = "nrd0"), alpha = 0.5, n = 2000) +
stat_slab(aes(xdist = x_sample), color = "red", density = density_bounded(bounder = "cdf", bandwidth = "dpi"), fill = NA, alpha = 0.75, linetype = "11", n = 2000) +
# stat_slab(aes(xdist = x_sample), color = "blue", density = "histogram", breaks = "FD", fill = NA, alpha = 0.75, linetype = "11", n = 2000) +
scale_thickness_shared() +
facet_wrap(~ x_name) +
theme_ggdist() +
theme(plot.title = element_markdown(lineheight = 0.7), plot.subtitle = element_markdown(lineheight = 1.2)) +
labs(
title = paste0("Kernel density estimates on samples of size <b>", sample_size, "</b> using *ggdist::stat_slab*"),
subtitle = paste0(
"Using the old default (<span style='color: gray25;'>**trimmed unbounded KDE with nrd0 bandwidth**</span>)",
"<br>or new default (<span style='color: red;'>**bounded KDE with loose bounds estimator and dpi bandwidth**</span>)",
# "<br>or *possible* new default (<span style='color: blue;'>**bounded KDE with looser bounds estimator**</span>)"
"<br>versus the <span style='color: gray65;'>**theoretical density**</span>."
),
x = NULL,
y = NULL
) +
scale_y_continuous(breaks = 0) +
geom_hline(yintercept = 0, color = "gray85", linetype = "22")
}
```
```{r}
set.seed(1234)
kde_comparison_plot(dist_beta(1:4, 1:4), 100)
```
```{r}
set.seed(1234)
kde_comparison_plot(dist_beta(1:4, 1:4), 5000)
```
```{r}
set.seed(1234)
kde_comparison_plot(c(dist_gamma(1:3, 1:3), dist_lognormal(-.25, 1)), 100) +
coord_cartesian(xlim = c(0, 6))
```
```{r}
set.seed(1234)
kde_comparison_plot(c(dist_gamma(1:3, 1:3), dist_lognormal(-.25, 1)), 5000) +
coord_cartesian(xlim = c(0, 6))
```
```{r}
set.seed(1234)
kde_comparison_plot(c(dist_lognormal(2, 1)), 5000) +
coord_cartesian(xlim = c(0, 10))
```
```{r}
set.seed(1234)
kde_comparison_plot(c(dist_beta(0.5, 0.5)), 5000)
```
```{r eval = FALSE}
set.seed(1234)
saveGIF(
for (i in 1:50) print(kde_comparison_plot(dist_beta(1:4, 1:4), 100)),
"beta_100.gif",
ani.width = 1344, ani.height = 960, ani.dev = "agg_png", ani.res = 150
)
```