Make a separate gallery post showing how you accomplished each of the following items: 1. Use bagged tree imputation to fill in missing values. - You do not need to show the results of the imputation process, but you should show how you incorporated bagged tree imputation into your recipe. 2. Use support vector machines (with any kernel option) to make predictions on the test set. Measure the accuracy of the model on the test set (using tidymodel’s default settings). 3. Use a multilayer perceptron or neural network to make predictions on the test set. Measure the accuracy of the model on the test set (using tidymodel’s default settings). 4. Use naive Bayes to make predictions on the test set. Measure the accuracy of the model on the test set (using tidymodel’s default settings). 5. Use bagged trees to make predictions on the test set. Measure the accuracy of the model on the test set (using tidymodel’s default settings). 6. Use glm to make predictions on the test set. Measure the accuracy of the model on the test set (using tidymodel’s default settings).

1.Filling in missing values

we will need this package. Do only once. Comment out after done

# install.packages("kernlab") 04/09/2024

We will need this package. Do only once. Comment out after done.

#install.packages("kernlab")
library(tidyverse)
## ── 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.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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
library(tidymodels)
## ── Attaching packages ────────────────────────────────────── tidymodels 1.2.0 ──
## ✔ broom        1.0.5      ✔ rsample      1.2.1 
## ✔ dials        1.2.1      ✔ tune         1.2.0 
## ✔ infer        1.0.6      ✔ workflows    1.1.4 
## ✔ modeldata    1.3.0      ✔ workflowsets 1.1.0 
## ✔ parsnip      1.2.1      ✔ yardstick    1.3.1 
## ✔ recipes      1.0.10     
## ── 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/
library(naniar)
library(baguette)
library(discrim)
## 
## Attaching package: 'discrim'
## 
## The following object is masked from 'package:dials':
## 
##     smoothness

Will I need to change my path?

getwd()
## [1] "/Users/peteryh/Desktop/LHS610"

I know my data is in the current directory. Check and change paths to yours.

load('namcs08.RData')

Recode as before

dataset = 
  patients %>%
  mutate(paytype = if_else(paytype == "Private insurance", "private_ins", "non_private_ins"))

Explore missing

vis_miss(patients)

gg_miss_upset(patients)

miss_var_summary(patients)

Remove unhelpful predictors

dataset = 
  dataset %>%
  select(-id, -visitreason)

Set up resampling: Test/Train split. Train = 2/3, Test = 1/3. I could change my seed to a different number and re-run to test sensitivity/robustness of model.

# Data
set.seed(1)

resampling_strategy = 
  validation_split(dataset, prop = 2/3)
## Warning: `validation_split()` was deprecated in rsample 1.2.0.
## ℹ Please use `initial_validation_split()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

2.Support Vector Machines

# Math
svm_model = 
    svm_linear(cost = 1) %>% 
    set_mode("classification") %>% 
    set_engine("kernlab")
svm_recipe = 
  recipe(paytype ~ age + sex + region + race + ethnicity + height + weight + temp + sbp + dbp, data = dataset) %>% 
  step_dummy(sex, region, race, ethnicity) %>%
  step_impute_bag(all_predictors())
# Metrics
svm_workflow = 
  workflow() %>% 
  add_model(svm_model) %>% 
  add_recipe(svm_recipe)
svm_result = 
  fit_resamples(svm_workflow,
                resamples = resampling_strategy, 
                metrics = metric_set(roc_auc, accuracy),
                control = control_resamples(save_pred = TRUE,
                                            extract = extract_fit_engine))
svm_result %>% 
  collect_metrics()

3.Multilayer perceptron

# Math
mlp_model = 
    mlp(penalty = 0.01) %>% 
    set_mode("classification") %>% 
    set_engine("nnet")

mlp_recipe = 
  recipe(paytype ~ age + sex + region + race + ethnicity + height + weight + temp + sbp + dbp, data = dataset) %>% 
  step_dummy(sex, region, race, ethnicity) %>%
  step_impute_bag(all_predictors())
# Metrics
mlp_workflow = 
  workflow() %>% 
  add_model(mlp_model) %>% 
  add_recipe(mlp_recipe)
mlp_result = 
  fit_resamples(mlp_workflow,
                resamples = resampling_strategy, 
                metrics = metric_set(roc_auc, accuracy),
                control = control_resamples(save_pred = TRUE,
                                            extract = extract_fit_engine))
mlp_result %>% 
  collect_metrics()

4.Naive Bayes

Investigate warnings later on

# Math
nb_model = 
  naive_Bayes() %>% 
  set_mode("classification") %>%
  set_engine("klaR")

nb_recipe = 
  recipe(paytype ~ age + sex + region + race + ethnicity + height + weight + temp + sbp + dbp, data = dataset) %>% 
  step_dummy(sex, region, race, ethnicity) %>%
  step_impute_bag(all_predictors())
# Metrics
nb_workflow = 
  workflow() %>% 
  add_model(nb_model) %>% 
  add_recipe(nb_recipe)

nb_result = 
  fit_resamples(nb_workflow,
                resamples = resampling_strategy, 
                metrics = metric_set(roc_auc, accuracy),
                control = control_resamples(save_pred = TRUE,
                                            extract = extract_fit_engine))
## → A | warning: Numerical 0 probability for all classes with observation 1320, Numerical 0 probability for all classes with observation 3185, Numerical 0 probability for all classes with observation 9390
## 
There were issues with some computations   A: x1

There were issues with some computations   A: x1
nb_result %>% 
  collect_metrics()

5.Bagged Trees

#install.packages("baguette")
library(baguette)
# Math
bagt_model = 
  bag_tree() %>%
  set_mode("classification") %>%
  set_engine("rpart")

bagt_recipe = 
  recipe(paytype ~ age + sex + region + race + ethnicity + height + weight + temp + sbp + dbp, data = dataset) %>% 
  step_dummy(sex, region, race, ethnicity) %>%
  step_impute_bag(all_predictors())
# Metrics
bagt_workflow = 
  workflow() %>% 
  add_model(bagt_model) %>% 
  add_recipe(bagt_recipe)

bagt_result = 
  fit_resamples(bagt_workflow,
                resamples = resampling_strategy, 
                metrics = metric_set(roc_auc, accuracy),
                control = control_resamples(save_pred = TRUE,
                                            extract = extract_fit_engine))
bagt_result %>% 
  collect_metrics()

6.GLM

# Math
glm_model = 
  logistic_reg() %>%
  set_mode("classification") %>%
  set_engine("glm")

glm_recipe = 
  recipe(paytype ~ age + sex + region + race + ethnicity + height + weight + temp + sbp + dbp, data = dataset) %>% 
  step_dummy(sex, region, race, ethnicity) %>%
  step_impute_bag(all_predictors())
# Metrics
glm_workflow = 
  workflow() %>% 
  add_model(glm_model) %>% 
  add_recipe(glm_recipe)

glm_result = 
  fit_resamples(glm_workflow,
                resamples = resampling_strategy, 
                metrics = metric_set(roc_auc, accuracy),
                control = control_resamples(save_pred = TRUE,
                                            extract = extract_fit_engine))
glm_result %>% 
  collect_metrics()