The inverse matrix of a contrast matrix has a useful property where, after removing the row corresponding to the intercept, the reference level is the column index where each value in that column is (1) the same and (2) less than or equal to 0.
find_same_col(invmatrix)
Arguments
invmatrix: Inverse of the hypothesis matrix
Returns
Integer index of the reference level, if there is no reference level, NA is returned
Details
For a contrast matrix generated from an expression x, with number of levels n, consider the inverse matrices from solve(.contrasts_to_hypotheses(x)) below for different x:
contr.treatment: The first column contains all 0s, hence the reference level is the first level
contr.SAS: The last column contains all 0s, hence the reference level is the last level
scaled_sum_code: The first column contains all -1/n, hence the reference level is the first level
helmert_code: While the first column contains all negative numbers, they are not the same EXCEPT if n=2
Note that "reference level" here is defined as a level from which every other level is compared to via pairwise comparisons.