count_stats: Count Network Statistics

View source: R/count_stats.R

count_statsR Documentation

Count Network Statistics

Description

This function is similar to what ergm::summary_formula does, but it provides a fast wrapper suited for matrix class objects (see benchmark in the examples).

Usage

count_stats(X, ...)

AVAILABLE_STATS()

## S3 method for class 'formula'
count_stats(X, ...)

## S3 method for class 'list'
count_stats(X, terms, attrs = NULL, ...)

Arguments

X

List of square matrices. (networks)

...

Passed to the method.

terms

Character vector with the names of the statistics to calculate. Currently, the only available statistics are: '\Sexpr{paste(ergmito::AVAILABLE_STATS(), collapse="', '")}'.

attrs

A list of vectors. This is used when term has a nodal attribute such as nodeicov(attrname="").

Value

A matrix of size length(X) * length(terms) with the corresponding counts of statistics.

Examples

# DGP 
set.seed(123199)
x <- rbernoulli(rep(5, 10))
ans0 <- count_stats(x, c("mutual", "edges"))

# Calculating using summary_formula
ans1 <- lapply(x, function(i) {
  ergm::summary_formula(i ~ mutual + edges)
})

ans1 <- do.call(rbind, ans1)

# Comparing
all.equal(unname(ans0), unname(ans1))

# count_stats is vectorized (and so faster)
bm <- benchmarkito(
  count_stats = count_stats(x, c("mutual", "edges")),
  lapply      = lapply(x, function(i) {
  ergm::summary_formula(i ~ mutual + edges)
}), times = 50
)

plot(bm)


muriteams/ergmito documentation built on Sept. 15, 2023, 7:07 a.m.