Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Created December 23, 2020 13:27
Show Gist options
  • Save tmasjc/8067d830715233a8932f11d66afe9ae7 to your computer and use it in GitHub Desktop.
Save tmasjc/8067d830715233a8932f11d66afe9ae7 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(tidymodels)
sf_trees <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-28/sf_trees.csv")
trees_df <- sf_trees %>%
mutate(
legal_status = case_when(
legal_status == "DPW Maintained" ~ legal_status,
TRUE ~ "Other"
),
plot_size = parse_number(plot_size)
) %>%
select(-address) %>%
na.omit() %>%
mutate_if(is.character, factor)
tree_rec <- recipe(legal_status ~ ., data = trees_train) %>%
update_role(tree_id, new_role = "ID") %>%
step_other(species, caretaker, threshold = 0.01) %>%
step_other(site_info, threshold = 0.005) %>%
step_dummy(all_nominal(), -all_outcomes()) %>%
step_date(date, features = c("year")) %>%
step_rm(date) %>%
step_downsample(legal_status)
tree_prep <- prep(tree_rec)
juiced <- bake(tree_prep, newdata = NULL)
tune_spec <- rand_forest(
mtry = tune(),
trees = 1000,
min_n = tune()
) %>%
set_mode("classification") %>%
set_engine("ranger")
tune_wf <- workflow() %>%
add_recipe(tree_rec) %>%
add_model(tune_spec)
set.seed(234)
trees_folds <- vfold_cv(trees_train)
set.seed(345)
tune_res <- tune_grid(
tune_wf,
resamples = trees_folds,
grid = 20
)
tune_res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment