# KeepNotes blog

Stay hungry, Stay Foolish.

0%

#### Hypothesis Tests

Correlation作hypothesis test是一个常见的分析，一般我们的零假设H0是`ρ=0`，也就是说想看下correlation与0的差别是否显著，此时满足t distribution，先计算t-statistics

``````data("iris")
> cor.test(iris\$Sepal.Length, iris\$Petal.Length)

Pearson's product-moment correlation

data:  iris\$Sepal.Length and iris\$Petal.Length
t = 21.646, df = 148, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.8270363 0.9055080
sample estimates:
cor
0.8717538 ``````

``````r <- 0.87175
> r / sqrt((1 - r^2) / (150 - 2))
[1] 21.64563

pvalue <- 2 * pt(-abs(21.64563), df=150-1)``````

Fisher transformation有哪些用处呢？

Fisher (1973, p. 199) describes the following practical applications of the z transformation:

• testing whether a population correlation is equal to a given value
• testing for equality of two population correlations
• combining correlation estimates from different samples

``````> (1/2*log((1+0.87175)/(1-0.87175)) - 1/2*log((1+0.8)/(1-0.8))) / sqrt(1/(150-3))
[1] 2.930596
> 2 * pnorm(abs(2.930596), lower.tail = F)
[1] 0.003383124``````

``````proc corr data=sashelp.iris nosimple fisher (rho0=0.8 biasadj=no);
var SepalLength PetalLength;
run;``````

#### Confidence Intervals

For the transformed , the approximate variance V(zr)=1/(n-3) is independent of the correlation . Furthermore, even the distribution of is not strictly normal, it tends to normality rapidly as the sample size increases for any values of (Fisher 1973, pp. 200–201).

``````# Correlation coefficient
r <- 0.87175
# Z statistics
Z_upper <- 1/2 * log((1+r)/(1-r)) + qnorm(p = 1 - 0.05/2, lower.tail = T) / sqrt(150 - 3)
Z_lower <- 1/2 * log((1+r)/(1-r)) - qnorm(p = 1 - 0.05/2, lower.tail = T) / sqrt(150 - 3)
# Correlation confidence interval
Cor_upper <- (exp(2 * Z_upper) - 1) / (exp(2 * Z_upper) + 1)
Cor_lower <- (exp(2 * Z_lower) - 1) / (exp(2 * Z_lower) + 1)
> c(Cor_lower, Cor_upper)
[1] 0.8270314 0.9055052``````

PS. 若想了解其他的correlation hypothesis test方法以及计算结果可参考：https://www.psychometrica.de/correlation.html，蛮有意思的一个网站。。。