简体   繁体   English

R 中的 if 语句中有 3 个条件是不可接受的

[英]3 conditions are not acceptable in if statement in R

Declare the process is out of control if Y>=UCL1 , Y<=LCL1 ( UCL1 and LCL1 control limits are called the outer control limits).如果Y>=UCL1Y<=LCL1UCL1LCL1控制限称为外部控制限),则声明过程失控。

Declare the process is in control if LCL2<=Y<=UCL2 (and are called the inner control limits).如果LCL2<=Y<=UCL2 (称为内部控制限),则声明过程处于受控LCL2<=Y<=UCL2 However if UCL2<Y<=UCL1 or LCL1<Y<=LCL2 , go to step I and repeat the process.但是,如果UCL2<Y<=UCL1LCL1<Y<=LCL2 ,请转到步骤 I 并重复该过程。 Now it is full R code please check it and tell me why is not working.现在它是完整的 R 代码,请检查它并告诉我为什么不起作用。 when I have run this code I do not get any results.当我运行此代码时,我没有得到任何结果。

ld=0.20;n=10; ssize=10000
p0=0.5;
k1=runif(ssize,0,3.2);k1
k2=runif(ssize,0,3.2);k2
mu.x=asin(sqrt(0.5));
var.x=1/(4*n)
M=c(); ARL=c();ARL=370;
shift=c(0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.46,0.47,0.48,0.49,0.50,0.51,0.52,0.53,0.54,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95)
UCL1=mu.x+k1*sqrt(var.x)*sqrt((ld/(2-ld)));UCL1
LCL1=mu.x-k1*sqrt(var.x)*sqrt((ld/(2-ld)));LCL1
UCL2=mu.x+k2*sqrt(var.x)*sqrt((ld/(2-ld)));UCL2
LCL2=mu.x-k2*sqrt(var.x)*sqrt((ld/(2-ld)));LCL2
for(l in 1:length(shift))
{
  p1=shift[l]
  x=c();rl=c();

  for(j in 1:10000){

    for(t in 1:10000){

      x[t]=asin(sqrt(rbinom(1,n,p1)/n))
      if(t==1){M[t]=ld*x[t]+(1-ld)*mu.x} else{M[t]=ld*x[t]+(1-ld)*M[t-1]}       # new Modified EWMA Statistic

      if((M[t]>UCL2 & M[t]<LCL2) | (M[t]>UCL1 & M[t]<UCL2) | (M[t]>LCL2 & M[t]<LCL1)) { 
        rl[j]=t
      }   
      else{ 
        rl[j]=0
      }
    }
  }
  ARL[l]=mean(rl)


}

print(cbind(shift,ARL,LCL1,LCL2,UCL1,UCL2))

Okay one major issue is the loop variable j is never defined.好吧,一个主要问题是循环变量j从未定义过。 Also you should enclose your for loop with curly braces.你也应该用花括号括起你的 for 循环。

Like this:像这样:

for(j in 1:10000){
  for(t in 1:10000){
    if(Y>UCL2 | (Y<LCL2 & Y>UCL1) | (Y<UCL2 & Y>LCL2) | Y<LCL1) { 
      rl[j]=t
    }   
    else{ 
      rl[j]=0
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM