# Read in your data (assuming it's saved as CSV, pasted here for reproducibility)
df <- read_csv("slides\\00_bga_2026\\rawnumbers.csv")
## Rows: 31 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): output_file
## dbl (5): mit, range_min, range_max, n_rows_unfiltered, n_rows_filtered
## 
## ℹ 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.
missing <- 4160931^2 - sum(df$n_rows_unfiltered)

df <- df %>%
  rbind(
    tibble(
      mit = c(0),
      range_min = c(0),
      range_max = c(0.0002197266),
      n_rows_unfiltered = missing,
      n_rows_filtered = 0.5,
      output_file = ""
    )
  )

# Calculate midpoint of the range for plotting
df <- df %>%
  mutate(range_mid = (range_min + range_max) / 2,
         proportion = ifelse(n_rows_unfiltered > 0.5, n_rows_filtered / n_rows_unfiltered, 0),
         n_rows_unfiltered = ifelse(n_rows_unfiltered == 0, .5, n_rows_unfiltered),
         n_rows_filtered = ifelse(n_rows_filtered == 0, .5, n_rows_filtered),
         mit = factor(mit, labels = c("0", "1")),
         total = n_rows_unfiltered + n_rows_filtered,
         remainder = n_rows_unfiltered - n_rows_filtered)
library(ggplot2)
library(ggpedigree)
library(tidyverse)

results <- read_csv("slides\\00_bga_2026\\cor_cohort19flat.csv") %>%
  select(cohort_groupings,n_pairs, addRel_max, addRel_min,
         mtdna, 
         cnu, 
         age_k1_meanFunction, 
         USA_flag_10_k1_meanFunction,
         USA_flag_10_polychorFunction_rho, 
         USA_flag_10_polychorFunction_se) %>%
  filter(cohort_groupings %in% c(1919,1818)) %>%
  rename(cohort = cohort_groupings,
         n_pairs = n_pairs,
         mtdna = mtdna, 
         cnu = cnu, 
         age = age_k1_meanFunction, 
         top10 = USA_flag_10_k1_meanFunction,
         rho = USA_flag_10_polychorFunction_rho, 
         se = USA_flag_10_polychorFunction_se) %>%
# reverse order of dataset
  mutate(cohort = ifelse(cohort == 1919, "20th",
                         ifelse(cohort == 1818, "Pre-20th", cohort)),
            z = atanh(rho))  %>%
    group_by(cohort, addRel_max,cnu) %>%
  summarise(
    addRel_max = first(addRel_max),
    addRel_min = first(addRel_min),
    cnu = first(cnu),
    n_pairs_total = sum(n_pairs, na.rm = TRUE),
    z_w = weighted.mean(z, w = n_pairs - 3, na.rm = TRUE),
    rho_w = tanh(z_w),
    age_w = weighted.mean(age, w = n_pairs, na.rm = TRUE),
    top10_w = weighted.mean(top10, w = n_pairs, na.rm = TRUE),
    se_w = sqrt(1 / sum(n_pairs - 3, na.rm = TRUE)),
    mtdna_w = weighted.mean(mtdna, w = n_pairs, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  mutate(
         rho_LL = rho_w - 1.96 *se_w*sqrt(2),
         rho_UL = rho_w + 1.96 * se_w*sqrt(2),
        mtdna = 0,
        n_pairs = n_pairs_total#,
        #cnu = 0
         )
## Rows: 145 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (13): cohort_groupings, n_pairs, addRel_min, addRel_max, mtdna, cnu, age...
## 
## ℹ 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.
savep <- ggPhenotypeByDegree(
  df = results%>% filter(cnu == 0),
  y_var = "rho_w",
 # y_se = "USA_flag_10_polychorFunction_se",
  y_ci_lb = "rho_LL", #"Has_Dementia_phi_LL",
  y_ci_ub = "rho_UL", #"Has_Dementia_phi_UL",
  config = list(
    use_only_classic_kin = T,
    drop_classic_kin = FALSE,
    group_by_kin = TRUE,
    use_relative_degree = T,
    drop_non_classic_sibs = T,
    filter_degree_min = 0,
    filter_degree_max = 4,
    grouping_column = "cohort",
    filter_n_pairs = 10,
    annotate_include = FALSE
  )
)  +
    coord_cartesian(ylim = c(NA, .20))

savep

ggsave(savep,filename = "img\\cohortpis_1st.png", width = 5, height = 6, dpi = 300)

savep <- ggPhenotypeByDegree(
  df = results %>% filter(cnu == 0),
  y_var = "rho_w",
 # y_se = "USA_flag_10_polychorFunction_se",
  y_ci_lb = "rho_LL", #"Has_Dementia_phi_LL",
  y_ci_ub = "rho_UL", #"Has_Dementia_phi_UL",
  config = list(
    use_only_classic_kin = F,
    drop_classic_kin = FALSE,
    group_by_kin = TRUE,
    use_relative_degree = T,
    drop_non_classic_sibs = FALSE,
    filter_degree_min = 1,
    filter_degree_max = 8,
    grouping_column = "cohort",
    filter_n_pairs = 10,
    annotate_include = FALSE
  )
)  +    coord_cartesian(ylim = c(0, .15))
savep

ggsave(savep,filename = "img\\cohortpis_all.png", width = 5, height = 6, dpi = 300)
## Warning in grDevices::dev.off(): agg could not write to the given file