Tuesday, April 12, 2016

R's predict() function object is not found

Leave a Comment

Im trying to pass in new data to the predict function , and the new data is partial only 3 of the 110+ columns are filled, Im getting the error:

Error in eval(expr, envir, enclos) : object 'Cell' not found

although column "Cell" is present, below is my R script:

library("AppliedPredictiveModeling") library("ElemStatLearn") library("pgmm") library("rpart") library("gtools") library("caret")  # load data data(segmentationOriginal)  # Subset the data to a training set and testing set based on # the Case variable in the data set.  training <- segmentationOriginal[which(segmentationOriginal$Case ==    "Train"), ]  testing <- segmentationOriginal[which(segmentationOriginal$Case ==    "Test"), ]  # Set the seed to 125 and fit a CART model with the rpart # method using all predictor variables and default caret # settings.  set.seed(125)  modFit <- train(Class ~ ., method = "rpart", data = training)  modFit$finalModel  # use new values to predict , TotalIntench2 = 23,000; # FiberWidthCh1 = 10; PerimStatusCh1=2  # create new data frame based on old data frame columns, this # will contain new data training1 <- training[0, ]  # create data frame with test values newdata = data.frame(TotalIntench2 = 50000, FiberWidthCh1 = 10,    VarIntenCh4 = 100)  # use gtools package smartbind() training1 <- training1[nrow(training1) + 1, ] training1 <- smartbind(training1, newdata)  # remove initial empty row training1[-c(1), ]  # inspect training1[1, 0]  predict(modFit, newdata = training1[1, 0]) 

How can I pass new data with partial columns populated to the predict() function?

Thanks

1 Answers

Answers 1

I guess, in general for rpart, you cannot transfer partial columns predict. if you have no data for some of the columns, set them as NAs and and pass those columns to the dataframe (newdata).

simply apply the below command with right data.

predict(modFit, newdata )

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment