merge with remote
[aggexp.git] / pkg / R / m_RidgeRegression.R
CommitLineData
a961f8a1
BA
1#' @include b_LinearAlgorithm.R
2
3#' @title Ridge Regression Algorithm
4#'
5#' @description Ridge Regression Algorithm.
6#' Inherits \code{\link{LinearAlgorithm}}
7#'
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
10#'
11RidgeRegression = setRefClass(
12 Class = "RidgeRegression",
13
14 fields = c(
15 lambda = "numeric",
16 lambdas = "numeric"
17 ),
18
19 contains = "LinearAlgorithm",
20
21 methods = list(
22 predict_noNA = function(XY, x)
23 {
24 if (length(lambda) > 0 || nrow(XY) < 30) #TODO: magic number
25 {
26 #simple ridge regression with fixed lambda (not enough history for CV)
27 X = matricize(XY[,names(XY) != "Measure"])
28 Y = XY[,"Measure"]
29 lambda_ = ifelse(length(lambda) > 0, lambda, LAMBDA)
30 weight = ridgeSolve(X, Y, lambda_)
31 }
32
33 else
34 {
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),]
41 }
42
43 lambdas <<- c(lambdas, lambda_)
44
45 appendWeight(weight)
46 return (matricize(x) %*% weight)
47 }
48 )
49)