# heart

reference:
- ISI, example 2.3, p.139

Calculate the $$p.value$$ as a function of the number of deaths.

library(tidyverse)

## hypotheses

$H_0 : \pi = 0.15$ $H_a : \pi > 0.15$

## simulation

Design an experiment: “success” means the patient died within 30 days of a heart transplant … “success” often stands for the event being studied, even if that doesn’t sound very “successful” … choose “success” with probabiity 0.15 … repeat 10 times

heart10 <- function(){
sample(0:1, size = 10, prob = c(0.85, 0.15), replace = TRUE)
}
heart10()
##  [1] 0 0 0 0 0 0 0 1 0 0

Modify the experiment: choose “success” with probabiity 0.15 … repeat 10 times … report the number of successes.

heart10 <- function(){
samp <- sample(0:1, size = 10, prob = c(0.85, 0.15), replace = TRUE)
n.successes <- sum(samp)
return(n.successes)
}
heart10()
## [1] 0

Repeat the experiment 10 times.

replicate(10, heart10())
##  [1] 2 0 2 2 3 2 0 1 1 2

## simulation results

Repeat the experiment 1,000,000 times and display the results.

results1 <- replicate(1e6, heart10())
df <- data.frame(k = results1)
df1 <- df %>%
group_by(k) %>%
summarize(n = n()) %>%
mutate(p = n / 1e6)
df1
## # A tibble: 9 x 3
##       k      n        p
##   <int>  <int>    <dbl>
## 1     0 196674 0.196674
## 2     1 346723 0.346723
## 3     2 276362 0.276362
## 4     3 130599 0.130599
## 5     4  39747 0.039747
## 6     5   8521 0.008521
## 7     6   1232 0.001232
## 8     7    134 0.000134
## 9     8      8 0.000008
a <- min(df1$k) b <- max(df1$k)
ggplot(df1, aes(k, n)) +
geom_bar(stat = "identity", color = "saddlebrown", fill = "wheat") +
scale_x_discrete(limits = a:b) +
labs(title = "Simulation")