Pattern drawing that changes the shape of the mother pattern

Background:

I am trying to change the shape of a bar graph obtained from a large Initial sample obtained using Initial = rbeta(1e5, 2, 3)

. In particular, I want the modified version of the original large sample to have 2 additional smaller (in height) "humps" (ie 2 more peaks of lower height in addition to the one that existed in the original large sample).

Coding:

I am wondering how to manipulate sample()

(possibly using its argument prob

) in base R so that this command samples so that the two extra humps are around ".5" and ". 6" on the x-axis?

Here is my current R code:

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


source to share


1 answer


You can tweak the density distribution by combining it with the beta distribution with the desired modes for a smoother tuning.

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)

      

The effects on the histogram are subtle ...

hist(Final)

      



... but more obvious in the density plot.

plot(density(Final))

      

Obviously, all settings are arbitrary. Please don't do scary things with your strength.

+3


source







All Articles