I have two variables, see sample table below:

Vegcat 50m_Short_veg
S 0.2
S 0.9
M 0.1
T 0.4

The variable 'vegetation category' represent the vegetation taken at a certain point, and denotes whether that vegetation is 'short', 'medium' or 'tall'.

The variable '50_Short_vegetation' is the proportion of vegetation within a 50metre radius around that same point as above, compared to the other types of vegetation. I.e. in row 1, at the specific location the vegetation was 'Short'. Within the 50m radius around that location, 20% of the vegetation is short.

It seems intuitive to me that this will be correlated, but I'd like to know what test you would use to test this. When I try to use Spearman's in R, I get the following error:

Error in cor.test.default(denran8$short_50,denran8$vegcat,method = "spearman") : 'y' must be a numeric vector

A sample of my data if helpful:

structure(list(vegcat = structure(c(3L, 3L, 3L, 3L, 2L, 2L), .Label = c("1", 
"2", "3"), class = "factor"), short_50 = c(0, 0.77778, 0.5, 0.44444, 
0, 0)), row.names = c(NA, 6L), class = "data.frame")

Note- S is represented by '1', M by '2', and T by '3'.


1 Answer 1


Your error is because you have not encoded the categorical variable into a discrete metric IE it is either "S", "M", "T", ... or is not meaning the number of variables you need equals the number if classes with each entry being either 0 or 1 (is "S" or is not "S").

  • $\begingroup$ Apologies, I don't totally understand your answer. You mean I need a column each for S, M, and T, filled with either 0 or 1, and do a spearman rho for "column S & 50m_short_veg", and another spearman rho test for "column M & 50m_short_veg", and another spearman rho test for "column T & 50m_short_veg"? $\endgroup$ Sep 16, 2021 at 1:00
  • $\begingroup$ Have a look at one-hot-encoding. $\endgroup$
    – Chris
    Sep 16, 2021 at 1:01
  • $\begingroup$ One way or another you are dealing with multiple variables. Will need to expand your methods. $\endgroup$
    – Chris
    Sep 16, 2021 at 1:02
  • $\begingroup$ I see. And with regards to the GLMM - is it possible to run the model with my data as is (forgetting the correlation question for a moment), or is it necessary for me to one-hot encode it? Thank you for your helpful responses! $\endgroup$ Sep 16, 2021 at 1:21
  • $\begingroup$ If you were to simply encode the classes as numeric (IE 1, 2, ..) you would define an ordering over vegcat. Maybe that's right (but from what I know about vegcat it is not). Thus you have k variables each of which is either 0 or 1. You need to one-hot encode. $\endgroup$
    – Chris
    Sep 16, 2021 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.