【发布时间】:2020-10-10 17:48:59
【问题描述】:
a) 在一个 20 人的小组中,有 3 个姐妹。该组被分开在 随机分成两组,每组 10 个。姐妹们在 同一组? b) 编写 R 代码以使用仿真解决问题。
我在 B 部分有困难。我无法理解如何模拟这种情况
【问题讨论】:
标签: r combinations probability
a) 在一个 20 人的小组中,有 3 个姐妹。该组被分开在 随机分成两组,每组 10 个。姐妹们在 同一组? b) 编写 R 代码以使用仿真解决问题。
我在 B 部分有困难。我无法理解如何模拟这种情况
【问题讨论】:
标签: r combinations probability
这似乎有点像家庭作业转储 - 最好看到你付出一些努力,但你似乎不知道从哪里开始。
我已注释以下代码以显示一种可能的方法
# Make the sample reproducible
set.seed(69)
# Calculate the probability that the brothers will be in the same group
prob_calculated <- 20/20 * 9/19 * 8/18
# Create our sample space. Here, 1 stands for one of the brothers,
# a zero means a non-brother:
space <- c(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
# Create an empty vector to store the results of our simulation
vec <- logical()
# For 10,000 simulations, we will draw 10 numbers from space without replacement.
# If the sum of the sample is three or zero, the brothers are all in the same group.
# We store the result of each simulation in the vector vec at position i.
for(i in 1:10000) {
samp <- sample(space, 10)
vec[i] <- sum(samp) == 0 | sum(samp) == 3
}
# The proportion of TRUE results in vec estmates the probability
prob_simulated <- length(which(vec))/10000
# Now we present the results for comparison
c(prob_calculated = prob_calculated, prob_simulated = prob_simulated)
#> prob_calculated prob_simulated
#> 0.2105263 0.2149000
由reprex package (v0.3.0) 于 2020 年 10 月 10 日创建
【讨论】: