在R中,我需要计算悬崖三角洲.以下是公式:
其中xi是A组中的观测值,xj是B组中的观测值,如果x i xj为真,则[xixj]为1
以下是克里夫(1993)的非数学解释:
...将一组中的n个X中的每一个与m中的每一个进行比较 另一个,并计算该成员的 第一组较高,多少次较低.
然后除以两两比较的次数,即每组观察次数的乘积.
问题来了:在我的数据中,A组有66208个观测数据,B组有228691个观测数据.66208*228691导致R中的整数溢出.
set.seed(123)
a <- runif(228691)
b <- runif(66208)
x <- length(a) * length(b)
Warning message:
In length(a) * length(b) : NAs produced by integer overflow
因此,我不确定在计算悬崖三角洲时是否可以信任effsize
的结果,因为它抛出了类似的警告:
library(effsize)
x <- cliff.delta(a, b)
Warning messages:
1: In n1 * n2 : NAs produced by integer overflow
2: In n1 * n2 : NAs produced by integer overflow
> x$estimate
[1] -0.0005022877
我可能可以找到一种不使用effsize
来实现计算的方法,但我看不到有任何方法可以绕过需要除以15141173728(228691*66208)的问题,我不知道怎么做,因为这会导致整数溢出.此外,分子还可能导致整数溢出.
有没有办法允许我处理大量数字,这样我就可以在我的数据中计算悬崖三角洲?
编辑:
我听从了普布尔斯的建议,找到了effsize:cliff.delta
个代码中计算d的部分.effsize:cliff.delta
返回10的列表,如果唯一需要的是d,则不会出现任何警告:
set.seed(123)
vector1 <- runif(228691)
vector2 <- runif(66208)
#Remove comments to replicate subsample sanity check
#vector1 <- vector1[1:1000]
#vector2 <- vector2[1:1000]
.bsearch.partition <- function(x, a, b = 1, e = length(a)) {
n <- length(x)
low <- rep(NA, n)
L <- rep(b, n)
H <- rep(e, n)
repeat {
M <- as.integer((L + H) / 2)
left <- x <= a[M]
H[left] <- M[left]
L[!left] <- M[!left] + 1
if (all(H <= L)) {
break
}
}
H <- L
repeat {
below <- a[H] == x
below[is.na(below)] <- FALSE
if (!any(below))
break
H[below] <- H[below] + 1
}
repeat {
L.clean <- L
L.clean[L.clean < 1] <- NA
above <- a[L.clean] >= x
above[is.na(above)] <- FALSE
if (!any(above))
break
L[above] <- L[above] - 1
}
if (any(L == H)){
H[H == L] <- L[H == L] + 1
}
H[H > length(a) + 1] <- length(a) + 1
cbind(below = L, above = H)
}
treatment <- sort(vector1)
control <- sort(vector2)
n1 <- length(treatment)
n2 <- length(control)
partitions <- .bsearch.partition(treatment, control)
partitions[, 2] <- n2 - partitions[, 2] + 1L
partitions[partitions[, 1] > n2, 1] <- n2
d_i. <- mean(partitions %*% c(1L, -1L) / n2)
d <- mean(d_i.)
> d
[1] -0.0005022877