Skip to content

Instantly share code, notes, and snippets.

@topepo
topepo / lm_vs_nnet.R
Created October 24, 2023 17:10
A simple comparison of two models with different feature set approaches
library(tidymodels)
library(doMC)
# ------------------------------------------------------------------------------
tidymodels_prefer()
theme_set(theme_bw())
options(pillar.advice = FALSE, pillar.min_title_chars = Inf)
registerDoMC(cores = parallel::detectCores())
@topepo
topepo / fuser_wrapper.R
Created April 1, 2023 18:54
example code for a model wrapper for the fuser package
# Allow fuser functions to work in tidymodels
# See https://stackoverflow.com/questions/75871678/how-can-i-pass-an-extra-variable-to-a-tidymodels-fit-function
# Create a function that can parse a formula that uses a special (non-existant)
# `groups()` function. This returns a list of character vectors that has the
# columns and their roles based on the formula. It works with '.'.
fuser_variables <- function(f, data) {
cl <- match.call()
trms <- terms(f, data = data, specials = "groups")
@topepo
topepo / extract_code
Created April 13, 2022 21:55
extract_code for purl'ing
extract_code <- function(file) {
# TODO we could put in an argument for the pattern
# or something more sophisticated...
target <- tempfile()
knitr::purl(file, output = target, quiet = TRUE)
code <- readLines(target)
code <- gsub("^##.*", "", code)
r_file <- gsub("[Rq]md$", "R", file)
writeLines(code, r_file)
invisible(TRUE)
@topepo
topepo / check_for_remotes
Created January 31, 2022 16:27
Check to see if a repo has remote dependencies
#' Check to see if a repo has remote dependencies.
#' @param repos A character vector of repos to query (e.g. `tidyverse/dplyr`).
#' @param hard_depend Should only depends and imports be included in the query.
#' Using `FALSE` will include suggests and others.
#' @details Packages listed in `Config/Needs/*` will not be returned.
#' Requires dplyr, pak, and purrr.
#' @return A tibble with columns for the repo and any github repos that are
#' listed as dependencies.
#' @examples
#' tests <- paste0("tidymodels/", c("broom", "parsnip", "recipes"))
@topepo
topepo / two_class_diag_plots.R
Created May 6, 2021 22:00
two class diagnostic plots for shinymodels
library(tidymodels)
tidymodels_prefer()
theme_set(theme_bw())
library(doMC)
registerDoMC(cores = 20)
# ------------------------------------------------------------------------------
data(ad_data)
@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