简体   繁体   English

概率,样本 function - 间隔

[英]Probability, sample function - intervals

I have a problem with the sample function.我对样品 function 有疑问。 I have an error that incorrect number of probabilities.我有一个错误,即概率数不正确。 Can I use probability in another way?我可以用另一种方式使用概率吗? I don't know that this function works on intervals.我不知道这个 function 是否适用于间隔。

OL_x = c(15.0:47.0,0.0:15.0,47:80,80:105)
x = sample(OL_x,1000,replace = TRUE,prob = c(0.60,0.22,0.13,0.05) )+ runif(1000,0,1)

You need to have a probability associated with each value, i don't know a way to assign a probability to an interval, so doing it "by hand" could be like:你需要有一个与每个值相关联的概率,我不知道如何将概率分配给一个区间,所以“手动”进行可能是这样的:

probs = c(rep(0.60, 48-15), rep(0.22,16-0), rep(0.13, 81-47), rep(0.05, 106-80))

x = sample(OL_x, 1000, replace = TRUE, prob = probs) + runif(1000,0,1)

This is not much efficient because you need to calculate the size of each interval by hand, there are probably better ways of doing this.这效率不高,因为您需要手动计算每个间隔的大小,可能有更好的方法来做到这一点。

The prob argument can be length 1 or one value for each element of x. prob 参数可以是长度 1 或 x 的每个元素的一个值。 OL_x is a vector with 109 elements, since the : integer sequence operator expands out your values. OL_x是一个包含 109 个元素的向量,因为: integer 序列运算符扩展了您的值。 Not quite sure what you are trying to create, but if you are after 1000 values drawn from the values presented with the probabilities described, try:不太确定您要创建什么,但如果您从描述的概率值中提取了 1000 个值,请尝试:

# keep groups separate as a list
OL_x = list(15.0:47.0,0.0:15.0,47:80,80:105)
# number of values in each group
vapply(X = OL_x, FUN = length, FUN.VALUE = 0L)
# [1] 33 16 34 26
# create 109 probabilities
rep(c(0.60,0.22,0.13,0.05), times = vapply(X = OL_x, FUN = length, FUN.VALUE = 0L))
#  [1] 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60
# [14] 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.60
# [27] 0.60 0.60 0.60 0.60 0.60 0.60 0.60 0.22 0.22 0.22 0.22 0.22 0.22
# ...
# create 1000 samples 
x = sample(
    x = unlist(OL_x), 
    size = 1000, 
    replace = TRUE, 
    prob = rep(c(0.60,0.22,0.13,0.05), 
        times = vapply(X = OL_x, FUN = length, FUN.VALUE = 0L))
) + runif(1000,0,1)
head(x)
# [1] 18.826530 36.948981 15.366685  5.142625 47.659682 14.946690

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM