来自 R 中偏态分布的样本

Sample from a skewed distribution in R

我想从 R 中的偏态分布中抽样数字。假设我想从模式为 10 且 95% 的值介于 5 和 [=12= 之间的分布中抽样数字].

R中是否有类似于rnorm()runif()的函数可以从这种分布中生成随机数?

选择 log normal distributionμ = 2.415195σ = 0.3355733 将给出(大约)您的要求的分布。

mu <- 2.415195
sigma <- 0.3355733

N <- 10000000
nums <- rlnorm(N, mu, sigma)

大约 95% 个值介于 520 之间。

sum(5 < nums & nums < 20) / N
#> [1] 0.9500141

模式为10

ggplot(tibble(x = nums), aes(x)) +
  geom_density() +
  geom_vline(xintercept = 10, color = "red") +
  geom_vline(xintercept = c(5, 20), color = "blue")


我使用 optimize 获得了这些参数。

从任何 σ,我们可以计算出 μ 给我们的众数 10,因为众数是:

所以我们想找到什么 σ 让我们最接近 520 之间的值的 95%。这可以根据 cdf(20)cdf(5) 之间的差值计算得出。对数正态分布的 CDF 为:。 (plnorm 在 R 中)。

f <- function(sigma) {
  mu <- log(10) + sigma^2
  
  abs(plnorm(20, mu, sigma) - plnorm(5, mu, sigma) - 0.95)
}

optimize(f, lower = 0, upper = 1)
#> $minimum
#> [1] 0.3355733
#> 
#> $objective
#> [1] 1.160349e-05