Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtraining 1.2.3 #101

Merged
merged 6 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rtraining
Title: R Training
Version: 1.2.2
Version: 1.2.3
Authors@R:
person("John", "Benninghoff", , "jbenninghoff@mac.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-6230-4742"))
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rtraining 1.2.3

* Added Constraints vs Performance: Visualizations exploring the use of constraints vs performance improvements in risk management.

# rtraining 1.2.2

* Updated for R 4.4.0
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ Notebooks in this package:
inspired by a 2015 blog
[post](https://statmodeling.stat.columbia.edu/2015/07/09/hey-guess-what-there-really-is-a-hot-hand/)
on the hot hand.
- [Constraints vs
Performance](https://jabenninghoff.github.io/rtraining/analysis/constraints.html)
(2024-04-30): Visualizations exploring the use of constraints vs
performance improvements in risk management.
- [ggplot2 (Getting
started)](https://jabenninghoff.github.io/rtraining/analysis/ggplot2-1.html)
(2022-11-20): Workbook for completing quizzes and exercises from the
Expand Down
17 changes: 17 additions & 0 deletions _freeze/analysis/constraints/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"hash": "aba248afd6fb391afb3f2368db154646",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Constraints vs Performance\"\nauthor: \"John Benninghoff\"\ndate: '2024-04-30'\ndate-modified: '2024-04-30'\ncategories: notes\norder: 107\noutput:\n html_notebook:\n theme:\n version: 5\n preset: bootstrap\n css: assets/extra.css\n pandoc_args: --shift-heading-level-by=1\n toc: yes\n toc_float:\n collapsed: no\n smooth_scroll: no\n---\n\n\nVisualizations exploring the use of constraints vs performance improvements in risk management.\n\n# Questions/TODO\n\n- [ ] Questions/TODO list here\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggplot2)\nlibrary(jbplot)\nlibrary(fs)\n```\n:::\n\n\n# Normal Performance\n\nReplicate a version of Figure 9 from the\n[Safety-II White Paper](https://www.england.nhs.uk/signuptosafety/wp-content/uploads/sites/16/2015/10/safety-1-safety-2-whte-papr.pdf),\nwith help from <https://ggplot2tutor.com/tutorials/sampling_distributions>:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nxmin <- -5\nxmax <- 5\n\nsave_png <- function(filename) {\n ggsave(filename = path(\"rendered\", filename), width = 16 * 0.6, height = 9 * 0.6, bg = \"white\")\n}\n\nbackground <- ggplot(data.frame(x = c(xmin, xmax)), aes(x)) +\n scale_x_continuous(breaks = -3:3, minor_breaks = NULL) +\n labs(x = NULL, y = NULL) +\n theme_quo(minor.y = FALSE)\n\nbaseline <- stat_function(fun = dnorm, geom = \"line\")\nbad <- stat_function(fun = dnorm, geom = \"area\", fill = \"red\", xlim = c(xmin, -2))\n\nbackground + bad + baseline\n```\n\n::: {.cell-output-display}\n![](constraints_files/figure-html/background-1.png){width=816}\n:::\n\n```{.r .cell-code}\nsave_png(\"01-baseline-bad.png\")\n```\n:::\n\n\nThe plot above shows \"bad\" outcomes in red. Let's add in \"good\" outcomes (>1) in green:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ngood <- stat_function(fun = dnorm, geom = \"area\", fill = \"green\", xlim = c(1, xmax))\n\nbackground + bad + good + baseline\n```\n\n::: {.cell-output-display}\n![](constraints_files/figure-html/good-1.png){width=816}\n:::\n\n```{.r .cell-code}\nsave_png(\"02-baseline-bad-good.png\")\n```\n:::\n\n\n# Constrained Performance\n\nOne way of reducing \"bad\" outcomes is by constraining performance - reducing the standard\ndeviation.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nconstrained <- stat_function(fun = dnorm, args = list(sd = 0.7), geom = \"line\", color = \"blue\")\ntaller <- scale_y_continuous(limits = c(0, 0.6))\n\nbackground +\n stat_function(\n fun = dnorm, args = list(sd = 0.7), geom = \"area\", fill = \"red\", xlim = c(xmin, -2)\n ) +\n stat_function(\n fun = dnorm, args = list(sd = 0.7), geom = \"area\", fill = \"green\", xlim = c(1, xmax)\n ) +\n constrained +\n taller\n```\n\n::: {.cell-output-display}\n![](constraints_files/figure-html/constrained-1.png){width=816}\n:::\n\n```{.r .cell-code}\nsave_png(\"03-constrained.png\")\n```\n:::\n\n\nPlotting both on the same grid shows the reduction in both \"bad\" and \"good\" outcomes:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nbackground +\n bad +\n good +\n baseline +\n constrained +\n taller\n```\n\n::: {.cell-output-display}\n![](constraints_files/figure-html/overlay-1.png){width=816}\n:::\n\n```{.r .cell-code}\nsave_png(\"04-baseline-constrained.png\")\n```\n:::\n\n\n# Improved Performance\n\nAnother way of reducing bad outcomes is by improving performance - shifting the mean.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nperformance <- stat_function(fun = dnorm, args = list(mean = 1), geom = \"line\", color = \"blue\")\nimproved <- stat_function(\n fun = dnorm, args = list(mean = 1), geom = \"area\", fill = \"green\", xlim = c(1, xmax)\n)\n\nbackground +\n stat_function(\n fun = dnorm, args = list(mean = 1), geom = \"area\", fill = \"red\", xlim = c(xmin, -2)\n ) +\n improved +\n performance\n```\n\n::: {.cell-output-display}\n![](constraints_files/figure-html/performance-1.png){width=816}\n:::\n\n```{.r .cell-code}\nsave_png(\"05-improved.png\")\n```\n:::\n\n\nPlotting both together shows a reduction in \"bad\" and an increase in \"good\" outcomes:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nbackground +\n bad +\n improved +\n baseline +\n performance\n```\n\n::: {.cell-output-display}\n![](constraints_files/figure-html/overlay_perf-1.png){width=816}\n:::\n\n```{.r .cell-code}\nsave_png(\"06-baseline-improved.png\")\n```\n:::\n\n\nComparing all three:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nbackground +\n bad +\n improved +\n baseline +\n constrained +\n performance +\n taller\n```\n\n::: {.cell-output-display}\n![](constraints_files/figure-html/overlay_all-1.png){width=816}\n:::\n\n```{.r .cell-code}\nsave_png(\"07-baseline-constrained-improved.png\")\n```\n:::\n",
"supporting": [
"constraints_files"
],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
149 changes: 149 additions & 0 deletions analysis/constraints.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
title: "Constraints vs Performance"
author: "John Benninghoff"
date: '2024-04-30'
date-modified: '2024-04-30'
categories: notes
order: 107
output:
html_notebook:
theme:
version: 5
preset: bootstrap
css: assets/extra.css
pandoc_args: --shift-heading-level-by=1
toc: yes
toc_float:
collapsed: no
smooth_scroll: no
---

Visualizations exploring the use of constraints vs performance improvements in risk management.

# Questions/TODO

- [ ] Questions/TODO list here

```{r setup, message = FALSE, warning = FALSE}
library(ggplot2)
library(jbplot)
library(fs)
```

# Normal Performance

Replicate a version of Figure 9 from the
[Safety-II White Paper](https://www.england.nhs.uk/signuptosafety/wp-content/uploads/sites/16/2015/10/safety-1-safety-2-whte-papr.pdf),
with help from <https://ggplot2tutor.com/tutorials/sampling_distributions>:

```{r background}
xmin <- -5
xmax <- 5

save_png <- function(filename) {
ggsave(filename = path("rendered", filename), width = 16 * 0.6, height = 9 * 0.6, bg = "white")
}

background <- ggplot(data.frame(x = c(xmin, xmax)), aes(x)) +
scale_x_continuous(breaks = -3:3, minor_breaks = NULL) +
labs(x = NULL, y = NULL) +
theme_quo(minor.y = FALSE)

baseline <- stat_function(fun = dnorm, geom = "line")
bad <- stat_function(fun = dnorm, geom = "area", fill = "red", xlim = c(xmin, -2))

background + bad + baseline

save_png("01-baseline-bad.png")
```

The plot above shows "bad" outcomes in red. Let's add in "good" outcomes (>1) in green:

```{r good}
good <- stat_function(fun = dnorm, geom = "area", fill = "green", xlim = c(1, xmax))

background + bad + good + baseline

save_png("02-baseline-bad-good.png")
```

# Constrained Performance

One way of reducing "bad" outcomes is by constraining performance - reducing the standard
deviation.

```{r constrained}
constrained <- stat_function(fun = dnorm, args = list(sd = 0.7), geom = "line", color = "blue")
taller <- scale_y_continuous(limits = c(0, 0.6))

background +
stat_function(
fun = dnorm, args = list(sd = 0.7), geom = "area", fill = "red", xlim = c(xmin, -2)
) +
stat_function(
fun = dnorm, args = list(sd = 0.7), geom = "area", fill = "green", xlim = c(1, xmax)
) +
constrained +
taller

save_png("03-constrained.png")
```

Plotting both on the same grid shows the reduction in both "bad" and "good" outcomes:

```{r overlay}
background +
bad +
good +
baseline +
constrained +
taller

save_png("04-baseline-constrained.png")
```

# Improved Performance

Another way of reducing bad outcomes is by improving performance - shifting the mean.

```{r performance}
performance <- stat_function(fun = dnorm, args = list(mean = 1), geom = "line", color = "blue")
improved <- stat_function(
fun = dnorm, args = list(mean = 1), geom = "area", fill = "green", xlim = c(1, xmax)
)

background +
stat_function(
fun = dnorm, args = list(mean = 1), geom = "area", fill = "red", xlim = c(xmin, -2)
) +
improved +
performance

save_png("05-improved.png")
```

Plotting both together shows a reduction in "bad" and an increase in "good" outcomes:

```{r overlay_perf}
background +
bad +
improved +
baseline +
performance

save_png("06-baseline-improved.png")
```

Comparing all three:

```{r overlay_all}
background +
bad +
improved +
baseline +
constrained +
performance +
taller

save_png("07-baseline-constrained-improved.png")
```
6 changes: 6 additions & 0 deletions docs/LICENSE.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<a href="./analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down
15 changes: 14 additions & 1 deletion docs/NEWS.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<a href="./analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down Expand Up @@ -229,7 +235,8 @@
<h2 id="toc-title">On this page</h2>

<ul>
<li><a href="#rtraining-1.2.2" id="toc-rtraining-1.2.2" class="nav-link active" data-scroll-target="#rtraining-1.2.2">rtraining 1.2.2</a></li>
<li><a href="#rtraining-1.2.3" id="toc-rtraining-1.2.3" class="nav-link active" data-scroll-target="#rtraining-1.2.3">rtraining 1.2.3</a></li>
<li><a href="#rtraining-1.2.2" id="toc-rtraining-1.2.2" class="nav-link" data-scroll-target="#rtraining-1.2.2">rtraining 1.2.2</a></li>
<li><a href="#rtraining-1.2.1" id="toc-rtraining-1.2.1" class="nav-link" data-scroll-target="#rtraining-1.2.1">rtraining 1.2.1</a></li>
<li><a href="#rtraining-1.2.0" id="toc-rtraining-1.2.0" class="nav-link" data-scroll-target="#rtraining-1.2.0">rtraining 1.2.0</a></li>
<li><a href="#rtraining-1.1.10" id="toc-rtraining-1.1.10" class="nav-link" data-scroll-target="#rtraining-1.1.10">rtraining 1.1.10</a></li>
Expand Down Expand Up @@ -317,6 +324,12 @@ <h2 id="toc-title">On this page</h2>



<section id="rtraining-1.2.3" class="level1">
<h1>rtraining 1.2.3</h1>
<ul>
<li>Added Constraints vs Performance: Visualizations exploring the use of constraints vs performance improvements in risk management.</li>
</ul>
</section>
<section id="rtraining-1.2.2" class="level1">
<h1>rtraining 1.2.2</h1>
<ul>
Expand Down
6 changes: 6 additions & 0 deletions docs/TODO.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<a href="./analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down
6 changes: 6 additions & 0 deletions docs/analysis/FaultTree.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@
<a href="../analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down
12 changes: 9 additions & 3 deletions docs/analysis/advanced-r-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<script src="../site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="../">
<link href="../analysis/advanced-r-2.html" rel="next">
<link href="../analysis/hockey-cards.html" rel="prev">
<link href="../analysis/constraints.html" rel="prev">
<script src="../site_libs/quarto-html/quarto.js"></script>
<script src="../site_libs/quarto-html/popper.min.js"></script>
<script src="../site_libs/quarto-html/tippy.umd.min.js"></script>
Expand Down Expand Up @@ -202,6 +202,12 @@
<a href="../analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down Expand Up @@ -5628,8 +5634,8 @@ <h3 class="anchored" data-anchor-id="exercises-27">8.6.6 Exercises</h3>
</script>
<nav class="page-navigation">
<div class="nav-page nav-page-previous">
<a href="../analysis/hockey-cards.html" class="pagination-link" aria-label="Hockey Cards Analysis">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text">Hockey Cards Analysis</span>
<a href="../analysis/constraints.html" class="pagination-link" aria-label="Constraints vs Performance">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text">Constraints vs Performance</span>
</a>
</div>
<div class="nav-page nav-page-next">
Expand Down
6 changes: 6 additions & 0 deletions docs/analysis/advanced-r-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@
<a href="../analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down
6 changes: 6 additions & 0 deletions docs/analysis/advanced-r-3.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@
<a href="../analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down
6 changes: 6 additions & 0 deletions docs/analysis/advanced-r-4.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@
<a href="../analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down
6 changes: 6 additions & 0 deletions docs/analysis/cond-prob.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@
<a href="../analysis/hockey-cards.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Hockey Cards Analysis</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../analysis/constraints.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Constraints vs Performance</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
Expand Down
Loading