> which(!0) [1] 1 > which(0) Error in which(0) : argument to 'which' is not logical > which(2) Error in which(2) : argument to 'which' is not logical > which(!3) integer(0) > 0526132人目の素数さん2018/09/11(火) 09:12:56.04ID:QUqp/jpE !ってベクトル対応しているな > which(!(-1:1)) [1] 2 > !(-2:2) [1] FALSE FALSE TRUE FALSE FALSE 0527132人目の素数さん2018/09/15(土) 08:05:49.52ID:Vl7XZ52q !をnotではなくis.falseとかis.zero変換すれば( ・∀・)イイ!! 0528132人目の素数さん2018/09/15(土) 16:42:00.50ID:h0gUCZ3r>>527 is.zero だと? だったら最初から == 0 てよいじゃないか。 0529132人目の素数さん2018/09/15(土) 16:45:38.59ID:h0gUCZ3r>>525 だいたい、if や while は関数じゃないし。 while (1) などと書くのはCに毒されているんじゃねーの? Rなら while (TRUE) のほうが良いし、repeat というのもある。 0530132人目の素数さん2018/09/15(土) 16:45:41.31ID:Vl7XZ52q>>528 スクリプトを読むときに脳内変換する癖をつけておけば 可読性が悪いと思わずにすむ。 0531132人目の素数さん2018/09/15(土) 16:46:13.42ID:Vl7XZ52q>>518 >521みた? if(length(x)!=0)になってないよ。 0532132人目の素数さん2018/09/15(土) 17:19:35.45ID:h0gUCZ3r>>531 Rのライブラリ?の書き方なんてあてにならないよ。 そもそも、rev も 509 の reverse も演算の回数を必要最低限にするなら、条件は length(x)) >= 2 または > 1 だ。 まあ if (length(x)) のほうが速かったのかも知れないが、常にそうなるとは限らないのだから、早すぎる最適化は諸悪の根源だ。 0533132人目の素数さん2018/09/15(土) 19:30:34.55ID:Vl7XZ52q 可読性の次は速度かよ。 自分の流儀と違う { の使い方でも文句いいそう。 0534132人目の素数さん2018/09/15(土) 19:44:27.43ID:VibLIqgl 0,1を1000万個作って ! != > == での真偽判定を各々10回する時間を出してみた。
> x=rbinom(1e7,1,0.5) > system.time(replicate(10,!x)) user system elapsed 1.27 0.33 1.59 > system.time(replicate(10,x!=0)) user system elapsed 2.39 0.36 2.75 > system.time(replicate(10,x>0)) user system elapsed 2.58 0.31 2.92 > system.time(replicate(10,x==1)) user system elapsed 2.60 0.36 2.99 0535132人目の素数さん2018/09/15(土) 19:57:45.72ID:VibLIqgl !x と x!=0 で10回やったが、
結果は、 > system.time(mean(replicate(1e4,mhs(sample(0:1,100,rep=T))>=5))) user system elapsed 4.56 0.01 4.61 > system.time(mean(replicate(1e4,seqn()))) user system elapsed 1.05 0.00 1.07
> system.time(mean(replicate(1e4,seqn()))) user system elapsed 4.290 0.000 4.377 >system.time(mean(replicate(1e4,max(rle(rbinom(100,1,0.5))$len)>=5))) user system elapsed 3.620 0.000 3.742 > system.time(mean(replicate(1e4,mhs(rbinom(100,1,0.5)>=5)))) user system elapsed 2.390 0.000 2.435 > system.time(searchseq()) user system elapsed 1.880 0.000 1.988 0556132人目の素数さん2018/11/08(木) 23:35:10.38ID:/ZlhhGjJ>>555 greplの逆転はsampleをrbinomに変えたためでしょう。
> system.time(replicate(1e5,sample(0:1,100,rep=T))) user system elapsed 7.200 0.180 7.591 > system.time(replicate(1e5,rbinom(100,1,0.5))) user system elapsed 5.980 0.230 6.319 0557132人目の素数さん2018/11/09(金) 00:20:12.33ID:BZRFS9lT 不勉強ながらrle関数って初めて知ったけど使いやすそうだな 0558132人目の素数さん2018/11/09(金) 00:36:46.75ID:Ui6BpICy>>555 rleは0も数えているから間違っているんじゃ? 0559132人目の素数さん2018/11/09(金) 02:01:36.95ID:Qla0VxTD>>558 ご指摘ありがとうございました。 修正しました。 rle1 <- function(N=100,n=5,p=0.5){ r=rle(rbinom(N,1,p)) max(r$len[which(r$val==1)]) } 結果 > system.time(mean(replicate(1e4,max(rle1()>=5)))) user system elapsed 4.430 0.000 4.546 > system.time(mean(replicate(1e4,seqn()))) user system elapsed 4.490 0.010 4.609 > system.time(mean(replicate(1e4,mhs(rbinom(100,1,0.5))>=5))) user system elapsed 7.440 0.000 7.656 > system.time(searchseq()) user system elapsed 1.950 0.000 2.066 > 0560132人目の素数さん2018/11/09(金) 07:25:00.97ID:Qla0VxTD 無理矢理1行にして実行
system.time(mean(replicate(1e4,any(diff(cumsum(rbinom(100,1,0.5)),5)==5)))) user system elapsed 1.820 0.000 1.886 > > system.time(mean(replicate(1e4,with(rle(rbinom(100,1,0.5)), max(lengths[wh <e(1e4,with(rle(rbinom(100,1,0.5)), max(lengths[whi ch(values==1)])>=5)))) user system elapsed 4.370 0.010 4.478 0561132人目の素数さん2018/11/09(金) 07:59:47.47ID:rijPDFSR>>560 意味もなくforループ回してた上に毎回sum使って真偽値を数値に変換してたけど replicate使って最後に一回だけmean取ると2.066→1.886で1割短くなるのね 他人のコード読むのは勉強になる 0562132人目の素数さん2018/11/09(金) 09:06:56.99ID:Ui6BpICy>>559 whichいらねーよ。 0563132人目の素数さん2018/11/09(金) 09:44:07.41ID:5AnUTlVm>>559 全部が0のとき、エラーになるので修正
rle01 <- function(x){ # c(0,1,1,1,0,0) => return 3 if(sum(x)==0) return(0) #c(0,0,0,0,0,0) => return 0 else{ r=rle(x) # Run Length Encoding max(r$lengths[which(r$values==1)]) # max length of value 1 } }
F = function(fun){ n=1:9 ans=NULL for(a in n){ for(b in n){ for(c in a:9){ for(d in n){ for(e in n){ for(f in n){ for(g in n){ if(fun(a,b,c,d,e,f,g)){ ans=rbind(ans,c(a,b,c,d,e,f,g))}}}}}}}}
MM1sim <- function(n=40,lambda=5/60,mu=6/60,seed=FALSE,Print=TRUE){ # service starc clock time(ssct) since 9:00 ssct=numeric(n) # waiting time(w8) w8=numeric(n) # service end clock time(sect) sect=numeric(n) # arrival clock time(act) if(seed) set.seed(1234) ; act=round(cumsum(rexp(n,lambda))) # duration of service(ds) if(seed) set.seed(5678) ; ds=round(rexp(n,mu))
# simulation assuming service starts at 9:00 head(act) # act : arrival clock time head(ds) # ds : duration of service # initial values ssct[1]=act[1] # 9:15 service start clock time for 1st guest sect[1]=act[1]+ds[1] # 9:25 sevice end clock time for 1st guest w8[1]=0
# simulation step by step # # act[2] # 9:16 arrival clock time of 2nd # max(sect[1]-act[2],0) # 9:25-9:16 vs 0 = ?sevice for 1st ends b4 2nd arrival # w8[2]=max(sect[1]-act[2],0) # 9 min : w8ing time of 2nd # ssct[2]=max(sect[1],act[2]) # 9:25 vs 9:16 = service start clock time for 2nd # sect[2]=ssct[2]+ds[2] # 9:25 + 8 = 9:33 service end clock time for 2nd # # act[3] # 9:17 arrival clock time of 3rd # max(sect[2]-act[3],0) # 9:33 - 9:17 vs 0 = ?serivce for 2nd ends b4 3rd arrival? # w8[3]=max(sect[2]-act[3],0) # 16 min : w8ting time of 3rd # ssct[3]=max(sect[2],act[3]) # 9:33 vs 9:17 = service start clock time for 3rd # sect[3]=ssct[3]+ds[3] # 9:33 + 11 = 9:44 service end clock time for 3rd # 0599132人目の素数さん2018/11/28(水) 19:28:45.39ID:r8zTzMor>>597 患者来院数40人程度では定常状態に達しないということみたいだな。
10万人での待ち時間の分布(理論値50に近い) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.000 5.532 28.619 47.540 67.370 528.511
100人での待ち時間の分布 (シミュレーションの度にばらつく) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.000 6.958 16.564 22.200 31.652 109.204
40人来院を1万回繰り返した平均の分布 Min. 1st Qu. Median Mean 3rd Qu. Max. 1.304 11.764 19.357 25.449 32.536 177.913