From 8b05ae9afed4d3de22bd439c5cb2ed42ea57c5b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bojanowski?= Date: Thu, 20 Jun 2024 15:40:46 +0200 Subject: [PATCH] Added test of degreedist() output ... for relevant argument configurations (prob, by, brgmod). References statnet/ergm.ego#82. --- tests/testthat/test-degreedist.R | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/testthat/test-degreedist.R b/tests/testthat/test-degreedist.R index fd2f4dd..faf91a1 100644 --- a/tests/testthat/test-degreedist.R +++ b/tests/testthat/test-degreedist.R @@ -89,3 +89,44 @@ test_that("weighted degreedist by attribute with weights disabled", { expect_equal(ignore_attr=TRUE,unclass(degreedist(e, plot=FALSE, by="x", weight=FALSE)), rbind(c(1/2,1/2), c(1/2,1/2))) }) + + + +# -------------------------------------------------------------------- + +# Test proper output depending on `prob`, `by` and `brgmod` arguments. +# Motivated by statnet/ergm.ego#82. + +# Argument configurations to test +arg_df <- expand.grid( + prob = c(FALSE, TRUE), + brgmod = c(FALSE, TRUE), + by = list(NULL, "Sex") +) +for(i in seq(1, nrow(arg_df))) { + a <- lapply(arg_df[i,], unlist) + test_that( + paste0("degreedist(", paste0(names(a), "=", a, collapse=", "), ")"), { + with( + a, { + expect_silent( + res <- degreedist( + fmh.ego, + prob = prob, + brgmod = brgmod, + by = by[[1]], + plot = FALSE + ) + ) + # Conditional probs sum to number of cat. of `by` + if(prob && !is.null(by[[1]])) + expect_equal(sum(res), length(unique(fmh.ego$ego[[by]]))) + # Unconditional probs sum to 1 + if(prob && is.null(by[[1]])) expect_equal(sum(res), 1) + # Counts sum to # of egos + if(!prob) expect_equal(sum(res), nrow(fmh.ego$ego)) + } + ) + } + ) +}