# T-test#

[1]:

import arviz as az
import bambi as bmb
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

[2]:

az.style.use("arviz-darkgrid")
np.random.seed(1234)


In this notebook we demo how to perform a two-sample Bayesian t-test to compare the mean value of two Gaussian populations using Bambi.

## Generate data#

We generate 160 values from a Gaussian with $$\mu=6$$ and $$\sigma=2.5$$ and another 120 values from a Gaussian’ with $$\mu=8$$ and $$\sigma=2$$

[3]:

np.random.seed(2935)
a = np.random.normal(6, 2.5, 160)
b = np.random.normal(8, 2, 120)
df = pd.DataFrame({"Group": ["a"] * 160 + ["b"] * 120, "Val": np.hstack([a, b])})
az.plot_violin({"a": a, "b": b});

/home/tomas/anaconda3/envs/bmb/lib/python3.8/site-packages/xarray/core/alignment.py:307: FutureWarning: Index.__or__ operating as a set operation is deprecated, in the future this will be a logical operation matching Series.__or__.  Use index.union(other) instead
index = joiner(matching_indexes)


### Model#

When we carry out a two sample t-test we’re implicitly using the following model:

$\mu_i = \beta_0 + \beta_1 (i) + \epsilon_i$

where $$i = 0$$ represents the population 1, $$i = 1$$ the popoulation 2 and $$\epsilon_i$$ is a random error with mean 0. If we replace the indicator variables for the two groups we have

$\mu_0 = \beta_0 + \epsilon_i$

and

$\mu_1 = \beta_0 + \beta_1 + \epsilon_i$

Thus, we can see that testing whether the mean of the two populations are equal is equivalent to testing whether $$\beta_1$$ is 0.

### Analysis#

We start by instantiating our model and specifying the model previously described.

[4]:

model = bmb.Model("Val ~ Group", df)
results = model.fit()

Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [Val_sigma, Group, Intercept]

100.00% [4000/4000 00:01<00:00 Sampling 2 chains, 0 divergences]
Sampling 2 chains for 1_000 tune and 1_000 draw iterations (2_000 + 2_000 draws total) took 2 seconds.


We’ve only specified the formula for the model and Bambi automatically selected priors distributions and values for their parameters. We can inspect both the setup and the priors as following:

[5]:

model

[5]:

Formula: Val ~ Group
Family name: Gaussian
Link: identity
Observations: 280
Priors:
Intercept ~ Normal(mu: 6.88884152, sigma: 2.77556648)
Group ~ Normal(mu: 0, sigma: 3.71960189)
sigma ~ HalfStudentT(nu: 4, sigma: 2.26806716)
------
* To see a plot of the priors call the .plot_priors() method.
* To see a summary or plot of the posterior pass the object returned by .fit() to az.summary() or az.plot_trace()

[6]:

model.plot_priors();


To inspect our posterior and the sampling process we can call az.plot_trace(). The option kind='rank_vlines' gives us a variant of the rank plot that uses lines and dots and helps us to inspect the stationarity of the chains. Since there is no clear pattern or serious deviations from the horizontal lines, we can conclude the chains are stationary.

[7]:

az.plot_trace(results, kind="rank_vlines");

[8]:

az.summary(results)

[8]:

mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_bulk ess_tail r_hat
Intercept 6.177 0.169 5.876 6.514 0.005 0.004 1087.0 1234.0 1.0
Group 1.666 0.262 1.145 2.131 0.008 0.006 1055.0 1156.0 1.0
Val_sigma 2.121 0.093 1.953 2.300 0.002 0.002 1437.0 1007.0 1.0

In the summary table we can see the 94% highest density interval for $$\beta_1$$ ranges from 1.201 to 2.173. Thus, according to the data and the model used, we conclude the difference between the two population means is somewhere between 1.2 and 2.2 and hence we support the hypotehsis that $$\beta_1 \ne 0$$.

Similar conclusions can be made with the density estimate for the posterior distribution of $$\beta_1$$. As seen in the table, most of the probability for the difference in the mean roughly ranges from 1.2 to 2.2.

[9]:

# Grab just the posterior of the term of interest (group)
group_posterior = results.posterior["Group"].values
ax = az.plot_kde(group_posterior)
ax.axvline(0, 0, 3, linestyle="--", color="k");


Another way to arrive to a similar conclusion is by calculating the probability that the parameter $$\beta_1 > 0$$. This probability, practically equal to 1, tells us that the mean of the two populations are different.

[10]:

# Probabiliy that posterior is > 0
(group_posterior > 0).mean()

[10]:

1.0

[11]:

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

Last updated: Mon Apr 19 2021

Python implementation: CPython
Python version       : 3.8.5
IPython version      : 7.18.1

arviz     : 0.11.2
pandas    : 1.2.2
json      : 2.0.9
numpy     : 1.20.1
matplotlib: 3.3.3
bambi     : 0.4.1

Watermark: 2.1.0