# Bayesian Estimation Supersedes the T-Test¶

```
[1]:
```

```
import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pymc3 as pm
print(f"Running on PyMC3 v{pm.__version__}")
```

```
Running on PyMC3 v3.11.0
```

```
[2]:
```

```
%config InlineBackend.figure_format = 'retina'
az.style.use("arviz-darkgrid")
rng = np.random.default_rng(seed=42)
```

This model replicates the example used in: Kruschke, John. (2012) **Bayesian estimation supersedes the t-test**. *Journal of Experimental Psychology*: General.

## The Problem¶

Several statistical inference procedures involve the comparison of two groups. We may be interested in whether one group is larger than another, or simply different from the other. We require a statistical model for this because true differences are usually accompanied by measurement or stochastic noise that prevent us from drawing conclusions simply from differences calculated from the observed data.

The *de facto* standard for statistically comparing two (or more) samples is to use a statistical test. This involves expressing a null hypothesis, which typically claims that there is no difference between the groups, and using a chosen test statistic to determine whether the distribution of the observed data is plausible under the hypothesis. This rejection occurs when the calculated test statistic is higher than some pre-specified threshold value.

Unfortunately, it is not easy to conduct hypothesis tests correctly, and their results are very easy to misinterpret. Setting up a statistical test involves several subjective choices (*e.g.* statistical test to use, null hypothesis to test, significance level) by the user that are rarely justified based on the problem or decision at hand, but rather, are usually based on traditional choices that are entirely arbitrary (Johnson 1999). The evidence that it provides to the user is indirect,
incomplete, and typically overstates the evidence against the null hypothesis (Goodman 1999).

A more informative and effective approach for comparing groups is one based on **estimation** rather than **testing**, and is driven by Bayesian probability rather than frequentist. That is, rather than testing whether two groups are different, we instead pursue an estimate of how different they are, which is fundamentally more informative. Moreover, we include an estimate of uncertainty associated with that difference which includes uncertainty due to our lack of knowledge of the model
parameters (epistemic uncertainty) and uncertainty due to the inherent stochasticity of the system (aleatory uncertainty).

### Example: Drug trial evaluation¶

To illustrate how this Bayesian estimation approach works in practice, we will use a fictitious example from Kruschke (2012) concerning the evaluation of a clinical trial for drug evaluation. The trial aims to evaluate the efficacy of a “smart drug” that is supposed to increase intelligence by comparing IQ scores of individuals in a treatment arm (those receiving the drug) to those in a control arm (those recieving a placebo). There are 47 individuals and 42 individuals in the treatment and control arms, respectively.

```
[3]:
```

```
# fmt: off
drug = (101,100,102,104,102,97,105,105,98,101,100,123,105,103,100,95,102,106,
109,102,82,102,100,102,102,101,102,102,103,103,97,97,103,101,97,104,
96,103,124,101,101,100,101,101,104,100,101)
placebo = (99,101,100,101,102,100,97,101,104,101,102,102,100,105,88,101,100,
104,100,100,100,101,102,103,97,101,101,100,101,99,101,100,100,
101,100,99,101,100,102,99,100,99)
# fmt: on
y1 = np.array(drug)
y2 = np.array(placebo)
y = pd.DataFrame(
dict(value=np.r_[y1, y2], group=np.r_[["drug"] * len(drug), ["placebo"] * len(placebo)])
)
y.hist("value", by="group", figsize=(12, 4));
```

```
/Users/larryshamalama/opt/anaconda3/envs/stat/lib/python3.8/site-packages/pandas/plotting/_matplotlib/hist.py:288: UserWarning: This figure was using constrained_layout==True, but that is incompatible with subplots_adjust and or tight_layout: setting constrained_layout==False.
fig.subplots_adjust(
```

The first step in a Bayesian approach to inference is to specify the full probability model that corresponds to the problem. For this example, Kruschke chooses a Student-t distribution to describe the distributions of the scores in each group. This choice adds robustness to the analysis, as a T distribution is less sensitive to outlier observations, relative to a normal distribution. The three-parameter Student-t distribution allows for the specification of a mean \(\mu\), a precision (inverse-variance) \(\lambda\) and a degrees-of-freedom parameter \(\nu\):

the degrees-of-freedom parameter essentially specifies the “normality” of the data, since larger values of \(\nu\) make the distribution converge to a normal distribution, while small values (close to zero) result in heavier tails.

Thus, the likelihood functions of our model are specified as follows:

As a simplifying assumption, we will assume that the degree of normality \(\nu\) is the same for both groups. We will, of course, have separate parameters for the means \(\mu_k, k=1,2\) and standard deviations \(\sigma_k\).

Since the means are real-valued, we will apply normal priors on them, and arbitrarily set the hyperparameters to the pooled empirical mean of the data and twice the pooled empirical standard deviation, which applies very diffuse information to these quantities (and importantly, does not favor one or the other *a priori*).

```
[4]:
```

```
μ_m = y.value.mean()
μ_s = y.value.std() * 2
with pm.Model() as model:
group1_mean = pm.Normal("group1_mean", mu=μ_m, sigma=μ_s)
group2_mean = pm.Normal("group2_mean", mu=μ_m, sigma=μ_s)
```

The group standard deviations will be given a uniform prior over a plausible range of values for the variability of the outcome variable, IQ.

In Kruschke’s original model, he uses a very wide uniform prior for the group standard deviations, from the pooled empirical standard deviation divided by 1000 to the pooled standard deviation multiplied by 1000. This is a poor choice of prior, because very basic prior knowledge about measures of human coginition dictate that the variation cannot ever be as high as this upper bound. IQ is a standardized measure, and hence this constrains how variable a given population’s IQ values can be. When you place such a wide uniform prior on these values, you are essentially giving a lot of prior weight on inadmissable values. In this example, there is little practical difference, but in general it is best to apply as much prior information that you have available to the parameterization of prior distributions.

We will instead set the group standard deviations to have a \(\text{Uniform}(1,10)\) prior:

```
[5]:
```

```
σ_low = 1
σ_high = 10
with model:
group1_std = pm.Uniform("group1_std", lower=σ_low, upper=σ_high)
group2_std = pm.Uniform("group2_std", lower=σ_low, upper=σ_high)
```

We follow Kruschke by making the prior for \(\nu\) exponentially distributed with a mean of 30; this allocates high prior probability over the regions of the parameter that describe the range from normal to heavy-tailed data under the Student-T distribution.

```
[6]:
```

```
with model:
ν = pm.Exponential("ν_minus_one", 1 / 29.0) + 1
az.plot_kde(rng.exponential(scale=30, size=10000), fill_kwargs={"alpha": 0.5});
```

Since PyMC3 parameterizes the Student-T in terms of precision, rather than standard deviation, we must transform the standard deviations before specifying our likelihoods.

```
[7]:
```

```
with model:
λ1 = group1_std ** -2
λ2 = group2_std ** -2
group1 = pm.StudentT("drug", nu=ν, mu=group1_mean, lam=λ1, observed=y1)
group2 = pm.StudentT("placebo", nu=ν, mu=group2_mean, lam=λ2, observed=y2)
```

Having fully specified our probabilistic model, we can turn our attention to calculating the comparisons of interest in order to evaluate the effect of the drug. To this end, we can specify deterministic nodes in our model for the difference between the group means and the difference between the group standard deviations. Wrapping them in named `Deterministic`

objects signals to PyMC that we wish to record the sampled values as part of the output.

As a joint measure of the groups, we will also estimate the “effect size”, which is the difference in means scaled by the pooled estimates of standard deviation. This quantity can be harder to interpret, since it is no longer in the same units as our data, but the quantity is a function of all four estimated parameters.

```
[8]:
```

```
with model:
diff_of_means = pm.Deterministic("difference of means", group1_mean - group2_mean)
diff_of_stds = pm.Deterministic("difference of stds", group1_std - group2_std)
effect_size = pm.Deterministic(
"effect size", diff_of_means / np.sqrt((group1_std ** 2 + group2_std ** 2) / 2)
)
```

Now, we can fit the model and evaluate its output.

```
[9]:
```

```
with model:
trace = pm.sample(2000, return_inferencedata=True)
```

```
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [ν_minus_one, group2_std, group1_std, group2_mean, group1_mean]
```

```
Sampling 2 chains for 1_000 tune and 2_000 draw iterations (2_000 + 4_000 draws total) took 23 seconds.
```

We can plot the stochastic parameters of the model. Arviz’s `plot_posterior`

function replicates the informative histograms portrayed in Kruschke (2012). These summarize the posterior distributions of the parameters, and present a 95% credible interval and the posterior mean. The plots below are constructed with the final 1000 samples from each of the 2 chains, pooled together.

```
[10]:
```

```
az.plot_posterior(
trace,
var_names=["group1_mean", "group2_mean", "group1_std", "group2_std", "ν_minus_one"],
color="#87ceeb",
);
```

Looking at the group differences below, we can conclude that there are meaningful differences between the two groups for all three measures. For these comparisons, it is useful to use zero as a reference value (`ref_val`

); providing this reference value yields cumulative probabilities for the posterior distribution on either side of the value. Thus, for the difference of means, at least 97% of the posterior probability are greater than zero, which suggests the group means are credibly
different. The effect size and differences in standard deviation are similarly positive.

These estimates suggest that the “smart drug” increased both the expected scores, but also the variability in scores across the sample. So, this does not rule out the possibility that some recipients may be adversely affected by the drug at the same time others benefit.

```
[11]:
```

```
az.plot_posterior(
trace,
var_names=["difference of means", "difference of stds", "effect size"],
ref_val=0,
color="#87ceeb",
);
```

When `plot_forest`

is called on a trace with more than one chain, it also plots the potential scale reduction parameter, which is used to reveal evidence for lack of convergence; values near one, as we have here, suggest that the model has converged.

```
[12]:
```

```
az.plot_forest(trace, var_names=["group1_mean", "group2_mean"]);
```

```
[13]:
```

```
az.plot_forest(trace, var_names=["group1_std", "group2_std", "ν_minus_one"]);
```

```
[14]:
```

```
az.summary(trace, var_names=["difference of means", "difference of stds", "effect size"])
```

```
[14]:
```

mean | sd | hdi_3% | hdi_97% | mcse_mean | mcse_sd | ess_bulk | ess_tail | r_hat | |
---|---|---|---|---|---|---|---|---|---|

difference of means | 1.021 | 0.440 | 0.225 | 1.866 | 0.008 | 0.006 | 2761.0 | 2488.0 | 1.0 |

difference of stds | 0.932 | 0.454 | 0.120 | 1.820 | 0.008 | 0.006 | 2836.0 | 2309.0 | 1.0 |

effect size | 0.607 | 0.277 | 0.109 | 1.136 | 0.005 | 0.004 | 2873.0 | 2564.0 | 1.0 |

### References¶

Goodman SN. Toward evidence-based medical statistics. 1: The P value fallacy. Annals of Internal Medicine. 1999;130(12):995-1004. doi:10.7326/0003-4819-130-12-199906150-00008.

Johnson D. The insignificance of statistical significance testing. Journal of Wildlife Management. 1999;63(3):763-772.

Kruschke JK. Bayesian estimation supersedes the t test. J Exp Psychol Gen. 2013;142(2):573-603. doi:10.1037/a0029146.

The original pymc2 implementation was written by Andrew Straw and can be found here: https://github.com/strawlab/best

Ported to PyMC3 by Thomas Wiecki (c) 2015, updated by Chris Fonnesbeck.

```
[15]:
```

```
%load_ext watermark
%watermark -n -u -v -iv -w
```

```
Last updated: Wed Mar 31 2021
Python implementation: CPython
Python version : 3.8.5
IPython version : 7.19.0
pandas : 1.1.5
numpy : 1.19.2
matplotlib: 3.3.2
arviz : 0.11.2
pymc3 : 3.11.0
Watermark: 2.2.0
```

```
[ ]:
```

```
```