Modelleren met tidymodels in R
David Svancer
Data Scientist
Correlatie meet de sterkte van een lineair verband tussen twee numerieke variabelen
Sterk gecorreleerde voorspellers rond -1 of 1
ggplot(leads_training,
aes(x = pages_per_visit, y = total_clicks)) +
geom_point() +
labs(title = 'Total Clicks vs Average Page Visits',
y = 'Total Clicks', x = 'Average Pages per Visit')

Bereken een correlatiematrix
select_if()is.numeric als argumentcor()leads_training %>%select_if(is.numeric) %>%cor()
total_visits total_time pages_per_visit total_clicks
total_visits 1.00 0.01 0.43 0.42
total_time 0.01 1.00 0.02 0.01
pages_per_visit 0.43 0.02 1.00 0.96
total_clicks 0.42 0.01 0.96 1.00
Multicollineariteit verwijderen met recipes
recipe-object met recipe()step_corr()threshold opleads_cor_rec <- recipe(purchased ~ ., data = leads_training) %>%step_corr(total_visits, total_time, pages_per_visit, total_clicks, threshold = 0.9)leads_cor_rec
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on total_visits,..., total_clicks
all_outcomes()all_numeric()Numerieke voorspellers kiezen voor recipe-stappen
all_numeric() door aan step_*()-functies-all_outcomes() doorgevenleads_cor_rec <- recipe(purchased ~ ., data = leads_training) %>%step_corr(all_numeric(), threshold = 0.9)leads_cor_rec
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on all_numeric()
prep()leads_training voor trainingbake()pages_per_visit verwijderd uit leads_testpages_per_visit wordt ook uit alle toekomstige data verwijderdleads_cor_rec %>%prep(training = leads_training) %>%bake(new_data = leads_test)
# A tibble: 332 x 6
total_visits total_time total_clicks ... purchased
<dbl> <dbl> <dbl> ... <fct>
1 8 100 24 ... no
2 4 1346 22 ... yes
3 3 176 27 ... no
4 2 16 12 ... no
5 9 1022 12 ... yes
# ... with 327 more rows
Centreren en schalen van numerieke variabelen
De variabele total_time in leads_training

Numerieke voorspellers normaliseren met recipes
step_normalize()all_numeric()-selectorMeerdere step_*()-functies kun je aan een recipe toevoegen
leads_norm_rec <- recipe(purchased ~ ., data = leads_training) %>%step_corr(all_numeric(), threshold = 0.9) %>% step_normalize(all_numeric())leads_norm_rec
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on all_numeric()
Centering and scaling for all_numeric()
pages_per_vist is verwijderd en numerieke voorspellers zijn genormaliseerd
leads_norm_rec %>%
prep(training = leads_training) %>%
bake(new_data = leads_test)
# A tibble: 332 x 6
total_visits total_time total_clicks lead_source us_location purchased
<dbl> <dbl> <dbl> <fct> <fct> <fct>
1 0.864 -0.984 -0.360 direct_traffic west no
2 -0.151 1.33 -0.506 direct_traffic northeast yes
3 -0.405 -0.843 -0.140 organic_search west no
4 -0.659 -1.14 -1.24 email midwest no
5 1.12 0.725 -1.24 direct_traffic west yes
# ... with 327 more rows
Modelleren met tidymodels in R