Advertisement
zildjiean

R Code (Yoye)

Aug 13th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.96 KB | None | 0 0
  1. function (m,n,p,alpha,m2,n2,round1)
  2. {
  3.     set.seed(5)
  4.     x=rbinom(m,n,p)
  5.     z=length(x)
  6.  
  7.         for(i in 1:z){
  8.             p[i]=(x[i]/m)
  9.         }
  10.         pbar=(sum(p)/n)
  11.         cat('\n','M =',p,' ','\n')
  12.         Mbar=pbar
  13. cat ('\n','M bar =',Mbar,'\n')
  14.  
  15. ####################### Calculate UCL,CL,LCL ##############################
  16. ######## Calculate CAL(L) ##########
  17.     CLl = (Mbar*alpha)
  18.     LCLl= CLl-(3*(sqrt(((CLl)*(1-CLl))/n)))
  19.     UCLl= CLl+(3*(sqrt(((CLl)*(1-CLl))/n)))
  20.  
  21. cat('\n','--------------- Real UCL,CL,LCL for (L) ------------------','\n')
  22. cat('\n','UCL(L) =',UCLl,'CL(L) =',CLl,'LCL(L) =',LCLl,'\n')
  23.  
  24.  
  25. ######## Calculate CAL(R) ##########
  26.     CLr = 1-((1-((Mbar*alpha)*alpha)))
  27.     LCLr= CLr-(3*(sqrt(((CLr)*(1-CLr))/n)))
  28.     UCLr= CLr+(3*(sqrt(((CLr)*(1-CLr))/n)))
  29.  
  30. cat('\n','--------------- Real UCL,CL,LCL for (R) ------------------','\n')
  31. cat('\n','UCL(R) =',UCLr,'CL(R) =',CLr,'LCL(R) =',LCLr,'\n')
  32.  
  33. ######## Check Condition for UCL,LCL (L) ##########
  34.         if(LCLl <= 0){
  35.             LCLl=0
  36.         } else LCLl=LCLl
  37.         if(UCLl > 1){
  38.             UCLl=1
  39.         } else UCLl=UCLl
  40.  
  41. ######## Check Condition for UCL,LCL (R) ##########
  42.         if(LCLr <= 0){
  43.             LCLr=0
  44.         }else LCLr=LCLr
  45.  
  46.         if(UCLr > 1){
  47.             UCLr=1
  48.         } else UCLr=UCLr
  49.  
  50. cat('\n','--------------- Calculate UCL,CL,LCL for (L) ------------------','\n')
  51. cat('\n','UCL(L) =',UCLl,'CL(L) =',CLl,'LCL(L) =',LCLl,'\n')
  52. cat('\n','--------------- Calculate UCL,CL,LCL for (R) ------------------','\n')
  53. cat('\n','UCL(R) =',UCLr,'CL(R) =',CLr,'LCL(R) =',LCLr,'\n')
  54.  
  55.  
  56. ######### Check Condition Process (Not work!) #########
  57. ######## Calculate Step 2 ##############
  58.  
  59.     for (i in 1:round1){
  60.         x2=rbinom(m2,n2,Mbar)
  61.         z1=length(x2)
  62.         pnew=rep(0,m2)
  63.         rl=rep(0,round1)
  64.    
  65.     for (v in 1:z1){
  66.         pnew[v]=(x2[v]/m2)
  67.     }
  68. #cat('\n',pnew,'\n')
  69. #cat('\n',length(pnew),'\n')
  70.  
  71.         in.control=TRUE
  72.             j=0
  73.             k=0
  74.  
  75.         while(in.control)
  76.             {
  77.                 j=j+1
  78.                 k=k+1
  79.             if((pnew[k]>UCLl)||(pnew[k]<LCLl))
  80.                 {
  81.                 in.control=FALSE
  82.                 }
  83.             }
  84.             rl[i]=j
  85.  
  86. }
  87. #cat('\n',rl,'\n')
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement