R에서 tidymodels로 모델링하기
David Svancer
Data Scientist
상관은 두 수치형 변수 간 선형 관계의 강도를 측정합니다
예측 변수가 -1 또는 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')

상관 행렬 계산
select_if() 함수에 데이터셋을 전달합니다is.numeric 제공cor() 함수에 전달합니다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
recipes로 다중공선성 제거
recipe()로 recipe 객체 지정step_corr()에 전달threshold 제공leads_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()recipe 단계에서 수치 예측 변수를 선택하려면
step_*() 함수에 all_numeric()을 전달합니다-all_outcomes()도 함께 전달합니다leads_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 제공bake()로 적용합니다leads_test에서 pages_per_visit가 제거됩니다pages_per_visit가 제거됩니다leads_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
수치형 변수 중심화와 표준화
leads_training의 total_time 변수

recipes로 수치 예측 변수 정규화
step_normalize()all_numeric() 선택자여러 step_*() 함수를 recipe에 추가할 수 있습니다
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가 제거되고 수치 예측 변수가 정규화됩니다
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
R에서 tidymodels로 모델링하기