departures <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-04-27/departures.csv')
## Rows: 9423 Columns: 19
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): coname, exec_fullname, interim_coceo, still_there, notes, sources...
## dbl (10): dismissal_dataset_id, gvkey, fyear, co_per_rol, departure_code, c...
## dttm (1): leftofc
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
departures %>%
count(ceo_dismissal)
## # A tibble: 3 × 2
## ceo_dismissal n
## <dbl> <int>
## 1 0 6121
## 2 1 1489
## 3 NA 1813
dismissal <-
departures %>%
filter(ceo_dismissal != "NA", !is.na(notes))%>%
mutate(ceo_dismissal = case_when(
ceo_dismissal == "0" ~ "Voluntary",
ceo_dismissal == "1" ~ " Involuntary"
))
library(tidytext)
library(tidylo)
## Warning: package 'tidylo' was built under R version 4.3.2
dismissal %>%
unnest_tokens(word, notes) %>%
count(ceo_dismissal, word) %>%
filter(n > 100) %>%
bind_log_odds(ceo_dismissal, word, n) %>%
arrange(-log_odds_weighted)
## # A tibble: 554 × 4
## ceo_dismissal word n log_odds_weighted
## <chr> <chr> <int> <dbl>
## 1 " Involuntary" the 6190 34.4
## 2 " Involuntary" to 2598 21.2
## 3 " Involuntary" a 2261 19.9
## 4 " Involuntary" of 3161 19.7
## 5 " Involuntary" and 2765 19.5
## 6 " Involuntary" in 2057 17.9
## 7 " Involuntary" was 1551 17.2
## 8 " Involuntary" company 1609 16.3
## 9 "Voluntary" served 1262 14.6
## 10 " Involuntary" that 1000 13.6
## # ℹ 544 more rows
library(tidymodels)
## ── Attaching packages ────────────────────────────────────── tidymodels 1.1.1 ──
## ✔ broom 1.0.5 ✔ rsample 1.2.0
## ✔ dials 1.2.0 ✔ tune 1.1.2
## ✔ infer 1.0.5 ✔ workflows 1.1.3
## ✔ modeldata 1.2.0 ✔ workflowsets 1.0.1
## ✔ parsnip 1.1.1 ✔ yardstick 1.2.0
## ✔ recipes 1.0.8
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ scales::discard() masks purrr::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ recipes::fixed() masks stringr::fixed()
## ✖ dplyr::lag() masks stats::lag()
## ✖ yardstick::spec() masks readr::spec()
## ✖ recipes::step() masks stats::step()
## • Search for functions across packages at https://www.tidymodels.org/find/
set.seed(123)
ceo_split <-
dismissal %>%
select(notes, ceo_dismissal) %>%
initial_split(strata = ceo_dismissal)
ceo_train <- training(ceo_split)
ceo_test <- testing(ceo_split)
set.seed(234)
ceo_folds <- vfold_cv(ceo_train, v = 5, strata = ceo_dismissal)
ceo_folds
## # 5-fold cross-validation using stratification
## # A tibble: 5 × 2
## splits id
## <list> <chr>
## 1 <split [4554/1140]> Fold1
## 2 <split [4555/1139]> Fold2
## 3 <split [4555/1139]> Fold3
## 4 <split [4556/1138]> Fold4
## 5 <split [4556/1138]> Fold5
library(textrecipes)
ceo_rec <-
recipe(ceo_dismissal ~ notes, data = ceo_train) %>%
step_tokenize(notes) %>%
step_tokenfilter(notes, max_tokens = 2e3) %>%
step_tfidf(notes)
ceo_rec
##
## ── Recipe ──────────────────────────────────────────────────────────────────────
##
## ── Inputs
## Number of variables by role
## outcome: 1
## predictor: 1
##
## ── Operations
## • Tokenization for: notes
## • Text filtering for: notes
## • Term frequency-inverse document frequency with: notes
glmnet_spec <-
logistic_reg(mixture = 1, penalty = tune()) %>%
set_engine("glmnet")
ceo_wf <- workflow(ceo_rec, glmnet_spec)
doParallel::registerDoParallel()
set.seed(123)
ceo_res <-
tune_grid(
ceo_wf,
ceo_folds,
grid = tibble(penalty = 10 ^ seq(-3, 0, by = 0.3))
)
autoplot(ceo_res)
show_best(ceo_res)
## Warning: No value of `metric` was given; metric 'roc_auc' will be used.
## # A tibble: 5 × 7
## penalty .metric .estimator mean n std_err .config
## <dbl> <chr> <chr> <dbl> <int> <dbl> <chr>
## 1 0.00794 roc_auc binary 0.926 5 0.00391 Preprocessor1_Model04
## 2 0.00398 roc_auc binary 0.926 5 0.00247 Preprocessor1_Model03
## 3 0.00200 roc_auc binary 0.914 5 0.00247 Preprocessor1_Model02
## 4 0.0158 roc_auc binary 0.909 5 0.00592 Preprocessor1_Model05
## 5 0.001 roc_auc binary 0.899 5 0.00392 Preprocessor1_Model01
select_by_pct_loss(ceo_res, desc(penalty), metric = "roc_auc")
## # A tibble: 1 × 9
## penalty .metric .estimator mean n std_err .config .best .loss
## <dbl> <chr> <chr> <dbl> <int> <dbl> <chr> <dbl> <dbl>
## 1 0.0158 roc_auc binary 0.909 5 0.00592 Preprocessor1_Mode… 0.926 1.87
ceo_final <-
ceo_wf %>%
finalize_workflow(
select_by_pct_loss(ceo_res, desc(penalty), metric = "roc_auc")
) %>%
last_fit(ceo_split)
## Warning: package 'glmnet' was built under R version 4.3.2
ceo_final
## # Resampling results
## # Manual resampling
## # A tibble: 1 × 6
## splits id .metrics .notes .predictions .workflow
## <list> <chr> <list> <list> <list> <list>
## 1 <split [5694/1898]> train/test split <tibble> <tibble> <tibble> <workflow>
collect_metrics(ceo_final)
## # A tibble: 2 × 4
## .metric .estimator .estimate .config
## <chr> <chr> <dbl> <chr>
## 1 accuracy binary 0.868 Preprocessor1_Model1
## 2 roc_auc binary 0.911 Preprocessor1_Model1
collect_predictions(ceo_final) %>%
conf_mat(ceo_dismissal, .pred_class)
## Truth
## Prediction Involuntary Voluntary
## Involuntary 145 23
## Voluntary 227 1503
library(vip)
##
## Attaching package: 'vip'
## The following object is masked from 'package:utils':
##
## vi
ceo_final %>%
extract_fit_engine() %>%
vi()
## # A tibble: 2,000 × 3
## Variable Importance Sign
## <chr> <dbl> <chr>
## 1 tfidf_notes_explanation 352. NEG
## 2 tfidf_notes_receive 211. POS
## 3 tfidf_notes_existing 208. POS
## 4 tfidf_notes_relating 200. POS
## 5 tfidf_notes_franklin 197. POS
## 6 tfidf_notes_necessary 193. NEG
## 7 tfidf_notes_falling 187. NEG
## 8 tfidf_notes_allowed 187. NEG
## 9 tfidf_notes_acquiring 184. POS
## 10 tfidf_notes_approval 183. POS
## # ℹ 1,990 more rows
1.1 How can we predict if a ceo was dismissed from their job based on the notes.
1.2 The original data was made up of CEO departures in S&P 1500 firms from 2000 through 2018.
1.3 In the analysis we used the ceo_dismissal and the notes variables. This was because I wanted to see if there was specific words from the notes that correlate to reason for dismissal.
2.1 In the transformation, in the ceo_dismissal the NA values were removed, and code 1 and 0 were relabeled as “voluntary” and “involuntary”. Dismissal notes were broken down into individual words, with their frequencies collected. The most informative words were highlighted.
3.1 The data underwent several preparation steps: First, it was filtered to exclude NA values in ceo_dismissal and relabeled code 0 and 1 to whether the dismissal was voluntary Then, notes descriptions were tokenized, breaking them down into individual words. Finally, term frequency-inverse document frequency (TF-IDF) was applied to identify and highlight the most informative words for predictive modeling
3.2 The machine learning model used in the analysis is the logistic regression model, specifically implemented with the glmnet engine.
4.1 The model evaluation uses two metrics: accuracy, which measures the proportion of correct predictions, and roc_auc, which assesses the model’s ability to distinguish between classes based on the area under the receiver operating characteristic curve.
5.1 The accuracy tests were relatively useful as the accuracy was 86.8% and ROC accuracy was 91.1%. According to the data if the word explanation is used in the dismissal notes than the ceo was involuntarly removed.