Skip to content

Instantly share code, notes, and snippets.

@ivyleavedtoadflax
Created March 28, 2016 13:11
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 ivyleavedtoadflax/73d4911cbb9fcc17c5c9 to your computer and use it in GitHub Desktop.
Save ivyleavedtoadflax/73d4911cbb9fcc17c5c9 to your computer and use it in GitHub Desktop.
# Load packages including ggplot2
library(dplyr)
library(purrr)
library(ggplot2)
# Follow example as at: https://github.com/hadley/purrr/blob/master/README.md
random_group <- function(n, probs) {
probs <- probs / sum(probs)
g <- findInterval(seq(0, 1, length = n), c(0, cumsum(probs)),
rightmost.closed = TRUE)
names(probs)[sample(g)]
}
partition <- function(df, n, probs) {
replicate(n, split(df, random_group(nrow(df), probs)), FALSE) %>%
transpose() %>%
as_data_frame()
}
msd <- function(x, y) sqrt(mean((x - y) ^ 2))
# Generate 100 random test-training splits
boot <- partition(mtcars, 100, c(training = 0.8, test = 0.2))
boot
boot <- boot %>%
mutate(
# Fit the models
models = map(training, ~ lm(mpg ~ wt, data = .)),
# Make predictions on test data
preds = map2(models, test, predict),
diffs = map2(preds, test %>% map("mpg"), msd)
)
# Fails with following error:
# Error in eval(expr, envir, enclos) : invalid term in model formula
# Now detach `ggplot2` and try again
detach("package:ggplot2", unload=TRUE)
boot <- boot %>%
dplyr::mutate(
# Fit the models
models = map(training, ~ lm(mpg ~ wt, data = .)),
# Make predictions on test data
preds = map2(models, test, predict),
diffs = map2(preds, test %>% map("mpg"), msd)
)
# Works this time...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment