Modeling with tidymodels in R
David Svancer
Data Scientist
Predikce hwy s využitím cty jako prediktoru
$$hwy = \beta_{0} + \beta_{1} cty$$
Parametry modelu
Predikce hwy s využitím cty jako prediktoru
$$hwy = \beta_{0} + \beta_{1} cty$$
Parametry modelu
Odhadnuté parametry z trénovacích dat
$$\small hwy = 0.77 + 1.35(cty)$$
Vzorce modelu v parsnip
Obecný tvar
outcome ~ predictor_1 + predictor_2 + ...
Zkrácená notace
outcome ~ .
Predikce hwy s využitím cty jako prediktoru
hwy ~ cty
Jednotná syntaxe pro specifikaci modelů v R
Specifikujte typ modelu
Specifikujte engine
Specifikujte mód
Definujte specifikaci modelu pomocí parsnip
linear_reg()
Předejte lm_model funkci fit()
data pro trénování modelu
lm_model <- linear_reg() %>%set_engine('lm') %>%set_mode('regression')
lm_fit <- lm_model %>%
fit(hwy ~ cty, data = mpg_training)
Funkce tidy()
parsnipterm a estimate obsahují odhadnuté parametry
tidy(lm_fit)
# A tibble: 2 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0.769 0.528 1.46 1.47e- 1
2 cty 1.35 0.0305 44.2 6.32e-97
Předejte natrénovaný model parsnip funkci predict()
new_data určuje dataset pro predikci nových hodnot
Standardizovaný výstup funkce predict()
new_data.predhwy_predictions <- lm_fit %>% predict(new_data = mpg_test)hwy_predictions
# A tibble: 57 x 1
.pred
<dbl>
1 25.0
2 27.7
3 25.0
4 25.0
5 22.3
# ... with 47 more rows
Funkce bind_cols()
Postup
hwy a cty z mpg_testbind_cols() a přidejte sloupec predikcímpg_test_results <- mpg_test %>% select(hwy, cty) %>%bind_cols(hwy_predictions) mpg_test_results
# A tibble: 57 x 3
hwy cty .pred
<int> <int> <dbl>
1 29 18 25.0
2 31 20 27.7
3 27 18 25.0
4 26 18 25.0
5 25 16 22.3
# ... with 47 more rows
Modeling with tidymodels in R