绘制一个改变母样本形状的样本

时间:2017-04-16 17:48:33

标签: r random bootstrapping resampling

背景

我正在尝试修改使用Initial = rbeta(1e5, 2, 3)获得的“初始”大样本导致的直方图的形状。具体来说,我希望初始大样本的修改版本具有 2个更小的​​(高度)“驼峰”(即另外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

1 个答案:

答案 0 :(得分:3)

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

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))

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