Skip to content

Instantly share code, notes, and snippets.

@topepo
topepo / regression_diag_plots.R
Created May 6, 2021 21:57
Regression diagnostic plots for shinymodels
library(tidymodels)
library(rules)
tidymodels_prefer()
theme_set(theme_bw())
library(doMC)
registerDoMC(cores = 20)
# ------------------------------------------------------------------------------
@topepo
topepo / pca_sign_flip_simulation.R
Last active March 25, 2021 17:45
A small simulation to see how often PCA loading and score signs flip form data perturbations
suppressPackageStartupMessages(library(tidymodels))
library(furrr)
plan(multisession, workers = 20)
data(cells)
set.seed(1)
split <- initial_split(cells, prop = .95)
cell_train <- training(split)
library(tidymodels)
library(patchwork)
library(doMC)
registerDoMC(cores = 20)
theme_set(theme_bw() + theme(legend.position = "top"))
# ------------------------------------------------------------------------------
url <- "https://github.com/topepo/cars/raw/master/2019_07_03_city/car_data_splits.RData"
@topepo
topepo / regularized_logistic_test.R
Created February 11, 2021 22:47
LiblineaR, glmnet, and glm
# pak::pak("tidymodels/parsnip@logistic-liblinear")
library(AppliedPredictiveModeling)
library(tidymodels)
theme_set(theme_bw())
# ------------------------------------------------------------------------------
lr_pull <- function(pen, eng = "glmnet", dat, ...) {
logistic_reg(penalty = pen, ...) %>%
@topepo
topepo / chicago_pls_dalex.R
Created July 30, 2020 03:16
Example code for using tidymodels, recipes, parsnip, and DALEX
library(tidymodels)
library(plsmod)
library(DALEX)
theme_set(theme_bw())
## ── Attaching packages ───────────────────────────────── tidymodels 0.1.1 ──
## ✓ broom 0.7.0 ✓ recipes 0.1.13
## ✓ dials 0.0.8 ✓ rsample 0.0.7
## ✓ dplyr 1.0.0 ✓ tibble 3.0.3
@topepo
topepo / component_plot.R
Created July 10, 2020 01:37
first of two S3 methods for plotting PCA components
#' @param x A prepped recipe or fitted workflow that uses a recipe. The recipe
#' must have used at least one `step_pca()`.
#' @param id A single numeric or character value that is used to pick the step
#' with the PCA results. If a single `step_pca()` was used, this argument is
#' ignored. *Note*: if used, `id` must be named.
#' @param ... An optional series of conditional statements used to filter the
#' PCA data before plotting. See Details below.
#' @examples
#' library(recipes)
#' library(parsnip)
## -----------------------------------------------------------------------------
# Simple demonstration of stacking using three models with the Ames housing data
## -----------------------------------------------------------------------------
library(tidymodels)
library(rules)
library(doMC)
library(AmesHousing)
library(ggforce)
library(tidymodels)
library(Matrix)
library(lobstr)
# ------------------------------------------------------------------------------
rand_values <- function (prefix = "step", len = 3, num_vals = Inf) {
candidates <- c(letters, LETTERS, paste(0:9))
candidates <- candidates[1:min(length(candidates), num_vals)]
library(tidyverse)
library(janitor)
library(tidymodels)
library(plotly)
theme_set(theme_bw())
body <-
read_csv("http://staff.pubhealth.ku.dk/~tag/Teaching/share/data/Bodyfat.csv") %>%
janitor::clean_names() %>%
library(tidyverse)
library(lubridate)
# ------------------------------------------------------------------------------
set.seed(2427)
hotels <-
readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv') %>%
filter(is_canceled == 0) %>%
mutate(