plot - Plotting gradient descent function in R -


i have implemented multivariate linear regression in r followed batch update gradient descent algorithm. trying plot results of gradient descent.

i have found instructional links on how create plot here , here.

the problem these tutorials in both cases, explicitly define linear regression equation (also not multivariate).

how can create similar plots overlay results of running graddescent function several times different learning rates , convergence thresholds in code listed below:

data <- read.csv("data/bike-sharing-dataset/hour.csv")  # select useable features data1 <- data[, c("season", "mnth", "hr", "holiday", "weekday", "workingday", "weathersit", "temp", "atemp", "hum", "windspeed", "cnt")]  # set seed set.seed(100)  # split data trainingobs<-sample(nrow(data1),0.70*nrow(data1),replace=false)  # create training dataset trainingds<-data1[trainingobs,]  # create test dataset testds<-data1[-trainingobs,]  # create variables y <- trainingds$cnt y_test <- testds$cnt x <- as.matrix(trainingds[-ncol(trainingds)]) x_test <- as.matrix(testds[-ncol(testds)])  int <- rep(1, length(y))  # add intercept column x x <- cbind(int, x) x_test <- cbind(int, x_test)  # solve beta betas <- solve(t(x) %*% x) %*% t(x) %*% y  # round beta values betas <- round(betas, 2)  # gradient descent 1 gradientdesc <- function(x, y, learn_rate, conv_threshold, max_iter) {   n <- nrow(x)    m <- runif(ncol(x), 0, 1)   yhat <- x %*% m    cost <- sum((y - yhat) ^ 2) / (2*n)    converged = f   iterations = 0    while(converged == f) {     ## implement gradient descent algorithm     m <- m - learn_rate * ( 1/n * t(x) %*% (yhat - y))     yhat <- x %*% m     new_cost <- sum((y - yhat) ^ 2) / (2*n)      if( abs(cost - new_cost) <= conv_threshold) {       converged = t     }     iterations = iterations + 1     cost <- new_cost      if(iterations >= max_iter) break   }   return(list(converged = converged,                num_iterations = iterations,                cost = cost,               new_cost = new_cost,               coefs = m) ) }  out <- gradientdesc(x, y, 0.005, 0.0000001, 200000) 

note: data being used is-

bike-sharing-dataset

uci machine learning repository


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -