Skip to content

Instantly share code, notes, and snippets.

@datalorax
Created May 22, 2020 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datalorax/598ead8bde5c408df1936ffe611bf5f6 to your computer and use it in GitHub Desktop.
Save datalorax/598ead8bde5c408df1936ffe611bf5f6 to your computer and use it in GitHub Desktop.
library(colorspace)
library(rayshader)
library(tidyverse)
mse <- function(x, y, a, b) {
pred <- a + b*x
resid2 <- (y - pred)^2
1/length(y)*sum(resid2)
}
set.seed(8675309)
n <- 1000
x <- rnorm(n)
a <- 5
b <- 1.3
e <- 4
y <- a + b*x + rnorm(n, sd = e)
sim_d <- tibble(x = x, y = y)
grid <- expand.grid(a = seq(-5, 10, 0.1), b = seq(-5, 5, 0.1))
surface <- grid %>%
mutate(cost = map2_dbl(a, b, ~mse(x, y, .x, .y))) %>%
ggplot(aes(a, b)) +
geom_raster(aes(fill = cost)) +
scale_fill_continuous_sequential(palette = "Terrain")
plot_gg(surface, multicore = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment