DataAdventurer
DataAdventurer

Reputation: 310

r function cor.test(): How is the p-value calculated for pearson correlation?

It's probably a very easy question.

I can't find the methodology behind the pvalue calculation in the cor.test() function in R.

Upvotes: 1

Views: 3999

Answers (1)

AK88
AK88

Reputation: 3026

Here is the code that calculates the p-value of Pearson's correlation:

x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6,  3.1,  2.5,  5.0,  3.6,  4.0,  5.2,  2.8,  3.8)
ct = cor.test(x, y, method = "pearson")
ct$p.value  ## this is what cor.test() gives

n <- length(x)
r <- cor(x, y)
df <- n - 2
t = sqrt(df) * r/sqrt(1 - r^2)
pval = 2 * min(pt(t, df), pt(t, df, lower.tail = FALSE))  ## this is calculated manually

ct$p.value == pval

Upvotes: 6

Related Questions