R代码用于Cox和Stuart测试以进行趋势分析
以下是用于趋势分析的Cox&Stuart测试的R代码。简单地,将代码复制并粘贴到R工作区中并使用它。与R包中名为“ randtests”的cox.stuart.test不同,此版本的测试不会返回大于1的p值。当测试统计量T是解开对数N的一半时,会发生此现象。
这是一个简单的示例,揭示了场景:
> x
[1] 1 4 6 7 9 7 1 6
> cox.stuart.test(x)
考克斯·斯图尔特测试
数据:x
统计量= 2,n = 4,p值= 1.375
替代假设:非随机性
> o.Cox.Stuart.test(x)
Cox和Stuart趋势分析测试
数据:x
检验统计量= 2,解开对的数量= 4,p值= 1
替代假设:任何类型的趋势,减少或增加
Cox和Stuart测试的R代码:
o.Cox.Stuart.test <-函数(x,Alternative = c(“ two。Side”,“ left。Side”,“ right。Side”)){
dname <-删除(替换(x))
替代<-match.arg(alternative)
stopifnot(is.numeric(x))
n0 <-长度(x)
if(n0 <2){
stop(“样本大小必须大于1”)
}
n0 <-圆(长度(x))%% 2
if(n0 == 1){
删除<-(length(x)+1)/ 2
x <-x [-remove]
}
一半<-length(x)/ 2
x1 <-x [1:half]
x2 <-x [(half + 1):( length(x))]
n <-sum((x2-x1)!= 0)
t <-总和(x1 <x2)
if(alternative ==“ left.side”){
p.value <-pbinom(t,n,0.5)
Alternative <-“下降趋势”
}
if(alternative ==“ right.side”){p.value
<-1-pbinom(t-1,n,0.5)
Alternative <-“上升趋势”
}
if(alternative ==“ two.side”){
替代<-“任何类型的趋势,减少或增加”
if(1-pbinom(t-1,n,0.5)== pbinom(t,n,0.5) ){
pdist <-dbinom(0:n,n,0.5)
p.value <-sum(pdist [pdist <= t + 1])
}
否则{
p.value <-2 * min(1-pbinom(t- 1,n,0.5),pbinom(t,n,0.5))
}
}
rval <-list(statistic = c(“ Test Statistic” = t),
alternative = alternative,
p.value = p.value,
method =“ Cox&Stuart Test for Trend Analysis”,
参数= c(“ Untied Pairs的数量“ = n),
data.name = dname)
class(rval)<-“ htest”
return(rval)
}
题库