如何在 R 中生成具有相同均值、标准差但形状或分布截然不同的数据?

How do I generate data that have the same mean, standard deviation but very different shapes or distributions in R?

如何生成均值、标准差相同但形状或分布截然不同的数据?以下是 jthetzel 关于如何创建具有相同均值和标准差的数据的一个答案,但我需要有关如何使其不正常的帮助。理想情况下,我想要具有相同均值和标准差的不同形状的图形。

#install.packages("Runuran")
library(Runuran)
## Discrete normal distribution bounded between 0 and 100
# Create UNU.RAN discrete distribution object
discrete <- unuran.discr.new(pv = dnorm(0:100, mean = 50, sd = 25), lb = 0, ub = 100)

# Create UNU.RAN object using the Guide-Table Method for Discrete Inversion
unr <- unuran.new(distr = discrete, method = "dgt")

# Generate random variates from the UNU.RAN object
d2 <- ur(unr = unr, n = 1000)

summary(d2)
sd(d2)
hist(d2)

您可以采用从任何分布生成的任何一组数字,translate/scale 它具有所需的标准偏差。在 vanilla R 中,这些函数看起来像 r* -- runifrnormrbetarpoisrlnorm 等。参见 ?Distributions获取支持的 built-in 发行版列表。

即使是没有有限方差的分布也可能以这种方式被滥用。

d1 <- rcauchy(1000)
d2 <- 12 * (d1 - mean(d1)) / sd(d1) + 5
sd(d2)   # will be 12
mean(d2) # will be 5

为方便起见,这里有一个函数可以执行此操作

shoehorn <- function(d, desired.mean, desired.sd) { 
  desired.sd * (d - mean(d)) / sd(d) + desired.mean
}