Search code examples
rmachine-learningregressionr-caret

How to plot predictive machine learning with caret?


I would like to plot knn regression, are there any functions or best ways to plot machine learning regressions? Once I chose the best model, what should I plot?

Many thanks for your help!

df <- mtcars
library(caret)
set.seed(123)
trainRowNumbers <- createDataPartition(df$mpg, p=0.8, list=FALSE)
trainData <- df[trainRowNumbers,]
testData <- df[-trainRowNumbers,]

y = trainData$mpg
preProcess_range_model <- preProcess(trainData, method='range')
trainData <- predict(preProcess_range_model, newdata = trainData)
trainData$mpg <- y

set.seed(123)
options(warn=-1)
subsets <- c(2:5, 8, 9, 12)
ctrl <- rfeControl(functions = rfFuncs,
                   method = "repeatedcv",
                   repeats = 5,
                   verbose = FALSE)
lmProfile <- rfe(x=trainData[, 2:11], y=trainData$mpg,
                 sizes = subsets,
                 rfeControl = ctrl)
lmProfile

control <- trainControl(method = "cv", number = 15)

set.seed(123)
model_lm = train(mpg ~ wt+hp+disp+cyl, data=trainData, method='lm', trControl = control)
model_lm
linear.predict <- predict(model_lm, testData)
linear.predict
postResample(linear.predict, testData$mpg) 
model_knn = train(mpg ~ wt+hp+disp+cyl, data=trainData, method='knn', trControl = control)
model_knn
knn.predict <- predict(model_knn, testData)
knn.predict
postResample(knn.predict, testData$mpg) 


Solution

  • You can plot two things as follows

    #To show the changes in RMSE with changing tuning parameters
    plot(model_knn)
    

    enter image description here

    #The observed vs. predicted plot
    library("lattice")
    library(mosaic)
    
    df1 <- data.frame(Observed=testData$mpg, Predicted=linear.predict)
    
    xyplot(Predicted ~ Observed, data = df1, pch = 19,  panel=panel.lmbands,
           band.lty = c(conf =2, pred = 1))
    

    enter image description here