2017-04-16 44 views
3

背景:绘制改变母体样品的形状的样品

我试图修改直方图的形状导致从“初始”大样品获得使用Initial = rbeta(1e5, 2, 3)。具体而言,我想初始大样本的修改版本以具有2附加的较小(高度)“驼峰”(即,除了在初始大样品中存在的彼此2较小高度的峰) 。

编码问题:

我想知道如何操纵sample()(也许使用其prob参数)中的R基使得以这样的方式该命令的样品,这两个附加隆起约为“ .5"。” 6"在X轴?

这是我的当前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 

回答

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

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