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)
lo = z - z_crit * se
hi = z + z_crit * se
return (z_to_r(lo), z_to_r(hi))
: