Luca Vignali
library(caret);library(ggplot2);library(Rmisc)
newdata <- data.frame(hp=c(mtcars$hp))
lm_model <- train(mpg ~ hp, data = mtcars, method = "gamLoess")
conf <- predict(lm_model$finalModel, newdata, interval="confidence")
newdata2 <- cbind(newdata,conf)
g <- ggplot(data = mtcars, aes(hp,mpg)) + geom_point()
g <- g + geom_smooth(method="loess", level = 0.95, linetype = 0)
g <- g + geom_point(data=newdata2, aes(x=hp,y=conf), col="red")
df <- data.frame(x = lm_model$finalModel$fitted.values, ydf = lm_model$finalModel$residuals)
g2 <- ggplot(df,aes(x,ydf)) + geom_point() +
labs(x = "mpg Fitted Values", y = "Residuals")
multiplot(g,g2,cols=2)