Skip to content

Instantly share code, notes, and snippets.

@ryanbthomas
Created November 25, 2019 22:33
Show Gist options
  • Save ryanbthomas/ee1d5e1ce9dd7c48c1363e6846644de8 to your computer and use it in GitHub Desktop.
Save ryanbthomas/ee1d5e1ce9dd7c48c1363e6846644de8 to your computer and use it in GitHub Desktop.
x <- rnorm(1000, 7, 2)
noise <- rnorm(1000)
y <- 5 + 2 * x + noise
df <- data.frame(x = x, y = y)
test_idx <- sample(1000, 100)
test_df <- df[test_idx, ]
train_df <- df[!(seq_len(1000) %in% test_idx), ]
my_model <- lm(y ~ x, data = train_df)
my_estimate <- test_df %>%
mutate(est_val = predict(my_model, newdata = .),
res = est_val - y)
ggplot(my_estimate, aes(x, y = res)) +
geom_point() +
geom_hline(yintercept = mean(my_estimate$res), color = "red")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment