从分布中抽取 R 中峰值周围 10% 的值
Sampling from a distribution the 10% of values around the peak value in R
我有一个包含 100 个值的向量。
bingo<-sample(0:5000, 100)
plot(density(bingo))
which.max(density(bingo)$y) # [1] 194
density(bingo)$x[194] # [1] 1507.085
我想对分布峰值 1507.085
周围 10% 的值进行采样。我可以实现吗?
非常感谢任何建议!
这取决于你所说的“周围”是什么意思。这里有几个选项:
set.seed(94)
bingo <- sample(0:5000, 100)
bingo <- sort(bingo)
bingo_mode <- density(bingo)$x[which.max(density(bingo)$y)]
idxMode <- match(TRUE, bingo > bingo_mode)
# to sample from 5 before and 5 after the peak (or the top/bottom decile if the
# peak is in the 95th/5th percentile)
if (idxMode < 5) {
idxFrom <- 1
idxTo <- 10
} else if (idxMode + 4 > 100) {
idxFrom <- 91
idxTo <- 100
} else {
idxFrom <- idxMode - 5
idxTo <- idxMode + 4
}
sample(bingo[idxFrom:idxTo], 1)
#> [1] 2557
# to sample from the 10 nearest the peak
if (idxMode < 10) {
idxFrom <- 1
idxTo <- 20
} else if (idxMode + 9 > 100) {
idxFrom <- 81
idxTo <- 100
} else {
idxFrom <- idxMode - 10
idxTo <- idxMode + 9
}
sample(bingo[idxFrom:idxTo][order(abs(bingo_mode - bingo[idxFrom:idxTo]))[1:10]], 1)
#> [1] 2678
我有一个包含 100 个值的向量。
bingo<-sample(0:5000, 100)
plot(density(bingo))
which.max(density(bingo)$y) # [1] 194
density(bingo)$x[194] # [1] 1507.085
我想对分布峰值 1507.085
周围 10% 的值进行采样。我可以实现吗?
非常感谢任何建议!
这取决于你所说的“周围”是什么意思。这里有几个选项:
set.seed(94)
bingo <- sample(0:5000, 100)
bingo <- sort(bingo)
bingo_mode <- density(bingo)$x[which.max(density(bingo)$y)]
idxMode <- match(TRUE, bingo > bingo_mode)
# to sample from 5 before and 5 after the peak (or the top/bottom decile if the
# peak is in the 95th/5th percentile)
if (idxMode < 5) {
idxFrom <- 1
idxTo <- 10
} else if (idxMode + 4 > 100) {
idxFrom <- 91
idxTo <- 100
} else {
idxFrom <- idxMode - 5
idxTo <- idxMode + 4
}
sample(bingo[idxFrom:idxTo], 1)
#> [1] 2557
# to sample from the 10 nearest the peak
if (idxMode < 10) {
idxFrom <- 1
idxTo <- 20
} else if (idxMode + 9 > 100) {
idxFrom <- 81
idxTo <- 100
} else {
idxFrom <- idxMode - 10
idxTo <- idxMode + 9
}
sample(bingo[idxFrom:idxTo][order(abs(bingo_mode - bingo[idxFrom:idxTo]))[1:10]], 1)
#> [1] 2678