这不会进行变量选择,但它将使用 kNN 进行估算,仅使用g
您在评论中建议的具有匹配性别的行:
Sys.setenv("PKG_CXXFLAGS"="-std=c++0x") # needed for the lambda functions in Rcpp
# install/load package, create example data
devtools::install_github("alexwhitworth/imputation")
library(imputation)
set.seed(1345)
g <- sample(c("M", "F"), 100, replace=T)
a <- matrix(rnorm(1000), ncol=10)
a[a>1.5] <- NA
df <- data.frame(a,g)
# subset by gender, exclude character column from kNN (which doesn't
# handle character variables)
df_f <- kNN_impute(df[df$g == "F", 1:10], k= 3, q= 2, check_scale = FALSE, parallel= FALSE)
df_m <- kNN_impute(df[df$g == "M", 1:10], k= 3, q= 2, check_scale = FALSE, parallel= FALSE)
# recombine. Can use rownames as key
df2 <- data.frame(rbind(df_f$x, df_m$x))
df2 <- df2[order(as.integer(rownames(df2))),]
df2$g <- df$g