markmfredrickson / optmatch

Functions for optimal matching in R
https://markmfredrickson.github.io/optmatch
Other
47 stars 14 forks source link

summary.optmatch() fails with a bad subproblem #155

Closed benthestatistician closed 5 years ago

benthestatistician commented 6 years ago

Here's a match with two subproblems, s.t. for one of the subproblems matching fails. (See comments for another example.) Unfortunately summary() also fails:

summary(fmx)
Error in dimnames(so$matching.failed) <- `*vtmp*` : 
  length of 'dimnames' [2] not equal to array extent

Looking inside I think I see the problem:

so$matching.failed <- table(subprobs, attr(object, "contrast.group"), 
    exclude = names(match.succeed)[match.succeed], useNA = "no")

generates

subprobs FALSE TRUE <NA>
     HFO   245  110    0

and then the last <NA> column is unexpected further down. (NB: it seems that last NA column only comes about if the failing subproblem had an NA in the treatment variable.)

If for instance we put

so$matching.failed <- table(subprobs, attr(object, "contrast.group"), 
exclude= c(names(match.succeed)[match.succeed], NA) )

then we'd get

subprobs FALSE TRUE
     HFO   245  110   

and all else would go through.

benthestatistician commented 5 years ago

Easier to reproduce example of same:


library(optmatch) ; library(magrittr)
data(nuclearplants)
np_mod <-  nuclearplants
which(np_mod$pt==1)
## [1] 27 28 29 30 31 32
np_mod[which.max(np_mod$pt==1), "pr"]  <- NA
np_mod %>% fullmatch(pr~cap + strata(pt), min.c=2, data=.) %>%
 optmatch:::subproblemSuccess()
##    0     1 
## TRUE FALSE 
 np_mod %>% fullmatch(pr~cap + strata(pt), min.c=2, data=.) %>% summary()
## Error in dimnames(so$matching.failed) <- `*vtmp*` : 
## length of 'dimnames' [2] not equal to array extent