Is it possible to find the p-value in pearson correlation in R?
To find the pearson correlation, I usually do this
col1 = c(1,2,3,4)
col2 = c(1,4,3,5)
cor(col1,col2)
# [1] 0.8315218
But how I can find the p-value of this?
Is it possible to find the p-value in pearson correlation in R?
To find the pearson correlation, I usually do this
col1 = c(1,2,3,4)
col2 = c(1,4,3,5)
cor(col1,col2)
# [1] 0.8315218
But how I can find the p-value of this?
you can use cor.test
:
col1 = c(1,2,3,4)
col2 = c(1,4,3,5)
cor.test(col1,col2)
which gives :
# Pearson's product-moment correlation
# data: col1 and col2
# t = 2.117, df = 2, p-value = 0.1685
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# -0.6451325 0.9963561
# sample estimates:
# cor
# 0.8315218
More information about the statistics and extra parameters at the official page:
https://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.test.html
If you want only the P value:
> cor.test(col1,col2)$p.value
[1] 0.1684782
The following will do as you ask:
library(Hmisc) # You need to download it first.
rcorr(x, type="pearson") # type can be pearson or spearman
Here x is a data frame, and rcorr returns every correlation which it is possible to form from the "x" data frame.
Or you could calculate the statistic yourself:
$$ t = \frac{\hat{\rho}}{\sqrt{\frac{1-\hat{\rho}^2}{n-2}}} $$
Where $\hat{\rho}$ is the pearson correlation estimated from the data, and n is the sample size.