Search code examples
rp-valuepearson-correlationr-corrplotiris-dataset

How to find p values for correlation matrix in R?


I have been able to generate the following correlation matrix using:

attach(iris)
library(corrplot)
library(Hmisc)
library(Formula)
library(survival)

#FOR SETOSA:
m<-levels(Species)
setosaCor=cor(iris[Species==m[1],1:4],method = "pearson")
corrplot(setosaCor,method="number",mar=c(0,0,1,0),tl.col="black")

However, struggling to get the p values for this matrix. I need the p values in the form a matrix. This is what I've tried but not having any luck

 p.value<-rcorr(as.matrix(iris[c(1,2,3,4)]), type=c("pearson"))
 cor_mat(iris, vars = NULL, method = "pearson", alternative = "two.sided",conf.level = 0.95) 

Solution

  • so like this?

    library("Hmisc")
    res2 <- rcorr(as.matrix(mtcars))
    res2$P
    
    

    output:

                  mpg          cyl         disp           hp         drat           wt         qsec           vs
    mpg            NA 6.112688e-10 9.380328e-10 1.787835e-07 1.776240e-05 1.293958e-10 1.708199e-02 3.415937e-05
    cyl  6.112688e-10           NA 1.803002e-12 3.477861e-09 8.244636e-06 1.217567e-07 3.660533e-04 1.843018e-08
    disp 9.380328e-10 1.803002e-12           NA 7.142679e-08 5.282022e-06 1.222311e-11 1.314404e-02 5.235012e-06
    hp   1.787835e-07 3.477861e-09 7.142679e-08           NA 9.988772e-03 4.145827e-05 5.766253e-06 2.940896e-06
    drat 1.776240e-05 8.244636e-06 5.282022e-06 9.988772e-03           NA 4.784260e-06 6.195826e-01 1.167553e-02
    wt   1.293958e-10 1.217567e-07 1.222311e-11 4.145827e-05 4.784260e-06           NA 3.388683e-01 9.798492e-04
    qsec 1.708199e-02 3.660533e-04 1.314404e-02 5.766253e-06 6.195826e-01 3.388683e-01           NA 1.029669e-06
    vs   3.415937e-05 1.843018e-08 5.235012e-06 2.940896e-06 1.167553e-02 9.798492e-04 1.029669e-06           NA
    am   2.850207e-04 2.151207e-03 3.662114e-04 1.798309e-01 4.726790e-06 1.125440e-05 2.056621e-01 3.570439e-01
    gear 5.400948e-03 4.173297e-03 9.635921e-04 4.930119e-01 8.360110e-06 4.586601e-04 2.425344e-01 2.579439e-01
    carb 1.084446e-03 1.942340e-03 2.526789e-02 7.827810e-07 6.211834e-01 1.463861e-02 4.536949e-05 6.670496e-04
                   am         gear         carb
    mpg  2.850207e-04 5.400948e-03 1.084446e-03
    cyl  2.151207e-03 4.173297e-03 1.942340e-03
    disp 3.662114e-04 9.635921e-04 2.526789e-02
    hp   1.798309e-01 4.930119e-01 7.827810e-07
    drat 4.726790e-06 8.360110e-06 6.211834e-01
    wt   1.125440e-05 4.586601e-04 1.463861e-02
    qsec 2.056621e-01 2.425344e-01 4.536949e-05
    vs   3.570439e-01 2.579439e-01 6.670496e-04
    am             NA 5.834043e-08 7.544526e-01
    gear 5.834043e-08           NA 1.290291e-01
    carb 7.544526e-01 1.290291e-01           NA
    
    

    also the output have matrix format you can subset with [] symbols