1、R语言代码
# 处理成为符号数据
cox_test <- function(dat = customers) {
n <- length(dat)
c <- (n - 1)/2
d <- rep(0, c)
if (c %% 2 == 0) { # dat长度为偶数
for (i in 1:c) {d[i] <- dat[c + i] - dat[i]}
} else { # dat长度为奇数时要去掉中间数
dat <- dat[-(c + 1)]
for (i in 1:c) {d[i] <- dat[c + i] - dat[i]}
}
d <- d[d != 0]
binom.test(sum(d > 0), length(d), alt = 'less')
}
customers <- c(5, 9, 12, 18, 17, 16, 19, 20, 4, 3, 18, 16, 17, 15)
cox_test(dat = customers)