Skip to content

What to do about "dimnames of 'x' contains duplicates" errors

Mark Fredrickson edited this page Jun 19, 2014 · 2 revisions

The fullmatch and pairmatch functions use the dimnames of the distance matrix or other distance object in order to produce a named factor that records which units in are matched to each other. If these names are not present or in some way damaged, the user will see the following error:

Error in fullmatch.matrix(distanceMatrix, data = originalData) : 
  dimnames of argument 'x' contain duplicates

Here x refers to the distanceMatrix object the user created.

Examples and Solutions

There are two ways this error can be generated, either there are 1) duplicates in the names of the original data or distance matrix or 2) there are missing entries for the treatment indicator variable. In the following examples, we'll demonstrate both issues and provide solutions.

The following preamble sets up the R environment with the basic propensity score matching problem:

set.seed(201406019)
library(optmatch)
data(nuclearplants)
pmod <- glm(pr ~ ., data = nuclearplants, family = binomial)
scores <- predict(pmod)
n <- length(scores)
names(scores) <- make.unique(sample(LETTERS, n , replace = T))

Duplicate names in original data or distance matrix

If there are duplicates in the names of treated or control units, fullmatch and pairmatch cannot return a named factor that uniquely determines which unit matches which. If unit A appears twice, in the data, and the named factor states that A is a member of both group 1 and group 2, how does the user know which of the two As in the which of the two groups?

For example, if we accidently duplicated a name in scores:

badscores <- scores
names(badscores)[1] <- names(badscores)[2]
d.bs <- match_on(badscores, z = nuclearplants$pr)
x <- fullmatch(d.bs, data = badscores)
Error in fullmatch.matrix(d.bs, data = badscores) : 
  dimnames of argument 'x' contain duplicates

This can be fixed by making the names unique:

names(badscores) <- make.unique(names(badscores))