Skip to content

Instantly share code, notes, and snippets.

@topepo
topepo / prob_cal.R
Created October 29, 2024 12:48
demonstration of probably's calibration functions with non-standard names and groups
library(tidymodels)
library(probably)
set.seed(12)
example <-
two_class_example %>%
mutate(
model = sample(c("logistic", "xgboost"), size = nrow(two_class_example), replace = TRUE)
) %>%
rename(`$@#!` = Class1, cls_2.5 = Class2)
@topepo
topepo / smooth_ph_linear_pred.R
Created June 18, 2024 16:05
function to produce a smooth linear predictor trend for survival models
smooth_ph_linear_pred <- function(formula, data, deg_free = 5, grid_size = 500) {
require(rlang)
rlang::is_installed("survival")
rlang::is_installed("ggplot2")
rlang::is_installed("splines2")
rlang::is_installed("cli")
require(ggplot2)
# check 1 pred and continuous
pred_sym <- rlang::f_rhs(formula)
@topepo
topepo / nested_example.R
Created June 6, 2024 13:30
nested resampling in tidymodels
# pak::pak(c("tidymodels/finetune@nested"), ask = FALSE)
library(tidymodels)
library(finetune)
library(rlang)
library(sfd)
library(doMC)
# ------------------------------------------------------------------------------
tidymodels_prefer()
@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)