不同子集的数据表之间的秩和检验秩检验

问题描述 投票:1回答:1

我有这样一个表:

chr  start    end   con1_1_1   con1_2_1   con1_3_1  con2_1_4  con2_2_4  con2_3_4
1    1      1   7512 0.45180723 0.21982759 0.06666667 0.4105960 0.1024735 0.2284710
2    1  13169  20070 0.07142857 0.77631579 0.90434783 0.1363636 0.8985507 0.6033058
3    1  36598  37518 0.13750000 0.43300248 0.09113300 0.9612403 0.1233596 0.7459016
4    1  37512  40365 0.64940239 0.95954693 0.46091644 0.7251656 0.1325648 0.4121901
5    1  40359  48801 0.09504132 0.96491228 0.15428571 0.6388889 0.5165165 0.8050847
6    1  77084  83129 0.91773779 0.28978224 0.56115108 0.9587302 0.5469256 0.6995614

我的数据是在2个与条件中的每个条件3次重复。我想为每行运行秩和检验秩和检验,这意味着每行中的CON1(用3个值)将与CON2进行测试(用3个值)。这就是我试图做的,但我不知道它是否工作正常,如果选择是正确的。

for (j in 1:len) { 

  data=newdata[len,]
  flabel<-factor(c(rep("con1",3),rep("con2",3)))
  data1=c(data[,4],data[,5],data[,6],data[,7],data[,8],data[,9])
  datav=data.frame(flabel,data1)
  test=kruskal.test(data1 ~ flabel, data = datav)
  print(test$p.value)

}

你知道任何更快或更优雅的方式做每一行的测试?

r bioinformatics kruskal-wallis
1个回答
0
投票

您可以尝试创建自己的功能,它apply到每一行。 broom::tidy可以帮助:

 library(broom)

 # function that put the second 3 cols vs the third 3 cols of the dats,
 # using the kruskal test
 fun <- function(x) {
   tidy(kruskal.test(list(x[4:6],x[7:9])))
 }

apply(dats, 1, fun)

使用整齐功能,你也可以这样做:

 # store the result as a list
 test <- apply(dats, 1, fun)

 # "flat" it
 test <- do.call(rbind,test)

cbind(dats,test)

  chr start   end   con1_1_1  con1_2_1   con1_3_1  con2_1_4  con2_2_4  con2_3_4  statistic   p.value parameter
1   1     1  7512 0.45180723 0.2198276 0.06666667 0.4105960 0.1024735 0.2284710 0.04761905 0.8272593         1
2   1 13169 20070 0.07142857 0.7763158 0.90434783 0.1363636 0.8985507 0.6033058 0.04761905 0.8272593         1
3   1 36598 37518 0.13750000 0.4330025 0.09113300 0.9612403 0.1233596 0.7459016 1.19047619 0.2752335         1
4   1 37512 40365 0.64940239 0.9595469 0.46091644 0.7251656 0.1325648 0.4121901 1.19047619 0.2752335         1
5   1 40359 48801 0.09504132 0.9649123 0.15428571 0.6388889 0.5165165 0.8050847 0.42857143 0.5126908         1
6   1 77084 83129 0.91773779 0.2897822 0.56115108 0.9587302 0.5469256 0.6995614 0.42857143 0.5126908         1
                        method
1 Kruskal-Wallis rank sum test
2 Kruskal-Wallis rank sum test
3 Kruskal-Wallis rank sum test
4 Kruskal-Wallis rank sum test
5 Kruskal-Wallis rank sum test
6 Kruskal-Wallis rank sum test

看来你两个具有相同的结果2:

# first row
x <-c( 0.07142857, 0.7763158, 0.90434783)
y <- c ( 0.1363636, 0.8985507, 0.6033058)
kruskal.test(list(x,y))
    Kruskal-Wallis rank sum test

data:  list(x, y)
Kruskal-Wallis chi-squared = 0.047619, df = 1, p-value = 0.8273

# second row
x <-c( 0.45180723,  0.2198276, 0.06666667)
y <- c (0.4105960, 0.1024735, 0.2284710)
kruskal.test(list(x,y))
    Kruskal-Wallis rank sum test

data:  list(x, y)
Kruskal-Wallis chi-squared = 0.047619, df = 1, p-value = 0.8273

随着数据:

dats <- read.table(text ="chr  start    end   con1_1_1   con1_2_1   con1_3_1  con2_1_4  con2_2_4  con2_3_4
1    1      1   7512 0.45180723 0.21982759 0.06666667 0.4105960 0.1024735 0.2284710
2    1  13169  20070 0.07142857 0.77631579 0.90434783 0.1363636 0.8985507 0.6033058
3    1  36598  37518 0.13750000 0.43300248 0.09113300 0.9612403 0.1233596 0.7459016
4    1  37512  40365 0.64940239 0.95954693 0.46091644 0.7251656 0.1325648 0.4121901
5    1  40359  48801 0.09504132 0.96491228 0.15428571 0.6388889 0.5165165 0.8050847
6    1  77084  83129 0.91773779 0.28978224 0.56115108 0.9587302 0.5469256 0.6995614", header = T)
热门问题
推荐问题
最新问题