This function calculates the beta-binomial distribution parameters (alpha, beta, mean, variance) for subsets of data grouped by a specified factor. It applies the `betaCoefficients` function to each group separately, aggregating the results into a single data frame. This is particularly useful for analyzing heterogeneity in success probabilities across different groups within a dataset.
betaByGroup(x, group, n)
A data frame where each row contains the beta-binomial distribution parameters (alpha `a`, beta `b`, mean `m`, variance `var`) for a group, along with the group identifier. The columns are named `a`, `b`, `m`, `var`, `n`, and `group`, with each row corresponding to a distinct group in the input.
A vector of non-negative integers representing the number of successes in trials for the entire dataset.
A factor or similar object that divides `x` into groups. Each element of `x` is associated with a group indicated by the corresponding element in `group`.
The maximum number of trials, assumed to be the same for all groups.
The function first identifies unique groups in the `group` argument and then iterates over these groups. For each group, it extracts the subset of `x` corresponding to that group and computes the beta-binomial distribution parameters using the `betaCoefficients` function. The results are compiled into a matrix that is then converted into a data frame for easier manipulation and interpretation.
x <- elfe$raw
group <- elfe$group
n <- 26
betaByGroup(x, group, n)
Run the code above in your browser using DataLab