Skip to content

Instantly share code, notes, and snippets.

@topepo
topepo / topic_gen.R
Last active July 31, 2018 01:32
Ph.D topic (and Biometrics journal) topic simulator
# https://twitter.com/topepos/status/1024001039278714880
topic_gen <- function(x) {
require(glue)
prefix <- c("adaptive", "auto-regressive", "regularized", "multivariate",
"semiparametric", "Bayesian", "elastic", "variational", "robust",
"nonparametric", "flexible", "overdispersed", "latent",
"interpretable", "dynamic", "interval censored", "novel")
mod <- c("logistic regression", "Cox model", "mixed effects model",
@topepo
topepo / example.Rmd
Created July 9, 2019 01:21
simple Rmd document containing test cases
---
title: "A simple Test Example"
output: html_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(testthat)
library(sessioninfo)
options(width = 100)
@topepo
topepo / min_grid.R
Last active July 19, 2019 17:27
prototype for processing grids in terms of sub-models
# _Prototype_ code to find the minimum grid that should be fit for models. This
# exploits the fact that some models can evaluate extra sub-models from the same
# object.
# devtools::install_github("tidymodels/parsnip")
# devtools::install_github("tidymodels/dials")
library(tidymodels)
#> ── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────── tidymodels 0.0.2 ──
#> ✔ broom 0.5.1 ✔ purrr 0.3.2
@topepo
topepo / vary.R
Created August 4, 2019 21:06
Alternative infrastructure for finding and coordinating varying parameters in recipes and models
# A prototype mechanism to collect the tunable parameters and their sources. It
# also allows for parameter with the same to be tuned. For example, you might
# model some variables with a spline but want to allow for different degrees
# of freedom by adding a different `step_ns()` for each variable. This interface
# allows the user to add an annotation for the parameter so that we can tell the
# different parameters apart.
# Limitations:
# - Currently, only one varying value is allowed per argument. For example, if
# some argument had a length two vector as its format, you can do something
product review score
B001E4KFG0 I have bought several of the Vitality canned dog food products and have found them all to be of good quality. The product looks more like a stew than a processed meat and it smells better. My Labrador is finicky and she appreciates this product better than most. great
B00813GRG4 "Product arrived labeled as Jumbo Salted Peanuts...the peanuts were actually small sized unsalted. Not sure if this was an error or if the vendor intended to represent the product as ""Jumbo""." other
B000LQOCH0 "This is a confection that has been around a few centuries. It is a light, pillowy citrus gelatin with nuts - in this case Filberts. And it is cut into tiny squares and then liberally coated with powdered sugar. And it is a tiny mouthful of heaven. Not too chewy, and very flavorful. I highly recommend this yummy treat. If you are familiar with the story of C.S. Lewis' ""The Lion, The Witch, and The Wardrobe"" - this is the treat that seduces Edmund into selling out his Brother and Sisters
@topepo
topepo / discrim_animate.R
Created October 11, 2019 14:30
animating a classification boundary
library(tidymodels)
library(tune)
library(discrim)
library(klaR)
library(gganimate)
options(width = 100)
theme_set(theme_bw())
data("parabolic", package = "rsample")
@topepo
topepo / finalize.R
Created October 30, 2019 13:16
prototype code to update parsnip and recipe objects with final parameter values
finalize_model <- function(x, param) {
if (!inherits(x, "model_spec")) {
stop("`x` should be a parsnip model specification.")
}
# check for nrow > 1 and tibble/list
pset <- parameters(x)
if (tibble::is_tibble(param)) {
param <- as.list(param)
}
@topepo
topepo / get_default_values.R
Created December 4, 2019 01:22
(try to) determine the default values for parsnip model arguments
get_default_values <- function(model, engine, mode) {
eng <- engine
mod_args <-
parsnip::get_from_env(paste0(model, "_args")) %>%
dplyr::filter(engine == eng)
model_fun <-
parsnip::get_from_env(paste0(model, "_fit")) %>%
dplyr::filter(engine == eng) %>%
dplyr::mutate(pkg = purrr::map_chr(value, ~ .x$func["pkg"]),
fun = purrr::map_chr(value, ~ .x$func["fun"]))
@topepo
topepo / roc_shiny.R
Created December 5, 2019 21:02
shiny application for teaching ROC curves
library(shiny)
library(ggplot2)
library(dplyr)
library(yardstick)
theme_set(theme_bw())
n <- 1000
set.seed(124254)
@topepo
topepo / flight_example.R
Created February 25, 2020 18:29
data analysis for nyc flights data set
library(tidymodels)
library(nycflights13)
set.seed(25213)
flight_data <-
flights %>%
mutate(
delay = ifelse(arr_delay >= 30, "late", "on_time"),
delay = factor(delay),
date = as.Date(time_hour)