@@ -763,9 +763,6 @@ scoreCaliper <- function(x, z, caliper) {
763763 # there may be a speed increase in pulling out the guts of that function and calling them directly
764764 control <- sort(control )
765765
766- treatedids <- c()
767- controlids <- c()
768-
769766 # NB: for reasons unknown, you must add the double.eps in the function
770767 # call, saving it in a variable (e.g. width.eps <- width +
771768 # .Machine$double.eps) will not work.
@@ -775,15 +772,39 @@ scoreCaliper <- function(x, z, caliper) {
775772 starts <- length(control ) - findInterval(- (treated - caliper -
776773 .Machine $ double.eps ), rev(- control ))
777774
775+ edges <- pmax(0 , (stops - starts ))
776+ n <- sum(edges )
777+
778+ treatedids <- rep.int(1 : k , times = edges )
779+ controlids <- integer(n )
780+
781+ idx <- 1
778782 for (i in 1 : k ) {
779- if (starts [i ] < length(control ) && stops [i ] > 0 && starts [i ] < stops [i ]) {
780- tmp <- seq(starts [i ] + 1 , stops [i ])
781- controlids <- c(controlids , tmp )
782- treatedids <- c(treatedids , rep(i , length(tmp )))
783+ if (starts [i ] < length(control ) && stops [i ] > 0 && edges [i ] > 0 ) {
784+ tmp <- seq.int(starts [i ] + 1 , stops [i ])
785+ j <- length(tmp )
786+ controlids [idx : (idx + j - 1 )] <- tmp
787+ idx <- idx + j
783788 }
784789 }
785790
786- makeInfinitySparseMatrix(rep(0 , length(treatedids )), controlids , treatedids , names(control ), names(treated ))
791+ v <- integer(n )
792+
793+ # I noticed that if a control was not reachable by any treated unit it would
794+ # not appear in the resulting matrix, so we set names explicitly if they don't exist.
795+ if (is.null(names(control ))) {
796+ nmsc <- as.character(1 : length(control ))
797+ } else {
798+ nmsc <- names(control )
799+ }
800+
801+ if (is.null(names(treated ))) {
802+ nmst <- as.character(1 : k )
803+ } else {
804+ nmst <- names(treated )
805+ }
806+
807+ makeInfinitySparseMatrix(v , controlids , treatedids , nmsc , nmst )
787808}
788809
789810# ' @details \bold{First argument (\code{x}): \code{matrix} or \code{InfinitySparseMatrix}.} These just return their
0 commit comments