# Titanic ML Model Comparison Shiny App Plan

## Overview
Build an interactive R Shiny application that allows users to train, evaluate, and compare 6 machine learning classification models on the Titanic survival dataset using the tidymodels framework.

## Models to Include
Based on the .qmd files, the app will support:
1. **Null Model** - Baseline model that predicts the majority class
2. **k-Nearest Neighbors (kNN)** - With hyperparameter tuning for k (neighbors)
3. **Boosted C5.0** - Decision tree boosting
4. **XGBoost** - Gradient boosting
5. **Random Forest** - With tuning for trees, mtry, min_n
6. **Logistic Regression** - With regularization (glmnet)
7. **Naive Bayes** - With Laplace smoothing

## App Structure & Features

### 1. Data Tab
- Display training data preview (first 20 rows)
- Show missing value visualizations (vis_miss, gg_miss_var)
- Summary statistics of the dataset

### 2. Preprocessing Tab
- **Variable Selection**: Checkbox group to select predictors (pclass, sex, age, sib_sp, parch, fare, embarked)
- **Options**:
  - Impute missing age values (mean imputation)
  - Normalize numeric predictors
  - Train/Test split ratio slider (50%-95%)
- Display recipe summary

### 3. Model Training Tab
- **Model Selection Dropdown**: Choose from all 6 models
- **Dynamic Hyperparameter UI**: Sliders that change based on selected model:
  - kNN: neighbors (1-20)
  - C5.0: trees (5-100)
  - XGBoost: trees (5-100), learn_rate (0.01-0.3)
  - Random Forest: trees (50-500), mtry (1-7)
  - Logistic Regression: penalty (0.001-1), mixture (0-1)
  - Naive Bayes: Laplace (0-5), smoothness (0-2)
- **Cross-validation option**: Checkbox for 10-fold CV
- **Action Buttons**:
  - "Train Model" - Train selected model with chosen hyperparameters
  - "Train All Models" - Train all models with default parameters
- Status display showing training progress

### 4. Results Tab
- **Model Selector**: Dropdown to view results for any trained model
- **Metrics Table**: Display accuracy, kappa, and ROC AUC
- **Confusion Matrix**: Visual heatmap
- **ROC Curve**: Plot with autoplot
- **Probability Distribution**: Density plot of predicted probabilities by actual class

### 5. Model Comparison Tab
- **Combined ROC Curves**: All models' ROC curves on one plot
- **Comparison Table**: Sortable table with all models' metrics (Accuracy, Kappa, ROC AUC)
- Highlight best performing model

### 6. Prediction Tab
- **Input Form**: Enter passenger details:
  - Pclass (1, 2, 3)
  - Sex (male, female)
  - Age (numeric)
  - SibSp (numeric)
  - Parch (numeric)
  - Fare (numeric)
  - Embarked (C, Q, S)
- **Model Selector**: Choose which trained model to use
- **Predict Button**: Make prediction
- **Result Display**: Color-coded result (green=SURVIVED, red=DIED) with probability
- **Test Set Predictions**: Table of predictions for the full test dataset
- **Download Button**: Export predictions as CSV

## Technical Implementation

### Libraries Required
```r
library(shiny)
library(shinydashboard)
library(tidymodels)
library(titanic)
library(tidyverse)
library(janitor)
library(naniar)
library(discrim)  # For Naive Bayes
library(yardstick)
library(ggplot2)
library(DT)
```

### Data Preprocessing Pipeline
1. Load titanic_train and titanic_test
2. Clean names with janitor::clean_names()
3. Convert variables to factors: survived, pclass, sex, embarked
4. Remove identifiers: passenger_id, name, ticket, cabin
5. Apply recipe-based preprocessing based on user selections

### Reactive Values
- `rv$results`: List storing trained models, predictions, and recipes for each model
- `rv$trained`: Boolean flag indicating if any models have been trained

### Key Functions
- `make_recipe()`: Build preprocessing recipe based on user inputs
- `train_model()`: Train a specific model with given hyperparameters
- `get_res()`: Extract metrics and results for a trained model

## UI Layout
- Use shinydashboard for professional appearance
- Sidebar navigation with 6 menu items
- Fluid rows with boxes for organized content
- Responsive tables with DT package
- Progress indicators for long-running training operations

## File Location
`/home/esuess/Documents/Classes/Stat652/Midterm-2026/Examples_Tidymodels/app.R`

## Next Steps
1. Review existing app.R (already exists and is functional)
2. Enhance with additional features if needed:
   - Add kNN hyperparameter tuning
   - Add C5.0 model support
   - Improve error handling
   - Add model export functionality
   - Add feature importance plots