아. 이것은 combine
의 감독이거나 귀하의 관점에 따라 무의미한 시도입니다.
투표 매트릭스는 각 응답 범주에 대한 학습 데이터의 각 사례에 대해 포리스트의 투표 수를 기록합니다. 당연히 교육 데이터의 행 수와 같은 수의 행을 갖습니다.
간단히 말하자면 각 교육 데이터 세트를 동일한 크기로 설정하면 실행됩니다. 하지만 그렇게한다면 새로운 결과물을 만드는 것 이외에는 결과물을 사용하지 않을 것입니다. 숲의 성능을 요약 한 내용이 합쳐지면 아무 의미가 없습니다.
편집 내가 나서서 불평등 한 교육 집합 크기를 "처리"로 combine
을 수정
. 그 모든 사실은 실제로 일치시키지 않을 것들을 함께 꿰매려고하는 많은 양의 코드를 제거했다는 것을 의미합니다.하지만 숲을 결합 부분을 유지, 그래서 당신은 여전히 predict
를 사용할 수 있습니다
my_combine <- function (...)
{
pad0 <- function(x, len) c(x, rep(0, len - length(x)))
padm0 <- function(x, len) rbind(x, matrix(0, nrow = len -
nrow(x), ncol = ncol(x)))
rflist <- list(...)
areForest <- sapply(rflist, function(x) inherits(x, "randomForest"))
if (any(!areForest))
stop("Argument must be a list of randomForest objects")
rf <- rflist[[1]]
classRF <- rf$type == "classification"
trees <- sapply(rflist, function(x) x$ntree)
ntree <- sum(trees)
rf$ntree <- ntree
nforest <- length(rflist)
haveTest <- !any(sapply(rflist, function(x) is.null(x$test)))
vlist <- lapply(rflist, function(x) rownames(importance(x)))
numvars <- sapply(vlist, length)
if (!all(numvars[1] == numvars[-1]))
stop("Unequal number of predictor variables in the randomForest objects.")
for (i in seq_along(vlist)) {
if (!all(vlist[[i]] == vlist[[1]]))
stop("Predictor variables are different in the randomForest objects.")
}
haveForest <- sapply(rflist, function(x) !is.null(x$forest))
if (all(haveForest)) {
nrnodes <- max(sapply(rflist, function(x) x$forest$nrnodes))
rf$forest$nrnodes <- nrnodes
rf$forest$ndbigtree <- unlist(sapply(rflist, function(x) x$forest$ndbigtree))
rf$forest$nodestatus <- do.call("cbind", lapply(rflist,
function(x) padm0(x$forest$nodestatus, nrnodes)))
rf$forest$bestvar <- do.call("cbind", lapply(rflist,
function(x) padm0(x$forest$bestvar, nrnodes)))
rf$forest$xbestsplit <- do.call("cbind", lapply(rflist,
function(x) padm0(x$forest$xbestsplit, nrnodes)))
rf$forest$nodepred <- do.call("cbind", lapply(rflist,
function(x) padm0(x$forest$nodepred, nrnodes)))
tree.dim <- dim(rf$forest$treemap)
if (classRF) {
rf$forest$treemap <- array(unlist(lapply(rflist,
function(x) apply(x$forest$treemap, 2:3, pad0,
nrnodes))), c(nrnodes, 2, ntree))
}
else {
rf$forest$leftDaughter <- do.call("cbind", lapply(rflist,
function(x) padm0(x$forest$leftDaughter, nrnodes)))
rf$forest$rightDaughter <- do.call("cbind", lapply(rflist,
function(x) padm0(x$forest$rightDaughter, nrnodes)))
}
rf$forest$ntree <- ntree
if (classRF)
rf$forest$cutoff <- rflist[[1]]$forest$cutoff
}
else {
rf$forest <- NULL
}
#
#Tons of stuff removed here...
#
if (classRF) {
rf$confusion <- NULL
rf$err.rate <- NULL
if (haveTest) {
rf$test$confusion <- NULL
rf$err.rate <- NULL
}
}
else {
rf$mse <- rf$rsq <- NULL
if (haveTest)
rf$test$mse <- rf$test$rsq <- NULL
}
rf
}
을 그리고 당신은 이런 식으로 테스트 할 수 있습니다 :
data(iris)
d <- iris[sample(150,150),]
d1 <- d[1:70,]
d2 <- d[71:150,]
rf1 <- randomForest(Species ~ ., d1, ntree=50, norm.votes=FALSE)
rf2 <- randomForest(Species ~ ., d2, ntree=50, norm.votes=FALSE)
rf.all <- my_combine(rf1,rf2)
predict(rf.all,newdata = iris)
물론, 이것은 전혀 보증이 제공됩니다! :)
두 데이터 세트의 정확한 구조는 무엇입니까? (볼 때'str '을 사용하십시오). 그들은 똑같은 변수를 가지고 있으며, 모두 똑같은 이름을 갖고 있습니까? – joran
@ joran. 그들은 수동으로 분리 된 더 큰 훈련 세트의 서브 세트입니다. 'str'을 실행할 때 첫번째 줄은 : 'data.frame': \t 38735 obs. 55 개의 변수들 :'d1과''data.frame ': \t 38734 obs. 55 개의 변수 중 'd2'. 각 데이터 세트에 대해 동일한 이름이옵니다. – josh
각 rf 개체의 votes 개체의 크기를 확인하십시오. 모든 요인 수준이 교육 데이터의 각 하위 집합에 존재하지 않을 수도 있습니다. – joran