我正在使用 R 中的 RWeka 包将 M5' 树适合使用“M5P”的数据集。然后我想将生成的树转换为“派对”树,以便我可以访问变量重要性。as.party
我遇到的问题是,如果没有出现以下错误,我似乎无法使该功能正常工作:
"Error: all(sapply(split, head, 1) %in% c("<=", ">")) is not TRUE"
仅当我在 for 循环中应用该函数时才会出现此错误,但 for 循环是必要的,因为我正在运行 5 折交叉验证。
下面是我一直在运行的代码:
n <- nrow(data)
k <- 5
indCV <- sample( rep(1:k,each=ceiling(n/k)), n)
for(i in 1:k){
#Training data is for all the observations where indCV is not equal to i
training_data <- data.frame(x[-which(indCV==i),])
training_response <- y[-which(indCV==i)]
#Test the data on the fifth of the data where the observation indices are equal to i
test_data <- x[which(indCV==i),]
test_response <- y[which(indCV==i)]
#Fit a pruned model to the training data
fit <- M5P(training_response~., data=training_data, control=Weka_control(N=TRUE))
#Convert to party
p <- as.party(fit)
}