Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wilcoxon test in R - x must be numeric error

Tags:

r

I have a problem with the wilcox.test in R. My data object is a matrix in which the first column contains a name, and all other columns contain a (gene expression) measurement, which is numeric:

str(myMatrix)
'data.frame':   2000 obs. of  143 variables:
$ precursor               : chr  "name1" "name2" "name3" "name4" ...
$ sample1: num  1.46e-03 2.64e+02 1.46e-03 1.46e-03 1.46e-03 ...
$ sample2: num  1.46e-03 1.91e+02 1.46e-03 1.46e-03 1.46e-03 ...
$ sample3: num  1.46e-03 3.01e+02 1.46e-03 1.46e-03 4.96 ...

For all of the 2000 rows I want to test whether there is a difference between 2 given parts of the matrix. I tried this in 4 different ways:

wilcox.test(as.numeric(myMatrix[i,2:87],myMatrix[i,88:98]))$p.value
#[1] 1.549484e-16

wilcox.test(myMatrix[i,2:87],myMatrix[i,88:98])$p.value
#Error in wilcox.test.default(myMatrix[i, 2:87], myMatrix[i, 88:98]) : 
#'x' must be numeric

t.test(as.numeric(myMatrix[i,2:87],myMatrix[i,88:98]))$p.value
#[1] 0.2973957

t.test(myMatrix[i,2:87],myMatrix[i,88:98])$p.value
#[1] 0.3098505

So as you can see, only if I use as.numeric() on the already numeric values I get a result without an error message for the Wilcoxon test, but the results completely differ from t.test results even if they should not.

Manually verifying by using an online tool shows that the t.test results using as.numeric() values are wrong.

Any suggestions about how I can solve this problem and do the correct Wilcoxon test? If you need more information let me know.

like image 755
stefanie Avatar asked Nov 10 '22 14:11

stefanie


1 Answers

Actually, myMatrix [i, 2:87] is still a data.frame. See the following example.

> myMat  
    fir X1 X2 X3 X4  
1 name1  1  5  9 13  
2 name2  2  6 10 14  
3 name3  3  7 11 15  
4 name4  4  8 12 16  
> class(myMat[1, 2:4])  
[1] "data.frame"  
> as.numeric(myMat[1, 2:4])  
[1] 1 5 9  

Change your data to a real Matrix will solve your problem.

> myMat_01 <- myMat[, 2:5]  
> rownames(myMat_01) <- myMat$fir  
> myMat_01 <- as.matrix(myMat_01)  
> class(myMat_01[1, 2:4])  
[1] "integer"
like image 192
Ven Yao Avatar answered Nov 15 '22 06:11

Ven Yao