generate_transitive_closure: Generate Transitive Closure

View source: R/generate_transitive_closure.R

generate_transitive_closureR Documentation

Generate Transitive Closure

Description

Generate the transitive closure for a set of consistent pairwise comparisons. The result can be given in the preferences argument to compute_mallows.

Usage

generate_transitive_closure(df, cl = NULL)

Arguments

df

A data frame with one row per pairwise comparison, and columns assessor, top_item, and bottom_item. Each column contains the following:

  • assessor is a numeric vector containing the assessor index, or a character vector containing the (unique) name of the assessor.

  • bottom_item is a numeric vector containing the index of the item that was disfavored in each pairwise comparison.

  • top_item is a numeric vector containing the index of the item that was preferred in each pairwise comparison.

So if we have two assessors and five items, and assessor 1 prefers item 1 to item 2 and item 1 to item 5, while assessor 2 prefers item 3 to item 5, we have the following df:

assessor bottom_item top_item
1 2 1
1 5 1
2 5 3
cl

Optional computing cluster used for parallelization, returned from parallel::makeCluster. Defaults to NULL.

Value

A dataframe with the same columns as df, but with its set of rows expanded to include all pairwise preferences implied by the ones stated in df. The returned object has S3 subclass BayesMallowsTC, to indicate that this is the transitive closure.

See Also

generate_initial_ranking

Other preprocessing: estimate_partition_function(), generate_constraints(), generate_initial_ranking(), obs_freq, prepare_partition_function()

Examples

# Let us first consider a simple case with two assessors, where assessor 1
# prefers item 1 to item 2, and item 1 to item 5, while assessor 2 prefers
# item 3 to item 5. We then have the following dataframe of pairwise
# comparisons:
pair_comp <- data.frame(
  assessor = c(1, 1, 2),
  bottom_item = c(2, 5, 5),
  top_item = c(1, 1, 3))
# We then generate the transitive closure of these preferences:
(pair_comp_tc <- generate_transitive_closure(pair_comp))
# In this case, no additional relations we implied by the ones
# stated in pair_comp, so pair_comp_tc has exactly the same rows
# as pair_comp.

# Now assume that assessor 1 also preferred item 5 to item 3, and
# that assessor 2 preferred item 4 to item 3.
pair_comp <- data.frame(
  assessor = c(1, 1, 1, 2, 2),
  bottom_item = c(2, 5, 3, 5, 3),
  top_item = c(1, 1, 5, 3, 4))

# We generate the transitive closure again:
(pair_comp_tc <- generate_transitive_closure(pair_comp))
# We now have one implied relation for each assessor.
# For assessor 1, it is implied that 1 is preferred to 3.
# For assessor 2, it is implied that 4 is preferred to 5.

## Not run: 
  # If assessor 1 in addition preferred item 3 to item 1,
  # the preferences would not be consistent. This is not yet supported by compute_mallows,
  # so it causes an error message. It will be supported in a future release of the package.
  # First, we add the inconsistent row to pair_comp
  pair_comp <- rbind(
    pair_comp,
    data.frame(assessor = 1, bottom_item = 1, top_item = 3))

  # This causes an error message and prints out the problematic rankings:
  (pair_comp_tc <- generate_transitive_closure(pair_comp))

## End(Not run)


## Not run: 
  # The computations can also be done in parallel
  library(parallel)
  cl <- makeCluster(detectCores() - 1)
  beach_tc <- generate_transitive_closure(beach_preferences, cl = cl)
  stopCluster(cl)

## End(Not run)

BayesMallows documentation built on Nov. 25, 2023, 5:09 p.m.