-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRidgeEstimator.R
More file actions
44 lines (38 loc) · 1.23 KB
/
RidgeEstimator.R
File metadata and controls
44 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
####################
# Ridge estimator ##
####################
RidgeEstimator <- function(y, X, r)
{
# Description :
# The Computation of ridge estimator based on a given tuning
# parameter.
# Usage :
# RidgeEstimato(y, X, r)
# Arguments :
# y : A vector of observations of length n.
# X : A design matrix of dimension n * p.
# r : A numerical value representing the tuning parameter for ridge.
# Returns :
# A vector of dimension p.
##### Compute the ridge solution by SVD
if (dim(X)[1] <= dim(X)[2]){
SVD <- svd(X)
U <- SVD$u
V <- SVD$v
} else {
SVD <- svd(X, nu = dim(X)[1], nv = dim(X)[2])
U <- SVD$u
V <- SVD$v
}
if (dim(X)[1] <= dim(X)[2]){
D.pseudo.Inverse <- matrix(rep(0, dim(X)[1] * dim(X)[1]),
nrow = dim(X)[1],
ncol = dim(X)[1])
} else {
D.pseudo.Inverse <- matrix(rep(0, dim(X)[1] * dim(X)[2]),
nrow = dim(X)[2],
ncol = dim(X)[1])
}
diag(D.pseudo.Inverse) <- SVD$d / (SVD$d ^ 2 + r)
return(as.vector(V %*% D.pseudo.Inverse %*% t(U) %*% y))
}