1

我正在使用 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)
}
4

1 回答 1

2

RWeka软件包有一个将M5P树转换为party对象的示例。如果您运行example("M5P", package = "RWeka"),则树可视化实际上是由partykit. 运行示例后,请参阅plot(m3)as.party(m3)

然而,虽然J48你可以获得一个完全成熟constparty的对象,但对于M5P. 在后一种情况下,树结构本身可以转换为,party但节点内的线性模型不能完全直接转换为lm对象。因此,如果您想使用party表示来计算仅依赖于树结构的度量(例如,用于拆分的变量、拆分数量、拆分点等),那么您可以这样做。但是,如果您想计算依赖于模型或预测的度量(例如,均方误差等),那么party该类将没有多大帮助。

于 2016-05-15T19:38:48.470 回答