forked from timothyfraser/sigma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03b_workshop.Rmd
808 lines (590 loc) · 30.2 KB
/
03b_workshop.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# Probability Functions
```{r setup_workshop_3b, include=FALSE}
## Global options
knitr::opts_chunk$set(cache = FALSE, message = FALSE, warning = FALSE)
library(knitr)
library(kableExtra)
library(magick)
library(cowplot)
library(DiagrammeR)
# Let's also write a functions to lable figures and captions
# Courtesy of Stack Overflow
#https://stackoverflow.com/questions/37116632/r-markdown-html-number-figures
#Determine the output format of the document
output_format = opts_knit$get("rmarkdown.pandoc.to")
#Figure and Table Caption Numbering, for HTML do it manually
tabnum = 1; fignum = 1;
#Function to add the Table Number
tabcap = function(x){
if(output_format == 'html'){
x = paste0("Table ",tabnum,". ",x)
tabnum <<- tabnum + 1
}; x
}
#Function to add the Figure Number
figcap = function(x){
if(output_format == 'html'){
x = paste0("Figure ",fignum,". ",x)
fignum <<- fignum + 1
}; x
}
```
## Getting Started {-}
Please open up your Posit.Cloud project. Start a new R script (File >> New >> R Script). Save a new R script. And let's get started!
### Load Packages {-}
In this tutorial, we're going to use more of the `dplyr` and `ggplot2` packages, plus the `broom` package and `mosaicCalc` package.
```{r}
library(dplyr)
library(ggplot2)
library(broom)
library(mosaicCalc)
```
<br>
<br>
## Probability Functions
How do we use probability in our statistical analyses of risk, performance, and other systems engineering concepts?
Probability allows us to measure for any statistic (or parameter) `mu`, how **extreme** is that statistic? This is called *type II error*, measured by a `p`-value, the probability that a **more** extreme value occurred than our statistic. It's an extremely helpful benchmark. In order to evaluate how **extreme** it is, we need values to compare it to. We can do this by:
1. assuming the probability function of an unobserved, hypothetical distribution, or;
2. making a probability function curve of the observed distribution.
<br>
<br>
## Hypothetical Probability Functions
Often, our sample of data is just one of the many samples we could have possibly gotten. For example, say we are examining customer behavior at storefronts. Had we looked at a different firm location (by chance), or a different sample of customers come in by chance, we might have gotten slightly different distribution of purchase value made by these customers.
The problem is, we almost never can see the distribution of the **true** population of all observations (eg. all purchases). But, if we can approximately guess *what type of distribution that population has*, we can very easily compute the probability density functions and cumulative distribution functions of several of the most well known distributions in `R` (eg. Normal, Poisson, Gamma, etc.)
### Example: Farmers Market {-}
The Ithaca Farmers Market is a vendor-owned cooperative that runs a massive Saturday-and-Sunday morning market for local produce, street food, and hand-made goods, on the waterfront of Cayuga Lake. In markets and street fairs, some stalls' brands are often better known than others, so businesses new to the market might worry that people won't come to their stalls without specific prompting. This past Saturday, a volunteer tracked 500 customers and recorded how many stalls each customers visited during their stay. They calculated the following statistics.
- The average customer visited a `mean` of `5.5` stalls and a `median` of `5` stalls, with a standard deviation of `2.5` stalls.
```{r, echo = FALSE, out.width = "100%", fig.height=0.5, fig.cap="[Ithaca Farmers Market!](https://ithacamarket.com/markets/saturday-at-the-pavilion/)"}
knitr::include_graphics("https://i0.wp.com/ithacamarket.com/wp-content/uploads/2019/04/IMG_3407-900px.jpg?w=900&ssl=1", dpi = 100)
```
Market operators wants to know:
1. What's the probability that customers will stop by *5 stalls*?
2. What's the probability that customers will stop by *at max 5 stalls*?
3. What's the probability that customers will stop by *over 5 stalls*?
4. How many visits did people *usually* make? Estimate the interquartile range (25th-75th percentiles).
Unfortunately, the wind blew the raw data away into Cayuga Lake before they could finish their analysis. How can we approximate the unobserved distribution of visits and compute these probabilities?
Below, we will (1) use these statistics to guess which of several possible archetypal hypothetical distributions it most resembles, and then (2) compute probabilities based off of the shape of that hypothetical distribution.
<br>
<br>
### Unobserved Distributions {-}
We don't have the actual data, but we know several basic features of our distribution!
- Our variable is `visits`, a count ranging from 0 to infinity. (Can't have -5 visits, can't have 1.3 visits.)
- The median (`5`) is less than the mean (`5.5`), so our distribution is **right-skewed**.
This sounds like a classic Poisson distribution! Let's simulate some poisson-distributed data to demonstrate.
```{r}
# Randomly sample 500 visits from a poisson distribution with a mean of 5.5
visits <- rpois(n = 500, lambda = 5.5)
# Check out the distribution!
visits %>% hist()
```
<br>
<br>
### Using Hypothetical Probability Functions {-}
Much like `rpois()` randomly generates poisson distributed values, `dpois()`, `ppois()`, and `qpois()` can help you get other quantities of interest from the Poisson distribution.
- `dpois()` generates the density of any value on a poisson distribution centered on a given mean (PDF).
- `ppois()` returns for any percentile in the distribution the cumulative probability (percentage of the area under the density curve) up until that point (CDF).
- `qpois()` returns for any percentile in the distribution the raw value.
See the Table below for several examples.
```{r, echo = FALSE}
tibble(
`Meaning` = c("Random Draws from Distribution", "Probability Density Function", "Cumulative Distribution Function", "Quantiles Function"),
`Purpose` = c("Simulate a distribution", "Get Probability of Value in Distribution", "Get % of Distribution LESS than Value", "Get Value of any Percentile in Distribution"),
`Main Input` = c("n = # of simulations", "x = value in distribution", "q = a cumulative probability", "p = percentile"),
`Normal` = c("rnorm()", "dnorm()", "pnorm()", "qnorm()"),
`Poisson` = c("rpois()", "dpois()", "ppois()", "qpois()"),
`Gamma` = c("rgamma()", "dgamma()", "pgamma()", "qgamma()"),
`Exponential` = c("rexp()", "dexp()", "pexp()", "qexp()")
) %>%
kable(caption = "Table 1: Probability Functions (r, d, p, and q)",
booktabs = TRUE) %>%
kableExtra::kable_styling("striped")
```
<br>
<br>
### Density (PDF)
So, what percentage of customers stopped by 1 stall?
Below, `dpois()` tells us the `density()` or frequency of your value, given a distribution where the `mean = 5.5`.
```{r}
# Get the frequency for 5 visits in the distribution
pd5 <- dpois(5, lambda = 5.5)
# Check it!
pd5
```
Looks like `r round(pd5*100, 1)`% of customers stopped by 5 stalls.
We can validate this using our simulated `visits` from above. We can calculate the `density()` function, extract it using `approxfun()`, and then assign it to `dsim()`, our own exact probability density function for our data. It works just like `dpois()`, but you don't need to specify `lambda`, because it only works for this exact distribution!
```{r}
# Approximate the PDF of our simulated visits
dsim <- visits %>% density() %>% approxfun()
# Try our density function for our simulated data!
dsim(5)
# Pretty close to our results from dpois()!
```
<br>
<br>
### Cumulative Probabilities (CDF)
What percentage of customers stopped by *at max 5 stalls?*
```{r}
# Get the cumulative frequency for a value (5) in the distribution
cd5 <- ppois(q = 5, lambda = 5.5)
# Check it!
cd5
```
Looks like just `r round(cd5*100, 1)`% of customers stopped by 1 stall or fewer.
What percentage of customers stopped by *over 5 stalls?*
```{r}
# Get the probability they will NOT stop at 5 or fewer stalls
1 - cd5
```
We can validate our results against our simulated distribution.
```{r}
psim <- visits %>% density() %>% tidy() %>%
# Get cumulative probability distribution
arrange(x) %>%
# Get cumulative probabilities
mutate(y = cumsum(y) / sum(y)) %>%
# Turn it into a literal function!
approxfun()
# Check it!
psim(5)
# Pretty close to cdf5!
```
<br>
<br>
### Quantiles
How many visits did people *usually* make? Estimate the interquartile range (25th-75th percentiles) of the unobserved distribution.
```{r}
q5 <- qpois(p = c(.25, .75), lambda = 5.5)
# Check it!
q5
```
Looks like 50% of folks visited between `r q5[1]` and `r q5[2]` stalls
We can compare against our simulated data using `quantile()`.
```{r}
# Approximate the quantile function of this distribution
qsim <- tibble(
# Get a vector of percentiles from 0 to 1, in units of 0.001
x = seq(0, 1, by = 0.001),
# Using our simulated distribution,
# get the quantiles (values) at those points from this distribution
y = visits %>% quantile(probs = x)) %>%
# Approximate function!
approxfun()
# Check it!
qsim(c(.25, .75))
```
<br>
<br>
```{r}
rm(visits, pd5, cd5, q5, dsim, psim, qsim)
```
<br>
<br>
---
## Learning Check 1 {.unnumbered .LC}
**Question**
What if we are not certain whether our unobserved vector of visits has a Poisson distribution or not? To give you more practice, please calculate the probability that customers will stop *at more than 5 stalls*, using appropriate functions for the (1) Normal, (2) Gamma, and (3) Exponential distribution! (See our table above for the list of function names.)
<details><summary>**[View Answer!]**</summary>
We know there were `n = 500` customers, with a mean of `5.5` visits, a median of `5` visits, and a standard deviation of `2.5` visits.
For a Normal Distribution:
We learned in Workshop 2 that `rnorm()` requires a `mean` and `sd` (standard deviation); we conveniently have both!
```{r}
1 - pnorm(5, mean = 5.5, sd = 2.5)
```
For a Gamma Distribution:
We learned in Workshop 2 that `rgamma()` requires a `shape` and `scale` (or `rate`); we can calculate these from the `mean` and `sd` (standard deviation).
```{r}
# shape = mean^2 / variance = mean^2 / sd^2
shape <- 5.5^2 / 2.5^2
# scale = variance / mean
scale <- 2.5^2 / 5.5
# AND
# rate = 1 / scale
rate <- 1 / scale
# So...
# Get 1 - cumulative probability up to 5
1 - pgamma(5, shape = shape, scale = scale)
# OR (same)
1 - pgamma(5, shape = shape, rate = rate)
```
For an Exponential Distribution:
We learned in Workshop 2 that `rexp()` requires a `rate`; we can calculate this from the `mean`.
```{r}
# For exponential distribution,
# rate = 1 / mean
rate <- 1 / 5.5
# So...
# Get 1 - cumulative probability up to 5
1 - pexp(5, rate = rate)
```
</details>
---
## Observed Probability Functions
### Example: Observed Distributions
```{r img_er, echo = FALSE, fig.cap = figcap("Your Local ER"), fig.height=0.5, out.width="100%"}
knitr::include_graphics("https://media.glamour.com/photos/5abd5bbd73c84932389610fd/master/pass/TCDEERR_EC008.jpg", dpi = 100)
```
For example, a local hospital wants to make their health care services more affordable, given the surge in inflation.
- They measured `n = 15` patients who stayed 1 night over the last 7 days, how much were they charged (before insurance)? Let's call this vector `obs` (for 'observed data').
- A 16th patient received a bill of `$3000` (above the national mean of ~`$2500`). We'll record this as `stat` below.
```{r}
# Let's record our vector of 15 patients
obs <- c(1126, 1493, 1528, 1713, 1912, 2060, 2541, 2612, 2888, 2915, 3166, 3552, 3692, 3695, 4248)
# And let's get our new patient data point to compare against
stat <- 3000
```
Here, we know the full observed distribution of values (`cost`), so we can directly compute the `p_value` from them, using the logical operator `>=`.
```{r}
# Which values of in vector obs were greater than or equal to stat?
obs >= stat
```
`R` interprets `TRUE == 1` & `FALSE == 0`, so we can take the `mean()` to get the percentage of values in `obs` greater than or equal to `stat`.
```{r}
# Get p-value, the probability of getting a value greater than or equal to stat
mean(obs >= stat)
# This means Total Probability, where probability of each cost is 1/n
sum( (obs >= stat) / length(obs) )
```
Unfortunately, this only takes into account the exact values we observed (eg. \$1493), but it can't tell us anything about values we *didn't* observe (eg. \$1500). But logically, we know that the probability of getting a bill of $1500 should be pretty similar to a bill of \$1493. So how do we fill in the gaps?
<br>
<br>
### Observed PDFs (Probability Density Functions)
Above, we calculated the probability of getting a **more** extreme hospital bill based on a limited sample of points, but for more *precise* probabilities, we need to fill in the gaps between our observed data points.
- For a vector `x`, the probability density function is a **curve** providing the probability (`y`) of each value across the range of `x`.
- It shows the **relative frequency** (probability) of each *possible* value in the range.
<br>
### `density()`
We can ask R to estimate the probability density function for any observed vector using `density()`. This returns the density (`y`) of a bunch of hypothetical values (`x`) matching our distribution's curve. We can access those results using the `broom` package, by `tidy()`-ing it into a data.frame.
```{r}
obs %>% density() %>% tidy() %>% tail(3)
```
But that's *data*, not a *function*, right? Functions are *equations*, machines you can pump an input into to get a specific output. Given a data.frame of 2 vectors, `R` can actually approximate the `function` (equation) connecting vector 1 to vector 2 using `approxfun()`, *creating your own function!* So cool!
```{r}
# Let's make dobs(), the probability density function for our observed data.
dobs <- obs %>% density() %>% tidy() %>% approxfun()
# Now let's get a sequence (seq()) of costs from 1000 to 3000, in units of 1000....
seq(1000, 3000, by = 1000)
# and let's feed it a range of data to get the frequencies at those costs!
seq(1000, 3000, by = 1000) %>% dobs()
```
For now, let's get the densities for costs ranging from the min to the max observed cost.
```{r}
mypd <- tibble(
# Get sequence from min to max, in units of $10
cost = seq(min(obs), max(obs), by = 10),
# Get probability densities
prob_cost_i = dobs(cost)) %>%
# Classify each row as TRUE (1) if cost greater than or equal to stat, or FALSE (0) if not.
# This is the probability that each row is extreme (1 or 0)
mutate(prob_extreme_i = cost >= stat)
# Check it out!
mypd %>% head(3)
```
- We'll save it to `mypd`, naming the x-axis `cost` and the y-axis `prob_cost_i`, to show the probability of each `cost` in row `i` (eg. \$1126, \$1136, \$1146, ... `n`).
- We'll also calculate `prob_extreme_i`, the probability that each *ith* `cost` is extreme (greater than or equal to our 16th patient's bill). Either it *is* extreme (`TRUE == 100% = 1`) or it *isn't* (`FALSE == 0% == 0`).
Our density function `dobs()` estimated `prob_cost_i` (`y`), the probability/relative frequency of `cost` (`x`) occurring, where `x` represents every possible value of `cost`.
- We can visualize `mypd` using `geom_area()` or `geom_line()` in `ggplot2`!
- We can add `geom_vline()` to draw a vertical line at the location of `stat` on the `xintercept`.
```{r plot_pdf, fig.cap=figcap("Visualizing a Probability Density Function!"), out.width="100%", fig.height = 5}
mypd %>%
ggplot(mapping = aes(x = cost, y = prob_cost_i, fill = prob_extreme_i)) +
# Fill in the area from 0 to y along x
geom_area() +
# Or just draw curve with line
geom_line() +
# add vertical line
geom_vline(xintercept = stat, color = "red", size = 3) +
# Add theme and labels
theme_classic() +
labs(x = "Range of Patient Costs (n = 15)",
y = "Probability",
subtitle = "Probability Density Function of Hospital Stay Costs") +
# And let's add a quick label
annotate("text", x = 3500, y = 1.5e-4, label = "(%) Area\nunder\ncurve??", size = 5)
```
<br>
<br>
### Using PDFs (Probability Density Functions)
Great! We can *view* the probability density function now above. But how do we translate that into **a single probability** that measures how extreme Patient 16's bill is?
- We have the probability `prob_cost_i` at points `cost` estimated by the probability density function saved in `mypd`.
- We can calculate the total probability or `p_value` that a value of `cost` will be greater than our statistic `stat`, using our total probability formula. We can even restate it, so that it looks a little more like the weighted average it truly is.
$$ P_{\ Extreme} = \sum_{i = 1}^{n}{ P (Cost | Extreme_{\ i}) \times P (Cost_{\ i}) } = \frac{ \sum_{i = 1}^{n}{ P (Cost_{i}) \times P(Extreme)_{\ i} } }{ \sum_{i = 1}^{n}{ P(Cost_{i}) } } $$
```{r}
p <- mypd %>%
# Calculate the conditional probability of each cost occurring given that condition
mutate(prob_cost_extreme_i = prob_cost_i * prob_extreme_i) %>%
# Next, let's summarize these probabilities
summarize(
# Add up all probabilities of each cost given its condition in row i
prob_cost_extreme = sum(prob_cost_extreme_i),
# Add up all probabilities of each cost in any row i
prob_cost = sum(prob_cost_i),
# Calculate the weighted average, or total probability of getting an extreme cost
# by dividing these two sums!
prob_extreme = prob_cost_extreme / prob_cost)
# Check it out!
p
```
Very cool! Visually, what's happening here?
```{r plot_pdf_area, fig.cap=figcap("PDF with Area Under Curve!"), out.width="100%", fig.height = 5}
ggplot() +
geom_area(data = mypd, mapping = aes(x = cost, y = prob_cost_i, fill = prob_extreme_i)) +
geom_vline(xintercept = stat, color = "red", size = 3) +
theme_classic() +
labs(x = "Range of Patient Costs (n = 15)",
y = "Probability",
subtitle = "Probability Density Function of Hospital Stay Costs") +
annotate("text", x = 3500, y = 1.5e-4,
label = paste("P(Extreme)", "\n", " = ", p$prob_extreme %>% round(2), sep = ""),
size = 5)
```
<br>
<br>
### Observed CDFs (Cumulative Distribution Functions)
Alternatively, we can calculate that `p`-value for `prob_extreme` a different way, by looking at the **cumulative probability**.
- To add a values/probabilities in a vector together sequentially, we can use `cumsum()` (short for cumulative sum). For example:
```{r}
# Normally
c(1:5)
# Cumulatively summed
c(1:5) %>% cumsum()
# Same as
c(1, 2+1, 3+2+1, 4+3+2+1, 5+4+3+2+1)
```
Every **probability density function (PDF)** can *also* be represented as a **cumulative distribution function (CDF)**. Here, we calculate the *cumulative* total probability of receiving *each* `cost`, applying `cumsum()` to the probability (`prob_cost`) of each value (`cost`). In this case, we're basically saying, we're interested in *all* the `costs`, so don't discount any.
$$ P_{\ Extreme} = \sum_{i = 1}^{n}{ P (Cost | Extreme_{\ i} = 1) \times P (Cost_{\ i}) } = \frac{ \sum_{i = 1}^{n}{ P (Cost_{i}) \times 1 } }{ \sum_{i = 1}^{n}{ P(Cost_{i}) } } $$
```{r}
mypd %>%
# For instance, we can do the first step here,
# taking the cumulative probability of costs i through j....
mutate(prob_cost_cumulative = cumsum(prob_cost_i)) %>%
head(3)
```
Our `prob_cost_cumulative` in row 3 above shows the total probability of `n = 3` patients receiving a cost of `r mypd$cost[1]` OR `r mypd$cost[2]` OR `r mypd$cost[3]`. But, we want an *average* estimate for 1 patient. So, like in a weighted average, we can divide by the total probability of *all* (`n`) hypothetical patients in the probability density function receiving any of these costs. This gives us our *revised* `prob_cost_cumulative`, which ranges from `0` to `1`!
```{r}
mycd <- mypd %>%
# For instance, we can do the first step here,
# taking the cumulative probability of costs i through j....
mutate(prob_cost_cumulative = cumsum(prob_cost_i) / sum(prob_cost_i)) %>%
# We can also then identify the segment that is extreme!
mutate(prob_extreme = prob_cost_cumulative * prob_extreme_i)
# Take a peek at the tail!
mycd %>% tail(3)
```
Let's visualize `mycd`, our cumulative probabilities!
```{r}
viz_cd <- ggplot() +
# Show the cumulative probability of each cost,
# shaded by whether it is "extreme" (cost >= stat) or not
geom_area(data = mycd, mapping = aes(x = cost, y = prob_cost_cumulative, fill = prob_extreme_i)) +
# Show cumulative probability of getting an extreme cost
geom_area(data = mycd, mapping = aes(x = cost, y = prob_extreme, fill = prob_extreme_i)) +
# Show the 16th patient's cost
geom_vline(xintercept = stat, color = "red", size = 3) +
# Add formatting
theme_classic() +
labs(x = "Cost of Hospital Stays (n = 15)", y = "Cumulative Probability of Cost",
fill = "P(Extreme i)",
title = "Cumulative Distribution Function for Cost of Hospital Stays",
subtitle = "Probability of Cost more Extreme than $3000 = 0.36")
```
```{r, eval = FALSE}
# View it!
viz_cd
# (Note: I've added some more annotation to mine
# than your image will have - don't worry!)
```
```{r, echo = FALSE, plot_cdf, fig.cap=figcap("Visualizing a Cumulative Distribution Function!"), out.width="100%", fig.height = 5}
p_value <- filter(mycd, prob_extreme_i == TRUE)[1,]$prob_extreme
viz_cd +
# Add some extra data for interpretation
annotate("point", x = 3000, y = p_value, shape = 21, color = "white", fill = "black", size = 5) +
annotate("text", x = 3200, y = p_value - 0.05, label = round(p_value, 2), size = 5) +
annotate("linerange", x = 3000, ymin = p_value, ymax = 1, linetype = "dashed") +
annotate("point", x = 3000, y = 1, shape = 15, size = 5) +
annotate("text", x = 3200, y = 0.85, label = round(1 - p_value, 2), size = 5) +
annotate("linerange", xmin = 3000, xmax = max(obs), y = p_value, linetype = "dashed") +
annotate("curve", x = 3300, y = 0.82, xend = 3800, yend = 0.75,
curvature = +0.50, arrow = arrow(ends = "last", type = "closed", length = unit(0.3, "cm"))) +
annotate("text", x = 3800, y = 0.78, label = "P(Extreme)")
```
But wouldn't it be handy if we could just make a literal cumulative distribution `function`, just like we did for the probability density `function` `dobs()`?
```{r}
pobs <- obs %>% density() %>% tidy() %>%
# Sort from smallest to largest
arrange(x) %>%
# take cumulative sum, divided by total probability
mutate(y = cumsum(y) / sum(y)) %>%
# Make cumulative distribution function pobs()!
approxfun()
# We'll test it out!
1 - pobs(3000)
# Pretty close to our probability we calculated before!
```
```{r}
# Clear unnecessary data.
remove(stat, mycd, p, viz_cd)
```
### Using Calculus!
Above we took a **computational-approach** to the CDF, using `R` to number-crunch the CDF. To summarize:
1. We took a vector of empirical data `obs`,
2. We estimated the probability density function (PDF) using `density()`
3. We calculated the cumulative probability distribution ourselves
4. We connected-the-dots of our CDF into a function with `approxfun()`.
We did this because we started with empirical data, where *where the density function is unknown!*
But *sometimes*, we do know the density function, perhaps because systems engineers have modeled it for decades! In these cases, we could alternatively use calculus in `R` to obtain the CDF and make probabilistic assessments. Here's how!
```{r, eval = FALSE, include = FALSE}
# This polynomial does a solid job, predicting ~89% of the variation in the PDF
# Let's use this for an example.
dobs <- obs %>% density() %>% tidy()
m <- dobs %>%
lm(formula = y ~ poly(x, 2, raw = TRUE) )
# See! Great fit!
m %>% broom::glance()
tibble(
x = dobs$x,
y = dobs$y,
yhat = m$model$y,
yhat2 = dobs$x %>% pdf()
) %>%
ggplot(mapping = aes(x = x)) +
geom_line(mapping = aes(y = y, color = "Observed")) +
#geom_line(mapping = aes(y = yhat, color = "Polynomial")) +
geom_line(mapping = aes(y = yhat2, color = "Approx Polynomial"))
```
For example, this equation does a pretty okay job of approximating the shape of our distribution in `obs`.
$$ f(x) = \frac{-2}{10^7} + \frac{25x}{10^8} - \frac{45x^2}{10^{12}} $$
We can write that up in a `function`, which we will call `pdf`. For every `x` value we supply, it will compute that equation to predict that value's relative refequency/probability.
```{r}
# Write out our nice polynomial function
pdf = function(x){
-2/10^7 + 25/10^8*x + -45/10^12*x^2
}
# Check it!
c(2000, 3000) %>% pdf()
```
The figure below demonstrates that it approximates the true density relatively closely.
```{r}
# We're going to add another column to our mypd dataset,
mypd <- mypd %>%
# approximating the probability of each cost with our new pdf()
mutate(prob_cost_i2 = pdf(cost))
ggplot() +
geom_line(data = mypd, mapping = aes(x = cost, y = prob_cost_i, color = "from raw data")) +
geom_line(data = mypd, mapping = aes(x = cost, y = prob_cost_i2, color = "from function")) +
theme_classic() +
labs(x = "Cost", y = "Probability", color = "Type")
```
So how do we generate the cumulative density function? The `mosaicCalc` package can help us with its functions `D()` and `antiD()`.
- `D()` computes the derivative of a function (Eg. CDF -> PDF)
- `antiD()` computes its integral (Eg. PDF -> CDF)
```{r}
# Compute the anti-derivative (integral) of the function pdf(x), solving for x.
cdf <- antiD(tilde = pdf(x) ~ x)
# It works just the same as our other functions
obs %>% head() %>% cdf()
# (Note: Our function is not a perfect fit for the data, so probabilities exceed 1!)
# Let's compare our cdf() function made with calculus with pobs(), our computationally-generated CDF function.
obs %>% head() %>% pobs()
# Pretty similar results. The differences are due the fact that our original function is just an approximation, rather than dobs(), which is a perfect fit for our densities.
```
And we can also take the derivative of our `cdf()` function with `D()` to get back our `pdf()`, which we'll call `pdf2()`.
```{r}
pdf2 <- D(tilde = cdf(x) ~ x)
# Let's compare results....
# Our original pdf...
obs %>% head() %>% pdf()
# Our pdf dervied from cdf...
obs %>% head() %>% pdf2()
# They're the same!
```
Tada! You can do calculus in `R`!
```{r}
remove(mypd, pdf, pdf2, cdf, obs)
```
<br>
<br>
---
## Learning Check 2 {.unnumbered .LC}
**Question**
A month has gone by, and our hospital has now billed 30 patients. You've heard that hospital bills at or above `$3000` a day may *somewhat* deter people from seeking future medical care, while bills at or above `$4000` may *greatly* deter people from seeking future care. (These numbers are hypothetical.)
Using the vectors below, please calculate the following, using a PDF or CDF.
- What's the probability that a bill might *somewhat* deter a patient from going to the hospital?
- What's the probability that a bill might *greatly* deter a patient from going to the hospital?
- What's the probability that a patient might be *somewhat* deterred but **not** *greatly* deterred from going to the hospital?
- Note: Assume that the PDF matches the range of observed patients.
```{r}
# Let's record our vector of 30 patients
patients <- c(1126, 1493, 1528, 1713, 1912, 2060, 2541, 2612, 2888, 2915, 3166, 3552, 3692, 3695, 4248,
3000, 3104, 3071, 2953, 2934, 2976, 2902, 2998, 3040, 3030, 3021, 3028, 2952, 3013, 3047)
# And let's get our statistics to compare against
somewhat <- 3000
greatly <- 4000
```
<details><summary>**[View Answer!]**</summary>
```{r}
# Get the probability density function for our new data
dobs2 <- patients %>% density() %>% tidy() %>% approxfun()
# Get the probability densities
mypd2 <- tibble(
cost = seq(min(patients), max(patients), by = 10),
prob_cost_i = cost %>% dobs2()) %>%
# Calculate probability of being somewhat deterred
mutate(
prob_somewhat_i = cost >= somewhat,
prob_greatly_i = cost >= greatly,
prob_somewhat_not_greatly_i = cost >= somewhat & cost < greatly)
```
To calculate these probabilities straight from the probability densities, do like so:
```{r}
mypd2 %>%
summarize(
# Calculate total probability of a cost somewhat deterring medical care
prob_somewhat = sum(prob_cost_i * prob_somewhat_i) / sum(prob_cost_i),
# Calculate total probability of a cost greatly deterring medical care
prob_greatly = sum(prob_cost_i * prob_greatly_i) / sum(prob_cost_i),
# Calculate total probability of a cost somewhat-but-not-greatly deterring medical care
prob_somewhat_not_greatly = sum(prob_cost_i * prob_somewhat_not_greatly_i) / sum(prob_cost_i))
```
To calculate these probabilities from the cumulative distribution functions, we can do the following:
```{r}
# Get cumulative probabilities of each
mycd2 <- mypd2 %>%
mutate(
# Calculate total probability of a cost somewhat deterring medical care
prob_somewhat = cumsum(prob_cost_i) * prob_somewhat_i / sum(prob_cost_i),
# Calculate total probability of a cost greatly deterring medical care
prob_greatly = cumsum(prob_cost_i) * prob_greatly_i / sum(prob_cost_i),
# Calculate total probability of a cost somewhat-but-not-greatly deterring medical care
prob_somewhat_not_greatly = cumsum(prob_cost_i) * prob_somewhat_not_greatly_i / sum(prob_cost_i))
# Check it!
mycd2 %>% tail(3)
```
Finally, if you want to visualize them, here's how you would do it!
```{r}
ggplot() +
# Get cumulative probability generally
geom_area(data = mycd2, mapping = aes(x = cost, y = cumsum(prob_cost_i) / sum(prob_cost_i),
fill = "Very Little")) +
# Get cumulative probability if somewhat but not greatly
geom_area(data = mycd2, mapping = aes(x = cost, y = prob_somewhat_not_greatly,
fill = "Somewhat-not-Greatly")) +
# Get cumulative probability if greatly
geom_area(data = mycd2, mapping = aes(x = cost, y = prob_greatly,
fill = "Greatly")) +
theme_classic() +
labs(x = "Cost of Hospital Stay (n = 30)",
y = "Cumulative Probability",
subtitle = "Probability of Hospital Bill Deterring Future Hospital Visits",
fill = "Deter Future Visits")
```
</details>
---
<br>
<br>
<br>
<br>
## Conclusion {-}
And that's a wrap! Nice work! You can now figure out *a lot* of things about the world if you (a) can guess their distribution and (b) have one or two statistics about that distribution. Here we go!
```{r, include = FALSE}
rm(list = ls())
```