2017-04-16 75 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)) 

顯然所有的調整是任意的。請不要用你的力量做可怕的事情。