Doug Fir
Doug Fir

Reputation: 21242

Difference in model object returned by caret vs. base

I have equivalent models from base r and caret:

base_lm <- lm(mpg ~ cyl, data = mtcars)

library(caret)
caret_lm <- train(
  mpg ~ cyl,
  data = mtcars,
  method = "lm"
)

I wanted to use the statisticalModelling package with my linear model from caret:

statisticalModeling::evaluate_model(caret_lm)
Error in UseMethod("explanatory_vars") : 
  no applicable method for 'explanatory_vars' applied to an object of class "c('train', 'train.formula')"

The tried:

statisticalModeling::evaluate_model(caret_lm$finalModel)
Error in eval(expr, envir, enclos) : object 'dat' not found

It does with with base r linear model

statisticalModeling::evaluate_model(base_lm)

  cyl model_output
1   0    37.884576
2   5    23.505626
3  10     9.126675

Is there a way to use caret models with statistical modelling package?

Upvotes: 0

Views: 518

Answers (1)

David Heckmann
David Heckmann

Reputation: 2939

Use the finalModel slot of the train object and specify the data to predict explicitly:

pred_base <- evaluate_model(base_lm , data = mtcars[,1:2])

pred_caret <- evaluate_model(caret_lm$finalModel , data = mtcars[,1:2])
# compare predictions:
all( pred_base$model_output == pred_caret$model_output )
[1] TRUE

Upvotes: 2

Related Questions