-1

I'm trying to run a random forest on the basic Kaggle Titanic dataset and am running into the error below when running CARET's predict function. I've tried an as.data.frame for the p1 variable but I get an error saying it can't be coerced. How can I work around this?

TrainRF <- read.csv("C:\\Users\\andre\\Downloads\\titanic\\train.csv")
TrainRF <- na.omit(TrainRF)
TrainRF$Survived <- as.factor(TrainRF$Survived)
set.seed(1013)
random_model <- randomForest::randomForest(TrainRF$Survived~.,TrainRF)
p1 <- predict(random_model,train)

Error in model.frame.default(Terms, newdata, na.action = na.omit) : 'data' must be a data.frame, environment, or list

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Try using `random_model <- randomForest::randomForest(Survived~.,TrainRF)` – Ronak Shah Nov 12 '20 at 03:24
  • Same result. Thanks though! – Fading Captain Nov 12 '20 at 03:31
  • Is there a dataset called `train`? Or did you meant to use TrainRF instead of `train` in the `predict` function? – kangaroo_cliff Nov 12 '20 at 03:41
  • I created TrainRF as a copy of the train set so I could use it in the random forest. – Fading Captain Nov 12 '20 at 03:46
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 12 '20 at 04:00
  • 1
    why are you predicting on a function? if the fit works, you should predict on the training data or some other data.frame. ```predict(random_model,TrainRF)``` . i am voting to close this as it looks like a typo and insufficient information is to allow a reproducible example – StupidWolf Nov 12 '20 at 10:43
  • I'm trying to tune the random forest model I've created. How would suggest I use the predict function then? – Fading Captain Nov 12 '20 at 14:42
  • can you edit your question.. explicitly saying what is your aim, is it to tune ? is it to predict? – StupidWolf Nov 12 '20 at 16:28
  • 1
    As @StupidWolf points out, predict makes predictions based on data in a data frame, but you are trying to make predictions based on the train function in caret. I agree that this should really be closed as a reproducible example has not been given, and insufficient effort has been made to figure out what should be an easy to understand error message means. – Robert Wilson Nov 13 '20 at 16:18

1 Answers1

0

I'm having trouble reproducing this problem. Does this code throw the same error?

library(tidyverse)
install.packages("titanic")
library(titanic)

data("Titanic")
TrainRF <- titanic_train %>% 
  na.omit()
TrainRF$Survived <- as.factor(TrainRF$Survived)
set.seed(1013)
random_model <- randomForest::randomForest(TrainRF$Survived~.,TrainRF)
p1 <- predict(random_model,TrainRF)
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46