jtilly / matchingR

Algorithms for Matching Markets in R and C++
https://cran.r-project.org/package=matchingR
48 stars 18 forks source link

Preserve the row and column names in the input matrix #41

Open msunij opened 3 years ago

msunij commented 3 years ago

The output from the algorithm doesn't preserve the row and column names associated with the input matrices, can you please add an option to keep them?

jtilly commented 3 years ago

This sounds reasonable in general.

Could you provide a concrete example with the desired output, please?

msunij commented 3 years ago

Since we're relying more and more on tidyverse let me show an example beginning with a tibble.

library(tidyverse)
library(matchingR)

set.seed(100)

pref <- expand_grid(college = LETTERS[1:5],
                    student = letters[1:7]) %>% 
  mutate(college_score = round(rnorm(nrow(.)), 2),
         student_score = round(rnorm(nrow(.)), 2))

college_score <- pref %>% 
  select(-student_score) %>% 
  pivot_wider(names_from = college, values_from = college_score) %>% 
  column_to_rownames(var = "student") %>% 
  as.matrix()

student_score <- pref %>% 
  select(-college_score) %>% 
  pivot_wider(names_from = student, values_from = student_score) %>% 
  column_to_rownames(var = "college") %>% 
  as.matrix()

matched <- galeShapley.collegeAdmissions(studentUtils = student_score,
                                         collegeUtils = college_score,
                                         slots = 2)

The input matrices looks like the following

> college_score
      A     B     C     D     E
a  1.66  0.38  0.17  1.02 -0.64
b  0.28  1.08 -0.44 -1.88  0.49
c  1.21 -0.21 -1.66 -0.56 -0.32
d -1.16  0.15  0.43 -0.51  0.35
e -0.39  0.13  0.77  0.57  1.16
f -1.03  1.28  0.01  1.94 -0.32
g  0.06  2.67 -1.48 -1.22  0.44
> student_score
      a     b     c     d     e     f     g
A  1.12  1.40  0.74 -1.76  0.72  1.56 -0.88
B -1.42 -0.68  1.99  1.29  0.67 -1.57 -1.23
C  0.25 -0.13 -0.06 -0.93 -0.49 -0.04  1.27
D -0.36  0.37  1.54  1.71 -0.26 -0.50  2.15
E -0.54 -1.18 -0.02 -0.24 -0.43 -0.25  1.66

Now the output from the matching algorithm looks like the following

> matched$matched.colleges
     [,1] [,2]
[1,]    7   NA
[2,]    4    6
[3,]    5   NA
[4,]    3    2
[5,]    1   NA

As you can see it doesn't use the rownames or colnames attached to the input matrices. The desired output would be something like this

> matched$matched.colleges
  slot_1 slot_2
A "g"    NA    
B "d"    "f"   
C "e"    NA    
D "c"    "b"   
E "a"    NA