我使用formula.tools包创建了一个函数,它“概括”了模型的sintaxis,后续的预测和混淆矩阵,但是我得到了一个错误。这是代码:
library(formula.tools)
#Function ej.predict, package formula.tools is neccesary to execute it
ej.predict <- function(df, formula, sample1, METHOD) {
ttesting <- df[sample1, ]
ttraining <- df[-sample1, ]
if(METHOD == "knn") {
mod.1 <- train.kknn(formula, data = ttraining, kmax=trunc(sqrt(dim(ttraining)[1])))}
else if (METHOD == "ada"){
mod.1 <- ada(formula, data = ttraining, iter = 20, nu = 1,type = "discrete")}
else if (METHOD == "svm"){
mod.1 <- svm(formula, data = ttraining, kernel = "radial")}
else if (METHOD == "Arbol"){
mod.1 <- rpart(formula, data = ttraining)}
else if (METHOD == "RF"){
mod.1 <- randomForest(formula, data = ttraining)}
else if (METHOD == "Redes"){
mod.1 <- nnet(formula, data = ttraining, size = 50, rang = 0.1,
decay = 5e-04, maxit = 200, trace = FALSE, MaxNWts = 20000)}
pred1 <- predict(mod.1, ttesting, type = "class")
var.disc <- lhs.vars(formula)
MC <- table(ttesting[, var.disc], pred1)
return(MC)
}
使用虹膜数据举例:
data(iris)
ttrain <- iris[60, ]
ej.predict(iris, Species ~ ., ttrain, "RF")
我收到了这个错误:
Error in xj[i] : invalid subscript type 'list'
有人可以告诉我如何解决它吗?