循环一次完成所有事情

sta*_*oob 4 loops r aggregate-functions dplyr

我正在尝试模拟以下“游戏:

  • 有100个单位的人口
  • 您随机抽取其中 10 个单位,记录您看到的单位的 ID,然后将它们放回总体中
  • 然后,您获取第二个样本,记录您在第二个样本中看到的单位的 ID 以及第一个样本,然后将第二个样本放回总体中
  • 重复此操作多次

我在 R 中编写了以下代码来执行上述过程:

library(dplyr)

var_1 = rnorm(100,10,10)
var_2 = rnorm(100,1,10)
var_3 = rnorm(100,5,10)
response = rnorm(100,1,1)

my_data = data.frame(var_1, var_2, var_3, response)
my_data$id = 1:100


results <- list()
results2<- list()

for (i in 1:100)
    
{
    
    iteration_i = i
    
    sample_i = my_data[sample(nrow(my_data), 10), ]
    
    
    results_tmp = data.frame(iteration_i, sample_i)
    
    results[[i]] <- results_tmp
    
}

results_df <- do.call(rbind.data.frame, results)

test_1 <- data.frame(results_df %>% 
    group_by(id) %>% 
    filter(iteration_i == min(iteration_i)) %>% 
    distinct)


summary_file = data.frame(test_1 %>% group_by(iteration_i) %>% summarise(Count = n()))

cumulative = cumsum(summary_file$Count)

summary_file$Cumulative = cumulative

summary_file$unobserved = 100 - cumulative
Run Code Online (Sandbox Code Playgroud)

结果看起来像这样:

> summary_file
   iteration_i Count Cumulative unobserved
1            1    10         10         90
2            2     8         18         82
3            3     9         27         73
4            4     8         35         65
5            5     6         41         59
6            6     5         46         54
7            7     7         53         47
8            8     7         60         40
9            9     4         64         36
10          10     3         67         33
11          11     4         71         29
12          12     4         75         25
13          13     1         76         24
14          14     4         80         20
15          15     1         81         19
16          16     2         83         17
17          17     2         85         15
18          18     1         86         14
19          20     1         87         13
20          22     1         88         12
21          23     2         90         10
22          24     1         91          9
23          25     1         92          8
24          27     2         94          6
25          28     1         95          5
26          30     1         96          4
27          35     1         97          3
28          37     1         98          2
29          44     1         99          1
30          46     1        100          0
Run Code Online (Sandbox Code Playgroud)

我现在想多次重复这个“游戏”。

  • 我想保留每个“游戏”的“summary_file”(例如summary_file_1、summary_file_2、summary_file_3等)

  • 然后,我想创建一个“总计”摘要文件,显示每个游戏中观察所有单元所需的迭代次数。

这个total_summary_file看起来像这样:

 game_id iterations_required
1  game_1                  47
2  game_2                  45
3  game_3                  44
4  game_4                  42
5  game_5                  42
Run Code Online (Sandbox Code Playgroud)

目前,我只是多次复制/粘贴我之前的代码并存储结果,然后我将所有内容附加在末尾并计算摘要统计数据 - 但我正在尝试找到一种“循环循环”并立即执行所有操作的方法。我不知道是否可以"results_df_i <- do.call(rbind.data.frame, results_i)"在循环中引入类似的命令并同时有效地创建所有内容,而不是手动复制/粘贴先前的循环。

All*_*ron 6

你这样做的效率比它应有的要低得多。比如说,要从 1:100 集合中获取 100 个重复样本,其中 10 个样本(有替换),我们可以这样做replicate(100, sample(100, 10, TRUE))

然后,我们可以将其强制转换为向量,并计算向量中每 10 个条目的唯一值的数量,直到达到 100。这为我们提供了耗尽样本所需的迭代次数。

如果我们将其放入 an 中sapply,我们甚至不需要显式循环,这意味着我们可以在一次调用中创建结果数据帧:

set.seed(1)

n_games <- 10

results <- data.frame(game_id = paste("game", seq(n_games), sep = "_"),
           iterations_required = sapply(seq(n_games), function(x) {
  samp <- c(replicate(100, sample(100, 10, TRUE)))
  sum(sapply(1:100 * 10, function(n) length(unique(samp[1:n]))) < 100)
  }))

results
#>    game_id iterations_required
#> 1   game_1                  59
#> 2   game_2                  44
#> 3   game_3                  54
#> 4   game_4                  59
#> 5   game_5                  57
#> 6   game_6                  58
#> 7   game_7                  96
#> 8   game_8                  60
#> 9   game_9                  71
#> 10 game_10                  33
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2022 年 6 月 11 日创建(v2.0.1)