[英]All pairwise differences between two vectors
Suppose I have two vectors:假设我有两个向量:
X1 <- c(44350, 38920, 37530, 42280, 37320, 36910, 35720, 31220, 33400, 40710, 43830, 37390, 32340, 30770, 35800, 40250, 31490, 40460, 33730, 35850, 35320, 37500, 35380, 40910, 29040, 33950)
X2 <- c(30390, 34170, 28910, 30660, 32510, 30540, 31990, 32380, 32110, 31260, 34670, 28240, 31840, 33350, 32150, 35640, 30730, 30280, 29420, 30990, 32880, 33280, 36960, 36990)
I am interested in counting all pairwise instances where a member of X1 < member of X2.我有兴趣计算 X1 的成员 < X2 的成员的所有成对实例。
How would I do this in R for any two vectors?我将如何在 R 中为任意两个向量执行此操作?
Now, suppose I wish to combine both vectors, randomize, split into two vectors of length(X1) and length(X2) then count as above--creating a randomized distribution to compare against the initial count.现在,假设我希望组合两个向量,随机化,分成两个长度(X1)和长度(X2)的向量,然后按上述方式计数 - 创建一个随机分布以与初始计数进行比较。
How would this be done?这将如何完成?
Count of instances where a in X1 and b in X2 with a < b X1 中的 a 和 X2 中的 b a < b 的实例计数
(s <- sum(outer(X1,X2,`<`)))
# [1] 106
Combine the two将两者结合
X <- c(X1,X2)
Shuffle, resplit, and retest many times多次洗牌、重新拆分和重新测试
set.seed(1)
r <- replicate(1000000, {
X <- sample(X)
X1 <- head(X,length(X1))
X2 <- tail(X,length(X2))
sum(outer(X1,X2,`<`)) })
(p <- 1 - sum(s <= r)/length(r))
# [1] 1e-05
Note: It sounds like you might be looking for something like the Mann-Whitney test, which is under wilcox.test
.注意:听起来您可能正在寻找类似于 Mann-Whitney 测试的东西,它位于
wilcox.test
下。
> wilcox.test(X2,X1)
Wilcoxon rank sum test
data: X2 and X1
W = 106, p-value = 2.858e-05
alternative hypothesis: true location shift is not equal to 0
Compare W = 106
to s = 106
above.将
W = 106
与上面的s = 106
进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.