【问题标题】:Drawing a sample that changes the shape of the mother sample绘制改变母样本形状的样本
【发布时间】:2017-09-12 09:45:20
【问题描述】:

背景:

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

编码问题:

我想知道如何在 R 基础中操作 sample()(可能使用它的 prob 参数),以便此命令以另外两个 驼峰 围绕 的方式进行采样".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

【问题讨论】:

    标签: r random resampling statistics-bootstrap


    【解决方案1】:

    您可以通过将密度分布与具有所需模式的 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))
    

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

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 2014-10-06
      • 1970-01-01
      • 2013-07-25
      • 2020-02-02
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多