# How to find p-value for correlation coefficient in R?

R ProgrammingServer Side ProgrammingProgramming

The t test is used to find the p−value for the correlation coefficient and on the basis of that we decide whether there exists a statistically significant relationship between two variables or not. In R, we can perform this test by using function cor.test. For example, if we have a vector x and y then we can find the p−value using cor.test(x,y).

## Example1

Live Demo

set.seed(444)
x1<−1:10
y1<−10:1
cor.test(x1,y1)

Pearson's product−moment correlation

data: x1 and y1
t = −134217728, df = 8, p−value < 2.2e−16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−1 −1
sample estimates:
cor
−1

## Example2

Live Demo

x2<−rnorm(5000,12,1)
y2<−rnorm(5000,12,3)
cor.test(x2,y2)

Pearson's product−moment correlation

data: x2 and y2
t = −1.0611, df = 4998, p−value = 0.2887
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−0.04270876 0.01271735
sample estimates:
cor
−0.01500724

## Example3

Live Demo

x3<−rpois(10000,10)
y3<−rpois(10000,8)
cor.test(x3,y3)

Pearson's product−moment correlation

data: x3 and y3
t = 1.2085, df = 9998, p−value = 0.2269
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−0.007516765 0.031677652
sample estimates:
cor
0.01208509

## Example4

Live Demo

x4<−runif(5557,10,20)
y4<−runif(5557,12,25)
cor.test(x4,y4)

Pearson's product−moment correlation

data: x4 and y4
t = −0.84014, df = 5555, p−value = 0.4009
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−0.03755372 0.01502620
sample estimates:
cor
−0.01127155

## Example5

Live Demo

x5<−rexp(479,3.2)
y5<−rexp(479,1.2)
cor.test(x5,y5)

Pearson's product−moment correlation

data: x5 and y5
t = −1.3626, df = 477, p−value = 0.1736
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−0.15101987 0.02747874
sample estimates:
cor
−0.06226847

## Example6

Live Demo

x6<−rlnorm(1000,2,1.5)
y6<−rlnorm(1000,4,0.8)
cor.test(x6,y6)

Pearson's product−moment correlation

data: x6 and y6
t = −1.4907, df = 998, p−value = 0.1364
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−0.10880908 0.01490269
sample estimates:
cor
−0.04713393

## Example7

Live Demo

x7<−sample(0:9,5000,replace=TRUE)
y7<−sample(1:10,5000,replace=TRUE)
cor.test(x7,y7)

Pearson's product−moment correlation

data: x7 and y7
t = −1.2418, df = 4998, p−value = 0.2144
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−0.04526022 0.01016128
sample estimates:
cor
−0.01756296

## Example8

Live Demo

x8<−sample(101:150,100000,replace=TRUE)
y8<−sample(51:150,100000,replace=TRUE)
cor.test(x8,y8)

Pearson's product−moment correlation

data: x8 and y8
t = −0.7474, df = 99998, p−value = 0.4548
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
−0.008561341 0.003834517
sample estimates:
cor
−0.002363503
Published on 17-Oct-2020 15:51:30