Skip to content

Instantly share code, notes, and snippets.

@smc77
Created October 17, 2011 01:53
Show Gist options
  • Save smc77/1291757 to your computer and use it in GitHub Desktop.
Save smc77/1291757 to your computer and use it in GitHub Desktop.
Linear regression gradient descent.
# Load data and initialize values
data <- read.csv("http://www.statalgo.com/wp-content/uploads/2011/10/housing.csv")
alpha <- 0.01
m <- nrow(data)
x <- matrix(c(rep(1,m), data$area), ncol=2)
y <- matrix(data$price, ncol=1) / 1000
# Z-Score the feature
x.scaled <- x
x.scaled[,2] <- (x[,2] - mean(x[,2]))/sd(x[,2])
# Gradient descent function
grad <- function(x, y, theta) {
gradient <- (t(x) %*% ((x %*% t(theta)) - y))
return(t(gradient))
}
gradient.path <- function(x) {
# Initialize the parameters
theta <- matrix(c(0, 0), nrow=1)
# Look at the values over each iteration
theta.path <- matrix(ncol=2)
for (i in 1:500) {
theta <- theta - alpha * 1/m * grad(x, y, theta)
if(all(is.na(theta))) break
theta.path <- rbind(theta.path, theta)
}
theta.path
}
unscaled.theta <- gradient.path(x)
scaled.theta <- gradient.path(x.scaled)
summary(lm(y ~ x[, 2]))
summary(lm(y ~ x.scaled[, 2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment