-
Notifications
You must be signed in to change notification settings - Fork 1
/
left_handed_FishersExactTest.Rmd
95 lines (68 loc) · 1.7 KB
/
left_handed_FishersExactTest.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
# 女性左撇子比男性左撇子多 {#lefthanded}
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(tidybayes)
library(bayesplot)
library(rstan)
library(loo)
library(broom.mixed)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
theme_set(bayesplot::theme_default())
```
```{r}
tb <- tibble::tribble(
~sex, ~left_handed, ~right_handed,
"male", 9L, 43L,
"female", 4L, 44L
)
```
```{r}
steps <- seq(from = 0, to = 1, by = .01)
ggplot(data.frame(x = steps, y = dbeta(steps, shape1 = 1, shape2 = 1))) +
geom_line(aes(x, y)) +
scale_x_continuous(breaks = (0:10) / 10) +
ylim(0, 2) +
labs(x = "p(left-handed)", y = "density", title = "beta(1,1)")
ggplot(data.frame(x = steps, y = dbeta(steps, shape1 = 5, shape2 = 40))) +
geom_line(aes(x, y)) +
scale_x_continuous(breaks = (0:10) / 10) +
labs(x = "p(left-handed)", y = "density", title = "beta(5,40)")
```
```{r}
stan_program <- "
data {
int<lower=1> event_1;
int<lower=1> event_2;
int<lower=1> n_1;
int<lower=1> n_2;
}
parameters {
real<lower=0,upper=1> p_1;
real<lower=0,upper=1> p_2;
}
model {
event_1 ~ binomial(n_1, p_1);
event_2 ~ binomial(n_2, p_2);
p_1 ~ beta(5, 40);
p_2 ~ beta(5, 40);
}
generated quantities {
real diff = p_1 - p_2;
}
"
stan_data <- list(
n_1 = 52, # men
event_1 = 9 , # left-handed men
n_2 = 48, # women
event_2 = 4 # left-handed women
)
stan_eq <- stan(model_code = stan_program, data = stan_data)
```
```{r}
stan_eq %>%
tidybayes::spread_draws(diff) %>%
ggplot(aes(x = diff, y = 1)) +
geom_halfeyeh() +
geom_vline(xintercept = 0)
```