1

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) 
daisybeats
  • 217
  • 1
  • 6

1 Answers1

0

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

Fatih Ekici
  • 123
  • 8
  • I tried this using the following: p.value<-rcorr(as.matrix(iris[c(1,2,3,4)]), type=c("pearson")) p.value$P However, this is my output > p.value$P Sepal.Length Sepal.Width Petal.Length Sepal.Length NA 1.518983e-01 0.000000e+00 Sepal.Width 0.1518983 NA 4.513314e-08 Petal.Length 0.0000000 4.513314e-08 NA Petal.Width 0.0000000 4.073229e-06 0.000000e+00 Petal.Width Sepal.Length 0.000000e+00 Sepal.Width 4.073229e-06 Petal.Length 0.000000e+00 Petal.Width NA – daisybeats Sep 14 '20 at 00:21
  • corrplot(rcorr(as.matrix(iris[,-5]))) p.value<-rcorr(as.matrix(iris[c(1,2,3,4)]), type=c("pearson"))$P it's work on me thats printscreen link (lightshot) https://prnt.sc/ugq9zj what is the problem? – Fatih Ekici Sep 14 '20 at 00:29
  • I got the same plot as you but there are only 2 pvalues showing? is that meant to be the case? as I was expecting more between each of the pairs of attributes – daisybeats Sep 14 '20 at 00:35
  • because that p values are too small and corrplot doesn't show small points. I guess that result not wrong. maybe filtering by group increase values. But if you found some mistake reply to me please – Fatih Ekici Sep 14 '20 at 00:41
  • last thing if you found it useful could you please mark with a tick? – Fatih Ekici Sep 14 '20 at 01:00