Skip to content

Instantly share code, notes, and snippets.

@dhicks
Created October 13, 2023 16:49
Show Gist options
  • Save dhicks/a237c23c45d08821ed75947352d95c1d to your computer and use it in GitHub Desktop.
Save dhicks/a237c23c45d08821ed75947352d95c1d to your computer and use it in GitHub Desktop.
Compare two approaches to managing Q&A queues
library(tidyverse)
## Parameters ----
## How many people have questions?
n_questions = 30
## How many questions will actually get answered?
max_questions = 15
## How many times to run the simulation?
N = 500
set.seed(2023-10-13)
#' Draw a random set of "questions"
#'
#' "Questions" are represented by their quality, drawn from a distribution of interest
#' @param n Number of questions to draw
#' @param distf Distribution of question quality, as a random deviate function such as `runif` or `rnorm`
#' @param ... Arguments passed to the distribution function
#' @returns Vector of quality values
rquestions = function(n, distf = runif, ...) {
distf(n, ...)
}
rquestions(n_questions)
## Simulate Q&A: Default approach ----
qa_default = function(n_questions, max_questions, N) {
one_sim = function(n_questions, max_questions) {
dataf = tibble(quality = rquestions(n_questions)) |>
slice_head(n = max_questions)
mean(dataf$quality)
}
map_dbl(1:N, one_sim)
}
default = qa_default(n_questions, max_questions, N)
summary(default)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.001636 0.224704 0.459936 0.480693 0.729205 0.999136
## Simulate Q&A: Randomize approach ----
qa_random = function(n_questions, max_questions, N) {
one_sim = function(n_questions, max_questions) {
dataf = tibble(quality = rquestions(n_questions)) |>
slice_sample(n = max_questions)
mean(dataf$quality)
}
map_dbl(1:N, one_sim)
}
random = qa_random(n_questions, max_questions, N)
summary(random)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.005136 0.253719 0.493340 0.492655 0.726744 0.998494
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment