Last active
February 26, 2019 01:49
-
-
Save toyeiei/2156c9b3745cccb9626c3d4bfbb5bcc6 to your computer and use it in GitHub Desktop.
Implementing gradient descent for linear regression in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gradient descent function takes five arguments | |
gradDesc <- function(df, x, y, alpha = 0.01, max_iter = 100000){ | |
# scale x for faster training | |
start_time <- proc.time() | |
n <- nrow(df) | |
x <- as.vector(scale(df[[x]])) | |
y <- df[[y]] | |
plot(x, y, pch = 16) | |
# initialize random weights | |
m <- runif(1,0,1) | |
c <- runif(1,0,1) | |
yhat <- m * x + c | |
mse <- (1/n) * sum((y - yhat) ** 2) | |
converged <- F | |
iteration <- 0 | |
# update weight using GD algorithm | |
cat("=== Implementing gradient descent algorithm ===") | |
while(converged == F){ | |
iteration <- iteration + 1 | |
m_new <- m - alpha * (1/n) * sum((yhat - y) * x) | |
c_new <- c - alpha * (1/n) * sum(yhat - y) | |
m <- m_new | |
c <- c_new | |
yhat <- m * x + c | |
mse_new <- (1/n) * sum((y - yhat) ** 2) | |
# if iteration hits max_iter, program ends | |
if(iteration == max_iter){ | |
converged <- T | |
abline(c, m) | |
return(cat("\nOptimal intercept:", c, | |
"\nOptimal slope:", m, | |
"\nIteration:", iteration, | |
"\nFinal MSE:", mse_new, | |
"\nTime for training:", | |
(proc.time() - start_time)[1], "seconds.")) | |
} | |
} | |
} | |
# test function x=wt, y=mpg | |
gradDesc(mtcars, x = "wt", y = "mpg", alpha = 0.001, max_iter = 100000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment