2014-06-16 49 views
1

我想使用隨機森林方法來推測缺失值。我讀過一些論文,聲稱MICE隨機森林比參數化小鼠表現更好。插補時使用隨機森林(MICE包)的錯誤

在我的情況下,我已經運行了默認鼠標的模型,並獲得了結果並與它們一起玩。但是,當我有一個方法隨機森林的選項,我得到一個錯誤,我不知道爲什麼。我看到了一些與隨機森林和老鼠的錯誤有關的問題,但那些不是我的例子。我的變量有不止一個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 

任何人有任何想法,爲什麼我得到這個錯誤?

編輯

我試圖改變的而不是虛擬變量所有變量的數值,並返回相同的錯誤和一些警告()

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

iter imp variable 
    1 1 Vac CliForm 
Error in if (n == 0) stop("data (x) has 0 rows") : argument is of length zero 
In addition: There were 50 or more warnings (use warnings() to see the first 50) 

50: In randomForest.default(x = xobs, y = yobs, ntree = 1, ... : 
    The response has five or fewer unique values. Are you sure you want to do regression? 

EDIT1

我我們只嘗試了5次插值和一小部分數據,只有2000行,並且出現了一些不同的錯誤:

> 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 

回答

2

我也遇到了這個錯誤,當我只有一個完全觀察到的變量,我猜也是你的情況的原因。我的同事Anoop Shah爲我提供了一個解決方案(下文),van Buuren教授(老鼠的作者)說他會在下一次更新包中加入它。

在R鍵入以下內容可以重新定義射頻功能。 fixInNamespace( 「mice.impute.rf」, 「老鼠」)

校正功能粘貼然後:

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) 
} 
+0

嗨@Jonathan!我做了你提到的並且使用了與之前相同的命令:fit < - randomForest(x = xobs,y = yobs,ntree = 1,...)。但是,我收到一個錯誤:「onetree(xobs,xmis,yobs,...)錯誤:無法找到函數」randomForest「」。任何想法爲什麼?原因是這樣的:fit < - randomForest(x = xobs,y = yobs,ntree = 1,...) – pavid

+1

抱歉Pavid,不確定,雖然看起來你沒有定義因爲某種原因而定義的randomForest函數。我剛剛看到Stef Van Buuren已於6月11日更新MICE,因此您可能想嘗試更新軟件包安裝並查看是否可以解決問題。 –

+0

哦哦!它現在正在運行!我必須承認,更新軟件包的想法並沒有超越我的想法!非常感謝你! :) – pavid