---
title: "American Ninja Warrior - Kaplan-Meier Survival Analyis"
author: "YOUR NAME HERE"
format:
  html:
    self-contained: true
    toc: true
    toc_float: true
    number_section: false
    highlight: tango
editor: visual
editor_options:
  chunk_output_type: console
# embed-resources: true
---

# Getting Started: American Ninja Warrior Survival Analysis

```{r, warning = FALSE, message = FALSE}
# Load packages
library(tidyverse)
library(survival)
library(survminer)

# Load Data
ninja <- read_csv("anw_2021_stage1.csv")
```

## Exercise 1: Data Structure

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

# Kaplan-Meier Survival Analysis

## Exercise 2: Fake Data Table Completion

YOUR ANSWER HERE

```{r}
# Makes a column called censor that is 1 if the competitor failed and 0 if they completed the course or ran out of time
ninja <- ninja |> 
  mutate(censor = if_else(cause %in%  c("Complete", "Time"), 0, 1))
```

## Exercise 3: Manual Kaplan-Meier Calculation

#### Part 1: Number of Events

```{r}
ninja_summary <- ninja |> 
  group_by(obstacle = obstacle_number) |>
  summarize(fails = sum(cause == "Fall"),
            censor = sum(cause %in% c("Complete", "Time")))
```

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

#### Part 2: At Risk Competitors

```{r}
ninja_summary <- ninja_summary |> 
  mutate(attempts = 69 - lag(cumsum(fails), default = 0) - lag(cumsum(censor), default = 0))
```

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

#### Part 3: Conditional Fail Probabilities

a.  

```{r, include=FALSE}
ninja_summary <- ninja_summary |> 
  mutate(fail_prob = fails / attempts)
```

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

d.  YOUR ANSWER HERE

e.  YOUR ANSWER HERE

#### Part 4: Kaplan-Meier

```{r}
ninja_summary <- ninja_summary |> 
  mutate(km = cumprod(1 - fail_prob))
```

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

#### Part 5: Plotting the Kaplan-Meier Estimate

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

# R for Kaplan-Meier

```{r}
ninja_km <- survfit(Surv(obstacle_number, censor) ~ 1, data = ninja)
```

## Exercise 4: Kaplan-Meier Estimates and Interpretation

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

## Exercise 5: Interprating Quantiles

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

## Plotting Kaplan-Meier with R

```{r}
ggsurvplot(ninja_km,
           conf.int = TRUE)
```

# Kaplan-Meier Estimator by Groups

```{r}
ninja_km_gender <- survfit(Surv(obstacle_number, 
                                censor)~ sex, data = ninja)
```

```{r}
ggsurvplot(ninja_km_gender,
           conf.int = TRUE)
```

## Exercise 6: Kaplan-Meier Estimates by Group

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

## Exercise 7: Comparing Survival Vs. Expected Survival

```{r}
ninja_km_diff <- survdiff(Surv(obstacle_number,
                               censor) ~ sex, data = ninja)
ninja_km_diff
```

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

## Exercise 8: Log Rank Tests

a.  YOUR ANSWER HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

d.  YOUR ANSWER HERE

# 

# More Practice

## Challenge 1: Cleaning Data

a.  YOUR CODE HERE

b.  YOUR ANSWER HERE

c.  YOUR CODE HERE

## Challenge 2: Fitting KM

a.  YOUR CODE HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

d.  YOUR ANSWER HERE

## Challenge 3: Comparing Groups

a.  YOUR CODE HERE

b.  YOUR ANSWER HERE

c.  YOUR ANSWER HERE

d.  YOUR ANSWER HERE

e.  YOUR ANSWER HERE

f.  YOUR ANSWER HERE
