day06examples

Ridge regression

set.seed(40)
lambda1 <- 1
X <- matrix(sample(c(-1,0,1), 15, replace=TRUE), nrow=3, ncol=5)
X
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1   -1   -1   -1   -1
## [2,]    1   -1    0   -1   -1
## [3,]    1    0    0    1    1
y <- c(10, 5, 8)
y
## [1] 10  5  8
beta <- solve(t(X)%*%X + diag(lambda1,5)) %*% t(X)%*% y
beta
##            [,1]
## [1,]  4.4528302
## [2,] -1.9622642
## [3,] -2.3207547
## [4,]  0.5283019
## [5,]  0.5283019
# marker 1
beta[1,]
## [1] 4.45283
# marker 2
beta[2,]
## [1] -1.962264
# marker 1
(X[,1]  %*% (y - X[,2:5] %*% matrix(beta[-1,]))) / (sum(X[,1]^2) + diag(lambda1,1))
##         [,1]
## [1,] 4.45283
# marker 2
(X[,2]  %*% (y - X[,-2] %*% matrix(beta[-2,]))) / (sum(X[,2]^2) + diag(lambda1,1))
##           [,1]
## [1,] -1.962264

Ridge regression - amounts of shrinkage

# marker 1
sum(X[,1]^2) / (sum(X[,1]^2) + diag(lambda1,1)) 
##      [,1]
## [1,] 0.75
# marker 2
sum(X[,2]^2) / (sum(X[,2]^2) + diag(lambda1,1)) 
##           [,1]
## [1,] 0.6666667
# marker 3
sum(X[,3]^2) / (sum(X[,3]^2) + diag(lambda1,1)) 
##      [,1]
## [1,]  0.5