library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Import Data

members <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/members.csv')
## Rows: 76519 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): expedition_id, member_id, peak_id, peak_name, season, sex, citizen...
## dbl  (5): year, age, highpoint_metres, death_height_metres, injury_height_me...
## lgl  (6): hired, success, solo, oxygen_used, died, injured
## 
## ℹ 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.

Data Set

members %>% glimpse()
## Rows: 76,519
## Columns: 21
## $ expedition_id        <chr> "AMAD78301", "AMAD78301", "AMAD78301", "AMAD78301…
## $ member_id            <chr> "AMAD78301-01", "AMAD78301-02", "AMAD78301-03", "…
## $ peak_id              <chr> "AMAD", "AMAD", "AMAD", "AMAD", "AMAD", "AMAD", "…
## $ peak_name            <chr> "Ama Dablam", "Ama Dablam", "Ama Dablam", "Ama Da…
## $ year                 <dbl> 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1…
## $ season               <chr> "Autumn", "Autumn", "Autumn", "Autumn", "Autumn",…
## $ sex                  <chr> "M", "M", "M", "M", "M", "M", "M", "M", "M", "M",…
## $ age                  <dbl> 40, 41, 27, 40, 34, 25, 41, 29, 35, 37, 23, 44, 2…
## $ citizenship          <chr> "France", "France", "France", "France", "France",…
## $ expedition_role      <chr> "Leader", "Deputy Leader", "Climber", "Exp Doctor…
## $ hired                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ highpoint_metres     <dbl> NA, 6000, NA, 6000, NA, 6000, 6000, 6000, NA, 681…
## $ success              <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ solo                 <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ oxygen_used          <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ died                 <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ death_cause          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ death_height_metres  <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ injured              <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ injury_type          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ injury_height_metres <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…

Rows: 76,519 Columns: 21 $ expedition_id “AMAD78301”, “AMAD78301”, “AMAD78301”, “AMAD78301”, “AMAD78301”, “AMAD78301”,… $ member_id “AMAD78301-01”, “AMAD78301-02”, “AMAD78301-03”, “AMAD78301-04”, “AMAD78301-05… $ peak_id ”AMAD”, “AMAD”, “AMAD”, “AMAD”, “AMAD”, “AMAD”, “AMAD”, “AMAD”, “AMAD”, “AMAD… $ peak_name ”Ama Dablam”, “Ama Dablam”, “Ama Dablam”, “Ama Dablam”, “Ama Dablam”, “Ama Da… $ year 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1978, 1979, 1979, 1979, 1979, 1979,… $ season ”Autumn”, “Autumn”, “Autumn”, “Autumn”, “Autumn”, “Autumn”, “Autumn”, “Autumn… $ sex ”M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M”, “M… $ age 40, 41, 27, 40, 34, 25, 41, 29, 35, 37, 23, 44, 25, 28, 32, 42, 32, 30, 28, 3… $ citizenship ”France”, “France”, “France”, “France”, “France”, “France”, “France”, “France… $ expedition_role ”Leader”, “Deputy Leader”, “Climber”, “Exp Doctor”, “Climber”, “Climber”, “Cl… $ hired FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, … $ highpoint_metres NA, 6000, NA, 6000, NA, 6000, 6000, 6000, NA, 6814, 6814, NA, NA, NA, NA, 681… $ success FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FA… $ solo FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, … $ oxygen_used FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, … $ died FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, … $ death_cause NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N… $ death_height_metres NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N… $ injured FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, … $ injury_type NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N… $ injury_height_metres NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…

ChatGPT Promt

Write code for a classification model that tells the probability of a member dying.

# Load libraries
library(tidymodels)
## Warning: package 'tidymodels' was built under R version 4.2.3
## ── 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.6     ✔ workflows    1.1.3
## ✔ modeldata    1.3.0     ✔ workflowsets 1.0.1
## ✔ parsnip      1.1.1     ✔ yardstick    1.3.0
## ✔ recipes      1.0.9
## Warning: package 'broom' was built under R version 4.2.3
## Warning: package 'dials' was built under R version 4.2.3
## Warning: package 'infer' was built under R version 4.2.3
## Warning: package 'modeldata' was built under R version 4.2.3
## Warning: package 'parsnip' was built under R version 4.2.3
## Warning: package 'recipes' was built under R version 4.2.3
## Warning: package 'rsample' was built under R version 4.2.3
## Warning: package 'tune' was built under R version 4.2.3
## Warning: package 'workflows' was built under R version 4.2.3
## Warning: package 'workflowsets' was built under R version 4.2.3
## Warning: package 'yardstick' was built under R version 4.2.3
## ── 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()
## • Learn how to get started at https://www.tidymodels.org/start/
library(h2o)
## Warning: package 'h2o' was built under R version 4.2.3
## 
## ----------------------------------------------------------------------
## 
## Your next step is to start H2O:
##     > h2o.init()
## 
## For H2O package documentation, ask for help:
##     > ??h2o
## 
## After starting H2O, you can use the Web UI at http://localhost:54321
## For more information visit https://docs.h2o.ai
## 
## ----------------------------------------------------------------------
## 
## Attaching package: 'h2o'
## The following objects are masked from 'package:lubridate':
## 
##     day, hour, month, week, year
## The following objects are masked from 'package:stats':
## 
##     cor, sd, var
## The following objects are masked from 'package:base':
## 
##     %*%, %in%, &&, ||, apply, as.factor, as.numeric, colnames,
##     colnames<-, ifelse, is.character, is.factor, is.numeric, log,
##     log10, log1p, log2, round, signif, trunc
# Load data
members_data <- members

# Preprocess data
members_data_processed <- members_data %>%
  # Remove unnecessary columns
    select(-expedition_id, -member_id, -peak_id, -peak_name) %>%  
  # Convert success to factor
    mutate(success = as.factor(success),  
         # Convert solo to factor
           solo = as.factor(solo),       
         # Convert oxygen_used to factor
           oxygen_used = as.factor(oxygen_used),  
         # Convert died to factor
           died = as.factor(died),        
         # Convert injured to factor
           injured = as.factor(injured))

# Set seed 
set.seed(456)

# Split data into training and testing sets
data_split <- initial_split(members_data_processed, prop = 0.7, strata = died)
train_data <- training(data_split)
test_data <- testing(data_split)

# Initialize h2o
h2o.init()
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         6 minutes 13 seconds 
##     H2O cluster timezone:       America/New_York 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.44.0.3 
##     H2O cluster version age:    4 months and 11 days 
##     H2O cluster name:           H2O_started_from_R_aesim_yfi818 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   3.88 GB 
##     H2O cluster total cores:    8 
##     H2O cluster allowed cores:  8 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     R Version:                  R version 4.2.2 (2022-10-31 ucrt)
## Warning in h2o.clusterInfo(): 
## Your H2O cluster version is (4 months and 11 days) old. There may be a newer version available.
## Please download and install the latest version from: https://h2o-release.s3.amazonaws.com/h2o/latest_stable.html
# Convert to h2o
train_h2o <- as.h2o(train_data)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
test_h2o <- as.h2o(test_data)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
# Define predictors and response variable
predictors <- setdiff(colnames(train_h2o), c("died"))
response <- "died"

# Train AutoML model
aml <- h2o.automl(x = predictors,
                  y = response,
                  training_frame = train_h2o,
                  leaderboard_frame = test_h2o,
                  max_runtime_secs = 30, 
                  seed = 456)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===                                                                   |   4%
## 14:02:08.240: AutoML: XGBoost is not available; skipping it.
## 14:02:08.244: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
## 14:02:09.441: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |=========                                                             |  13%
## 14:02:13.140: _train param, Dropping unused columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |================                                                      |  22%
## 14:02:14.954: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |======================                                                |  31%
## 14:02:16.740: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |============================                                          |  40%
## 14:02:19.607: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |===================================                                   |  50%
## 14:02:22.86: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |=========================================                             |  58%
## 14:02:24.583: _train param, Dropping unused columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
## 14:02:26.627: _train param, Dropping unused columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |===============================================                       |  67%
## 14:02:29.326: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |======================================================                |  77%
## 14:02:30.518: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |============================================================          |  86%
## 14:02:33.709: _train param, Dropping bad and constant columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |===================================================================   |  96%
## 14:02:35.989: _train param, Dropping unused columns: [expedition_role, season, death_cause, sex, citizenship, injury_type]
  |                                                                            
  |======================================================================| 100%
# View AutoML leaderboard
print(aml@leaderboard)
##                                                  model_id       auc     logloss
## 1 StackedEnsemble_BestOfFamily_3_AutoML_2_20240502_140208 0.9958623 0.004196131
## 2 StackedEnsemble_BestOfFamily_1_AutoML_2_20240502_140208 0.9953959 0.004351004
## 3                          GBM_1_AutoML_2_20240502_140208 0.9953568 0.004570709
## 4                          GBM_4_AutoML_2_20240502_140208 0.9952180 0.005266805
## 5 StackedEnsemble_BestOfFamily_2_AutoML_2_20240502_140208 0.9948895 0.004304416
## 6                          GBM_3_AutoML_2_20240502_140208 0.9948147 0.005240974
##       aucpr mean_per_class_error       rmse          mse
## 1 0.9740063           0.01960041 0.02432935 0.0005919173
## 2 0.9722955           0.01960041 0.02442641 0.0005966497
## 3 0.9722806           0.01960041 0.02438116 0.0005944409
## 4 0.9726629           0.01960041 0.02479681 0.0006148817
## 5 0.9733986           0.01960041 0.02440678 0.0005956909
## 6 0.9734421           0.01960041 0.02496449 0.0006232257
## 
## [13 rows x 7 columns]
# Get best model from AutoML
best_model <- aml@leader

# Make predictions on test data
predictions <- h2o.predict(best_model, test_h2o)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
# View predictions
head(predictions)
##   predict     FALSE         TRUE
## 1   FALSE 0.9997430 0.0002569530
## 2   FALSE 0.9997925 0.0002074977
## 3   FALSE 0.9995665 0.0004334634
## 4   FALSE 0.9998757 0.0001243475
## 5   FALSE 0.9997434 0.0002566410
## 6   FALSE 0.9998738 0.0001261594