@@ -435,6 +435,58 @@ test_that("keep_ids subset produces correct relatedness values across all famili
435435})
436436
437437
438+ test_that(" keep_ids subset produces correct relatedness values across all families" , {
439+ library(ggpedigree )
440+ data(ASOIAF )
441+ df <- ASOIAF | > dplyr :: rename(ID = id )
442+
443+ r_full_sparse <- ped2com(df ,
444+ component = " additive" , sparse = TRUE ,
445+ keep_ids = NULL ,
446+ mz_twins = FALSE
447+ )
448+ for (i in unique(df $ famID )) {
449+ n_rows <- sum(df $ famID == i )
450+ if (n_rows < 3 ) {
451+ next
452+ }
453+ keep <- as.character(sample(df $ ID [df $ famID == i ], min(15 ,n_rows )))
454+
455+
456+ r_full <- r_full_sparse
457+ r_sub <- ped2com(df ,
458+ component = " additive" , sparse = TRUE ,
459+ keep_ids = keep ,
460+ mz_twins = FALSE
461+ )
462+
463+ expect_equal(dim(r_sub ), c(length(keep ), length(keep )))
464+ expect_equal(rownames(r_sub ), keep )
465+
466+ # values in the subset must match the corresponding entries of the full matrix
467+ expect_equal(r_sub , r_full [keep , keep ], tolerance = 1e-10 , info = paste(" Family" , i ))
468+
469+ # entirely random subset of IDs across the whole dataset (not just within family)
470+ keep <- as.character(sample(df $ ID , 15 ))
471+
472+
473+ r_full <- r_full_sparse
474+ r_sub <- ped2com(df ,
475+ component = " additive" , sparse = TRUE ,
476+ keep_ids = keep ,
477+ mz_twins = FALSE
478+ )
479+
480+ expect_equal(dim(r_sub ), c(length(keep ), length(keep )))
481+ expect_equal(rownames(r_sub ), keep )
482+
483+ # values in the subset must match the corresponding entries of the full matrix
484+ expect_equal(r_sub , r_full [keep , keep ], tolerance = 1e-10 , info = paste(keep ))
485+ }
486+ })
487+
488+
489+
438490test_that(" keep_ids with unknown IDs warns and drops missing entries" , {
439491 data(hazard )
440492 keep <- c(as.character(hazard $ ID [1 : 3 ]), " BOGUS_ID" )
0 commit comments