简体   繁体   English

从定义范围 [min, max] 的泊松分布生成随机数

[英]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.

相关问题 当大小是R中的泊松随机变量的数目时,如何从指数分布中生成随机数? - How to generate random number from exponential distribution when the sizes are the numbers of Poisson random variable in R? 从指数分布生成随机数 - Generate random numbers from an exponential distribution 如何通过 R 中的平均参数化从 COM-POISSON 分布中有效地生成随机数? - How to efficiently generate random number from COM-POISSON distribution via mean parametrization in R? 从一定范围内的分布中抽取随机数 - Draw random numbers from distribution within a certain range 模拟从R中的截断逻辑分布生成随机数 - Simulation to generate random numbers from a truncated logistic distribution in R 如何从总和为1的指数分布中生成随机数(概率) - how to generate random numbers (probabilities) from exponential distribution that sum up to 1 从混合生成随机数,并将每个分布的结果按顺序排列 - Generate random numbers from mixture and put outcome of each distribution in sequence 如何从负二项分布生成n个随机数? - How to generate n random numbers from negative binomial distribution? 在 R 中生成卡方分布的随机数 - Generate random numbers of a chi squared distribution in R 生成指定范围内 5 的倍数的数字的正态分布 - Generate normal distribution of numbers that are multiples of 5 with in a specified range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM