crp.r (565B)
1 crp = function(n, a) { 2 restaurant = data.frame(table = 1, customers = 1) 3 for (j in seq(n - 1)) { 4 restaurant = arrival(restaurant, a) 5 } 6 restaurant 7 } 8 9 arrival = function(r, a) { 10 p = 1 - a / (sum(r$customers) + a) 11 if (rbinom(1, 1, p)) { 12 join_table(r, a) 13 } else { 14 start_table(r) 15 } 16 } 17 18 join_table = function(r, a) { 19 probs = r$customers / sum(r$customers) 20 table = sample(1:nrow(r), size = 1, prob = probs) 21 r[table, 'customers'] = r[table, 'customers'] + 1 22 r 23 } 24 25 start_table = function(r) { 26 rbind(r, c(nrow(r) + 1, 1)) 27 } 28