概率、样本函数 - 区间
Probability, sample function - intervals
示例函数有问题。我有一个错误,概率数不正确。我可以用另一种方式使用概率吗?我不知道这个函数是按时间间隔工作的。
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)
你需要有一个与每个值相关联的概率,我不知道如何将概率分配给一个区间,所以“手工”做它可能是这样的:
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)
效率不高,因为您需要手动计算每个区间的大小,可能有更好的方法。
prob 参数可以是长度 1 或 x 的每个元素一个值。 OL_x
是一个包含 109 个元素的向量,因为 :
整数序列运算符会扩展您的值。不太确定您要创建的是什么,但是如果您在从描述的概率呈现的值中提取 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
示例函数有问题。我有一个错误,概率数不正确。我可以用另一种方式使用概率吗?我不知道这个函数是按时间间隔工作的。
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)
你需要有一个与每个值相关联的概率,我不知道如何将概率分配给一个区间,所以“手工”做它可能是这样的:
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)
效率不高,因为您需要手动计算每个区间的大小,可能有更好的方法。
prob 参数可以是长度 1 或 x 的每个元素一个值。 OL_x
是一个包含 109 个元素的向量,因为 :
整数序列运算符会扩展您的值。不太确定您要创建的是什么,但是如果您在从描述的概率呈现的值中提取 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