我正在为文本分析项目构建分类算法。一切似乎都在我构建模型,但是当我使用预测函数时,我得到了这个错误,我不明白。以下是我的代码。
#Load packages
library(caret)
library(tm)
library(SnowballC)
library(stringr)
#Training data.
data.t <- data[1:60]
data.t <- data.t[c(24,25,26)]
data.t[] <- lapply(data.t, str_trim)
is.na(data.t) <- data.t==''
data.t <- na.omit(data.t)
corpus <- VCorpus(VectorSource(data.t$PurpCk1))
##Create a text document matrix.
tdm <- DocumentTermMatrix(corpus, list(removePunctuation = TRUE, stripWhitespace = TRUE, content_transformer(tolower), stopwords = TRUE, stemming = FALSE, removeNumbers = FALSE))
##Convert to a data.frame for training and assign a classification (factor) to each document.
train <- as.matrix(tdm)
#Create condition code that differentiates individuals from conditions.
cond.code <- as.data.frame(data.t$Condition)
cond.code <- as.data.frame(ifelse(cond.code$`data.t$Condition`== "CSManipulation", 1,0))
train <- cbind(train, cond.code)
colnames(train)[ncol(train)] <- 'y'
train <- as.data.frame(train)
train$y <- as.factor(train$y)
##Train.
require(foreach)
registerDoSEQ()
#Training control prevents model over-fitting.
tc <- trainControl(method = "cv", number = 7, verboseIter=FALSE , preProcOptions="pca", allowParallel=TRUE)
bayesglm <- train(y ~ ., data = train, method = 'bayesglm', trControl=tc)
rf <- train(y ~ ., data = train, method = 'rf', trControl=tc)
NN <- train(y ~ ., data = train, method = 'nnet', trControl=tc, verbose=FALSE)
svml <- train(y ~ ., data = train, method = 'svmLinear', trControl=tc)
logitboost <- train(y ~ ., data = train, method = 'LogitBoost', trControl=tc)
#This is used to complare the models against one anoher.
model <- c("Bayes GLM", "Neural Net", "SVM (linear)", "LogitBoost")
Accuracy <- c(max(bayesglm$results$Accuracy),
max(NN$results$Accuracy),
max(svml$results$Accuracy),
max(logitboost$results$Accuracy))
Kappa <- c(max(bayesglm$results$Kappa),
max(NN$results$Kappa),
max(svml$results$Kappa),
max(logitboost$results$Kappa))
performance <- cbind(model,Accuracy,Kappa)
knitr::kable(performance)
#Create new dataset and fit models to new data.
data.n <- data[1:60]
data.n <- data.n[c(24,25,26)]
data.n <- rbind(data.t,data.n)
data.n[] <- lapply(data.n, str_trim)
is.na(data.n) <- data.n==''
data.n <- na.omit(data.n)
data.n <- unique(data.n)
data.n <- data.n$PurpCk1
data.n <- as.data.frame(data.n)
这是返回错误的代码部分。
#Fit model to new data
predict(svml,data.n)
Error in eval(predvars, data, env): object 'active' not found.