Skip to content

Instantly share code, notes, and snippets.

@conormm
Created January 3, 2018 23:14
Show Gist options
  • Select an option

  • Save conormm/4719c04073cf1fb030a4162a150bbc40 to your computer and use it in GitHub Desktop.

Select an option

Save conormm/4719c04073cf1fb030a4162a150bbc40 to your computer and use it in GitHub Desktop.
library(modelr)
library(dplyr)
library(ggplot2)
library(animation)
library(gganimate)
options(scipen = 999)
n <- 200 # number of observations
bias <- 4
slope <- 3.5
dot <- `%*%` # defined for personal preference
x <- rnorm(n) * 2
x_b <- cbind(x, rep(1, n))
y <- bias + slope * x + rnorm(n)
df <- data_frame(x = x, y = y)
learning_rate <- 0.05
n_iterations <- 100
theta <- matrix(c(20, 20))
b0 <- vector("numeric", length = n_iterations)
b1 <- vector("numeric", length = n_iterations)
sse_i <- vector("numeric", length = n_iterations)
for (iteration in seq_len(n_iterations)) {
residuals_b <- dot(x_b, theta) - y
gradients <- 2/n * dot(t(x_b), residuals_b)
theta <- theta - learning_rate * gradients
sse_i[[iteration]] <- sum((y - dot(x_b, theta))**2)
b0[[iteration]] <- theta[2]
b1[[iteration]] <- theta[1]
}
model_i <- data.frame(model_iter = 1:n_iterations,
sse = sse_i,
b0 = b0,
b1 = b1)
model_i$affine <- model_iter
p1 <- df %>%
ggplot(aes(x=x, y=y)) +
geom_abline(aes(intercept = b0,
slope = b1,
colour = -sse,
frame = model_iter),
data = model_i,
alpha = .50
) +
geom_point(alpha = 0.4) +
geom_abline(aes(intercept = b0,
slope = b1),
data = model_i[100, ],
alpha = 0.5,
size = 2,
colour = "dodger blue") +
geom_abline(aes(intercept = b0,
slope = b1),
data = model_i[1, ],
colour = "red",
alpha = 0.5,
size = 2) +
scale_color_continuous(low = "red", high = "grey") +
guides(colour = FALSE) +
theme_minimal()
p_gif <- df %>%
ggplot(aes(x=x, y=y)) +
geom_abline(aes(intercept = b0,
slope = b1,
colour = -sse,
frame = model_iter),
data = model_i[1:30, ],
alpha = .50,
size = 2
) +
geom_point(alpha = 0.4) +
scale_color_continuous(low = "red", high = "dodger blue")
library(gganimate)
gganimate(p_gif, interval = 0.2, "lr_output.gif")
p2 <- model_i[1:30,] %>%
ggplot(aes(model_iter, sse, colour = -sse)) +
geom_point(alpha = 0.4) +
theme_minimal() +
labs(x = "Model iteration",
y = "Sum of Sqaured errors") +
scale_color_continuous(low = "red", high = "dodger blue") +
guides(colour = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment