简体   繁体   English

斯皮尔曼的等级相关性

[英]Spearman's rank correlation

i'm writing a script that reads two .txt file in two vectors. 我正在编写一个脚本,在两个向量中读取两个.txt文件。 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). 第一个向量值的长度为12-13个字符(例如7.3445555667或10.3445555667),第二个向量值的长度为一个字符(例如1或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. .txt文件仅包含数字值。

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" 我得到两个错误,第4行第一个就像“'x'必须是一个数字向量”,第二个错误发生在第5行,就像“无法找到对象向量1.var”

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 但是有一个错误,如“stripchart.default(x1,...)中的错误:无效的plot-method

The implementation is orientated at http://www.gardenersown.co.uk/Education/Lectures/R/correl.htm#correlation 实施方向见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. str是一个非常有用的函数(参见?str for more),应该经常使用,特别是验证R对象类型。 A quick str(vector1) and str(vector2) will tell you if those columns were read as characters instead of numeric. 快速str(vector1)str(vector2)将告诉您这些列是否被读取为字符而不是数字。 If so, then use as.numeric(vector1) to typecast the data in each vector. 如果是,则使用as.numeric(vector1)来对每个向量中的数据进行类型转换。

Also, names(vector1) and names(vector2) will tell you what the column names are and likely resolve your plotting issue. 此外, names(vector1)names(vector2)将告诉您列名称是什么,并可能解决您的绘图问题。

I doubt vector1 and vector2 are vectors. 我怀疑vector1vector2是向量。 Reading ?read.table we note in the Value section: 阅读?read.table我们在Value部分注意到:

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. 其次,您的数据文件不包含标题,因此R将组成变量名称。 I haven't tested this but IIRC your the variables in vector1 and vector2 will both be called X1 . 我没有测试过这个但是IIRC你的vector1vector2的变量都被称为X1 Do head(vector1) and the same on vector2 (or names(vector1) ) to see how your objects look in R. vector2 (或names(vector1) )上执行head(vector1)和相同操作,以查看对象在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 . 我可以看出你为什么认为vector1.var可能有效,但你应该意识到,就R而言,它正在寻找一个名为vector1.var的对象。 The . . is just any other character in R object names. 是R对象名称中的任何其他字符。 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 [[ . 作为子集或选择运算符,那么你需要阅读R中的子集运算符。这些是$[[[ See for example the R Language Definition manual or the R manual . 例如,参见R语言定义手册R手册

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... 但我想你的两个文本文件中有什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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