Bradley-Terry Models in R : The BradleyTerry2 Package

This is a short overview of the R add-on package BradleyTerry2 , which facilitates the speciﬁcation and ﬁtting of Bradley-Terry logit, probit or cauchit models to pair-comparison data. Included are the standard ‘unstructured’ Bradley-Terry model, structured versions in which the parameters are related through a linear predictor to explanatory variables, and the possibility of an order or ‘home advantage’ eﬀect or other ‘contest-speciﬁc’ eﬀects. Model ﬁtting is either by maximum likelihood, by penalized quasi-likelihood (for models which involve a random eﬀect), or by bias-reduced maximum likelihood in which the ﬁrst-order asymptotic bias of parameter estimates is eliminated. Also provided are a simple and eﬃcient approach to handling missing covariate data, and suitably-deﬁned residuals for diagnostic checking of the linear predictor.


Introduction
The Bradley-Terry model (Bradley and Terry 1952) assumes that in a 'contest' between any two 'players', say player i and player j (i, j ∈ {1, . . . , K}), the odds that i beats j are α i /α j , where α i and α j are positive-valued parameters which might be thought of as representing 'ability'. A general introduction can be found in Bradley (1984) or Agresti (2002). Applications are many, ranging from experimental psychology to the analysis of sports tournaments to genetics (for example, the allelic transmission/disequilibrium test of Sham and Curtis 1995 is based on a Bradley-Terry model in which the 'players' are alleles). In typical psychometric applications the 'contests' are comparisons, made by different human subjects, between pairs of items.
The model can alternatively be expressed in the logit-linear form logit [pr(i beats j) where λ i = log α i for all i. Thus, assuming independence of all contests, the parameters {λ i } can be estimated by maximum likelihood using standard software for generalized linear models, with a suitably specified model matrix. The primary purpose of the BradleyTerry2 package (Turner and Firth 2012), implemented in the R statistical computing environment (Ihaka and Gentleman 1996;R Development Core Team 2012), is to facilitate the specification and fitting of such models and some extensions.
The BradleyTerry2 package supersedes the earlier BradleyTerry package (Firth 2005), providing a more flexible user interface to allow a wider range of models to be fitted. In particular, BradleyTerry2 allows the inclusion of simple random effects so that the ability parameters can be related to available explanatory variables through a linear predictor of the form The inclusion of the prediction error U i allows for variability between players with equal covariate values and induces correlation between comparisons with a common player. BradleyTerry2 also allows for general contest-specific effects to be included in the model and allows the logit link to be replaced, if required, by a different symmetric link function (probit or cauchit).
The remainder of the paper is organised as follows. Section 2 demonstrates how to use the BradleyTerry2 package to fit a standard (i.e., unstructured) Bradley-Terry model, with a separate ability parameter estimated for each player, including the use of bias-reduced estimation for such models. Section 3 considers variations of the standard model, including the use of player-specific variables to model ability and allowing for contest-specific effects such as an order effect or judge effects. Sections 4 and 5 explain how to obtain important information about a fitted model, in particular the estimates of ability and their standard errors, and player-level residuals, whilst Section 6 notes the functions available to aid model search. Section 7 explains in more detail how to set up data for use with the BradleyTerry2 package, Section 8 lists the functions provided by the package and finally Section 9 comments on two directions for further development of the software. Note that the self-citation counts are ignored -these provide no information on the ability parameters, since the abilities are relative rather than absolute quantities. The binomial response can then be modelled by the difference in player abilities as follows: R> citeModel <-BTm(cbind(win1, win2), journal1, journal2,~journal, + id = "journal", data = citations.sf) R> citeModel The coefficients here are maximum likelihood estimates of λ 2 , λ 3 , λ 4 , with λ 1 (the log-ability for Biometrika) set to zero as an identifying convention.
The one-sided model formulã journal specifies the model for player ability, in this case the 'citeability' of the journal. The id argument specifies that "journal" is the name to be used for the factor that identifies the player -the values of which are given here by journal1 and journal2 for the first and second players respectively. Therefore in this case a separate citeability parameter is estimated for each journal.
If a different 'reference' journal is required, this can be achieved using the optional refcat argument: for example, making use of update to avoid re-specifying the whole model, The use of the standard Bradley-Terry model for this application might perhaps seem rather questionable -for example, citations within a published paper can hardly be considered independent, and the model discards potentially important information on self-citation. Stigler (1994) provides arguments to defend the model's use despite such concerns.

Bias-reduced estimates
Estimation of the standard Bradley-Terry model in BTm is by default computed by maximum likelihood, using an internal call to the glm function. An alternative is to fit by bias-reduced maximum likelihood (Firth 1993): this requires additionally the brglm package (Kosmidis 2007), and is specified by the optional argument br = TRUE. The resultant effect, namely removal of first-order asymptotic bias in the estimated coefficients, is often quite small. One notable feature of bias-reduced fits is that all estimated coefficients and standard errors are necessarily finite, even in situations of 'complete separation' where maximum likelihood estimates take infinite values (Heinze and Schemper 2002).
For the citation data, the parameter estimates are only very slightly changed in the biasreduced fit: Here the bias of maximum likelihood is small because the binomial counts are fairly large.
In more sparse arrangements of contests -that is, where there is less or no replication of the contests -the effect of bias reduction would typically be more substantial than the insignificant one seen here.

'Player-specific' predictor variables
In some application contexts there may be 'player-specific' explanatory variables available, and it is then natural to consider model simplification of the form in which ability of each player i is related to explanatory variables x i1 , . . . , x ip through a linear predictor with coefficients β 1 , . . . , β p ; the {U i } are independent errors. Dependence of the player abilities on explanatory variables can be specified via the formula argument, using the standard S -language model formulae. The difference in the abilities of player i and player j is modelled by where U i ∼ N (0, σ 2 ) for all i. The Bradley-Terry model is then a generalized linear mixed model, which the BTm function currently fits by using the penalized quasi-likelihood algorithm of Breslow and Clayton (1993).
As an illustration, consider the following simple model for the flatlizards data, which predicts the fighting ability of Augrabies flat lizards by body size (snout to vent length): R> options(show.signif.stars = FALSE) R> data("flatlizards", package = "BradleyTerry2") R> lizModel <-BTm(1, winner, loser,~SVL[..] + (1|..), data = flatlizards) Here the winner of each fight is compared to the loser, so the outcome is always 1. The special name '..' appears in the formula as the default identifier for players, in the absence of a userspecified id argument. The values of this factor are given by winner for the winning lizard and loser for the losing lizard in each contest. These factors are provided in the data frame contests that is the first element of the list object flatlizards. The second element of flatlizards is another data frame, predictors, containing measurements on the observed lizards, including SVL, which is the snout to vent length. Thus SVL[..] represents the snout to vent length indexed by lizard (winner or loser as appropriate). Finally a random intercept for each lizard is included using the bar notation familiar to users of the lme4 package (Bates, Mächler, and Bolker 2011). (Note that a random intercept is the only random effect structure currently implemented in BradleyTerry2.) The fitted model is summarized below:

R> summary(lizModel)
Call: BTm ( The coefficient of snout to vent length is weakly significant; however, the standard deviation of the random effect is quite large, suggesting that this simple model has fairly poor explanatory power. A more appropriate model is considered in the next section.

Missing values
The contest data may include all possible pairs of players and hence rows of missing data corresponding to players paired with themselves. Such rows contribute no information to the Bradley-Terry model and are simply discarded by BTm.
Where there are missing values in player-specific predictor (or explanatory) variables which appear in the formula, it will typically be very wasteful to discard all contests involving players for which some values are missing. Instead, such cases are accommodated by the inclusion of one or more parameters in the model. If, for example, player 1 has one or more of its predictor values x 11 , . . . , x 1p missing, then the combination of Equations 1 and 4 above yields for all other players j. This results in the inclusion of a 'direct' ability parameter for each player having missing predictor values, in addition to the common coefficients β 1 , . . . , β p -an approach which will be appropriate when the missingness mechanism is unrelated to contest success. The same device can be used also to accommodate any user-specified departures from a structured Bradley-Terry model, whereby some players have their abilities determined by the linear predictor but others do not.
In the original analysis of the flatlizards data (Whiting, Stuart-Fox, O'Connor, Firth, Bennett, and Blomberg 2006), the final model included the first and third principal components of the spectral reflectance from the throat (representing brightness and UV intensity respectively) as well as head length and the snout to vent length seen in our earlier model. The spectroscopy data was missing for two lizards, therefore the ability of these lizards was estimated directly. The following fits this model, with the addition of a random intercept as before: Note that BTm detects that lizards 96 and 99 have missing values in the specified predictors and automatically includes separate ability parameters for these lizards. This model was found to be the single best model based on the principal components of reflectance and the other predictors available and indeed the standard deviation of the random intercept is much reduced, but still highly significant. Allowing for this significant variation between lizards with the same predictor values produces more realistic (i.e., larger) standard errors for the parameters when compared to the original analysis of Whiting et al. (2006). Although this affects the significance of the morphological variables, it does not affect the significance of the principal components, so in this case does not affect the main conclusions of the study.

Order effect
In certain types of application some or all contests have an associated 'bias', related to the order in which items are presented to a judge or with the location in which a contest takes place, for example. A natural extension of the Bradley-Terry model (Equation 1) is then where z = 1 if i has the supposed advantage and z = −1 if j has it. (If the 'advantage' is in fact a disadvantage, δ will be negative.) The scores λ i then relate to ability in the absence of any such advantage.
As an example, consider the baseball data given in Agresti (2002) The data set records the home wins and losses for each baseball team against each of the 6 other teams in the data set. The head function is used to show the first 6 records, which are the Milwaukee home games. We see for example that Milwaukee played 7 home games against Detroit and won 4 of them. The 'standard' Bradley-Terry model without a home-advantage parameter will be fitted if no formula is specified in the call to BTm: R> baseballModel1 <-BTm(cbind(home.wins, away.wins), home.team, away.team, + data = baseball, id = "team") R> summary(baseballModel1) Call: BTm(outcome = cbind(home.wins, away.wins), player1 = home.team, player2 = away.team, id = "team", data = baseball) The reference team is Baltimore, estimated to be the weakest of these seven, with Milwaukee and Detroit the strongest.
In the above, the ability of each team is modelled simply as~team where the values of the factor team are given by home.team for the first team and away.team for the second team in each game. To estimate the home-advantage effect, an additional variable is required to indicate whether the team is at home or not. Therefore data frames containing both the team factor and this new indicator variable are required in place of the factors home.team and away.team in the call to BTm. This is achieved here by over-writing the home.team and away.team factors in the baseball data frame: R> baseball$home.team <-data.frame(team = baseball$home.team, at.home = 1) R> baseball$away.team <-data.frame(team = baseball$away.team, at.home = 0) The at.home variable is needed for both the home team and the away team, so that it can be differenced as appropriate in the linear predictor. With the data organised in this way, the ability formula can now be updated to include the at.home variable as follows: This reproduces the results given on page 438 of Agresti (2002): the home team has an estimated odds-multiplier of exp(0.3023) = 1.35 in its favour.

More general (contest-specific) predictors
The 'home advantage' effect is a simple example of a contest-specific predictor. Such predictors are necessarily interactions, between aspects of the contest and (aspects of) the two 'players' involved.
For more elaborate examples of such effects, see ?chameleons and ?CEMS. The former includes an 'experience' effect, which changes through time, on the fighting ability of male chameleons. The latter illustrates a common situation in psychometric applications of the Bradley-Terry model, where subjects express preference for one of two objects (the 'players'), and it is the influence on the results of subject attributes that is of primary interest.
As an illustration of the way in which such effects are specified, consider the following model specification taken from the examples in ?CEMS, where data on students' preferences in relation to six European management schools is analysed. R> data("CEMS", package = "BradleyTerry2") R> table8.model <-BTm(outcome = cbind ( This model reproduces results from Table 8 of Dittrich, Hatzinger, and Katzenbeisser (2001) apart from minor differences due to the different treatment of ties. Here the outcome is the binomial frequency of preference for school1 over school2, with ties counted as half a 'win' and half a 'loss'. The formula specifies the model for school 'ability' or worth. In this formula, the default label '..' represents the school (with values given by school1 or school2 as appropriate) and student is a factor specifying the student that made the comparison. The remaining variables in the formula use R's standard indexing mechanism to include student-specific variables, e.g., WOR: whether or not the student was in full-time employment, and school-specific variables, e.g., LAT: whether the school was in a 'Latin' city. Thus there are three types of variables: contest-specific (school1, school2, student), subject-specific (WOR, DEG, . . . ) and object-specific (LAT, St.Gallen, . . . ). These three types of variables are provided in three data frames, contained in the list object CEMS.

Ability scores
The function BTabilities extracts estimates and standard errors for the log-ability scores λ 1 , . . . , λ K . These will either be 'direct' estimates, in the case of the standard Bradley-Terry model or for players with one or more missing predictor values, or 'model-based' estimates of the formλ i = p r=1β r x ir for players whose ability is predicted by explanatory variables. As a simple illustration, team ability estimates in the home-advantage model for the baseball data are obtained by: R> library("qvcalc") R> baseball.qv <-qvcalc(BTabilities(baseballModel2)) R> plot(baseball.qv, + levelNames = c("Bal", "Bos", "Cle", "Det", "Mil", "NY", "Tor")) The 'comparison intervals' as shown in Figure 1 are based on 'quasi standard errors', and can be interpreted as if they refer to independent estimates of ability for the journals. This has the advantage that comparison between any pair of journals is readily made (i.e., not only comparisons with the 'reference' journal). For details of the theory and method of calculation see Firth and de Menezes (2004).

Residuals
There are two main types of residuals available for a Bradley-Terry model object.
First, there are residuals obtained by the standard methods for models of class "glm". These all deliver one residual for each contest or type of contest. For example, Pearson residuals for the model lizModel2 can be obtained simply by More useful for diagnostics on the linear predictor β r x ir are 'player'-level residuals, obtained by using the function residuals with argument type = "grouped". These residuals can then be plotted against other player-specific variables (output not shown).

Model search
In addition to update() as illustrated in preceding sections, methods for the generic functions add1(), drop1() and anova() are provided. These can be used to investigate the effect of adding or removing a variable, whether that variable is contest-specific, such as an order effect, or player-specific; and to compare the fit of nested models.
7. Setting up the data 7.1. Contest-specific data The outcome argument of BTm represents a binomial response and can be supplied in any of the formats allowed by the glm function. That is, either a two-column matrix with the columns giving the number of wins and losses (for player1 vs. player2), a factor where the first level denotes a loss and all other levels denote a win, or a binary variable where 0 denotes a loss and 1 denotes a win. Each row represents either a single contest or a set of contests between the same two players.
The player1 and player2 arguments are either factors specifying the two players in each contest, or data frames containing such factors, along with any contest-specific variables that are also player-specific, such as the at.home variable seen in Section 3.3. If given in data frames, the factors identifying the players should be named as specified by the id argument and should have identical levels, since they represent a particular sample of the full set of players.
Thus for the model baseballModel2, which was specified by the following call:
In this case home.team and away.team are both data frames, with the factor team specifying the team and the variable at.home specifying whether or not the team was at home. Contest-specific variables that are not player-specific -for example, whether it rained or not during a contest -should only be used in interactions with variables that are player-specific, otherwise the effect on ability would be the same for both players and would cancel out. Such variables can conveniently be provided in a single data frame along with the outcome, player1 and player2 data.
An offset in the model can be specified by using the offset argument to BTm. This facility is provided for completeness: the authors have not yet encountered an application where it is needed.
To use only certain rows of the contest data in the analysis, the subset argument may be used in the call to BTm. This should either be a logical vector of the same length as the binomial response, or a numeric vector containing the indices of rows to be used.

Non contest-specific data
Some variables do not vary by contest directly, but rather vary by a factor that is contestspecific, such as the player ID or the judge making the paired comparison. For such variables, it is more economical to store the data by the levels of the contest-specific factor and use indexing to obtain the values for each contest.
The CEMS example in Section 3.4 provides an illustration of such variables. In this example student-specific variables are indexed by student and school-specific variables are indexed by .., i.e., the first or second school in the comparison as appropriate. There are then two extra sets of variables in addition to the usual contest-specific data as described in the last section. A good way to provide these data to BTm is as a list of data frames, one for each set of variables, e.g.,
Player-specific offsets should be included in the formula by using the offset function.

Converting data from a 'wide' format
The BTm function requires data in a 'long' format, with one row per contest, provided either directly as in Section 7.1 or via indexing as in Section 7.2. In studies where the same set of paired comparisons are made by several judges, as in a questionnaire for example, the data may be stored in a 'wide' format, with one row per judge.
As an example, consider the cemspc data from the prefmod package (Hatzinger and Dittrich 2012), which provides data from the CEMS study in a wide format. Each row corresponds to one student; the first 15 columns give the outcome of all pairwise comparisons between the 6 schools in the study and the last two columns correspond to two of the student-specific variables: ENG (indicating the student's knowledge of English) and SEX (indicating the student's gender).
The following steps convert these data into a form suitable for analysis with BTm. First a new data frame is created from the student-specific variables and these variables are converted to factors: R> library("prefmod") R> student <-cemspc[c("ENG", "SEX")] R> student$ENG <-factor(student$ENG, levels = 1:2, + labels = c("good", "poor")) R> student$SEX <-factor(student$SEX, levels = 1:2, + labels = c("female", "male")) This data frame is put into a list, which will eventually hold all the necessary data. Then a student factor is created (303 students, 15 comparisons) for indexing the student data to produce contest-level data. This is put in a new data frame that will hold the contest-specific data.

R>
Note that both factors have exactly the same levels, even though only five of the six players are represented in each case. In other words, the numeric factor levels refer to the same players in each case, so that the player is unambiguously identified. This ensures that player-specific parameters and player-specific covariates are correctly specified.
Finally the contest data frame is added to the main list:

R> cems$contest <-contest
This creates a single data object that can be passed to the data argument of BTm. Of course, such a list could be created on-the-fly as in data = list(contest, student), which may be more convenient in practice.

Converting data from the format required by the BradleyTerry package
The BradleyTerry package described in Firth (2005) required contest/comparison results to be in a data frame with columns named winner, loser and Freq. The following example shows how xtabs and countsToBinomial can be used to convert such data for use with the BTm function in BradleyTerry2: R> library("BradleyTerry") R> data("citations", package = "BradleyTerry") R> citations <-xtabs(Freq~winner + loser, citations) R> citations.sf <-countsToBinomial(citations) The citations.sf data frame can then be used with BTm as shown in Section 2.1.

A list of the functions provided in BradleyTerry2
The standard R help files provide the definitive reference. Here we simply list the main user-level functions and their arguments, as a convenient overview: BTabilities ( The present version of BradleyTerry2 provides no sophisticated facilities for handling tied contests/comparisons; the well-known models of Rao and Kupper (1967) and Davidson (1970) are not implemented here. At present the BTm function requires a binary or binomial response variable, the third ('tied') category of response is not allowed.
In several of the data examples (e.g., ?CEMS, ?springall, ?sound.fields), ties are handled by the crude but simple device of adding half of a 'win' to the tally for each player involved; in each of the examples where this has been done it is found that the result is very similar, after a simple re-scaling, to the more sophisticated analyses that have appeared in the literature. Note that this device when used with BTm typically gives rise to warnings produced by the back-end glm function, about non-integer 'binomial' counts; such warnings are of no consequence and can be safely ignored.
It is likely that a future version of BradleyTerry2 will have a more general method for handling ties.

A note on 'contest-specific' random effects
The current version of BradleyTerry2 provides facilities for fitting models with random effects in 'player-specific' predictor functions, as illustrated in Section 3. For more general, 'contestspecific' random-effect structures, such as random 'judge' effects in psychological studies (e.g., Böckenholt 2001), BradleyTerry2 provides (through BTm) the necessary user interface but as yet no back-end calculation. It is hoped that this important generalization can be made successfully in a future version of BradleyTerry2.