r caret train with different function signature different result

13 Views Asked by At

I suppose in caret, we can train model with different signature:

train(X,y,...)

or

train(y~.,data,...)

Seems it should give the same result, since it only differ in form, they should essentially be the same, however, In one case I found something unexpected, their results could be different, with the training data has NA,the one using formula won't succeed.

library(caret)
Titanic_df = as.data.frame(Titanic)
Titanic_df$Age[c(1,3,5)] = NA

#we can't get a valid result here.
titanic_model = train(Survived~.,data = Titanic_df,method = 'glm',family = "binomial")
#> Error in na.fail.default(structure(list(Survived = structure(c(1L, 1L, : missing values in object

#model succeed in training,although if predict on the row has NA, result is also NA.
titanic_model = train(x = within(Titanic_df,rm(Survived)),y = Titanic_df$Survived,method = 'glm')

Created on 2024-02-09 with reprex v2.0.2

0

There are 0 best solutions below