[英]Generate random numbers from mixture and put outcome of each distribution in sequence
我想将随机数来自向量中的第一个伽玛[gamma with shape=2 , rate=4]
[gamma with shape=3 , rate=4]
和另一个向量中的第二个伽玛[gamma with shape=3 , rate=4]
x <- rep(0,20)
for (i in 1:20){
U[i] <- runif(1,0,1)
if( U[i] < 0.8){
x[i] <- rgamma(1,shape=2,rate=4)
} else {
x[i] <- rgamma(1,shape=3,rate=4)
}
}
您可以尝试(如果我明白了您的意思):
rgamma(20,shape=sample(2:3,20,replace=TRUE,prob=c(0.8,0.2)),rate=4)
您的shape
是从2:3
sample
d,其中2的概率为80%。 然后,由于它的向量化性质,您可以只调用一次rgamma
。
如果需要两个不同的向量,则可以:
nShape2<-rbinom(1,20,.80)
vecShape2<-rgamma(nShape2,shape=2,rate=4)
vecShape3<-rgamma(20-nShape2,shape=3,rate=4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.