我们经常听这样的描述,数据呈上升/下降/无明显趋势,这个结论往往是靠人眼观察出来的,不够严谨。我们需要更科学的方法,下面就借助非参里的符号检验,来谈谈检验趋势存在性的两种常用方法。
- Cox-Stuart趋势检验
-
原理
对于数据序列x1,x2,x3,……,xn,我们以位置中间数c为界把该序列分成两部分,并两两配对成(x1,xc+1),(x2,xc+2),……(xc,xn)的形式。接着我们以每一组中后一个数减去前一个数并记下正负性,s+表示得到正数的个数,s-表示得到负数的个数。如果s+远远大于s-,则我们认为序列存在上升趋势,反之序列存在下降趋势,如果s+与s-很接近,那就无明显趋势,这里度量s+和s-有无明显差异性靠的是binom.test()。 -
实现
R语言里没有现成的函数,需要自己编写,这里给出一种作为参考。案例结果表明,置信水平为0.05时,测试序列customers呈下降趋势,但趋势并不显著(P值0.1094)
cox.stuart.test =
function (x)
{
method = "Cox-Stuart test for trend analysis"
leng = length(x)
apross = round(leng) %% 2
if (apross == 1) {
delete = (length(x)+1)/2
x = x[ -delete ]
}
half = length(x)/2
x1 = x[1:half]
x2 = x[(half+1):(length(x))]
difference = x1-x2
signs = sign(difference)
signcorr = signs[signs != 0]
pos = signs[signs>0]
neg = signs[signs<0]
if (length(pos) < length(neg)) {
prop = pbinom(length(pos), length(signcorr), 0.5)
names(prop) = "Increasing trend, p-value"
rval <- list(method = method, statistic = prop)
class(rval) = "htest"
return(rval)
}
else {
prop = pbinom(length(neg), length(signcorr), 0.5)
names(prop) = "Decreasing trend, p-value"
rval <- list(method = method, statistic = prop)
class(rval) = "htest"
return(rval)
}
}
#We can now use the function just created:
customers = c(5, 9, 12, 18, 17, 16, 19, 20, 4, 3, 18, 16, 17, 15, 14)
cox.stuart.test(customers)
Cox-Stuart test for trend analysis
data:
Decreasing trend, p-value = 0.1094
- Mann-Kendall趋势检验
-
原理
比较复杂,具体请参考python中的Mann-Kendall单调趋势检验–及原理说明 -
实现
好在R语言里现成的函数,可直接用。依然使用测试序列customers,结果跟上面有点不同,大于0的z值表明呈上升趋势,不过P值也说明了趋势并不显著,也不能说跟Cox-Stuart test的结果有矛盾。
library(trend)
customers = c(5, 9, 12, 18, 17, 16, 19, 20, 4, 3, 18, 16, 17, 15, 14)
mk.test(customers, continuity = TRUE)
Mann-Kendall trend test
data: customers
z = 0.24835, n = 15, p-value = 0.8039
alternative hypothesis: true S is not equal to 0
sample estimates:
S varS tau
6.0000000 405.3333333 0.0579771
以上就是关于趋势存在性检验的两种非参统计方法:Cox-Stuart趋势检验和Mann-Kendall趋势检验的大致内容,可作为人眼观察识别趋势(不科学、不精确)参数线性回归,利用k值的正负性和显著性来判断趋势(只能识别线性趋势,且要满足一系列经典假定前提)的重要补充。
参考链接:
Mann-Kendall趋势检验-学术百科-知网空间
Analysis with the Cox-Stuart test in R | R-bloggers
python中的Mann-Kendall单调趋势检验–及原理说明_python_留下的,留不下的-CSDN博客
使用R语言进行Mann-Kendall趋势检验 - 知乎