我正在尝试生成在两侧截断的分段常数指数随机变量。但在我的情况下,每次左截断点将改变,以及每个时间点的速率。也就是说,每个数字必须大于入门年龄且小于或等于89.我必须生成50,000个数字。
我已经尝试了以下代码,这在生成1000-2000个随机数时是可以的,但是当我想生成50,000时,它会永远消失。有什么方法可以更快地完成吗?
library(PWEALL)
time_breaks = c(0,18,30,35,40,45,50,55,60,65,70,75,80,85)
cbc.hazard.sim=
c(0.000000000,0.001232438,0.001857680,0.002381863,0.002678475,
0.003070916,0.003068740,0.003400704, 0.003407023,
0.004028654, 0.003825065, 0.004224777, 0.003891890, 0.003590939)
cbc.time=function(x,y,cbc_hazard,time_interval){
repeat {
cbc_time=rpwe(1, rate=cbc_hazard*x, tchange=time_interval)
if(cbc_time>y & cbc_time<=89) {
return(cbc_time)
break
}
}
}
rr.test=c(rep(1.5,15000), rep(2,15000), rep(2.25,10000), rep(2.5,10000))
age_entry=c(rep(35,10000), rep(45,30000), rep(65,10000))
cbc.age=unlist(mapply(cbc.time, rr.test,age_entry,MoreArgs =
list(cbc_hazard=cbc.hazard.sim,time_interval=time_breaks)))
我也试过了&#39; rpexp&#39;功能来自&#39; msm&#39;包裹,但没有运气!
提前致谢。