Description Usage Arguments Details Value Examples
View source: R/ridge_regression.R
The ridge regression model coeffecients are given by
\arg\min_{β} \parallel Y - Xβ \parallel^2 + λ \parallel β \parallel^2.
This is the implementation of the bayesian interpretation. Namely,
[y_i|x_i, β, τ] \sim N(x_i^tβ, τ^{-1}), i = 1,...,n ,
[β|η] \sim N(0, η^{-1}I),
τ \sim Gamma(\frac{1}{100}, \frac{1}{100}),
σ_β \sim unif(0, A)
where A = 1000 and τ = \frac{1}{σ_β^2}.
1 2 3 4 | ridge_regression <- function(
Y,
X,
niter = 10000)
|
Y |
n by 1 |
X |
n by p predictor matrix |
niter |
number of gibbs sampling iterations |
This function returns the generated parameters from the gibbs sampling markov chain.
beta |
An niter by p matrix |
sigma |
An niter by 1 matrix |
sigma_beta |
An niter by 1 matrix |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # Load the data
prostate.data = "https://web.stanford.edu/~hastie/ElemStatLearn/datasets/prostate.data"
prostate = read.table(file = "prostate.data", sep="", header = TRUE)
# Training data:
prostate_train = prostate[which(prostate$train),-10]
# Testing data:
prostate_test = prostate[which(!prostate$train),-10]
# Response:
y = prostate_train$lpsa
# Center and scale the data:
y = scale(y)
# And the predictors
X = scale(prostate_train[,names(prostate_train) != "lpsa"])
gibbs_ridge <- ridge_regression(y, X, niter=10000)
shrinkage_regression_plot(gibbs_ridge$beta, y, X, main = "Ridge Prior")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.