Skip to content

Commit

Permalink
Implemented an EgoStat for absdiffcat(), and added tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
krivit committed Jan 8, 2024
1 parent 33a49a0 commit 9f6415e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
31 changes: 31 additions & 0 deletions R/EgoStat.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ split_aaties_by_ego <- function(x, egor){
#' * `nodematch`
#' * `nodemix`
#' * `absdiff`
#' * `absdiffcat`
#' * `degree`
#' * `degrange`
#' * `concurrent`
Expand Down Expand Up @@ -397,6 +398,36 @@ EgoStat.absdiff <- function(egor, attr, pow=1){
h
}

EgoStat.absdiffcat <- function(egor, attr, base=NULL, levels=NULL){
if(!missing(base)) message("In term `absdiffcat' in package `ergm.ego': Argument \"base\" has been superseded by \"levels\" and it is recommended to use the latter. Note that its interpretation may be different.")

egos <- as_tibble(egor$ego)
alters <- egor$alter

xe <- ergm.ego_get_vattr(attr, egos)
xa <- ergm.ego_get_vattr(attr, alters)

attrname <- attributes(xe)$name

ux <- unique(c(xe,xa))
u <- sort(unique(as.vector(abs(outer(ux,ux,"-")))),na.last=NA)
u <- u[u>0]
if(missing(levels) && any(NVL(base,0)!=0)) u <- u[-base]

levs <- ergm.ego_attr_levels(levels, u, egor)
nlevs <- length(levs)

if (nlevs==0)
stop("Argument to `absdiffcat' has too few distinct differences.")

xal <- split_alters_by_ego(xa, egor)

h <- .mapply_col(function(e,a) .extabulate(.matchNA(abs(e-a), levs, 0), nbins=nlevs)/2, xe, xal, SIMPLIFY=TRUE)
colnames(h) <- paste("absdiff",attrname,levs,sep=".")
attr(h, "order") <- 1
h
}

EgoStat.degree <- function(egor, d, by=NULL, homophily=FALSE, levels=NULL){
## if(any(d==0)) warning("degree(0) (isolate) count statistic depends strongly on the specified population network size.")

Expand Down
1 change: 1 addition & 0 deletions man/ergm.ego-terms.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions tests/testthat/test-EgoStat.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ test_that("egostats are close to complete network stats", {

absdiff("a") + absdiff("a", 2) + absdiff(~a + 2*c - exp(d)) +
absdiff(function(x) if(is(x, "data.frame")) x[["a"]] + 2*x[["c"]] - exp(x[["d"]]) else (x %v% "a") + 2*(x %v% "c") - exp(x %v% "d")) +


absdiffcat("a") + absdiffcat("a", levels=-1) + absdiffcat("a", levels=2) +

degree(0) + degree(3) + degree(0:6) +
degree(0, by="a") + degree(3, by="a") + degree(0:6, by="a") +
degree(0, by="a", homophily=TRUE) + degree(3, by="a", homophily=TRUE) + degree(0:6, by="a", homophily=TRUE) + degree(0:6, by="a", homophily=FALSE, levels = -1) +
Expand Down Expand Up @@ -132,11 +134,12 @@ test_that("egostats with alter missing data are close to complete network stats"
f <- ~ edges +
nodecov("a") +

nodefactor("a", 0) + nodefactor("a", 1) + nodefactor("a", 2) +
nodefactor("a", levels=TRUE) + nodefactor("a", levels=-1) + nodefactor("a", levels=-2) +

nodematch("a") + nodematch("a", TRUE) + nodematch("a", TRUE, 2) +
nodematch("a") + nodematch("a", TRUE) + nodematch("a", TRUE, levels=-2) +

absdiff("a") + absdiff("a", 2) +
absdiffcat("a") + absdiffcat("a", levels=-1) + absdiffcat("a", levels=I(2)) +

degree(0) + degree(3) + degree(0:6) +
degree(0, by="a") + degree(3, by="a") + degree(0:6, by="a") +
Expand Down

0 comments on commit 9f6415e

Please sign in to comment.