gpt4 book ai didi

r - 绘制改变母样形状的样例

转载 作者:行者123 更新时间:2023-12-01 13:35:46 24 4
gpt4 key购买 nike

背景:

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

编码问题:

我想知道如何在 R base 中操作 sample()(可能使用其 prob 参数),以便此命令以两个附加 < strong>驼峰在 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

最佳答案

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

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

关于r - 绘制改变母样形状的样例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43440218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com