A few days ago, I watched a terrific lecture by Bob Carpenter on Bayesian models. He started with a Bayesian approach to Fisher’s exact test. I had never heard of this classical procedure, so I was curious to play with the example. In this post, I use the same data that he used in the lecture and in an earlier, preStan blog post. I show how I would go about fitting the model in Stan and inspecting the results in R.
Problem statement
We observed the following data.
sex  n left handed  n right handed 

male  9  43 
female  4  44 
Question: Is the rate of lefthandedness different between the male and female groups? Specifically, is lefthandedness more likely in the male group?
Classical approach
In frequentist statistics, we might run Fisher’s exact test. At least, that’s what the flow charts tell us. To do that, we first put the data in a matrix.
# Create a matrix representation of the data for the fisher test
m < matrix(
c(9, 43, 4, 44),
nrow = 2,
byrow = TRUE,
dimnames = list(
sex = c("male", "female"),
handedness = c("left", "right"))
)
m
#> handedness
#> sex left right
#> male 9 43
#> female 4 44
We can run the twotailed test: Are the two groups different?
fisher.test(m)
#>
#> Fisher's Exact Test for Count Data
#>
#> data: m
#> pvalue = 0.2392
#> alternative hypothesis: true odds ratio is not equal to 1
#> 95 percent confidence interval:
#> 0.582996 10.927993
#> sample estimates:
#> odds ratio
#> 2.283832
The output is a little verbose, but I do like how it spells out a sentence describing the alternative hypothesis.
We can also consider onesided test: Is lefthandedness greater in the male group?
fisher.test(m, alternative = "greater")
#>
#> Fisher's Exact Test for Count Data
#>
#> data: m
#> pvalue = 0.1502
#> alternative hypothesis: true odds ratio is greater than 1
#> 95 percent confidence interval:
#> 0.7006563 Inf
#> sample estimates:
#> odds ratio
#> 2.283832
In both tests, we cannot reject the null hypothesis because the pvalue is greater than .05 ❌, so we would conclude that the two groups are not different.
But I don’t really know this test that well. We never covered it in any of my
stats classes, and indeed, this post is the first time I ever used the function
fisher.test()
. If I had never heard of the test, I am not quite sure what I
would have done. Maybe a logistic regression (p = .191 ❌).
Creating a Stan model
I’m very not fluent in the classical bag of tricks, but that’s okay. I know some Stan, I have an idea about how the data could have been generated, and that’s good enough :relaxed:. I can just write down my datagenerating story in a model and let Stan compute a posterior distribution for the difference in handedness rates between the two groups.
For my model, I’m going to suppose that in each group, there is a probability of being lefthanded called θ and that the counts we see result from a binomial process. The 9 lefthanded males we observe are the number of successes from 52 observations of a process that “succeeds” with probability θ_{male}.
To fit the model, we need a prior distribution. The prior’s job is to generate possible θ’s, and we will use our data to update the prior. The Beta distribution generates values between 0 and 1, so it’s an obvious choice.
In math, the model for a group would be:
\[\begin{align*} n_\text{lefthanded in group} &\sim \text{Binomial}(n_\text{total in group}, \theta_\text{group}) &\text{[likelihood]}\\ \theta_\text{group} &\sim \text{Beta}(a, b) &\text{[prior prob. of lefthandedness]}\\ a, b &: \text{shape terms for prior} \end{align*}\]We can use a flat, uninformative prior by using a = 1, b = 1. This prior considers all probabilities from 0 to 1 as equally plausible.
library(ggplot2)
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(lefthanded)", y = "density", title = "beta(1,1)")
But I also think that 10ish% of people are left handed. (I don’t know where I
first heard this number, but it’ll serve as my prior information.) I toyed
around with shape1
and shape2
parameters in dbeta()
until I got the prior
Beta(5, 40), which is peaked around .1ish but wide enough to keep .5 and .15 as
plausible values too.
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(lefthanded)", y = "density", title = "beta(5,40)")
Let’s write out a really simple model in Stan. Okay, it used to be really
simple. Then I made the parameters for the Beta prior data values, and then I
created an option to just sample the prior distribution. But the core of it is
simple. The most important lines are the one with ~
symbols. These
correspond to the sampling statements in the mathematical description of the
model.
model_code < "
data {
int<lower=0> beta_a;
int<lower=0> beta_b;
int<lower=0> n_total_1;
int<lower=0> n_total_2;
int<lower=0> n_hits_1;
int<lower=0> n_hits_2;
int<lower=0, upper=1> sample_prior_only;
}
parameters {
real<lower=0, upper=1> theta_1;
real<lower=0, upper=1> theta_2;
}
model {
theta_1 ~ beta(beta_a, beta_b);
theta_2 ~ beta(beta_a, beta_b);
if (sample_prior_only != 1) {
n_hits_1 ~ binomial(n_total_1, theta_1);
n_hits_2 ~ binomial(n_total_2, theta_2);
}
}
generated quantities {
real diff;
diff = theta_1  theta_2;
}
"
The generated quantities
block runs on every sample of the posterior
distribution. Here, we compute the
θ_{male} − θ_{female} on every
draw. Computing the difference inside the model code means that Stan
will treat the diff
values like any other parameter of the model. It will show
up in summary functions and in plots of model parameters. That saves us some
work later on.
library(rstan)
#> Loading required package: StanHeaders
#> rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
#> For execution on a local, multicore CPU with excess RAM we recommend calling
#> options(mc.cores = parallel::detectCores()).
#> To avoid recompilation of unchanged Stan programs, we recommend calling
#> rstan_options(auto_write = TRUE)
#> Do not specify 'march=native' in 'LOCAL_CPPFLAGS' or a Makevars file
#>
#> Attaching package: 'rstan'
#> The following object is masked from 'package:tidyr':
#>
#> extract
I begin by compiling the model. This step will create an executable program that can sample from the model. I do the compilation in its own step so that I can reuse the program for different versions of the model.
model_program < stan_model(model_code = model_code)
For convenience, I wrote a function that fits different versions of this model.
This step is not necessary, but I don’t like repeating myself. (Normally, you
would use sampling(model_program, stan_data)
to get samples from a
modelprogram.)
run_model < function(beta_a, beta_b, sample_prior_only) {
stan_data < list(
beta_a = beta_a,
beta_b = beta_b,
n_total_1 = 9 + 43,
n_total_2 = 4 + 44,
n_hits_1 = 9,
n_hits_2 = 4,
sample_prior_only = sample_prior_only)
# Use quietly() to hide the sampler's output text
model < purrr::quietly(sampling)(model_program, stan_data)
# But print any warnings that would have appeared
invisible(lapply(model$warnings, warning, call. = FALSE))
model$result
}
The actual lefthanded versus righthanded numbers are hardcoded, but I can
adjust the parameters for the Beta prior and toggle between sampling from the
prior and the posterior. Stan normally prints out verbose progress information
but I suppress that by using purrr::quietly()
. I still want warnings, so I
print them if they arise.
Checking our prior information
Now, let’s draw samples from the priors of each model. This step lets us check that our program works as expected. We know how the values should be distributed—we made up the numbers!
m_informative_pd < run_model(beta_a = 5, beta_b = 40, sample_prior_only = 1)
m_informative_pd
#> Inference for Stan model: 7e241131e72a0ec0b2cfd4f9a73290f1.
#> 4 chains, each with iter=2000; warmup=1000; thin=1;
#> postwarmup draws per chain=1000, total postwarmup draws=4000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> theta_1 0.11 0.00 0.05 0.04 0.08 0.11 0.14 0.22 3184 1
#> theta_2 0.11 0.00 0.05 0.04 0.08 0.11 0.14 0.22 2857 1
#> diff 0.00 0.00 0.07 0.13 0.04 0.00 0.04 0.13 3146 1
#> lp__ 32.41 0.02 1.00 35.20 32.79 32.11 31.70 31.42 1639 1
#>
#> Samples were drawn using NUTS(diag_e) at Tue Nov 16 12:35:39 2021.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
m_flat_pd < run_model(beta_a = 1, beta_b = 1, sample_prior_only = 1)
m_flat_pd
#> Inference for Stan model: 7e241131e72a0ec0b2cfd4f9a73290f1.
#> 4 chains, each with iter=2000; warmup=1000; thin=1;
#> postwarmup draws per chain=1000, total postwarmup draws=4000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> theta_1 0.5 0.00 0.29 0.03 0.24 0.50 0.75 0.98 3718 1
#> theta_2 0.5 0.00 0.29 0.03 0.26 0.50 0.75 0.98 3636 1
#> diff 0.0 0.01 0.41 0.78 0.30 0.01 0.30 0.77 3883 1
#> lp__ 4.0 0.03 1.20 7.11 4.50 3.64 3.13 2.80 1289 1
#>
#> Samples were drawn using NUTS(diag_e) at Tue Nov 16 12:35:39 2021.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
We can confirm (by inspecting the diff
row) that the difference in
lefthandedness in both groups is 0 according to our priors. The
informative prior says that the values between 0.04 and
0.22 are plausible rates of lefthandedness in each group.
It’s worth a moment to reflect on how obviously wrong the uninformative prior is. The central θ value in each group is .5. Therefore, half of the prior samples assert that there are more lefthanded individuals than righthanded ones! If there is anything we know about handedness, it’s that lefthandedness is less common than righthandedness. Uninformative sometimes connotes “unbiased” or “letting the data speak for itself”, but in this case, I would say “gullible”.
Sampling the posterior
Now, the fun part. We update our prior information with our data.
m_informative < run_model(beta_a = 5, beta_b = 40, sample_prior_only = 0)
m_informative
#> Inference for Stan model: 7e241131e72a0ec0b2cfd4f9a73290f1.
#> 4 chains, each with iter=2000; warmup=1000; thin=1;
#> postwarmup draws per chain=1000, total postwarmup draws=4000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> theta_1 0.14 0.00 0.04 0.08 0.12 0.14 0.17 0.22 3193 1
#> theta_2 0.10 0.00 0.03 0.04 0.08 0.09 0.12 0.16 2738 1
#> diff 0.05 0.00 0.05 0.04 0.02 0.05 0.08 0.14 3108 1
#> lp__ 70.63 0.02 1.04 73.50 71.02 70.33 69.89 69.63 1853 1
#>
#> Samples were drawn using NUTS(diag_e) at Tue Nov 16 12:35:40 2021.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
m_flat < run_model(beta_a = 1, beta_b = 1, sample_prior_only = 0)
m_flat
#> Inference for Stan model: 7e241131e72a0ec0b2cfd4f9a73290f1.
#> 4 chains, each with iter=2000; warmup=1000; thin=1;
#> postwarmup draws per chain=1000, total postwarmup draws=4000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> theta_1 0.18 0.00 0.05 0.09 0.15 0.18 0.22 0.30 3470 1
#> theta_2 0.10 0.00 0.04 0.04 0.07 0.10 0.13 0.19 3165 1
#> diff 0.08 0.00 0.07 0.04 0.04 0.08 0.13 0.22 3389 1
#> lp__ 43.14 0.03 1.04 45.95 43.51 42.81 42.41 42.16 1463 1
#>
#> Samples were drawn using NUTS(diag_e) at Tue Nov 16 12:35:40 2021.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
The flat model puts the difference at 0.08 and 90% of the plausible values fall in the interval [0.02, 0.2]. The informative model is more skeptical of higher lefthandedness rates, so it puts the difference at 0.05 with 90% of the values between [0.03, 0.12]. Both of these intervals contain 0 and negative values ❌, so there is not much evidence for higher lefthandedness in the male group.
To compute a “Bayesian pvalue”, we could ask what proportion of differences are 0 or negative. There are more proper ways to make this inference in a Bayesian framework, but this approach is the easiest and it works for a model this simple. If 10% of the plausible values for the group differences are negative, then we assign a 10% probability to a negative group difference.
df_flat < as.data.frame(m_flat)
mean(df_flat$diff <= 0)
#> [1] 0.1015
df_informative < as.data.frame(m_informative)
mean(df_informative$diff <= 0)
#> [1] 0.149
It’s also worth comparing the two models. I’ve recently become a fan of the
ggmcmc package for quick visualization
of Stan models. The package uses a function ggs()
to create a long dataframe
of MCMC samples. Then you plug those dataframes into various plotting functions
that start with ggs_
. I especially like how the package returns a plain
ggplot2 plot that I can easily adjust with a few extra lines of code.
For example, not much effort is required—after some practice and trialanderror, of course—to visualize the posterior samples in each model.
library(ggmcmc)
# A helper dataframe for relabeling parameters. I'm writing them in a way that
# works with ?plotmath conventions.
labels < data.frame(
Parameter = c("theta_1", "theta_2", "diff"),
Label = c("theta[male]", "theta[female]", "theta[male]  theta[female]")
)
# Get ggmcmc's tidy dataframe of each model.
# ggs() doesn't like that labels I made have brackets so I am suppressing its
# warnings.
ggs_flat < suppressWarnings(
ggs(m_flat, description = "flat", par_labels = labels)
)
ggs_informative < suppressWarnings(
ggs(m_informative, description = "informative", par_labels = labels)
)
ggs_density(ggs_flat) +
facet_grid(Parameter ~ ., labeller = label_parsed) +
ggtitle("flat prior: beta(1, 1)") +
theme_grey(base_size = 14) +
theme(legend.position = "bottom") +
# so the two models can be compared
xlim(.2, .5)
ggs_density(ggs_informative) +
facet_grid(Parameter ~ ., labeller = label_parsed) +
ggtitle("informative prior: beta(5, 40)") +
theme_grey(base_size = 14) +
theme(legend.position = "bottom") +
# so the two models can be compared
xlim(.2, .5)
We can also compare the models together in a single plot by passing a list of
model dataframes into ggs_caterpillar()
.
ggs_caterpillar(
D = list(ggs_flat, ggs_informative),
line = 0,
thick_ci = c(0.05, 0.95),
thin_ci = c(0.025, 0.975)
) +
# Parse the labels as formatted math
scale_y_discrete(
breaks = as.character(unique(ggs_flat$Parameter)),
labels = parse(text = as.character(unique(ggs_flat$Parameter)))
) +
labs(
caption = "Intervals: thick 90%, thin 95%. Point: median.",
y = NULL,
x = NULL
) +
theme_grey(base_size = 14)
There’s a lot of useful information here. First, the intervals in the flat prior model are wider than the ones for the informative model. The two models largely agree on the values in the female group, although the flat prior model is wider. These wider intervals indicate greater uncertainty about the parameter values. The two models disagree on the male group, because the informative model assigns little prior probability to values greater than .25 but the flat model doesn’t discount those possibilities.
The models also demonstrate the regularizing effect of prior information. Regularization broadly refers to techniques to avoid overfitting a dataset. Priors can regularize a model by making it skeptical of certain parameter values—in this case, high values of lefthandedness. The male probabilities in the flat model are basically pulled towards the values in the informative prior (.1ish). We can see this effect in how the midpoint is shifted in the flat model versus the informative model.
I would be remiss if I didn’t end with the following disclaimer/trivia. In a way, the question behind this post is illposed because handedness is not quite a binary measure. Some years ago, I had a class on stuttering and fluency disorders. (I used to be a speech pathologist.) There once was a lot of research on the association between handedness and stuttering, and at some point, researchers figured out that they could measure handedness as a continuous measure. They gave people a survey asking which hands they use for certain tasks and then computed a socalled dextrality quotient from the responses. I had the concept driven home when I once saw my wife casually switch between her left and right hands while brushing her teeth. I would probably poke a hole through my cheek if I used my left hand! So: Handedness may be a matter of degree. (Or maybe not. I mostly wanted to mention the dextrality quotient. It’s fun to think about.)
Last knitted on 20211116. Source code on GitHub.^{1}

sessioninfo::session_info() #>  Session info  #> hash: person golfing: mediumlight skin tone, left arrow, backhand index pointing left: mediumlight skin tone #> #> setting value #> version R version 4.1.2 (20211101) #> os Windows 10 x64 (build 22000) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz America/Chicago #> date 20211116 #> pandoc NA #> #>  Packages  #> ! package * version date (UTC) lib source #> assertthat 0.2.1 20190321 [1] CRAN (R 4.1.0) #> callr 3.7.0 20210420 [1] CRAN (R 4.1.0) #> cli 3.1.0 20211027 [1] CRAN (R 4.1.1) #> codetools 0.218 20201104 [2] CRAN (R 4.1.2) #> colorspace 2.02 20210624 [1] CRAN (R 4.1.0) #> crayon 1.4.2 20211029 [1] CRAN (R 4.1.1) #> curl 4.3.2 20210623 [1] CRAN (R 4.1.0) #> DBI 1.1.1 20210115 [1] CRAN (R 4.1.0) #> digest 0.6.28 20210923 [1] CRAN (R 4.1.1) #> dplyr * 1.0.7 20210618 [1] CRAN (R 4.1.0) #> ellipsis 0.3.2 20210429 [1] CRAN (R 4.1.0) #> emo 0.0.0.9000 20211014 [1] Github (hadley/emo@3f03b11) #> evaluate 0.14 20190528 [1] CRAN (R 4.1.0) #> fansi 0.5.0 20210525 [1] CRAN (R 4.1.0) #> farver 2.1.0 20210228 [1] CRAN (R 4.1.0) #> generics 0.1.1 20211025 [1] CRAN (R 4.1.1) #> GGally 2.1.2 20210621 [1] CRAN (R 4.1.1) #> ggmcmc * 1.5.1.1 20210210 [1] CRAN (R 4.1.1) #> ggplot2 * 3.3.5 20210625 [1] CRAN (R 4.1.0) #> git2r 0.28.0 20210110 [1] CRAN (R 4.1.1) #> glue 1.4.2 20200827 [1] CRAN (R 4.1.1) #> gridExtra 2.3 20170909 [1] CRAN (R 4.1.0) #> gtable 0.3.0 20190325 [1] CRAN (R 4.1.0) #> here 1.0.1 20201213 [1] CRAN (R 4.1.0) #> highr 0.9 20210416 [1] CRAN (R 4.1.0) #> inline 0.3.19 20210531 [1] CRAN (R 4.1.0) #> jsonlite 1.7.2 20201209 [1] CRAN (R 4.1.0) #> knitr * 1.36 20210929 [1] CRAN (R 4.1.1) #> labeling 0.4.2 20201020 [1] CRAN (R 4.1.0) #> lifecycle 1.0.1 20210924 [1] CRAN (R 4.1.1) #> loo 2.4.1 20201209 [1] CRAN (R 4.1.0) #> lubridate 1.8.0 20211007 [1] CRAN (R 4.1.1) #> magrittr 2.0.1 20201117 [1] CRAN (R 4.1.0) #> matrixStats 0.61.0 20210917 [1] CRAN (R 4.1.1) #> munsell 0.5.0 20180612 [1] CRAN (R 4.1.0) #> pillar 1.6.4 20211018 [1] CRAN (R 4.1.1) #> pkgbuild 1.2.0 20201215 [1] CRAN (R 4.1.0) #> pkgconfig 2.0.3 20190922 [1] CRAN (R 4.1.0) #> plyr 1.8.6 20200303 [1] CRAN (R 4.1.0) #> prettyunits 1.1.1 20200124 [1] CRAN (R 4.1.0) #> processx 3.5.2 20210430 [1] CRAN (R 4.1.0) #> ps 1.6.0 20210228 [1] CRAN (R 4.1.0) #> purrr 0.3.4 20200417 [1] CRAN (R 4.1.0) #> R6 2.5.1 20210819 [1] CRAN (R 4.1.1) #> ragg 1.2.0 20211030 [1] CRAN (R 4.1.1) #> RColorBrewer 1.12 20141207 [1] CRAN (R 4.1.0) #> Rcpp 1.0.7 20210707 [1] CRAN (R 4.1.0) #> D RcppParallel 5.1.4 20210504 [1] CRAN (R 4.1.0) #> reshape 0.8.8 20181023 [1] CRAN (R 4.1.1) #> rlang 0.4.12 20211018 [1] CRAN (R 4.1.1) #> rprojroot 2.0.2 20201115 [1] CRAN (R 4.1.0) #> rstan * 2.21.2 20200727 [1] CRAN (R 4.1.0) #> rstudioapi 0.13 20201112 [1] CRAN (R 4.1.0) #> scales 1.1.1 20200511 [1] CRAN (R 4.1.0) #> sessioninfo 1.2.1 20211102 [1] CRAN (R 4.1.2) #> StanHeaders * 2.21.07 20201217 [1] CRAN (R 4.1.0) #> stringi 1.7.5 20211004 [1] CRAN (R 4.1.1) #> stringr 1.4.0 20190210 [1] CRAN (R 4.1.0) #> systemfonts 1.0.3 20211013 [1] CRAN (R 4.1.1) #> textshaping 0.3.6 20211013 [1] CRAN (R 4.1.1) #> tibble * 3.1.5 20210930 [1] CRAN (R 4.1.1) #> tidyr * 1.1.4 20210927 [1] CRAN (R 4.1.1) #> tidyselect 1.1.1 20210430 [1] CRAN (R 4.1.0) #> utf8 1.2.2 20210724 [1] CRAN (R 4.1.0) #> V8 3.4.2 20210501 [1] CRAN (R 4.1.1) #> vctrs 0.3.8 20210429 [1] CRAN (R 4.1.0) #> withr 2.4.2 20210418 [1] CRAN (R 4.1.0) #> xfun 0.27 20211018 [1] CRAN (R 4.1.1) #> #> [1] C:/Users/trist/Documents/R/winlibrary/4.1 #> [2] C:/Program Files/R/R4.1.2/library #> #> D  DLL MD5 mismatch, broken installation. #> #> 
Leave a comment