tidymodels-vorlage3

tidymodels
statlearning
template
string
Published

November 15, 2023

Aufgabe

Schreiben Sie eine prototypische Analyse für ein Vorhersagemodell, das sich als Vorlage für Analysen dieser Art eignet!

Verzichten Sie auf Resampling und Tuning.

Hinweise:

  • Berechnen Sie ein Modell
  • Tunen Sie keinen Parameter des Modells
  • Verwenden Sie keine Kreuzvalidierung.
  • Verwenden Sie Standardwerte, wo nicht anders angegeben.
  • Fixieren Sie Zufallszahlen auf den Startwert 42.











Lösung

# Setup:
library(tidymodels)
library(tidyverse)
library(tictoc)  # Zeitmessung
library(easystats)   # NAs zählen


# Data:
d_path <- "https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv"
d <- read_csv(d_path)

set.seed(42)
d_split <- initial_split(d)
d_train <- training(d_split)
d_test <- testing(d_split)


# model:
mod1 <-
  rand_forest(mode = "regression")


# cv:
set.seed(42)
rsmpl <- vfold_cv(d_train)


# recipe:
rec1 <- recipe(body_mass_g ~  ., data = d_train) |> 
  step_unknown(all_nominal_predictors(), new_level = "NA") |> 
  step_naomit(all_predictors()) |> 
  step_dummy(all_nominal_predictors()) |> 
  step_zv(all_predictors()) |> 
  step_normalize(all_predictors()) 



# workflow:
wf1 <-
  workflow() %>% 
  add_model(mod1) %>% 
  add_recipe(rec1)


# tuning:
tic()
wf1_fit <-
  wf1 %>% 
  last_fit(split = d_split)
toc()
0.496 sec elapsed
collect_metrics(wf1_fit)
.metric .estimator .estimate .config
rmse standard 310.3661061 pre0_mod0_post0
rsq standard 0.8627167 pre0_mod0_post0

Als Check: Das gepreppte/bebackene Rezept:

rec1_prepped <- prep(rec1)
d_train_baked <- bake(rec1_prepped, new_data = NULL)
d_train_baked |> 
  head()
rownames bill_length_mm bill_depth_mm flipper_length_mm year body_mass_g species_Chinstrap species_Gentoo island_Dream island_Torgersen sex_male sex_NA.
-1.2404291 -1.5309296 0.3858343 -0.7943918 -1.2921757 3450 -0.5026644 -0.7579203 1.3366001 -0.4093011 -0.9636924 -0.1543093
1.4505356 1.3186250 0.3858343 -0.3653368 1.1407119 3675 1.9816579 -0.7579203 1.3366001 -0.4093011 -0.9636924 -0.1543093
-0.2115308 0.4006477 -1.9691393 0.7073009 -1.2921757 4500 -0.5026644 1.3142661 -0.7452558 -0.4093011 -0.9636924 -0.1543093
-0.9930977 0.3432741 0.8868925 -0.2938276 -0.0757319 4150 -0.5026644 -0.7579203 -0.7452558 2.4336824 1.0336378 -0.1543093
0.5304631 0.8787609 -0.5661763 2.0659752 -0.0757319 5800 -0.5026644 1.3142661 -0.7452558 -0.4093011 1.0336378 -0.1543093
-0.2807836 -0.9571938 0.7866809 -1.1519377 1.1407119 3650 -0.5026644 -0.7579203 1.3366001 -0.4093011 1.0336378 -0.1543093
describe_distribution(d_train_baked)
Variable Mean SD IQR Min Max Skewness Kurtosis n n_Missing
rownames 0.000 1.0000 1.696693 -1.7153052 1.678080 -0.0140987 -1.2072129 257 0
bill_length_mm 0.000 1.0000 1.682958 -2.2767861 2.982459 0.0138271 -0.7949178 257 0
bill_depth_mm 0.000 1.0000 1.603386 -2.0192451 2.189644 -0.1101092 -0.8689102 257 0
flipper_length_mm 0.000 1.0000 1.644711 -1.9385386 2.065975 0.3185041 -1.0170679 257 0
year 0.000 1.0000 2.432888 -1.2921757 1.140712 -0.1160338 -1.5114889 257 0
body_mass_g 4200.973 792.5366 1212.500000 2700.0000000 6300.000000 0.4897600 -0.6875459 257 0
species_Chinstrap 0.000 1.0000 0.000000 -0.5026644 1.981658 1.4905934 0.2235476 257 0
species_Gentoo 0.000 1.0000 2.072186 -0.7579203 1.314266 0.5607093 -1.6988872 257 0
island_Dream 0.000 1.0000 2.081856 -0.7452558 1.336600 0.5959823 -1.6577672 257 0
island_Torgersen 0.000 1.0000 0.000000 -0.4093011 2.433682 2.0402588 2.1795571 257 0
sex_male 0.000 1.0000 1.997330 -0.9636924 1.033638 0.0704940 -2.0107396 257 0
sex_NA. 0.000 1.0000 0.000000 -0.1543093 6.455274 6.3503836 38.6279271 257 0

Categories:

  • tidymodels
  • statlearning
  • template
  • string