【问题标题】:Generating combinations of data and then running Wilcoxon test on each combination生成数据组合,然后对每个组合运行 Wilcoxon 测试
【发布时间】:2017-03-13 04:29:30
【问题描述】:

我有一个由 50 个学生(行)组成的数据集,每个学生有 15 个分数(列,1-5 分的分数),我正在尝试做几件事。首先,我试图从原来的 15 分中为每个学生生成 10 分的所有可能组合。然后,我试图针对原始的 15 分集对每个分数组合进行 Wilcoxon 测试(我相信 Mann-Whitney U)让学生确定哪些组合显着不同。到目前为止,我在 R 中编写的代码与循环一起发布在下面,以对每个组合运行测试,但我不断收到错误“错误没有足够(有限)观察”,我不确定我是否正在这样做适当地。我尝试了 as.numeric() ,但似乎仍然没有解决它。该代码似乎可以很好地生成组合,我只是无法让测试正常工作。感谢所有帮助,完全准备好被告知我做错了,非常感谢整理代码的智慧。

m<-10
n<-choose(15,m)

for(j in 1:50)
{df3 <- expand.grid(combn(data[j,],m))  

x<-(length = 0)    

for(i in 1:n)    
{niceArray <- c(df3[1,(m*(i-1)+1)],df3[1,(m*(i-1)+2)],df3[1,(m*(i-  1)+3)],df3[1,(m*(i-1)+4)],df3[1,(m*(i-1)+5)],df3[1,(m*(i-1)+6)],df3[1,(m*(i-1)+7)],df3[1,(m*(i-1)+8)],df3[1,(m*(i-1)+9)],df3[1,(m*(i-1)+10)])

tst<-wilcox.test(niceArray, as.numeric(data[j,]), alternative="two.sided", paired=FALSE)
if(tst$p.value<0.05)
{print(tst$p.value)    
x<-append(x,1,after=length(x))}}
print(length(x)-1)}

【问题讨论】:

    标签: r


    【解决方案1】:

    因此,解析您所做的事情有点困难。这是我为解决您的问题所做的。

    我首先创建了一个包含 2 个学生的数据集,他们的分数为 15。

    student_ds<-rbind(abs(sample(rnorm(15)*100)),abs(sample(rnorm(15)*100)))
    

    然后我创建一个名为 testresults 的空数据集。 在循环内部,我们创建了一个数据框,其中包含 15 个分数中的 10 个的所有可能组合。然后我们使用 apply 来获取 wilcoxon p 值,用于将原始学生的 15 分与所有可能的 3003 分进行比较,并将其附加到子样本中。最后,我们在 testresults 数据框中将它们绑定在一起。

    for (i in 1:nrow(student_ds)){
      combinatoric<-data.frame(t(combn(student_ds[i,],10)))  # represents all ways to choose 10 from the 15
      combinatoric$wilcox.pvalue<- apply(combinatoric[,1:10],1,function(x) wilcox.test(x,student_ds[i,], alternative="two.sided", paired=FALSE)$p.value)
      combinatoric$student <- paste0("student",i)
      testresults <- rbind(testresults,combinatoric)  
      }
    
    > head(testresults);tail(testresults)
            X1       X2       X3       X4       X5       X6       X7       X8       X9        X10 wilcox.pvalue  student
    1 1.974715 72.51776 119.8322 12.26869 85.91388 209.4265 65.30297 17.25499 27.32821  79.272713     0.8894956 student1
    2 1.974715 72.51776 119.8322 12.26869 85.91388 209.4265 65.30297 17.25499 27.32821  80.104210     0.9335609 student1
    3 1.974715 72.51776 119.8322 12.26869 85.91388 209.4265 65.30297 17.25499 27.32821  93.908482     1.0000000 student1
    4 1.974715 72.51776 119.8322 12.26869 85.91388 209.4265 65.30297 17.25499 27.32821 107.429904     0.9778308 student1
    5 1.974715 72.51776 119.8322 12.26869 85.91388 209.4265 65.30297 17.25499 27.32821  50.930822     0.7598532 student1
    6 1.974715 72.51776 119.8322 12.26869 85.91388 209.4265 65.30297 17.25499 27.32821   5.215228     0.5975122 student1
                X1        X2       X3       X4       X5       X6       X7       X8       X9      X10 wilcox.pvalue  student
    6001  47.59236 222.62861  9.04522 78.70014 83.69301 123.9315 14.30569 40.72565 56.99276 129.4363     0.9778308 student2
    6002  47.59236 222.62861  9.04522 78.70014 32.81073 123.9315 14.30569 40.72565 56.99276 129.4363     0.7598532 student2
    6003  47.59236 222.62861  9.04522 83.69301 32.81073 123.9315 14.30569 40.72565 56.99276 129.4363     0.8025127 student2
    6004  47.59236 222.62861 78.70014 83.69301 32.81073 123.9315 14.30569 40.72565 56.99276 129.4363     0.8894956 student2
    6005  47.59236   9.04522 78.70014 83.69301 32.81073 123.9315 14.30569 40.72565 56.99276 129.4363     0.5595174 student2
    6006 222.62861   9.04522 78.70014 83.69301 32.81073 123.9315 14.30569 40.72565 56.99276 129.4363     0.9335609 student2
    

    如果这不是你想要的,它可能足以激励你走向伟大。

    【讨论】:

    • 顺便说一句,我不确定您为什么要这样做。请告知:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    相关资源
    最近更新 更多