R 插补过程中使用随机林(MICE包)的错误

R 插补过程中使用随机林(MICE包)的错误,r,random-forest,r-mice,R,Random Forest,R Mice,我想使用随机森林法来估算缺失值。我读过一些论文,声称随机森林中的老鼠比参数化老鼠表现得更好 在我的例子中,我已经为默认的鼠标运行了一个模型,得到了结果并使用它们。然而,当我有一个随机森林方法的选项时,我得到了一个错误,我不知道为什么。我见过一些关于随机森林和老鼠错误的问题,但这些不是我的案例。我的变量不止一个NA imp <- mice(data1, m=70, pred=quickpred(data1), method="pmm", seed=71152, printFlag=TRUE)

我想使用随机森林法来估算缺失值。我读过一些论文,声称随机森林中的老鼠比参数化老鼠表现得更好

在我的例子中,我已经为默认的鼠标运行了一个模型,得到了结果并使用它们。然而,当我有一个随机森林方法的选项时,我得到了一个错误,我不知道为什么。我见过一些关于随机森林和老鼠错误的问题,但这些不是我的案例。我的变量不止一个NA

imp <- mice(data1, m=70, pred=quickpred(data1), method="pmm", seed=71152, printFlag=TRUE)
impRF <- mice(data1, m=70, pred=quickpred(data1), method="rf", seed=71152, printFlag=TRUE)

iter imp variable
 1   1  Vac
Error in if (n == 0) stop("data (x) has 0 rows") : argument is of length zero

imp-imp当我只有一个完全观察到的变量时,我也遇到了这个错误,我猜这也是您案例中的原因。我的同事Anoop Shah为我提供了一个修复方案(见下文),van Buuren教授(mice的作者)表示,他将在下一次更新软件包时加入该方案

在R中,键入以下命令,以便重新定义rf插补函数。 fixInNamespace(“mices.impute.rf”,“mices”)

然后,要粘贴的更正功能为:

mice.impute.rf <- function (y, ry, x, ntree = 100, ...){
ntree <- max(1, ntree)
xobs <- as.matrix(x[ry, ])
xmis <- as.matrix(x[!ry, ])
yobs <- y[ry]
onetree <- function(xobs, xmis, yobs, ...) {
    fit <- randomForest(x = xobs, y = yobs, ntree = 1, ...)
    leafnr <- predict(object = fit, newdata = xobs, nodes = TRUE)
    nodes <- predict(object = fit, newdata = xmis, nodes = TRUE)
    donor <- lapply(nodes, function(s) yobs[leafnr == s])
    return(donor)
}
forest <- sapply(1:ntree, FUN = function(s) onetree(xobs, 
    xmis, yobs, ...))
impute <- apply(forest, MARGIN = 1, FUN = function(s) sample(unlist(s), 
    1))
return(impute)
}

mices.impute.rf Hi@Jonathan!我做了您提到的事情,并使用了与之前相同的命令:fit-Sorry-Pavid,不确定,尽管出于某种原因,您似乎还没有定义randomForest函数。我刚刚看到Stef Van Buuren在6月11日更新了MICE,所以您可能想尝试更新您的软件包安装,看看这是否解决了问题。哦,uau!它正在运行!我必须承认,更新软件包的想法并没有在我脑海中出现!非常感谢!:)
> imp <- mice(data2, m=5, pred=quickpred(data2), method="rf", seed=71152, printFlag=TRUE)

iter imp variable
 1   1  Vac  Radio  Origin  Job  Alc  Smk  Drugs  Prison  Commu  Hmless  Symp
Error in randomForest.default(x = xobs, y = yobs, ntree = 1, ...) : NAs in foreign   
 function call (arg 11)
 In addition: Warning messages:
 1: In randomForest.default(x = xobs, y = yobs, ntree = 1, ...) : invalid mtry: reset to within valid range
 2: In max(ncat) : no non-missing arguments to max; returning -Inf
 3: In randomForest.default(x = xobs, y = yobs, ntree = 1, ...) : NAs introduced by coercion
mice.impute.rf <- function (y, ry, x, ntree = 100, ...){
ntree <- max(1, ntree)
xobs <- as.matrix(x[ry, ])
xmis <- as.matrix(x[!ry, ])
yobs <- y[ry]
onetree <- function(xobs, xmis, yobs, ...) {
    fit <- randomForest(x = xobs, y = yobs, ntree = 1, ...)
    leafnr <- predict(object = fit, newdata = xobs, nodes = TRUE)
    nodes <- predict(object = fit, newdata = xmis, nodes = TRUE)
    donor <- lapply(nodes, function(s) yobs[leafnr == s])
    return(donor)
}
forest <- sapply(1:ntree, FUN = function(s) onetree(xobs, 
    xmis, yobs, ...))
impute <- apply(forest, MARGIN = 1, FUN = function(s) sample(unlist(s), 
    1))
return(impute)
}