knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(bonuslabgroup8) library(caret) library(mlbench) library(leaps)
This vignette showcases how to do a simple prediction using the ridgereg() function.
reg1 = ridgereg(Petal.Length~Species, data = iris, lambda = 0.01) reg1$print() head(reg1$predict())
This example uses data from the BostonHousing dataset.
data("BostonHousing")
The data is split into a training and test set using the caret package, with 80% of the data in the training set and 20% of the data in the test set.
set.seed(3456) trainIndex <- createDataPartition(BostonHousing$medv, p = 0.8, list = FALSE, times = 1) bhTrain <- BostonHousing[trainIndex,] bhTest <- BostonHousing[-trainIndex,]
lmFit = caret::train(medv ~ ., data = bhTrain, method = "lm") lmFit
lfFit = caret::train(medv ~ ., data = bhTrain, method = "leapForward") lfFit
By looking at the metrics (RMSE, Rsquared, and MAE), we can see that the linear regression performs better than the linear regression with forward selection.
rr_create = function(){ rr = list(type = "Regression", library = "bonuslabgroup8", loop = NULL, prob = NULL) prm = data.frame(parameter = "lambda", class = "numeric", label = "Lambda") rr$parameters = prm rrgrid = function(x, y, len = NULL, search = "grid"){ data.frame(lambda = c(0.5, 0.1, 0.05, 0.01, 0.005, 0.001)) } rr$grid = rrgrid rrfit <- function(x, y, wts, param, lev, last, weights, classProbs, ...) { df = as.data.frame(x) df$resp = y ridgereg(formula = resp ~ ., data = df, lambda = param$lambda, ...) } rr$fit = rrfit rrpred <- function(modelFit, newdata, preProc = NULL, submodels = NULL){ modelFit$predict(newdata) } rr$predict <- rrpred rrsort <- function(x) x[order(x$lambda),] rr$sort <- rrsort rr$levels <- function(x) x@levels return(rr) } rr_method = rr_create() set.seed(998) fitControl <- trainControl(method = "repeatedcv", number = 10, repeats = 10) set.seed(825) ridgeFit <- train(medv ~., data = bhTrain, method = rr_method, trControl = fitControl ) # keep getting errors: predictions failed for ... Error in modelFit$predict(newdata) : unused argument (newdata)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.