[英]Generate random numbers from a Poisson distribution with defined range [min, max]
I would like to draw 100 random numbers from a Poisson distribution with a mean of 0.5, ranging from 0 to 10.我想从泊松分布中抽取 100 个随机数,平均值为 0.5,范围从 0 到 10。
Using the rpois(n = 100, lambda = 0.50)
function, I can obtain the random numbers from the Poisson distribution, but I am unable to specify the min/max values.使用
rpois(n = 100, lambda = 0.50)
function,我可以从泊松分布中获得随机数,但我无法指定最小值/最大值。
Any way of doing this in R?在 R 中有什么方法可以做到这一点?
A bit more context: In a dataset, my dependent variable has the properties mentioned above (mean, min, max values), and I would like to estimate some models with simulated data for my dependent variable.更多上下文:在数据集中,我的因变量具有上述属性(平均值、最小值、最大值),我想用我的因变量的模拟数据估计一些模型。
Edit: My dependent variable (for which I want to simulate the random numbers in addition to the observed values) is neither truncated nor censored.编辑:我的因变量(除了观察值之外,我还想模拟随机数)既不截断也不删减。 However, I should note that its standard deviation is 1, ie, the variance is not equal to the mean of 0.5.
但是,我应该注意到它的标准差是 1,即方差不等于 0.5 的平均值。
Thanks!谢谢!
You have limited number of items to sample, just make probabilities to follow Poisson and sample您要采样的项目数量有限,只需按照泊松的概率进行采样即可
Along the lines顺势而为
l <- 0.5
x <- c(0,1,2,3,4,5,6,7,8,9,10)
p <- rep(0, length(x)) # probabilities
s <- 0.0
for (k in x) {
p[k+1] = l**k * exp(-l) / gamma(k+1)
s <- s + p[k+1]
}
p = p / s # normalization
print(sample(x, 1, replace=TRUE, prob=p))
print(sample(x, 1, replace=TRUE, prob=p))
print(sample(x, 1, replace=TRUE, prob=p))
print(sample(x, 1, replace=TRUE, prob=p))
print(sample(x, 1, replace=TRUE, prob=p))
print(sample(x, 1, replace=TRUE, prob=p))
print(sample(x, 1, replace=TRUE, prob=p))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.