Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix degreedist.egor() #84

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions R/degreedist.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,13 @@ degreedist.egor <- function(object, freq = FALSE, prob = !freq,
if(prob){
if(is.null(by)){
scaledeg <- sum(deg.ego)
deg.ego <- deg.ego/scaledeg
maxfreq <- max(deg.ego, na.rm = TRUE)
} else {
scaledeg <- rowSums(deg.ego)
deg.ego <- deg.ego/scaledeg
deg.ego <- deg.ego
maxfreq <- max(max(deg.ego, na.rm = TRUE))
beside <- TRUE
}
deg.ego <- deg.ego/scaledeg
}
maxfreq <- max(deg.ego, na.rm=TRUE)

if(plot){
if(brgmod) {
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-degreedist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
plot = FALSE
)
)
# Conditional probs sum to number of cat. of `by`
if(prob && !is.null(by))
expect_equal(sum(res), length(unique(fmh.ego$ego[[by]])))
# Unconditional probs sum to 1
if(prob && is.null(by)) expect_equal(sum(res), 1)
# Counts sum to # of egos
if(!prob) expect_equal(sum(res), nrow(fmh.ego$ego))
}
)
}
)
}
Loading