r中的递归采样

| 我正在尝试以7年以下的累积概率模拟死亡:
tab <- data.frame(id=1:1000,char=rnorm(1000,7,4))

cum.prob <- c(0.05,0.07,0.08,0.09,0.1,0.11,0.12)
如何根据
cum.prob
中的累积概率从
tab$id
中进行采样而不进行矢量化处理?从yr 1采样的id不一定要在yr 2再次采样。因此Hence3ѭ将不起作用。有可能将其向量化吗? // M     
已邀请:
这是一种方法:首先获得给定个人在给定年份死亡的概率为
probYrDeath
,即
probYrDeath[i] = Prob( individual dies in year i )
,其中
i=1,2,...,7
probYrDeath <- c(diff(c(0,cum.prob)).
现在根据
probYrDeath
中的概率,从序列1:8中随机生成1000个“死亡年”的样本,并进行替换,并增加到7年不死的概率:
set.seed(1) ## for reproducibility
tab$DeathYr <- sample( 8, 1000, replace = TRUE, 
                       prob = c(probYrDeath, 1-sum(probYrDeath)))
我们将\“ \'DeathYr = 8 \'\”解释为\“在7年内不死亡\”,并提取
tab
的子集,其中
DeathYr != 8
tab_sample <- subset(tab, DeathYr != 8 )
您可以验证每年的累计死亡比例是否接近
cum.prob
中的值:
> cumsum(table(tab_sample$DeathYr)/1000)
    1     2     3     4     5     6     7 
0.045 0.071 0.080 0.094 0.105 0.115 0.124 
    
这对您有用吗:
prob.death.per.year<-c(1-cum.prob[length(cum.prob)], cum.prob - c(0, cum.prob[-length(cum.prob)]))
dead.in.years<-as.vector(rmultinom(1, length(tab$id),prob.death.per.year))[-1]
totsamp<-sum(dead.in.years)
data.frame(id=sample(tab$id, totsamp), dead.after=rep(seq_along(dead.in.years), dead.in.years))
根据所需结果的形式,可以更改最后一步。     

要回复问题请先登录注册