绘制改变母样形状的样例

Drawing a sample that changes the shape of the mother sample

背景:

我正在尝试修改使用 Initial = rbeta(1e5, 2, 3) 获得的“Initial”大样本生成的直方图的形状。具体来说,我希望初始大样本的修改版本有 2 个额外的小(高度)"humps"(即另一个除了初始大样本中存在的峰外,还有 2 个较小高度的峰)。

编码问题:

我想知道如何在 R base 中操作 sample()(可能使用其 prob 参数),以便此命令以两个额外的 驼峰 在 X 轴上围绕 ".5"".6"

这是我当前的 R 代码:

Initial = rbeta(1e5, 2, 3) ## My initial Large Sample

hist (Initial)             ## As seen, here there is only one "hump" say near 
                            # less than ".4" on the X-Axis


Modified.Initial = sample(Initial, 1e4 ) ## This is meant to be the modified version of the
                                          # the Initial with two additional "humps"

hist(Modified.Initial)          ## Here, I need to see two additional "humps" near  
                                 # ".5" and ".6" on the X-Axis

您可以通过将密度分布与具有所需模式的 beta 分布相结合来调整密度分布,以进行平滑调整。

set.seed(47)

Initial = rbeta(1e5, 2, 3)
d <- density(Initial)

# Generate densities of beta distribution. Parameters determine center (0.5) and spread.
b.5 <- dbeta(seq(0, 1, length.out = length(d$y)), 50, 50)
b.5 <- b.5 / (max(b.5) / max(d$y))    # Scale down to max of original density

# Repeat centered at 0.6
b.6 <- dbeta(seq(0, 1, length.out = length(d$y)), 60, 40)
b.6 <- b.6 / (max(b.6) / max(d$y))

# Collect maximum densities at each x to use as sample probability weights
p <- pmax(d$y, b.5, b.6)

plot(p, type = 'l')

# Sample from density breakpoints with new probability weights
Final <- sample(d$x, 1e4, replace = TRUE, prob = p)

对直方图的影响很微妙...

hist(Final)

...但在密度图中更明显。

plot(density(Final))

显然所有的调整都是随意的。请不要用你的力量做可怕的事情。