Survival Analysis of Game of Thrones Characters

Machine Learning

This survival analysis was conducted on a Game of Thrones dataset to analyze the expected survival time of characters. The analysis includes visualizing the survival probability by initial allegiance and whether the characters switched allegiances.

Dataset Overview

The dataset contains information about Game of Thrones characters, including their allegiance, survival time, and whether they switched allegiances.

Show the code
# Load necessary libraries and dataset
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.0
✔ ggplot2   3.5.1     ✔ 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
Show the code
library(lubridate)
library(survival)
library(survminer)
Loading required package: ggpubr

Attaching package: 'survminer'

The following object is masked from 'package:survival':

    myeloma
Show the code
thePath <- "/Users/Shared/Survival Analysis/Got_dataset"
ds <- read_csv(paste(thePath, "character_data_S01-S08.csv", sep = "/"))
New names:
Rows: 359 Columns: 41
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(8): name, dth_description, icd10_dx_code, icd10_dx_text, icd10_cause_c... dbl
(27): id, sex, religion, occupation, social_status, allegiance_last, all... lgl
(6): ...36, ...37, ...38, ...39, ...40, ...41
ℹ 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.
• `` -> `...36`
• `` -> `...37`
• `` -> `...38`
• `` -> `...39`
• `` -> `...40`
• `` -> `...41`

Survival Analysis by Allegiance

Characters Loyal to Their Allegiances

Show the code
# Filter data for characters loyal to their allegiances
character_ds_2_1 <- ds %>%
  filter(allegiance_switched == 1 & allegiance_last %in% 1:4)

# Fit survival model
no_switch <- survfit(Surv(character_ds_2_1$exp_time_hrs, character_ds_2_1$dth_flag) ~ character_ds_2_1$allegiance_last)

# Plot survival curves
surv_loyal <- ggsurvplot(fit = no_switch, data = character_ds_2_1,
                         legend = "bottom", 
                         legend.title = "Allegiance",
                         legend.labs = c("Stark", "Targaryen", "Night's Watch", "Lannister"),
                         risk.table = F, conf.int = F) +
              labs(title = "Survival of Characters Loyal to Their Allegiances",
                   x = "Time in Hours")
surv_loyal

Characters Not Loyal to Their Allegiances

Show the code
# Filter data for characters not loyal to their allegiances
character_swap <- ds %>%
  filter(allegiance_switched == 2 & allegiance_last %in% 1:4)

# Fit survival model
switch <- survfit(Surv(character_swap$exp_time_hrs, character_swap$dth_flag) ~ character_swap$allegiance_last)

# Plot survival curves
surv_not_loyal <- ggsurvplot(fit = switch, data = character_swap,
                             legend = "bottom", 
                             legend.title = "Allegiance",
                             legend.labs = c("Stark", "Targaryen", "Night's Watch", "Lannister"),
                             risk.table = F, conf.int = F) +
                  labs(title = "Survival of Characters Not Loyal to Their Allegiances",
                       x = "Time in Hours")
surv_not_loyal

Comparison of Survival Curves

Show the code
# Compare survival curves for characters who switched allegiances and those who didn't
allegiance_switch <- survfit(Surv(ds$exp_time_hrs, ds$dth_flag) ~ ds$allegiance_switched)

# Plot survival curves
allegiance_switch2 <- ggsurvplot(fit = allegiance_switch, data = ds,
                                 legend = "bottom", 
                                 legend.title = "Swapped Allegiance?",
                                 legend.labs = c("Yes", "No"),
                                 risk.table = F, conf.int = T) +
                      labs(title = "Survival Curves for Characters who Swapped/Kept Allegiance",
                           x = "Time to Death (Hours)")
allegiance_switch2

Overall Survival Curve for Game of Thrones Characters

Show the code
# Fit overall survival model
km2 <- survfit(Surv(time = ds$exp_time_hrs, event = ds$dth_flag) ~ 1)

# Plot overall survival curve
got_surv <- ggsurvplot(fit = km2, data = ds,
                       legend = "bottom", 
                       legend.title = "GoT Characters",
                       risk.table = F, conf.int = F, surv.median.line = "hv") +
            labs(title = "Survival Curve for Game of Thrones Characters",
                 x = "Time to Death (Hours)")
got_surv

Key Insights & Analysis:

1. Survival by Allegiance:

Characters loyal to their allegiances, such as Stark, Targaryen, Night’s Watch, and Lannister, tend to have higher survival rates compared to those who switch allegiances.

The survival curves for characters loyal to their allegiances show relatively higher probabilities of survival over time.

2. Effect of Allegiance Switching:

Characters who switch allegiances exhibit different survival patterns compared to those who remain loyal.

The survival curves for characters who switched allegiances demonstrate varying probabilities of survival, depending on their new allegiance.

3. Overall Survival:

The overall survival curve for Game of Thrones characters provides a comprehensive view of the survival probability across all allegiances.

It showcases the general trend of character survival throughout the series, highlighting critical periods of high mortality.

4. Comparative Analysis:

Comparing survival curves between characters who remained loyal and those who switched allegiances offers insights into the impact of allegiance on survival outcomes.

Statistical tests, such as the log-rank test, can be employed to determine significant differences in survival between various allegiance groups.

5. Character-Specific Analysis:

Further analysis can be conducted to explore survival patterns for specific characters or character groups, considering factors like gender, age, and storyline involvement.

Examining survival trends for prominent characters can reveal narrative arcs and thematic elements influencing survival outcomes.

6. Implications for Plot and Storytelling:

Survival analysis offers a unique perspective on narrative dynamics, character development, and plot progression within the Game of Thrones universe.

Understanding survival patterns can shed light on the storytelling decisions made by authors and the thematic elements driving character fates.

Conclusion

By combining statistical techniques with narrative analysis, this survival analysis provides valuable insights into the complex world of Game of Thrones, uncovering patterns of allegiance, betrayal, and survival that shape the story’s rich tapestry.