Skip to content

Commit

Permalink
Merge pull request #84 from statnet/i82-degreedist
Browse files Browse the repository at this point in the history
Fix degreedist.egor()
  • Loading branch information
mbojan authored Jun 20, 2024
2 parents b93619c + 15687c1 commit 23191e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
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))
}
)
}
)
}

0 comments on commit 23191e2

Please sign in to comment.