Power and Error Calculations for Correlation Coefficients

Patrick C. Phillips
Biology Department
University of Oregon
pphil@darkwing.uoregon.edu

Initialization of Functions

rpdf[ro_,r_,n_] := (n-2)/((n-1)Sqrt[2]Beta[1/2,n-1/2]) (1-ro^2)^((n-1)/2) (1-r^2)^((n-4)/2) (1-ro r)^(3/2-n) Hypergeometric2F1[1/2,1/2,n-1/2,1/2(1+ ro r)]

rpdf[0,r_,n_] := (1 - r^2)^((n-4)/2) / Beta[1/2,(n-2)/2]

rpdf[ro_,r_,n_] := (n-2)/((n-1)Sqrt[2]Beta[1/2,n-1/2]) (1-ro^2)^((n-1)/2) (1-r^2)^((n-4)/2) (1-ro r)^(3/2-n) /; n > 40 && ro != 0

rcdf[ro_,r_,n_] := Block[{rr},NIntegrate[rpdf[ro,rr,n],{rr,-1,r}]]

upcritr[ro_,n_,alpha_] := Block[{r},
	r /. FindRoot[rcdf[ro,r,n] == 1.0-alpha/2.0,{r,-.999,.999}]]

rpower[ro_,n_,alpha_] := Block[{critr0,rr},
	critr0 = upcritr[0,n,alpha];
	NIntegrate[rpdf[ro,rr,n],{rr,critr0,1}]
  ]
rpower[ro_,n_] := rpower[ro,n,0.05]

findn[ro_, pow_, alpha_]  :=  
      Block[{n}, 
        n  /.  FindRoot[rpower[ro, n, alpha]  ==  pow, {n, 4, 1000}]]findn[ro_, pow_]  :=  findn[ro, pow, 0.05]findn[ro_]  :=  findn[ro, 0.9]

upconfr[r_,n_,alpha_] := Block[{ro},
	ro /. FindRoot[rcdf[ro,r,n] == alpha/2.0,{ro,-.999,.999}]]

lowconfr[r_,n_,alpha_] := Block[{ro},
	ro /. FindRoot[rcdf[ro,r,n] == 1.0-alpha/2.0,{ro,-.999,.999}]]

confr[r_, n_, alpha_]  := 
       {lowconfr[r, n, alpha], upconfr[r, n, alpha]}confr[r_, n_]  :=  confr[r, n, 0.05]

Power Calculations

Use the function "rpower[ρ, n]" where ρ is the correlation coefficient that you want to be able to detect and n is the size of your sample.  Use "findn[ρ]" to find the the sample size necessary to detect ρ with a power of 0.9, or more generally use "findn[ρ, power]" to find the sample size for an arbitrary power.  (Note that this latter procedure is probably more precise and time consuming than necessary.  It can take a few minutes even on a fast computer).  These power calculations are based on taking a Type-I error rate, α = 0.05.  You may want to use a lower error rate (for example for multiple comparisons), and can do this by supplying α as the last parameter to the above functions.  If you specify the α in findn, then be sure that you specify the power too, even if it is 0.9.

Examples

rpower[.2, 50]

findn[.2]

findn[.2, .8]

\(\( (*\ Protect\ against\ 5\ multiple\ comparisons, \ 
      α\  = \ \(0.05/5\  = \ 0.01\)\ *) 
rpower[.2, 50, 0.01]\ \)\)

findn[.2, .9, 0.01]

findn[.2, .8, 0.01]

Confidence Intervals

Use the function "confr[r, n]" to calculate the upper and lower 95% confidence interval for the observed correlation coefficient r with sample size n.  For other confidence intervals, specify the α as the last parameter in confr.  There may be a little complaining about integrals not converging; this should not affect the final answer unless r is very close to -1 or 1.

Examples

confr[.3, 100]

confr[.3, 100, .01]