Nothing
################################################################################
test_that("estimate_liability_multi errors clearly when target_phenotype is missing for PA", {
# Regression test: with method = "PA" and target_phenotype left at its
# default (NULL), cur_target_id was built as paste0("g_", NULL) == "g_",
# which matches no real covariance-matrix column, so PA_algorithm()
# failed later with an unrelated-looking "Target ID not found" error
# instead of a clear message about the actual, upstream problem.
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower_p1 = c(-Inf, -Inf), upper_p1 = c(0.5, 0.8),
lower_p2 = c(-Inf, -Inf), upper_p2 = c(0.3, 0.6)
)
expect_error(
estimate_liability_multi(.tbl = tbl, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"),
fid = "fid", pid = "pid", role = "role",
method = "PA"),
"target_phenotype"
)
})
test_that("estimate_liability_multi does not validate target_phenotype for Gibbs", {
# target_phenotype is never consulted by Gibbs_estimator(), so a mismatch
# with phen_names should not block a Gibbs run.
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower_p1 = c(-Inf, -Inf), upper_p1 = c(0.5, 0.8),
lower_p2 = c(-Inf, -Inf), upper_p2 = c(0.3, 0.6)
)
expect_no_error(
estimate_liability_multi(.tbl = tbl, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"),
fid = "fid", pid = "pid", role = "role",
method = "Gibbs", target_phenotype = "not_a_real_phenotype",
out = 1, tol = 0.5)
)
})
################################################################################
# .tbl and family_graphs are mutually exclusive inputs
################################################################################
test_that("estimate_liability_single errors when both .tbl and family_graphs are given", {
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower = c(-Inf, 1.5), upper = c(Inf, 1.5)
)
fam <- data.frame(fam = 1, i = c("proband", "mom"), f = c("0", "0"), m = c("mom", "0"))
thresholds <- data.frame(i = c("proband", "mom"), lower = -Inf, upper = 0.8)
graph <- prepare_graph(fam, icol = "i", fcol = "f", mcol = "m", node_attributes = thresholds)
fg <- get_family_graphs(graph, ndegree = 2, proband_vec = "proband", fid = "fid")
expect_error(
estimate_liability_single(.tbl = tbl, family_graphs = fg, h2 = 0.5,
pid = "pid", fid = "fid", role = "role", method = "PA"),
"only one"
)
})
test_that("estimate_liability_multi errors when both .tbl and family_graphs are given", {
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower_p1 = c(-Inf, -Inf), upper_p1 = c(0.5, 0.8),
lower_p2 = c(-Inf, -Inf), upper_p2 = c(0.3, 0.6)
)
fam <- data.frame(fam = 1, i = c("proband", "mom"), f = c("0", "0"), m = c("mom", "0"))
thresholds <- data.frame(i = c("proband", "mom"), lower_p1 = -Inf, upper_p1 = 0.8,
lower_p2 = -Inf, upper_p2 = 0.8)
graph <- prepare_graph(fam, icol = "i", fcol = "f", mcol = "m", node_attributes = thresholds)
fg <- get_family_graphs(graph, ndegree = 2, proband_vec = "proband", fid = "fid")
expect_error(
estimate_liability_multi(.tbl = tbl, family_graphs = fg, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"),
fid = "fid", pid = "pid", role = "role", method = "PA",
target_phenotype = "p1"),
"only one"
)
})
################################################################################
# PA correctness, against a hand-built PA_algorithm() ground truth
################################################################################
test_that("estimate_liability_single (PA) matches a direct PA_algorithm call", {
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower = c(-Inf, 1.5), upper = c(Inf, 1.5)
)
res <- estimate_liability_single(.tbl = tbl, h2 = 0.5, pid = "pid", fid = "fid", role = "role",
method = "PA", out = c(1))
gt_cov <- construct_covmat(h2 = 0.5, fam_vec = c("m"))
gt <- PA_algorithm(mu = rep(0, 3), covmat = gt_cov, target_id = "g",
lower = c(-Inf, -Inf, 1.5), upper = c(Inf, Inf, 1.5))
expect_equal(res$est, gt$est)
expect_equal(res$var, gt$var)
})
test_that("estimate_liability_single (PA, useMixture) matches a direct PA_algorithm call", {
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower = c(-Inf, -Inf), upper = c(Inf, 1.2),
K_i = c(NA, 0.05), K_pop = c(NA, 0.10)
)
res <- estimate_liability_single(.tbl = tbl, h2 = 0.5, pid = "pid", fid = "fid", role = "role",
method = "PA", out = c(1), useMixture = TRUE)
gt_cov <- construct_covmat(h2 = 0.5, fam_vec = c("m"))
gt <- PA_algorithm(mu = rep(0, 3), covmat = gt_cov, target_id = "g",
lower = c(-Inf, -Inf, -Inf), upper = c(Inf, Inf, 1.2),
K_i = c(NA, NA, 0.05), K_pop = c(NA, NA, 0.10))
expect_equal(res$est, gt$est)
expect_equal(res$var, gt$var)
})
test_that("estimate_liability_multi (PA, useMixture) matches a direct PA_algorithm call", {
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower_p1 = c(-Inf, -Inf), upper_p1 = c(Inf, 1.2), K_i_p1 = c(NA, 0.05), K_pop_p1 = c(NA, 0.10),
lower_p2 = c(-Inf, -Inf), upper_p2 = c(Inf, 1.3), K_i_p2 = c(NA, 0.06), K_pop_p2 = c(NA, 0.12)
)
res <- estimate_liability_multi(.tbl = tbl, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"), pid = "pid", fid = "fid", role = "role",
method = "PA", out = c(1), useMixture = TRUE, target_phenotype = "p1")
gt_cov <- construct_covmat(h2 = c(0.5, 0.5), genetic_corrmat = diag(2), full_corrmat = diag(2),
fam_vec = c("m"), phen_names = c("p1", "p2"))
gt <- PA_algorithm(mu = rep(0, nrow(gt_cov)), covmat = gt_cov, target_id = "g_p1",
lower = rep(-Inf, 6), upper = c(Inf, Inf, 1.2, Inf, Inf, 1.3),
K_i = c(NA, NA, 0.05, NA, NA, 0.06), K_pop = c(NA, NA, 0.10, NA, NA, 0.12))
expect_equal(res$est, gt$est)
expect_equal(res$var, gt$var)
})
################################################################################
# pid reported for .tbl input
################################################################################
test_that("estimate_liability_single reports the proband's own pid for .tbl input, not fid", {
# Regression test: the .tbl-input branch always set pid := cur_fid
# (copying estimate_liability_multi()'s graph-input branch, where pid
# and fid are the same by convention), instead of looking up the
# proband's actual pid (role == "o") the way
# estimate_liability_multi()'s own .tbl-input branch already did. Any
# dataset where fid and the proband's pid genuinely differ - the normal
# case - got the family id back in the pid column instead of the
# individual's id.
tbl <- tibble::tibble(
fid = "family_42", pid = c("person_107", "person_108"), role = c("o", "m"),
lower = c(-Inf, 1.5), upper = c(Inf, 1.5)
)
res <- estimate_liability_single(.tbl = tbl, h2 = 0.5, pid = "pid", fid = "fid", role = "role",
method = "PA", out = c(1))
expect_equal(res$pid, "person_107")
})
################################################################################
# Gibbs_estimator's out handling for multiple phenotypes
################################################################################
test_that("Gibbs_estimator respects out for multiple phenotypes instead of always returning genetic", {
# Regression test: updated_out used to be
# which(str_detect(colnames(cov), "g_")), which always selected every
# phenotype's genetic-liability column regardless of `out`. Requesting
# out = 2 ("full") silently returned genetic liability mislabeled as
# "full", and out = c(1, 2) crashed outright, since updated_out (length
# = n phenotypes) and return_names (length = n phenotypes * length(out))
# disagreed.
tbl <- tibble::tibble(
fid = "fam1", pid = c("proband", "mom"), role = c("o", "m"),
lower_p1 = c(-Inf, -Inf), upper_p1 = c(Inf, 1.2),
lower_p2 = c(-Inf, -Inf), upper_p2 = c(Inf, 1.3)
)
res_genetic <- estimate_liability_multi(.tbl = tbl, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"), pid = "pid", fid = "fid", role = "role",
method = "Gibbs", out = c(1), tol = 1)
res_full <- estimate_liability_multi(.tbl = tbl, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"), pid = "pid", fid = "fid", role = "role",
method = "Gibbs", out = c(2), tol = 1)
res_both <- estimate_liability_multi(.tbl = tbl, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"), pid = "pid", fid = "fid", role = "role",
method = "Gibbs", out = c(1, 2), tol = 1)
expect_setequal(colnames(res_genetic), c("fid", "pid", "genetic_p1_est", "genetic_p1_se", "genetic_p2_est", "genetic_p2_se"))
expect_setequal(colnames(res_full), c("fid", "pid", "full_p1_est", "full_p1_se", "full_p2_est", "full_p2_se"))
expect_setequal(colnames(res_both), c("fid", "pid",
"genetic_p1_est", "genetic_p1_se", "full_p1_est", "full_p1_se",
"genetic_p2_est", "genetic_p2_se", "full_p2_est", "full_p2_se"))
# genetic and full liability must actually differ (h2 = 0.5, not 0 or 1) -
# catches the bug where "full" silently returned the genetic values
expect_false(isTRUE(all.equal(res_full$full_p1_est, res_genetic$genetic_p1_est, tolerance = 1e-3)))
# out = c(1,2) must reproduce the separate single-out runs (within Monte
# Carlo noise, same seed-free Gibbs run - loose tolerance)
expect_equal(res_both$genetic_p1_est, res_genetic$genetic_p1_est, tolerance = 0.2)
expect_equal(res_both$full_p1_est, res_full$full_p1_est, tolerance = 0.2)
})
test_that("Gibbs_estimator's out handling works for the graph path too, not just .tbl", {
# Regression test: the first fix for the above used
# match(paste0(c("g","o")[out], "_", phen_names), colnames(cov)), which
# assumes .tbl-style column names ("g_p1", "o_p1"). The graph path names
# its columns from the proband's own id instead (e.g. "proband_g_p1" for
# genetic, just "proband_p1" - no "o" marker at all - for full liability
# via graph_based_covariance_construction_multi()), so every name-based
# match failed (all NA), surfacing as "out is not of the required
# format" warnings and a dimnames-length error. Fixed by position
# instead of name: both paths guarantee genetic liability is the 1st and
# full liability the 2nd entry of each phenotype's block in cov.
fam <- data.frame(fam = 1, i = c("proband", "mom"), f = c("0", "0"), m = c("mom", "0"))
thresholds <- data.frame(i = c("proband", "mom"), lower_p1 = -Inf, upper_p1 = 0.8,
lower_p2 = -Inf, upper_p2 = 0.8)
graph <- prepare_graph(fam, icol = "i", fcol = "f", mcol = "m", node_attributes = thresholds)
fg <- get_family_graphs(graph, ndegree = 2, proband_vec = "proband", fid = "fid")
res_genetic <- estimate_liability_multi(family_graphs = fg, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"), fid = "fid", pid = "pid",
method = "Gibbs", out = c(1), tol = 1)
res_full <- estimate_liability_multi(family_graphs = fg, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"), fid = "fid", pid = "pid",
method = "Gibbs", out = c(2), tol = 1)
res_both <- estimate_liability_multi(family_graphs = fg, h2_vec = c(0.5, 0.5),
genetic_corrmat = diag(2), full_corrmat = diag(2),
phen_names = c("p1", "p2"), fid = "fid", pid = "pid",
method = "Gibbs", out = c(1, 2), tol = 1)
expect_setequal(colnames(res_genetic), c("fid", "pid", "genetic_p1_est", "genetic_p1_se", "genetic_p2_est", "genetic_p2_se"))
expect_setequal(colnames(res_full), c("fid", "pid", "full_p1_est", "full_p1_se", "full_p2_est", "full_p2_se"))
expect_setequal(colnames(res_both), c("fid", "pid",
"genetic_p1_est", "genetic_p1_se", "full_p1_est", "full_p1_se",
"genetic_p2_est", "genetic_p2_se", "full_p2_est", "full_p2_se"))
expect_false(isTRUE(all.equal(res_full$full_p1_est, res_genetic$genetic_p1_est, tolerance = 1e-3)))
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.