Sample Quality of Life (QoL) Scores from a Multinomial Distribution
Source:R/qol.R
sample_qol_scores.RdThis function generates a vector of random Quality of Life (QoL) scores by sampling from a discrete probability distribution. It simulates outcomes where each sample has a specific probability of falling into one of five predefined QoL change categories.
Arguments
- prob_vector
A numeric vector containing exactly 5 probabilities, which **must sum to 1**. Each element corresponds to the probability of the QoL scores `[-2, -1, 0, +1, +2]`, respectively.
- n_samples
An integer specifying the total number of samples to generate (i.e., the length of the output vector).
Value
A numeric vector of length `n_samples` containing the sampled QoL scores. Each element in the vector will be one of the values from `[-2, -1, 0, 1, 2]`.
Details
This function is a robust wrapper around the base R `sample()` function. It's specifically configured for sampling with replacement from the discrete set of scores `[-2, -1, 0, 1, 2]`. It includes validation to ensure the input probability vector is correctly formatted. This is useful for simulating patient cohorts in health economic models or clinical trial simulations.
Examples
# --- 1. Define a probability vector ---
# This vector represents a "No Difference / Marginal Benefit" scenario.
# P(-2)=0.1, P(-1)=0.15, P(0)=0.5, P(+1)=0.15, P(+2)=0.1
my_probs <- c(0.1, 0.15, 0.5, 0.15, 0.1)
# --- 2. Set the number of samples ---
# For example, simulate a cohort of 1000 patients.
num_patients <- 1000
# --- 3. Generate the sampled scores ---
qol_draws <- sample_qol_scores(prob_vector = my_probs, n_samples = num_patients)
# --- 4. View the results ---
# Look at the first few sampled scores
head(qol_draws)
#> [1] 0 0 0 0 1 2
# Check the distribution of the generated samples.
# It should approximate the input probabilities.
table(qol_draws) / num_patients
#> qol_draws
#> -2 -1 0 1 2
#> 0.107 0.165 0.473 0.158 0.097