Skip to content

Instantly share code, notes, and snippets.

@smc77
Created October 5, 2011 02:01
Show Gist options
  • Save smc77/1263432 to your computer and use it in GitHub Desktop.
Save smc77/1263432 to your computer and use it in GitHub Desktop.
Fitting various random lines to the housing data to get an intuition about the loss function.
# Example of randomly chosen lines
plot(housing)
abline(0, 5, col="red")
abline(-50, 10, col="blue")
# Create the loss function
loss <- function(intercept, slope) sum(((intercept + (slope * housing[, "num.rooms"])) - housing[, "median.values"])^2)/2
# Create some data for a given line and compute the loss
loss(0, 5)
loss(-30, 10)
# Test a few different slopes with different intercepts
x <- -50:50
y <- -10:10
z <- sapply(x, function(intercept) (sapply(y, function(slope, intercept) loss(intercept, slope), intercept=intercept)))
rownames(z) <- y
colnames(z) <- x
# 3D plot of loss function
library(lattice)
wireframe(z, shade=TRUE, xlab="theta0", ylab="theta1", zlab="loss function", aspect = c(61/87, 0.4), light.source = c(10,0,10))
# Contour plot
library(reshape)
library(ggplot2)
loss.values <- as.data.frame(melt(z))
names(loss.values) <- c("slope", "intercept", "loss")
v <- ggplot(loss.values, aes(intercept, slope, z = loss))
v + geom_tile(aes(fill = loss)) + stat_contour()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment