EDIT2:
Okay, so based on responses below, I've gotten a somewhat clearer understanding of the problem. I've also closely re-read the section about RR in TESL by Hastie, Tibshirani and Friedman, where I discovered that the intercept is often estimated simply as the mean of the response. It seems that many sources on RR online are overly vague. I actually suspect many writers have never implemented RR themselves and might not have realized some important things as many of them leave out 3 important facts:
- Intercept is not penalized in the normal case, the formula above only applies to the other coefficients.
- RR is not equivariant under scaling, i.e. different scales gives different results even for the same data.
- Following from 1, how one actually estimates intercept.
I tried altering my function accordingly:
fitRidge <- function(X, Y, lambda) {
# Standardize X and Y
X <- scale(X)
Y <- scale(Y)
# Generate penalty matrix
penalties <- lambda * diag(ncol(X))
# Estimate intercept
inter <- mean(Y)
# Solve ridge system
coeff <- solve(t(X) %*% X + penalties, t(X) %*% Y)
# Create standardized weight vector
wz <- c(inter, coeff )
return(wz)
}
I still don't get results equivalent to lm.ridge though, but it might just be a question of translating the formula back into the original scales. However, I can't seem to work out how to do this. I thought it would just entail multiplying by the standard deviation of the response and adding the mean, as usual for standard scores, but either my function is still wrong or rescaling is more complex than I realize.
Any advice?