fitted.btfit
returns the fitted values from a fitted btfit model object.
# S3 method for btfit
fitted(object, subset = NULL, as_df = FALSE, ...)
An object of class "btfit", typically the result ob
of ob <- btfit(..)
. See btfit
.
A condition for selecting one or more subsets of the components. This can either be a character vector of names of the components (i.e. a subset of names(object$pi)
), a single predicate function (that takes a vector of object$pi
as its argument), or a logical vector of the same length as the number of components, (i.e. length(object$pi)
).
Logical scalar, determining class of output. If TRUE
, the function returns a data frame. If FALSE
(the default), the function returns a matrix (or list of matrices). Note that setting as_df = TRUE
can have a significant computational cost when any of the components have a large number of items.
Other arguments
If as_df = FALSE
and the model has been fit on the full dataset, returns a matrix where the \(i,j\)-th element is the Bradley-Terry expected value \(m_{ij}\) (See Details). Otherwise, a list of such matrices is returned, one for each fully-connected component. If as_df = TRUE
, returns a five-column data frame, where the first column is the component that the two items are in, the second column is item1
, the third column is item2
, the fourth column, fit1
, is the expected number of times that item 1 beats item 2 and the fifth column, fit2
, is the expected number of times that item 2 beats item 1. If btdata$wins
has named dimnames, these will be the colnames
for columns one and two. Otherwise these colnames will be item1
and item2
. See Details.
Consider a set of \(K\) items. Let the items be nodes in a graph and let there be a directed edge \((i, j)\) when \(i\) has won against \(j\) at least once. We call this the comparison graph of the data, and denote it by \(G_W\). Assuming that \(G_W\) is fully connected, the Bradley-Terry model states that the probability that item \(i\) beats item \(j\) is $$p_{ij} = \frac{\pi_i}{\pi_i + \pi_j},$$ where \(\pi_i\) and \(\pi_j\) are positive-valued parameters representing the skills of items \(i\) and \(j\), for \(1 \le i, j, \le K\).
The expected, or fitted, values under the Bradley-Terry model are therefore:
$$m_{ij} = n_{ij}p_{ij},$$
where \(n_{ij}\) is the number of comparisons between item \(i\) and item \(j\).
If there are values on the diagonal in the original btdata$wins
matrix, then these appear as the values on the diagonal of the fitted matrix. These values do not appear in the data frame if the as_df
argument is set to TRUE
.
The function btfit
is used to fit the Bradley-Terry model. It produces a "btfit"
object that can then be passed to fitted.btfit
to obtain the fitted values \(m_{ij}\). Note that the Bradley-Terry probabilities \(p_{ij}\) can be calculated using btprob
.
If \(G_W\) is not fully connected, then a penalised strength parameter can be obtained using the method of Caron and Doucet (2012) (see btfit
, with a > 1
), which allows for a Bradley-Terry probability of any of the \(K\) items beating any of the others. Alternatively, the MLE can be found for each fully-connected component of \(G_W\) (see btfit
, with a = 1
), and the probability of each item in each component beating any other item in that component can be found.
Bradley, R. A. and Terry, M. E. (1952). Rank analysis of incomplete block designs: 1. The method of paired comparisons. Biometrika, 39(3/4), 324-345.
Caron, F. and Doucet, A. (2012). Efficient Bayesian Inference for Generalized Bradley-Terry Models. Journal of Computational and Graphical Statistics, 21(1), 174-196.
citations_btdata <- btdata(BradleyTerryScalable::citations)
fit1 <- btfit(citations_btdata, 1)
fitted(fit1)
fitted(fit1, as_df = TRUE)
toy_df_4col <- codes_to_counts(BradleyTerryScalable::toy_data, c("W1", "W2", "D"))
toy_btdata <- btdata(toy_df_4col)
fit2a <- btfit(toy_btdata, 1)
fitted(fit2a)
fitted(fit2a, as_df = TRUE)
fitted(fit2a, subset = function(x) "Amy" %in% names(x))
fit2b <- btfit(toy_btdata, 1.1)
fitted(fit2b, as_df = TRUE)
Run the code above in your browser using DataLab