简体   繁体   中英

Spearman's rank correlation

i'm writing a script that reads two .txt file in two vectors. After that I want to make a Spearman's rank correlation and plot the result. The first vectors value's length is 12-13 characters (eg 7.3445555667 or 10.3445555667) and the second vectors value's length is one character (eg 1 or 2).

The code:

vector1 <- read.table ("D:...path.../mytext1.txt", header=FALSE)
vector2 <- read.table ("D:...path.../mytext2.txt", header=FALSE)
cor.coeff = cor(vector1 , vector2 , method = "spearman")
cor.test(vector1 , vector2 , method = "spearman")
plot(vector1.var, vector2.var)

The .txt files contain only numeric values.

I'm getting two errors, the first in line 4 it's like " 'x' have to be a numeric vector" and the second error occurs in line 5 it's like "object vector 1.var couldn't be found"

I also tried

 plot(vector1, vector2)

instead of

 plot(vector1.var, vector2.var)

But then there's an error like "Error in stripchart.default (x1,...) : invalid plot-method

The implementation is orientated at http://www.gardenersown.co.uk/Education/Lectures/R/correl.htm#correlation

str is a very useful function (see ?str for more) that one should use often, especially to verify R object types. A quick str(vector1) and str(vector2) will tell you if those columns were read as characters instead of numeric. If so, then use as.numeric(vector1) to typecast the data in each vector.

Also, names(vector1) and names(vector2) will tell you what the column names are and likely resolve your plotting issue.

I doubt vector1 and vector2 are vectors. Reading ?read.table we note in the Value section:

Value:

  A data frame ('data.frame') containing a representation of the data in the file. 

....

So even if your two text files contain just a single variable, the two objects read in will be data frames with a single component each.

Secondly, your data files don't contain headers so R will make up a variable name. I haven't tested this but IIRC your the variables in vector1 and vector2 will both be called X1 . Do head(vector1) and the same on vector2 (or names(vector1) ) to see how your objects look in R.

I can see why you might think vector1.var might work, but you should realise that as far as R was concerned it was looking for an object named vector1.var . The . is just any other character in R object names. If you meant to use . as a subsetting or selection operator, then you need to read up on subsetting operators in R. These are $ and [ and [[ . See for example the R Language Definition manual or the R manual .

I suspect you could just change your code to:

vector1 <- read.table ("D:...path.../mytext1.txt", header=FALSE)[, 1]
vector2 <- read.table ("D:...path.../mytext2.txt", header=FALSE)[, 1]
cor.coeff <- cor(vector1 , vector2 , method = "spearman")
cor.test(vector1 , vector2 , method = "spearman")
plot(vector1, vector2)

But I am supposing quite a bit about what is in your two text files...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM