rlabelr-labelledmatchit

Variable labels in summary.matchit


How can I use variable labels in summary.matchit?

For example:

mydf <- MatchIt::lalonde
mydf  %<>% labelled::set_variable_labels(
  age = "MyAge",
  educ = "MyEduc",
  race = "MyRace")

m.out0 <- matchit(treat ~ age + educ + race + married + 
                    nodegree + re74 + re75,
                  data = lalonde,
                  method = "quick",
                  distance = "glm")
summary(m.out0, un = FALSE)$sum.matched[seq(1,5),seq(1,2)]

returns

              Means Treated    Means Control
distance    0.5774355466557  0.5764010328956
age        25.8162162162162 25.4475708575709
educ       10.3459459459459 10.6780791680792
raceblack   0.8432432432432  0.8376126126126
racehispan  0.0594594594595  0.0606976956977

but I want:

              Means Treated    Means Control
distance      0.5774355466557  0.5764010328956
MyAge         25.8162162162162 25.4475708575709
MyEduc        10.3459459459459 10.6780791680792
MyRaceblack   0.8432432432432  0.8376126126126
MyRacehispan  0.0594594594595  0.0606976956977

Solution

  • Workaround for the time being:

    mydf <- MatchIt::lalonde
    mydf  %<>% labelled::set_variable_labels(
      age = "MyAge",
      educ = "MyEduc",
      race = "MyRace")
    
    m.out0 <- matchit(treat ~ age + educ + race + married + 
                        nodegree + re74 + re75,
                      data = lalonde,
                      method = "quick",
                      distance = "glm")
    
    test <- summary(m.out0, un = FALSE)$sum.matched[seq(1,5),seq(1,2)]
    
    label_rows <- function (tbl, df) {
      var_map <- unlist(labelled::var_label(df))
      for(var_name in names(var_map)) {
        rownames(tbl) %<>% stringr::str_replace(glue("^{var_name}"), var_map[[var_name]])
      }
      return(tbl)
    }
    
    test %>% label_rows(mydf)