1

I used the following code to get an entire table of Spearman Rho correlations in R from a CSV file with only 1 data type (numbers):

> myDataset <- read.csv(file.choose())
> attach(myDataset)
> spearmanRhoTable <- cor(myDataset, use="complete.obs",method="spearman")

Is there any way I could take my table of r-values (spearmanRhoTable) and get a table of all the p-values, rather than having to enter each Spearman correlation manually to get individual p-values?

Here is my table of r-values. It's called spearmanRhoTable, and I was wondering if there was a quick way to get p-values for everything in this table. picture of Spearman Rho values

Audrey
  • 165
  • 1
  • 1
  • 9
  • What happened to your previous question? You should always add sample data to work with. Otherwise, questions may be closed or if answered will not be very useful for future users(you, me and everyone else). See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) details on what makes a great reproducible R example/ – NelsonGon Jun 14 '19 at 18:30
  • **Self promotion** I did write up a function that does the same: `get_var_corr(mtcars, "mpg",get_all = TRUE,method="spearman",exact=F)` available in the developer version of a package I wrote: [manymodelr](https://github.com/Nelson-Gon/manymodelr/tree/develop). It returns a data.frame with the `rho` coefficient and p values. – NelsonGon Jun 14 '19 at 18:34
  • 1
    Thanks @NelsonGon, the get_var_corr function was helpful for getting p-values... Do you also have a function where I can get a matrix of p-values for all the variables and their correlations with all the other variables? (it seems like the get_var_corr function gets p-values for just one variable and its correlations with the other variables) – Audrey Jun 14 '19 at 19:27
  • Unfortunately not(yet!). Currently can be done only manual which really isn't efficient. It's something I overlooked. Will work on adding one in a few days. – NelsonGon Jun 14 '19 at 19:29
  • I did add a new(currently slow) function `get_var_corr_` that aims to do the same. Let me know if it achieves your aim. – NelsonGon Jun 15 '19 at 16:57

1 Answers1

1

The Hmisc package has a function rcorr that can give you a matrix of p-values.

library(Hmisc)

df <- data.frame(x = runif(10), 
                 y = runif(10), 
                 z = runif(10))

rcorr(as.matrix(df))$P
the-mad-statter
  • 5,650
  • 1
  • 10
  • 20