Skip to content

Instantly share code, notes, and snippets.

@chatchavan
Created November 14, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chatchavan/1966d6441734342021e591523644fcb1 to your computer and use it in GitHub Desktop.
Save chatchavan/1966d6441734342021e591523644fcb1 to your computer and use it in GitHub Desktop.
Matching pairs of participants based on rank questions.
Items User1 User2 User3 User4
Mazda 1 1 8 1
BMW 2 3 7 2
Honda 3 2 6 3
Audi 4 4 5 4
Toyota 5 7 4 5
VW 6 6 3 6
Ford 7 5 2 8
Nissan 8 8 1 7
---
title: "Rank-order matching"
output: html_notebook
---
```{r, include=FALSE}
# Package installation
if (!require("pacman")) install.packages("pacman", repos='https://stat.ethz.ch/CRAN/'); library(pacman)
p_load(combinat)
```
Read input data frim `input.csv`. In this dataset User1 and User3 have opposite ranks. User1 and User4 differs only one rank ("Ford" vs "Nissan")
```{r}
inputData <- read.csv("input.csv")
inputData
```
A function to calculate Kendall's Tau from the given pair of user names:
```{r}
calcTau <- function(r,c){
name1 <- as.character(r)
name2 <- as.character(c)
ranks1 <- as.numeric(unlist(inputData[,name1]))
ranks2 <- as.numeric(unlist(inputData[,name2]))
cor(ranks1, ranks2, method="kendall")
}
calcTau <- Vectorize(calcTau,vectorize.args = c('r','c'))
```
Calculate Tau in all combinations of users:
```{r}
userNames <- names(inputData)[-1]
scoreMat <- outer(userNames, userNames, FUN=calcTau)
rownames(scoreMat) <- userNames
colnames(scoreMat) <- userNames
scoreMat
```
Mark the lower triangle duplicate with arbitrary value of -10. (Tau is between -1 and 1.)
```{r}
scoreMat[lower.tri(scoreMat, diag =TRUE)] <- -10
scoreMat
```
Put data into list format. Pairs that have the same ranking have tau = 1. Pairs taht have the opposite ranking have tau = -1.
```{r}
d <- data.frame(i=rep(row.names(scoreMat),ncol(scoreMat)),
j=rep(colnames(scoreMat),each=nrow(scoreMat)),
tau=as.vector(scoreMat))
d <- d[d["tau"] != -10,]
d[order(-d["tau"]),]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment