0

I am trying to test the correlation for a given dataset:

> file <- read.csv("dataset.csv", header = TRUE, sep = ";", dec = ".")

> cor.test(file[, -file$ID], method = "spearman") 

ID is identifying each point, so I dont want to consider that in the analysis. Even when considering the whole file, the error is the same

I get this error:

Error in cor.test.default(file[, -file$ID], method = "spearman") : argument "y" is missing, with no default

Why is this happening? Do you know how can I solve this? I know I am supposed to have both vectors (x and y), but I wanted to consider the whole dataset at the same time.

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
mto23
  • 303
  • 1
  • 5
  • 15
  • 1
    `cor.test` needs both x and y argument. Please check `?cor.test` – akrun Jun 18 '16 at 11:17
  • @akrun: Can't I consider them in the same file? Because I have more than 2 columns to test, and hundreds of rows, and it is not viable to express the data as they suggest (ex.: x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)) – mto23 Jun 18 '16 at 11:23
  • Have you looked at examples in the `?cor.test` i.e. `cor.test(~ CONT + INTG, data = USJudgeRatings)` – akrun Jun 18 '16 at 11:25
  • @Akrun: with the approach "cor.test(~ CONT + INTG, data = USJudgeRatings)", how do I add more columns? When I add more "+ column", I get "Invalid formula" – mto23 Jun 18 '16 at 11:46
  • With `cor`, you may pass in a dataframe, but that works because `cor` returns a single numeric value. `cor.test` returns an object of class `htest`, which does not fit into a dataframe as nicely. If you need all of the output of `cor.test`, you will need to do some sort of loop/apply. An earlier question from today may help. http://stackoverflow.com/questions/37894768/r-how-to-apply-a-function-that-outputs-a-dataframe-to-multiple-columns-using-dp/37895054#37895054 – Benjamin Jun 18 '16 at 11:56
  • @Teresa Take a closer look at at the help file, the description says "Test for association between paired samples." You cannot accomplish what you want to solely with this function as it works with paired samples. Further, be careful with what you are trying to do: running multiple related tests can lead to a higher rate of rejections than is valid. Search for "Bonferroni correction" for details. – lmo Jun 18 '16 at 11:59
  • @lmo I want to test the correlation between paired variables, which is why I was using this function. I tried with simply the "cor()", and it seemed to work. However, I don't know how to obtain the p-value for each paired combination and (please correct me if I am wrong) I think I need the p-value to interpret the spearman's rank values. – mto23 Jun 18 '16 at 13:22

1 Answers1

1

Teresa, I have found more than one answer already on stackoverflow with some Google query. The most relevant seems to come from

A matrix version of cor.test()

It gives as clue to look at corr.test in psych package.

So that you don't have to care having to lapply.

Once package installed, should then be:

psych::corr.test(file[, -file$ID], method = "spearman")
Community
  • 1
  • 1
Eric Lecoutre
  • 1,461
  • 16
  • 25
  • That one works, thank you very much! But it provides two matrix, "Correlation matrix" and "Probability values": is this last one referring to the p-values? Sorry, I don't understand very well how this correlation theme works. – mto23 Jun 18 '16 at 13:16
  • Indeed you have several results, not only the ones printed. Have a look at the help and `names(result)`. P-values are given with `results$p`. – Eric Lecoutre Jun 18 '16 at 14:04