I want to simulate the predicted value from a linear regression model 1000 times and see how many of those times each type of car has the highest predicted mpg base on the independent variables of the model. I use the test and training set because I want to evaluate the fit of the model outside the training data.
data(mtcars)
library(caret)
trainingIndex <- createDataPartition(mtcars$mpg, p = 0.8, list = FALSE)
trainingset <- mtcars[trainingIndex,]
testingset <- mtcars[-trainingIndex,]
I create a data partition to have a training set and a test set. Now I have a test set and a training set I create the linear model
fit <- lm(mpg~., data = trainingset)
Now I have the linear model I tried to create a bootstrap to make the prediction from a simulation. I use boot_predict but it gives me an error.
library(finalfit)
boot_predict(fit,testingset, type = "response", R = 1000, estimate_name = NULL,
confint_sep = "to", condense = TRUE, boot_compare = TRUE, compare_name = NULL,
comparison = "difference", ref_symbol = "-", digits = c(2,3))
Error: Problem with mutate()
input term
.
x invalid format '%.2f'; use format %s for character objects
i Input term
is (function (x, digits) ...
.
Run rlang::last_error()
to see where the error occurred.
In addition: Warning message:
In predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
prediction from a rank-deficient fit may be misleading
I am not sure if this is the best way to obtaining the 1000 prediction from the bootstrap