使用Page的CUSUM程序获取平均样本数?

时间:2019-03-11 16:40:47

标签: r statistics sequential

我正在尝试使用第13页中给出的公式从第12页中复制表1的结果。要访问期刊文章,请单击https://arxiv.org/pdf/math/0605322.pdf。相应的方程式如下。

enter image description here

我的r code在下面给出。我编程正确吗?

mytest=function(n,s,c1){
  t = sum(s)
  k=which.max(s[19:n]>=c1)
  if(k==1 && s[19]<c1) 
    return(c(n,0)) 
  else 
    return(c(k,1))
}


for (n in c(100,200,400)){

for (i in c(-0.5, -1.0)){
a1=0
c1 = 20
asn1=0
for (m in 1:1000){
  g=c(dnorm(n,0,1))
  f=c(dnorm(n,i,1))
  s = log(g/f)
  test=mytest(n,s,c1)
  a1=a1+test[2]
  asn1=asn1+test[1]
}

}
out <- list(power= a1/m, asn=asn1/m)
return(out)
}

但是我遇到以下错误。

Error in if (k == 1 && s[19] < c1) return(c(n, 0)) else return(c(k, 1)) : 
  missing value where TRUE/FALSE needed

1 个答案:

答案 0 :(得分:0)

第一次调用function test() { for(let z = 0; z < 69; z++) { //todo } //z is not defined :( } 时,您会得到mytest并产生n=100, i=-0.5。因此,在给定s=NaN的情况下,在行if(k==1 && s[19]<c1)上会出现错误。

这是一种解决方法,但是您需要确保它能够达到您的期望/希望:

s[19]=NaN