Skip to content

Instantly share code, notes, and snippets.

@Laurae2
Created February 24, 2017 15:00
Show Gist options
  • Save Laurae2/1a0f6df90f3d1231c99b195722bf33ee to your computer and use it in GitHub Desktop.
Save Laurae2/1a0f6df90f3d1231c99b195722bf33ee to your computer and use it in GitHub Desktop.
Gradient Descent levelplot for linear regression
# Generate random values
set.seed(11111)
x <- rnorm(n = 50)
y <- 10 * x + rnorm(n = 50)
# Add intercept
x <- cbind(1,x)
# Assume random null parameters
param <- c(0,0)
# Store count of observations
m <- length(y)
# Calculate Mean Squared Error cost
cost <- function(x, y, param) {mean(((x %*% param)- y) ^ 2)}
# Plot 2D level plot using base/graphics R
z <- matrix(nrow = 200, ncol = 200)
for (i in 1:200) {
for (j in 1:200) {
z[i, j] <- cost(x, y, c(i/10 - 10, j/10 - 5))
}
}
image(x = (1:200)/10 - 10, y = (1:200)/10 - 5, z = z, xlab = "Intercept", ylab = "x Multiplicator", main = "Loss function vs Coefficients", sub = paste0("Cost = ", min(z))) + abline(v = -0.2381) + abline(h = 9.9966)
# Plot 2D level plot using lattice
z <- data.frame(x = (1:200)/10 - 10,
y = (1:200)/10 - 5)
z <- data.frame(expand.grid(z))
z <- data.frame(z, z = apply(z, 1, function(x) {cost(get("x", envir = globalenv()), get("y", envir = globalenv()), c(x[1], x[2]))}))
lattice::levelplot(z~x*y, data = z, panel = function(...) {panel.levelplot(...); panel.abline(v = -0.2381); panel.abline(h = 9.9966)}, cuts = 50, xlab = "Intercept", ylab = "x Multiplicator", main = "Loss function vs Coefficients", sub = paste0("Cost = ", min(z$z)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment