Why Outcome Encoding Deserves an Audit
A binary-outcome model can only use the information that reaches its response variable. A value coded as 0 represents an observed non-event; it does not automatically represent a missing response, an unknown status, or an unrecorded trial. Similarly, a grouped binomial response is not just a success count: it also specifies how many trials produced that count. Errors in either piece change the data supplied to the model.
This tutorial concerns representing binary responses and binomial denominators in R. The examples use hypothetical records. They focus on data structure and verification, not on fitting a particular GLMM.
Choose the Representation From the Observation Unit
For individual records, one row represents one observed trial and the response takes the values 0 or 1. For grouped records, one row represents a set of trials and the response must preserve both the number of successes and the number of trials. In R, a clear grouped representation is cbind(successes, failures). For example, if 7 of 10 trials succeed, the two columns are 7 and 3, not 7 and 10.
R also accepts a binomial proportion as the response when the number of trials is supplied as a weight. For 7 successes in 10 trials, the response is 0.7 and the corresponding trial weight is 10. This is a different data representation of the same grouped binomial information; the proportion alone is not enough. Without the trial count, 0.7 does not distinguish 7 of 10 from 70 of 100.
A grouped row is appropriate only when its trials share the same modeled conditions, including the relevant predictors and random-effect grouping. Aggregating across people, occasions, or clusters that need distinct model contributions can erase information. If repeated binary responses are represented in long format, retain rows at the level required by the study design rather than collapsing them merely because their outcome values match.
Define and Check the Event Before Coding
Before recoding, state exactly what counts as the event. For example, a hypothetical follow-up record might use the labels “improved” and “not improved.” If improvement is the event, then improved maps to 1 and not improved maps to 0. Reversing those values changes the modeled event and therefore the interpretation of any later coefficient.
Do not map every value other than the event label to 0. A spelling error, an unexpected category, or a missing status is not evidence of a non-event. Inspect the observed labels and define an explicit mapping; then check that every nonmissing source value was either mapped as intended or flagged for review. In R, an explicit nested ifelse expression can create the binary values, but unmatched labels should be mapped to missing for audit rather than silently treated as 0. A two-level factor can also be used as a binomial response, but its level order matters: R treats the first level as failure and the second as success. Explicit numeric coding makes the intended event easier to verify.
Worked Example: Coding Individual Follow-Up Outcomes
Suppose a hypothetical dataset has four follow-up records with status labels improved, not improved, improved, and not improved. Improvement is the event. The intended response values, in record order, are therefore 1, 0, 1, 0.
In R, an explicit mapping is conceptually: response = ifelse(status == "improved", 1L, ifelse(status == "not improved", 0L, NA_integer_)). Before using this response, inspect the distinct status values and check for missing values. For these four stated records, the mapping yields 1, 0, 1, 0; the total number of observed trials is 4 and the success count is \(1+0+1+0=2\).
The checks agree: the response contains only 0 and 1, there are two successes, and there are two failures. If a fifth record had status “unknown,” this mapping would produce a missing value, not a 0. That record would require a documented data decision rather than an automatic claim that no improvement occurred.
Grouped Counts and Their Binomial Meaning
For a group with \(y\) successes among \(m\) trials, the number of failures is \(m-y\). If trials have a common success probability \(p\) under the model and are conditionally independent given the modeled information, then the grouped success count has a binomial distribution. Its probability of observing \(y\) successes is:
This explains why the denominator is part of the response. Changing \(m\) changes both the possible count values and the probability of the observed count. R’s cbind(successes, failures) form records the two counts directly. The equivalent proportion form records \(y/m\) and gives the trial count \(m\) as the weight; it is not a license to treat a proportion as though it came from one trial.
A Short Justification for the Two Grouped Forms
Let \(r=y/m\) be the observed proportion. Substituting \(y=mr\) and \(m-y=m(1-r)\) into the binomial likelihood gives:
Thus the proportion \(r\) together with trial count \(m\) carries the same success and failure exponents as the counts \(y\) and \(m-y\). The binomial coefficient \(\binom{m}{y}\) does not depend on \(p\), so it does not change the likelihood’s dependence on \(p\). The two encodings preserve the same binomial response information when the proportion is paired with its correct trial count. Omitting that count loses information about the amount of binomial data.
Worked Example: Encoding Grouped Successes and Failures
A hypothetical clinic table summarizes three sites. Site A records 8 successes in 10 trials, Site B records 3 in 5, and Site C records 0 in 4. For each site, calculate failures as denominator minus successes.
For Site A, failures are \(10-8=2\), so the grouped response is cbind(8, 2). For Site B, failures are \(5-3=2\), giving cbind(3, 2). For Site C, failures are \(4-0=4\), giving cbind(0, 4). The corresponding proportions and denominators are 0.8 with 10, 0.6 with 5, and 0 with 4.
The arithmetic checks are \(8+2=10\), \(3+2=5\), and \(0+4=4\). Across all sites, there are \(8+3+0=11\) successes and \(2+2+4=8\) failures, for \(11+8=19\) trials. These totals should agree with the underlying records if all three site summaries cover the same set of trials.
In R, the grouped response can be formed with cbind(successes, failures), where the first column is successes and the second is failures. If instead the response is the three proportions, use trial counts 10, 5, and 4 as the corresponding weights. The all-zero success count at Site C is valid: it means four observed failures, not zero trials.
Audit the Counts Against the Study Records
A useful audit checks both the values and the meaning of the fields. For individual data, verify that the coded response contains only 0 and 1 among observed outcomes, that each value corresponds to the declared event definition, and that the number of response records matches the intended observation unit. Compare frequencies from the coded response with a direct tabulation of the source labels.
For grouped data, verify that successes and failures are nonnegative whole-number counts, and that each row satisfies successes plus failures equals the denominator recorded by the study. If the denominator is stored separately, compare it rather than assuming it was calculated from the two counts. Also check that proportions are between 0 and 1 and agree with successes divided by trials, allowing only expected rounding if the source stored rounded proportions.
In R, logical checks can be phrased as tests such as all(successes >= 0), all(failures >= 0), and all(successes + failures == trials). The equality check is meaningful only if the fields have first been confirmed to refer to the same set of trials. Count variables should also be checked for integer values; a fractional count generally signals a parsing, aggregation, or unit problem, not a legitimate number of binary trials.
Worked Example: Finding a Denominator Mismatch
A hypothetical summary row reports 12 successes, 5 failures, and a denominator of 20. The counts imply \(12+5=17\) trials, which does not equal the recorded denominator of 20. The row is inconsistent by \(20-17=3\) trials.
Do not resolve this by changing the denominator to 17 or adding three failures without checking the source records. The discrepancy might reflect unrecorded outcomes, a denominator defined differently from the response counts, or a data-entry error. Trace the row to the study records and establish which trials the denominator and counts represent.
Suppose that audit confirms the denominator should be 17. Then the corrected grouped pair remains 12 successes and 5 failures, with \(12+5=17\). If instead the source confirms 20 eligible trials but only 17 outcome records, the three absent outcomes are not automatically failures. The response representation must reflect the observed outcome information and the study’s documented data rules.
Common Mistakes, Diagnostics, and Limits
- Using the denominator as the failure count. For 7 successes in 10 trials, cbind(7, 10) represents 7 successes and 10 failures, or 17 trials. The correct pair is cbind(7, 3).
- Dropping the trial count from a proportion. A response of 0.7 alone does not distinguish 7 of 10 from 70 of 100. Retain the corresponding count of trials.
- Treating missing or unknown as 0. A 0 is an observed non-event. Missing status is not an observed non-event, so keep it distinguishable and investigate it under the study’s data rules.
- Combining incompatible trials. Aggregation is not appropriate when the grouped trials differ in modeled predictors or random-effect grouping that must be represented separately.
- Ignoring impossible values. Negative counts, fractional trial counts, successes greater than trials, or proportions outside 0 to 1 signal a problem to resolve before analysis.
These checks can detect coding and denominator inconsistencies; they cannot establish that the study’s event definition is scientifically appropriate or that every record was captured correctly. They also do not decide how missing outcomes should be handled. The encoding must be checked against the protocol and source records, and the chosen observation unit must preserve the design information needed for the eventual model.
The main alternatives are individual 0/1 rows and grouped counts or proportions with trial weights. Individual rows preserve trial-level covariates and identifiers, while grouped rows can be compact when trials truly share the same modeled conditions. Choosing the compact form solely for convenience is risky: it can discard distinctions needed by the model. Conversely, storing a grouped count as an unexplained proportion discards the denominator and is not a complete binomial response.
Check Your Understanding
Answer each question, then compare with the brief solutions.
- A hypothetical group has 9 successes among 12 trials. What success/failure pair should be supplied, and what proportion and trial weight represent the same information?
- Why is a response proportion of 0.4 incomplete without a trial count?
- A source field contains “yes,” “no,” and “not recorded.” If “yes” is the event, what should the three values map to?
- A grouped row reports 4 successes, 3 failures, and 8 trials. What audit discrepancy appears?
- When is it inappropriate to combine individual binary records into one grouped row?
Solutions. (1) Failures are \(12-9=3\), so use the pair (9, 3), or proportion \(9/12=0.75\) with trial weight 12. (2) A proportion of 0.4 could be 2 of 5, 4 of 10, or another count ratio; the denominator records the number of trials. (3) Map “yes” to 1 and “no” to 0; retain “not recorded” as missing or otherwise flagged, not as 0. (4) The counts sum to \(4+3=7\), not 8, leaving a one-trial discrepancy that must be traced to the records. (5) Do not aggregate when trials differ in relevant predictors or random-effect units that need separate representation.