在 R 中模拟遵循分布曲线的值
Simulate Values that follow a Distribution Curve in R
我想模拟遵循不同分布的需求值(例如上文:线性开始>指数>invlog>等)我对概率分布的概念有点困惑,但我认为我可以使用 rnorm、rexp、 rlogis 等。有什么办法可以做到吗?
我认为可能是这样,但在 R 中:Generating smoothed randoms that follow a distribution
如果您知道要使用的分布及其参数,使用 rnorm()
、rexp()
等从 R 中常用的概率分布模拟随机值是相当简单的。例如,rnorm(10, mean=5, sd=2)
returns 10 来自均值为 5 且标准差为 2 的正态分布。
rnorm(10, mean = 5, sd = 2)
## [1] 5.373151 7.970897 6.933788 5.455081 6.346129 5.767204 3.847219 7.477896 5.860069 6.154341
## or here's a histogram of 10000 draws...
hist(rnorm(10000, 5, 2))
您可能对指数分布感兴趣 - 查看 hist(rexp(10000, rate=1))
了解相关信息。
最简单的解决方案是调查您感兴趣的概率分布及其在 R 中的实现。
仍然可以 return 从一些自定义函数中随机抽取,并且有一些技术可以做到这一点 - 但它可能会变得混乱。这是从 x^3 - 3x^2 + 4 区域定义的概率中随机抽取在零和 3 之间的一个非常粗略的实现。
## first a vector of random uniform draws from the region
unifdraws <- runif(10000, 0, 3)
## assign a probability of "keeping" draws based on scaled probability
pkeep <- (unifdraws^3 - 3*unifdraws^2 + 4)/4
## randomly keep observations based on this probability
keep <- rbinom(10000, size=1, p=pkeep)
draws <- unifdraws[keep==1]
## and there it is!
hist(draws)
## of course, it's less than 10000 now, because we rejected some
length(draws)
## [1] 4364
我想模拟遵循不同分布的需求值(例如上文:线性开始>指数>invlog>等)我对概率分布的概念有点困惑,但我认为我可以使用 rnorm、rexp、 rlogis 等。有什么办法可以做到吗?
我认为可能是这样,但在 R 中:Generating smoothed randoms that follow a distribution
如果您知道要使用的分布及其参数,使用 rnorm()
、rexp()
等从 R 中常用的概率分布模拟随机值是相当简单的。例如,rnorm(10, mean=5, sd=2)
returns 10 来自均值为 5 且标准差为 2 的正态分布。
rnorm(10, mean = 5, sd = 2)
## [1] 5.373151 7.970897 6.933788 5.455081 6.346129 5.767204 3.847219 7.477896 5.860069 6.154341
## or here's a histogram of 10000 draws...
hist(rnorm(10000, 5, 2))
您可能对指数分布感兴趣 - 查看 hist(rexp(10000, rate=1))
了解相关信息。
最简单的解决方案是调查您感兴趣的概率分布及其在 R 中的实现。
仍然可以 return 从一些自定义函数中随机抽取,并且有一些技术可以做到这一点 - 但它可能会变得混乱。这是从 x^3 - 3x^2 + 4 区域定义的概率中随机抽取在零和 3 之间的一个非常粗略的实现。
## first a vector of random uniform draws from the region
unifdraws <- runif(10000, 0, 3)
## assign a probability of "keeping" draws based on scaled probability
pkeep <- (unifdraws^3 - 3*unifdraws^2 + 4)/4
## randomly keep observations based on this probability
keep <- rbinom(10000, size=1, p=pkeep)
draws <- unifdraws[keep==1]
## and there it is!
hist(draws)
## of course, it's less than 10000 now, because we rejected some
length(draws)
## [1] 4364