Skip to content

Instantly share code, notes, and snippets.

@zhiyzuo
Last active January 4, 2023 06:19
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save zhiyzuo/d38159a7c48b575af3e3de7501462e04 to your computer and use it in GitHub Desktop.
Save zhiyzuo/d38159a7c48b575af3e3de7501462e04 to your computer and use it in GitHub Desktop.
calculate Pearson correlation along with the confidence interval using scipy and numpy
import numpy as np
from scipy import stats
def pearsonr_ci(x,y,alpha=0.05):
''' calculate Pearson correlation along with the confidence interval using scipy and numpy
Parameters
----------
x, y : iterable object such as a list or np.array
Input for correlation calculation
alpha : float
Significance level. 0.05 by default
Returns
-------
r : float
Pearson's correlation coefficient
pval : float
The corresponding p value
lo, hi : float
The lower and upper bound of confidence intervals
'''
r, p = stats.pearsonr(x,y)
r_z = np.arctanh(r)
se = 1/np.sqrt(x.size-3)
z = stats.norm.ppf(1-alpha/2)
lo_z, hi_z = r_z-z*se, r_z+z*se
lo, hi = np.tanh((lo_z, hi_z))
return r, p, lo, hi
@javiribera
Copy link

In line 26, x.size should be replaced by len(x). x.size assumes that the input is a numpy array and the docstring says that the input can also be a list.

@AzadehFeizpour
Copy link

Given that we're doing Fisher's Z transformation of r, Is that ok to use this formula for stats.spearmanr as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment