## ----setup, include=FALSE----------------------------------------------------- knitr::opts_chunk$set(echo = TRUE) ## ----results = 'hide',warning = FALSE, message = FALSE------------------------ # load libraries library(LTFGRS) library(dplyr) library(stringr) library(lubridate) library(ggplot2) library(rmarkdown) # setting seed set.seed(555) ## ----------------------------------------------------------------------------- h2 = 0.5 # heritability K = 0.1 # population prevalence ## ----------------------------------------------------------------------------- family = tribble( ~id, ~momcol, ~dadcol, "pid", "mom", "dad", "sib", "mom", "dad", "mom", NA, NA, "dad", NA, NA) ## ----------------------------------------------------------------------------- # constructing covariance matrix for the family tmp_graph = prepare_graph(family, icol = "id", mcol = "momcol", fcol = "dadcol") cov_mat_obj = get_covmat(fam_graph = tmp_graph, h2 = 0.5, add_ind = TRUE, index_id = "pid") SIGMA = cov_mat_obj[rev(colnames(cov_mat_obj)),rev(colnames(cov_mat_obj)) ] ## ----------------------------------------------------------------------------- # number of families to construct (iterations) nsim = 2e3 # add an identifieer to each family member per simulation # format is: trio = lapply(1:nsim, function(i) { tmp_res = lapply(seq_along(family), function(col) { paste(family[[col]], i, sep = "") }) names(tmp_res) = colnames(family) tmp_res %>% as_tibble() }) %>% bind_rows # setting NAs to be NAs again trio = trio %>% mutate(across(everything(), ~ ifelse(str_detect(.x, "NA"), NA, .x))) print(trio) ## ----------------------------------------------------------------------------- # simulated liabilities for each individual liabs = MASS::mvrnorm(n = nsim, mu = rep(0, nrow(SIGMA)), Sigma = SIGMA) # generating age of onset and status info liabs2 = as_tibble(liabs) %>% mutate(across(-pid_g, ~ round(purrr::map_dbl(.x = .x, .f = LTFGRS:::convert_liability_to_aoo, pop_prev = K)), .names = "{col}_aoo"), # adding age columns with random ages for controls and aoo for cases across(contains("aoo"), ~ ifelse(is.na(.x), sample(1:100, size = sum(is.na(.x)), replace = TRUE), .x), .names = "{col}_age"), # generating status across(6:9, ~ (!is.na(.x)) + 0, .names = "{col}_status")) colnames(liabs2) = str_replace(colnames(liabs2), "aoo_", "") ## ----echo=FALSE--------------------------------------------------------------- paged_table(liabs2, options = list(max.print = 100)) ## ----------------------------------------------------------------------------- # if we assume the population is fully observed, i.e. no censoring. Then we can calculate the cIP as: CIP = lapply(c("pid", "sib", "mom", "dad"), function(x) { ph = liabs2 %>% select(contains(x)) %>% # we do not need the liability select(contains("aoo"), contains("age"), contains("status")) colnames(ph) = str_replace_all(colnames(ph), paste0(x, "_"), "") ph }) %>% bind_rows() %>% group_by(age) %>% summarise(n_cases = sum(status), n_total = n()) %>% ungroup() %>% arrange(age) %>% mutate(cip = cumsum(n_cases) / sum(n_total)) ## ----------------------------------------------------------------------------- CIP_ds = CIP %>% mutate(cip = cip * 5) ## ----out.width="90%", fig.alt = "Plot of the two different CIPs as a function of age."---- plot_data_CIPs = bind_rows( CIP %>% mutate(type = "Population Representative"), CIP_ds %>% mutate(type = "Over-Ascertained") ) # how does the CIP look like in both scenarios? plot_data_CIPs %>% ggplot(aes(x = age, y = cip)) + geom_point() + facet_grid(~type) + theme_classic() ## ----results = 'hide'--------------------------------------------------------- thresholds = lapply(c("pid", "sib", "mom", "dad"), function(x) { ph = liabs2 %>% mutate(fid = 1:n()) %>% select(fid, contains(x)) %>% # we do not need the liability select(fid, contains("aoo"), contains("age"), contains("status")) colnames(ph) = str_replace_all(colnames(ph), paste0(x, "_"), "") left_join(ph %>% mutate(age = pmax(pmin(age, 99), 1)), select(CIP, age, cip), by = join_by(age)) %>% mutate(thr = qnorm(cip, lower.tail = F), lower = ifelse(status == 1, thr, -Inf), upper = ifelse(status == 1, Inf, thr), pid = paste0(x, fid)) }) %>% bind_rows() ## ----------------------------------------------------------------------------- # using duplicated families to create a population graph pop_graph = prepare_graph(.tbl = trio, icol = "id", mcol = "momcol", fcol = "dadcol", node_attributes = thresholds %>% select(pid,lower, upper) %>% rename(id = pid)) # autmatically detecting family members: fam_graphs = get_family_graphs(pop_graph = pop_graph, ndegree = 1, proband_vec = str_subset(trio$id, "pid")) ## ----------------------------------------------------------------------------- pop_rep_fgrs = estimate_liability( family_graphs = fam_graphs, h2 = h2, family_graphs_col = "fam_graph") ## ----results = 'hide'--------------------------------------------------------- # thresholds from over-ascertained CIPs thresholds_ds = lapply(c("pid", "sib", "mom", "dad"), function(x) { ph = liabs2 %>% mutate(fid = 1:n()) %>% select(fid, contains(x)) %>% # we do not need the liability select(fid, contains("aoo"), contains("age"), contains("status")) colnames(ph) = str_replace_all(colnames(ph), paste0(x, "_"), "") left_join(ph %>% mutate(age = pmax(pmin(age, 99), 1)), select(CIP_ds, age, cip), by = join_by(age)) %>% mutate(thr = qnorm(cip, lower.tail = F), lower = ifelse(status == 1, thr, -Inf), upper = ifelse(status == 1, Inf, thr), pid = paste0(x, fid)) }) %>% bind_rows # using duplicated families to create a population graph pop_graph_ds = prepare_graph(.tbl = trio, icol = "id", mcol = "momcol", fcol = "dadcol", node_attributes = thresholds_ds %>% select(pid,lower, upper) %>% rename(id = pid)) # autmatically detecting family members: fam_graphs_ds = get_family_graphs(pop_graph = pop_graph_ds, ndegree = 1, proband_vec = str_subset(trio$id, "pid"), fam_graph_col = "fam_graph_ds") # estimating the gentic liabilities: fgrs_ds = estimate_liability( family_graphs = fam_graphs_ds, h2 = h2, family_graphs_col = "fam_graph_ds") ## ----out.width="90%", fig.alt = "Plot of the true genetic liability against the estimated FGRS using both CIPs."---- plot_data = bind_rows( tibble(genetic_liab = liabs2$pid_g, FGRS = pop_rep_fgrs$est, type = "Population Representative CIP"), tibble(genetic_liab = liabs2$pid_g, FGRS = fgrs_ds$est, type = "Over-Ascertained CIP")) cor_data = group_by(plot_data, type) %>% summarise(r = cor(genetic_liab, FGRS)) plot_data %>% ggplot(aes(x = FGRS, y = genetic_liab)) + geom_point() + geom_abline(slope = 1, intercept = 0, color = "red") + facet_grid(~type) + geom_text(data = cor_data, aes(x = 1, y = -2, label = paste0("cor(g,FGRS) =", round(r, 3))), size = 5) + ylab("True Genetic Liability") + theme_classic()