1 #' @include b_LinearAlgorithm.R
3 #' @title Ridge Regression Algorithm
5 #' @description Ridge Regression Algorithm.
6 #' Inherits \code{\link{LinearAlgorithm}}
8 #' @field lambda Value of lambda (let undefined for cross-validation). Default: undefined
9 #' @field lambdas Vector of "optimal" lambda values over time. TODO: remove for production
11 RidgeRegression = setRefClass(
12 Class = "RidgeRegression",
19 contains = "LinearAlgorithm",
22 predict_noNA = function(XY, x)
24 if (length(lambda) > 0 || nrow(XY) < 30) #TODO: magic number
26 #simple ridge regression with fixed lambda (not enough history for CV)
27 X = matricize(XY[,names(XY) != "Measure"])
29 lambda_ = ifelse(length(lambda) > 0, lambda, LAMBDA)
30 weight = ridgeSolve(X, Y, lambda_)
35 #enough data for cross-validations
36 require(MASS, quietly=TRUE)
37 gridLambda = seq(0.05,5.05,0.1)
38 res_lmr = lm.ridge(Measure ~ . + 0, data=XY, lambda = gridLambda)
39 lambda_ = res_lmr$lambda[which.min(res_lmr$GCV)]
40 weight = as.matrix(coef(res_lmr))[which.min(res_lmr$GCV),]
43 lambdas <<- c(lambdas, lambda_)
46 return (matricize(x) %*% weight)