Contents

Introduction to Statistics in python

Peer Herholz (he/him)
Postdoctoral researcher - NeuroDataScience lab at MNI/McGill, UNIQUE
Member - BIDS, ReproNim, Brainhack, Neuromod, OHBM SEA-SIG

logo logo   @peerherholz

In this section, we will cover how you can use Python to do some statistics. There are many packages to do so, but we will focus on four:

This notebook is strongly based on the scipy-lectures.org section about statistics.

Data representation and interaction

Data as a table

The setting that we consider for statistical analysis is that of multiple observations or samples described by a set of different attributes or features. The data can than be seen as a 2D table, or matrix, with columns giving the different attributes of the data, and rows the observations. For example purposes, we will make use of phenotypic information within a dataset from OpenNeuro, provided as part of the following publication:

Keren H., Zheng C., Jangraw D.C., Chang K., Vitale A., Rutledge R.B., Pereira F., Nielson D.M., Stringaris A. (2021). The temporal representation of experience in subjective mood. eLife, 2021;10:e62051.

It contains sex, age, handedness, group id, Mood and Feelings Questionnaire total scores and Snaith-Hamilton Pleasure Scale total scores for a fair amount of participants.

We will use curl to gather the respective file:

%%bash
curl --silent --output /dev/null -O participants_stats.tsv https://openneuro.org/crn/datasets/ds003709/snapshots/1.0.0/files/participants.tsv
!head participants_stats.tsv

The pandas data-frame

Creating dataframes: reading data files or converting arrays

Reading from a CSV file

Using the above CSV file that gives observations of brain size and weight and IQ (Willerman et al. 1991), the data are a mixture of numerical and categorical values::

import pandas as pd
data = pd.read_csv('participants_stats.tsv', sep='\t', na_values=".")
data

Creating from arrays

A pandas.DataFrame can also be seen as a dictionary of 1D ‘series’, eg arrays or lists. If we have 3 numpy arrays:

import numpy as np
t = np.linspace(-6, 6, 20)
sin_t = np.sin(t)
cos_t = np.cos(t)

We can expose them as a pandas.DataFrame:

pd.DataFrame({'t': t, 'sin': sin_t, 'cos': cos_t}).head()

Other inputs: pandas can input data from SQL, excel files, or other formats. See the pandas documentation.

Manipulating data

data is a pandas.DataFrame, that resembles R’s dataframe:

data.shape    # 51 rows and 7 columns
data.columns  # It has columns
print(data['group'].head())  # Columns can be addressed by name
# Simpler selector
data[data['group'] == 'HV']['MFQ'].mean()

Note: For a quick view on a large dataframe, use its describe pandas.DataFrame.describe.

data.describe()
# Frequency count for a given column
data['age'].value_counts()
# Dummy-code # of group (i.e., get N-binary columns)
pd.get_dummies(data['group'])[:15]

The split-apply-combine pattern

  • A very common data processing strategy is to…

    • Split the dataset into groups

    • Apply some operation(s) to each group

    • (Optionally) combine back into one dataset

Pandas provides powerful and fast tools for this. For example the groupby function.

groupby: splitting a dataframe on values of categorical variables:

groupby_group = data.groupby('group')
for group, value in groupby_group['MFQ']:
     print((group, value.mean()))

groupby_group is a powerful object that exposes many operations on the resulting group of dataframes:

groupby_group.mean()

Plotting data

Pandas comes with some plotting tools (pandas.tools.plotting, using matplotlib behind the scene) to display statistics of the data in dataframes.

For example, let’s use boxplot (in this case even groupby_group.boxplot) to better understand the structure of the data.

%matplotlib inline
groupby_group.boxplot(column=['age', 'MFQ', 'SHAPS']);

Scatter matrices

pd.plotting.scatter_matrix(data[['age', 'MFQ', 'SHAPS']]);

Plot content of a dataset

data.head()
data.plot(lw=0, marker='.', subplots=True, layout=(-1, 2), figsize=(15, 6));

Pingouin is an open-source statistical package written in Python 3 and based mostly on Pandas and NumPy.

  • ANOVAs: one- and two-ways, repeated measures, mixed, ancova

  • Post-hocs tests and pairwise comparisons

  • Robust correlations

  • Partial correlation, repeated measures correlation and intraclass correlation

  • Linear/logistic regression and mediation analysis

  • Bayesian T-test and Pearson correlation

  • Tests for sphericity, normality and homoscedasticity

  • Effect sizes and power analysis

  • Parametric/bootstrapped confidence intervals around an effect size or a correlation coefficient

  • Circular statistics

  • Plotting: Bland-Altman plot, Q-Q plot, etc…

Pingouin is designed for users who want simple yet exhaustive statistical functions.

**material scavenged from 10 minutes to Pingouin and the pingouin docs

import pingouin as pg

Measures of correlation

“In the broadest sense correlation is any statistical association, though in common usage it most often refers to how close two variables are to having a linear relationship with each other” - Wikipedia

When talking about correlation, we commonly mean the Pearson correlation coefficient, also referred to as Pearson’s r

pearson_correlation = pg.corr(data['MFQ'], data['SHAPS'])
display(pearson_correlation)
cor_coeeficient = pearson_correlation['r']
n =  len(data) # sample size

Test summary

  • ‘n’ : Sample size (after NaN removal)

  • ‘outliers’ : number of outliers (only for ‘shepherd’ or ‘skipped’)

  • ‘r’ : Correlation coefficient

  • ‘CI95’ : 95% parametric confidence intervals

  • ‘r2’ : R-squared

  • ‘adj_r2’ : Adjusted R-squared

  • ‘p-val’ : one or two tailed p-value

  • ‘BF10’ : Bayes Factor of the alternative hypothesis (Pearson only)

  • ‘power’ : achieved power of the test (= 1 - type II error)

Before we calculate: Testing statistical premises

Statistical procedures can be classfied into either parametric or non parametric prcedures, which require different necessary preconditions to be met in order to show consistent/robust results. Generally people assume that their data follows a gaussian distribution, which allows for parametric tests to be run. Nevertheless it is essential to first test the distribution of your data to decide if the assumption of normally distributed data holds, if this is not the case we would have to switch to non parametric tests.

Shapiro Wilk normality test

Standard procedure to test for normal distribution. Tests if the distribution of you data deviates significtanly from a normal distribution. returns:

  • normal : boolean True if x comes from a normal distribution.

  • p : float P-value.

# Return a boolean (true if normal) and the associated p-value
pg.normality(data, method='normaltest')

Henze-Zirkler multivariate normality test

Same procedure for multivariate normal distributions

returns

  • normal : boolean True if X comes from a multivariate normal distribution.

  • p : float P-value.

# Return a boolean (true if normal) and the associated p-value
import numpy as np
np.random.seed(123)
mean, cov, n = [4, 6], [[1, .5], [.5, 1]], 30
X = np.random.multivariate_normal(mean, cov, n)
pg.multivariate_normality(X, alpha=.05)

Mauchly test for sphericity

“Sphericity is the condition where the variances of the differences between all combinations of related groups (levels) are equal. Violation of sphericity is when the variances of the differences between all combinations of related groups are not equal.” - https://statistics.laerd.com/statistical-guides/sphericity-statistical-guide.php

returns

  • spher : boolean True if data have the sphericity property.

  • W : float Test statistic

  • chi_sq : float Chi-square statistic

  • ddof : int Degrees of freedom

  • p : float P-value.

pg.sphericity(data)

Testing for homoscedasticity

“In statistics, a sequence or a vector of random variables is homoscedastic /ˌhoʊmoʊskəˈdæstɪk/ if all random variables in the sequence or vector have the same finite variance.” - wikipedia

returns:

  • equal_var : boolean True if data have equal variance.

  • p : float P-value.

Note: This function first tests if the data are normally distributed using the Shapiro-Wilk test. If yes, then the homogeneity of variances is measured using the Bartlett test. If the data are not normally distributed, the Levene test, which is less sensitive to departure from normality, is used.

np.random.seed(123)
# Scale = standard deviation of the distribution.
array_1 = np.random.normal(loc=0, scale=1., size=100)
array_2 = np.random.normal(loc=0, scale=0.8,size=100)
print(np.var(array_1), np.var(array_2))

pg.homoscedasticity(data, dv='age', group='group', alpha=.05)

Parametric tests

Student’s t-test: the simplest statistical test

1-sample t-test: testing the value of a population mean

tests if the population mean of data is likely to be equal to a given value (technically if observations are drawn from a Gaussian distributions of given population mean).

pingouin.ttest returns the T_statistic, the p-value, the [degrees of freedom](https://en.wikipedia.org/wiki/Degrees_of_freedom_(statistics), the Cohen d effect size, the achieved [power](https://en.wikipedia.org/wiki/Power_(statistics)) of the test ( = 1 - type II error (beta) = P(Reject H0|H1 is true)), and the Bayes Factor of the alternative hypothesis

pg.ttest(data['MFQ'],0)

2-sample t-test: testing for difference across populations

We have seen above that the mean MFQ in the MDD and HV populations were different. To test if this is significant, we do a 2-sample t-test:

MDD_MFQ = data[data['group'] == 'MDD']['MFQ']
HV_MFQ = data[data['group'] == 'HV']['MFQ']
pg.ttest(MDD_MFQ, HV_MFQ)

Plot achieved power of a paired T-test

Plot the curve of achieved power given the effect size (Cohen d) and the sample size of a paired T-test.

import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='ticks', context='notebook', font_scale=1.2)

d = 0.5  # Fixed effect size
n = np.arange(5, 80, 5)  # Incrementing sample size

# Compute the achieved power
pwr = pg.power_ttest(d=d, n=n, contrast='paired', tail='two-sided')

# Start the plot
plt.plot(n, pwr, 'ko-.')
plt.axhline(0.8, color='r', ls=':')
plt.xlabel('Sample size')
plt.ylabel('Power (1 - type II error)')
plt.title('Achieved power of a paired T-test')
sns.despine()

Non parametric tests:

Unlike the parametric test these do not require the assumption of normal distributions.

Mann-Whitney U Test (= Wilcoxon rank-sum test). It is the non-parametric version of the independent T-test. Mwu tests the hypothesis that data in x and y are samples from continuous distributions with equal medians. The test assumes that x and y are independent. This test corrects for ties and by default uses a continuity correction.” - mwu-function

Test summary

  • ‘W-val’ : W-value

  • ‘p-val’ : p-value

  • ‘RBC’ : matched pairs rank-biserial correlation (effect size)

  • ‘CLES’ : common language effect size

pg.mwu(MDD_MFQ, HV_MFQ)

Wilcoxon signed-rank test is the non-parametric version of the paired T-test.

The Wilcoxon signed-rank test tests the null hypothesis that two related paired samples come from the same distribution. A continuity correction is applied by default.” - wilcoxon - func

# example from the function definition
# Wilcoxon test on two related samples.
x = [20, 22, 19, 20, 22, 18, 24, 20]
y = [38, 37, 33, 29, 14, 12, 20, 22]
print("Medians = %.2f - %.2f" % (np.median(x), np.median(y)))
pg.wilcoxon(x, y, tail='two-sided')

scipy.stats - Hypothesis testing: comparing two groups

For simple statistical tests, it is also possible to use the scipy.stats sub-module of scipy.

from scipy import stats

1-sample t-test: testing the value of a population mean

scipy.stats.ttest_1samp tests if the population mean of data is likely to be equal to a given value (technically if observations are drawn from a Gaussian distributions of given population mean). It returns the T statistic, and the p-value (see the function’s help):

stats.ttest_1samp(data['MFQ'].dropna(), 0)

With a p-value of 8^-09 we can claim that the population mean for the MFQ is not 0.

2-sample t-test: testing for difference across populations

We have seen above that the mean MFQ in the MDD and HV populations were different. To test if this is significant, we do a 2-sample t-test with scipy.stats.ttest_ind:

stats.ttest_ind(MDD_MFQ.dropna(), HV_MFQ.dropna())

Paired tests: repeated measurements on the same indivuals

MFQ and SHAPS are measures of interest. Let us test if they are significantly different. We can use a 2 sample test:

stats.ttest_ind(data['MFQ'].dropna(), data['SHAPS'].dropna())

The problem with this approach is that it forgets that there are links between observations: MFQ and SHAPS are measured on the same individuals.

Thus the variance due to inter-subject variability is confounding, and can be removed, using a “paired test”, or “repeated measures test”:

stats.ttest_rel(data['MFQ'].dropna(), data['SHAPS'].dropna())

This is equivalent to a 1-sample test on the difference::

stats.ttest_1samp(data['MFQ'].dropna() - data['SHAPS'].dropna(), 0)

T-tests assume Gaussian errors. We can use a Wilcoxon signed-rank test, that relaxes this assumption:

stats.wilcoxon(data['MFQ'].dropna(), data['SHAPS'].dropna())

Note: The corresponding test in the non paired case is the Mann–Whitney U test, scipy.stats.mannwhitneyu.

statsmodels - use “formulas” to specify statistical models in Python

Use statsmodels to perform linear models, multiple factors or analysis of variance.

A simple linear regression

Given two set of observations, x and y, we want to test the hypothesis that y is a linear function of x.

In other terms:

\(y = x * coef + intercept + e\)

where \(e\) is observation noise. We will use the statsmodels module to:

  1. Fit a linear model. We will use the simplest strategy, ordinary least squares (OLS).

  2. Test that \(coef\) is non zero.

First, we generate simulated data according to the model:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 20)
np.random.seed(1)

# normal distributed noise
y = -5 + 3*x + 4 * np.random.normal(size=x.shape)

# Create a data frame containing all the relevant variables
data = pd.DataFrame({'x': x, 'y': y})

plt.plot(x, y, 'o');

Then we specify an OLS model and fit it:

from statsmodels.formula.api import ols
model = ols("y ~ x", data).fit()

Note: For more about “formulas” for statistics in Python, see the statsmodels documentation.

We can inspect the various statistics derived from the fit::

print(model.summary())

Terminology

Statsmodels uses a statistical terminology: the y variable in statsmodels is called endogenous while the x variable is called exogenous. This is discussed in more detail here. To simplify, y (endogenous) is the value you are trying to predict, while x (exogenous) represents the features you are using to make the prediction.

Categorical variables: comparing groups or multiple categories

Let us go back to our data. We can write a comparison between MFQ of MDD and HV groups using a linear model:

data = pd.read_csv('participants_stats.tsv', sep='\t', na_values=".")
model = ols("MFQ ~ group + 1", data).fit()
print(model.summary())

Tips on specifying model

Forcing categorical - the ‘group’ variable is automatically detected as a categorical variable, and thus each of its different values is treated as different entities.

An integer column can be forced to be treated as categorical using:

model = ols('MFQ ~ C(group)', data).fit()

Intercept: We can remove the intercept using - 1 in the formula, or force the use of an intercept using + 1.

Multiple Regression: including multiple factors

Consider a linear model explaining a variable z (the dependent variable) with 2 variables x and y:

\(z = x \, c_1 + y \, c_2 + i + e\)

Such a model can be seen in 3D as fitting a plane to a cloud of (x, y, z) points (see the following figure).

from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-5, 5, 21)

# We generate a 2D grid
X, Y = np.meshgrid(x, x)

# To get reproducable values, provide a seed value
np.random.seed(1)

# Z is the elevation of this 2D grid
Z = -5 + 3*X - 0.5*Y + 8 * np.random.normal(size=X.shape)

# Plot the data
fig = plt.figure(figsize=(8, 8))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap=plt.cm.coolwarm,
                       rstride=1, cstride=1)
ax.view_init(20, -120)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

Example: the iris dataset

Sepal and petal size tend to be related: bigger flowers are bigger! But is there, in addition, a systematic effect of species?

from pandas.plotting import scatter_matrix

#Load the data
data = sns.load_dataset('iris')

# Express the names as categories
categories = pd.Categorical(data['species'])

# The parameter 'c' is passed to plt.scatter and will control the color
scatter_matrix(data, c=categories.codes, marker='o', figsize=(8, 8))

# Plot figure
fig.suptitle("blue: setosa, green: versicolor, red: virginica", size=13)
plt.show()
model = ols('sepal_width ~ species + petal_length', data).fit()
print(model.summary())

Post-hoc hypothesis testing: analysis of variance (ANOVA)

In the above iris example, we wish to test if the petal length is different between versicolor and virginica, after removing the effect of sepal width. This can be formulated as testing the difference between the coefficient associated to versicolor and virginica in the linear model estimated above (it is an Analysis of Variance, ANOVA. For this, we write a vector of ‘contrast’ on the parameters estimated: we want to test "name[T.versicolor] - name[T.virginica]", with an F-test:

print(model.f_test([0, 1, -1, 0]))

Is this difference significant?

seaborn - use visualization for statistical exploration

Seaborn combines simple statistical fits with plotting on pandas dataframes.

Let us consider a data giving wages and many other personal information on 500 individuals (Berndt, ER. The Practice of Econometrics. 1991. NY:Addison-Wesley) which we will gather using a small function based on the scipy tutorials.

def download_wages_data():
    
    import urllib
    import os
    
    import pandas as pd

    if not os.path.exists('wages.txt'):
        # Download the file if it is not present
        urllib.request.urlretrieve('http://lib.stat.cmu.edu/datasets/CPS_85_Wages',
                           'wages.txt')

    # Give names to the columns
    names = [
        'EDUCATION: Number of years of education',
        'SOUTH: 1=Person lives in South, 0=Person lives elsewhere',
        'SEX: 1=Female, 0=Male',
        'EXPERIENCE: Number of years of work experience',
        'UNION: 1=Union member, 0=Not union member',
        'WAGE: Wage (dollars per hour)',
        'AGE: years',
        'RACE: 1=Other, 2=Hispanic, 3=White',
        'OCCUPATION: 1=Management, 2=Sales, 3=Clerical, 4=Service, 5=Professional, 6=Other',
        'SECTOR: 0=Other, 1=Manufacturing, 2=Construction',
        'MARR: 0=Unmarried,  1=Married',
    ]

    short_names = [n.split(':')[0] for n in names]

    data = pd.read_csv('wages.txt', skiprows=27, skipfooter=6, sep=None,
                           header=None)
    data.columns = short_names

    # Log-transform the wages, because they typically are increased with
    # multiplicative factors
    import numpy as np
    data['WAGE'] = np.log10(data['WAGE'])
    
    return data
import pandas as pd
data = download_wages_data()
data.head()

Pairplot: scatter matrices

We can easily have an intuition on the interactions between continuous variables using seaborn.pairplot to display a scatter matrix:

import seaborn
seaborn.set()
seaborn.pairplot(data, vars=['WAGE', 'AGE', 'EDUCATION'], kind='reg')

Categorical variables can be plotted as the hue:

seaborn.pairplot(data, vars=['WAGE', 'AGE', 'EDUCATION'], kind='reg', hue='SEX')

lmplot: plotting a univariate regression

A regression capturing the relation between one variable and another, e.g. wage and eduction, can be plotted using seaborn.lmplot:

seaborn.lmplot(y='WAGE', x='EDUCATION', data=data)

Robust regression

Given that, in the above plot, there seems to be a couple of data points that are outside of the main cloud to the right, they might be outliers, not representative of the population, but driving the regression.

To compute a regression that is less sensitive to outliers, one must use a robust model. This is done in seaborn using robust=True in the plotting functions, or in statsmodels by replacing the use of the OLS by a “Robust Linear Model”, statsmodels.formula.api.rlm.

Testing for interactions

Do wages increase more with education for people with dark hair than with light hair?

seaborn.lmplot(y='WAGE', x='EDUCATION', hue='SEX', data=data)

The plot above is made of two different fits. We need to formulate a single model that tests for a variance of slope across the population. This is done via an “interaction”.

from statsmodels.formula.api import ols
result = ols(formula='WAGE ~ EDUCATION + SEX + EDUCATION * SEX', data=data).fit()
print(result.summary())

Can we conclude that education benefits people with dark hair more than people with light hair?

Take home messages

  • Hypothesis testing and p-value give you the significance of an effect / difference

  • Formulas (with categorical variables) enable you to express rich links in your data

  • Visualizing your data and simple model fits matters!

  • Conditioning (adding factors that can explain all or part of the variation) is an important modeling aspect that changes the interpretation.