How do you calculate confidence interval for Pearson r in Python?

In Python, I know how to calculate r and the associated p-value with scipy.stats.pearsonr, but I cannot find a way to calculate the confidence interval r. How it's done? Thanks for any help :)

+4
source share
2 answers

According to [1], the calculation of the confidence interval directly with Pearson r is complicated due to the fact that it is not distributed normally. The following steps must be completed:

  • Convert r to z ',
  • Calculate confidence interval z. The z 'sample distribution is approximately uniformly distributed and has a standard error of 1 / sqrt (n-3).
  • r.

:

def r_to_z(r):
    return math.log((1 + r) / (1 - r)) / 2.0

def z_to_r(z):
    e = math.exp(2 * z)
    return((e - 1) / (e + 1))

def r_confidence_interval(r, alpha, n):
    z = r_to_z(r)
    se = 1.0 / math.sqrt(n - 3)
    z_crit = stats.norm.ppf(1 - alpha/2)  # 2-tailed z critical value

    lo = z - z_crit * se
    hi = z + z_crit * se

    # Return a sequence
    return (z_to_r(lo), z_to_r(hi))

:

+2

rpy2 ( R, install.packages( "" ) R)

from rpy2.robjects.packages import importr
psychometric=importr('psychometric')
psychometric.CIr(r=.9, n = 100, level = .95)

0.9 - , n 0,95

+2

Source: https://habr.com/ru/post/1611980/


All Articles