简体   繁体   中英

R caret package (rpart)

I get the below error when using rpart library

dt <- rpart(formula, method="class", data=full.df.allAttr.train);

Error in model.frame.default(formula = formula, data = full.df.allAttr.train,  : 
  object is not a matrix

When i convert full.df.allAttr.trainto matrix

dt <- rpart(formula, method="class", data= as.matrix( full.df.allAttr.train));

Error in model.frame.default(formula = formula, data = as.matrix(full.df.allAttr.train),  : 
  'data' must be a data.frame, not a matrix or an array

When i check for the class type its a data frame

class(full.df.allAttr.train)

[1] "data.frame"

thank you for the inputs , the error went off when i created the formula with the proper column name which has the outcomes.

measurevar <- "SpeakerName"
formula_str <- paste(measurevar, paste(rowNames, collapse=" + "), sep=" ~ ")
formula <- as.formula(formula_str) 

It give a different error since my data frame has row.names as text below is the snapshot

Error in model.frame.default(formula = formula, data = full.df.train,  : 
  variable lengths differ (found for 'character(0)')

在此处输入图片说明

Sorry new to this i will add the full source code and data sets

library(tm)
library(rpart)
obamaCorpus <- Corpus(DirSource(directory = "D:/R/Chap 6/Speeches/obama" , encoding="UTF-8"))
romneyCorpus <- Corpus(DirSource(directory = "D:/R/Chap 6/Speeches/romney" , encoding="UTF-8"))
fullCorpus <- c(obamaCorpus,romneyCorpus)#1-22 (obama), 23-44(romney)
fullCorpus.cleansed <- tm_map(fullCorpus, removePunctuation)
fullCorpus.cleansed <- tm_map(fullCorpus.cleansed, stripWhitespace)
fullCorpus.cleansed <- tm_map(fullCorpus.cleansed, tolower)
fullCorpus.cleansed <- tm_map(fullCorpus.cleansed, removeWords, stopwords("english"))
fullCorpus.cleansed <- tm_map(fullCorpus.cleansed, PlainTextDocument)
#fullCorpus.cleansed <- tm_map(fullCorpus.cleansed, stemDocument)

full.dtm <- DocumentTermMatrix(fullCorpus.cleansed)
full.dtm.spars <- removeSparseTerms(full.dtm , 0.6)

full.matix <- data.matrix(full.dtm.spars)
full.df <- as.data.frame(full.matix)

full.df[,"SpeakerName"] <- "obama"
full.df$SpeakerName[21:44] <- "romney"

train.idx <- sample(nrow(full.df) , ceiling(nrow(full.df)* 0.6))
test.idx <- (1:nrow(full.df))[-train.idx]
rowNames <- colnames(full.df)

measurevar <- "SpeakerName"
formula_str <- paste(measurevar, paste(rowNames, collapse=" + "), sep=" ~ ")
formula <- as.formula(formula_str)
dt <- rpart(formula, method="class", data=full.df.train);

Fails at the last step

Data Sets are here https://drive.google.com/folderview?id=0B1SogodTE-kJSHF6aFRmQURsV0U&usp=sharing

You forgot to include full.df.train and your formula is not fine.

This will work:

full.df.train <- full.df[train.idx, ]
dt <- rpart(SpeakerName ~ ., method = "class", data = full.df.train)

The problem with your formula is that you include SpeakerName in both sides of ~ . If you want to use all variables, using the . expression is much easier and compact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM