Skip to content

Commit

Permalink
fix matchImpute infinite loop in case of all missing + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkowa committed Jun 21, 2023
1 parent c82b59e commit 963a302
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# VIM 6.x.x
- fix infinite loop in matchImpute in case all observations of a variable are missing

# VIM 6.2.3
- default robust regression method for irmi for numeric variables changes from rlm to lmrob.

Expand Down
6 changes: 6 additions & 0 deletions R/matchImpute.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ matchImpute <- function(data,variable=colnames(data)[!colnames(data)%in%match_va
data <- as.data.table(data)
else
data <- data.table::copy(data)
tfna <- data[,sapply(lapply(.SD,is.na),all),.SDcols=variable]
if(any(tfna)){
stop(paste0(variable[tfna],collapse=", ")," ", ifelse(sum(tfna)>1,"are","is")," completely missing")

}
na_present <- data[,sum(sapply(lapply(.SD,is.na),sum)),.SDcols=variable]


if(imp_var){
data[,paste(variable,imp_suffix,sep="_"):=lapply(.SD,is.na),.SDcols=variable]
}
Expand Down
22 changes: 22 additions & 0 deletions inst/tinytest/test_matchImpute.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
library(VIM)
message("matchImpute general")
d <- data.frame(x=LETTERS[1:6],y=as.double(1:6),z=as.double(1:6),
w=ordered(LETTERS[1:6]), stringsAsFactors = FALSE)
dorig <- rbind(d,d)
# minimal example with one match var
d1 <- matchImpute(setna(dorig,7:12,1)[,1:2],match_var = "y", variable="x")
expect_identical(d1$x[d1$x_imp],d1$x[!d1$x_imp])

d1b <- matchImpute(setna(dorig,7:12,1)[,1:2],match_var = "y", variable="x", imp_var = FALSE)
expect_identical(d1b$x[d1$x_imp],d1b$x[!d1$x_imp])
expect_false("x_imp" %in% colnames(d1b))
expect_true("x_imp" %in% colnames(d1))


# all missing in x -> error
expect_error(matchImpute(setna(dorig,1:12,1)[,1:2],match_var = "y", variable="x"))


# example with two match vars
d1 <- matchImpute(setna(dorig,7:12,1)[,1:3],match_var = c("y","z"), variable="x")
expect_identical(d1$x[d1$x_imp],d1$x[!d1$x_imp])

0 comments on commit 963a302

Please sign in to comment.