Skip to content

Instantly share code, notes, and snippets.

@vankesteren
Created February 21, 2018 21:19
Show Gist options
  • Save vankesteren/88621f8a0d532083691e6f2a505dd5c7 to your computer and use it in GitHub Desktop.
Save vankesteren/88621f8a0d532083691e6f2a505dd5c7 to your computer and use it in GitHub Desktop.
multiple regression through coordinate descent
mrc <- function(X, y, max.iter = 1000, tol = .Machine$double.eps) {
# Multiple regression using coordinate descent
# Input: X matrix, y vector
# Output: estimated beta vector
# init ----
bhat <- numeric(ncol(X))
convergence <- FALSE
i <- 0L
# Coordinate descent ----
# calculate the sum of squares of each x
xss <- apply(X, 2, crossprod)
while (!convergence && i < max.iter) {
i <- i + 1
bprev <- bhat # remember previous beta estimate
for (j in 1:length(bhat)) {
# calculate residuals w.r.t. remaining variables
yres <- y - X[,-j] %*% bhat[-j]
# calculate current bhat using univariate regression of y_res on x_j
bhat[j] <- t(X[,j]) %*% yres / xss[j]
}
# calculate squared difference to determine convergence
dif <- (bprev - bhat) %*% (bprev - bhat)
if (dif < tol) convergence <- TRUE
}
if (!convergence) warning("Algorithm did not converge!")
bhat
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment