Package {tabulergm}


Title: Publication-Ready Tables and Summaries for Exponential-Family Random Graph Models
Version: 0.1.0
Description: Creates publication-ready tables documenting exponential-family random graph models (ERGMs), a class of statistical models for social networks (Robins et al., 2007, <doi:10.1016/j.socnet.2006.08.002>). Tables describe model terms through their definitions, mathematical representations, and graphical representations, and can be generated from ERGM formulas or from models fitted with the 'ergm' package (Hunter et al., 2008, <doi:10.18637/jss.v024.i03>). Resulting tables can be integrated into 'quarto' and 'rmarkdown' documents.
License: MIT + file LICENSE
Encoding: UTF-8
Depends: R (≥ 4.2)
Imports: base64enc, ergm (≥ 4.0), netplot (≥ 0.4-0), network, yaml
Suggests: knitr, rmarkdown, rstudioapi, tinytest
URL: https://gvegayon.github.io/tabulergm/, https://github.com/gvegayon/tabulergm
BugReports: https://github.com/gvegayon/tabulergm/issues
Config/roxygen2/version: 8.0.0
RoxygenNote: 7.3.3
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2026-08-20 02:21:33 UTC; runner
Author: George Vega Yon ORCID iD [aut, cre]
Maintainer: George Vega Yon <g.vegayon@gmail.com>
Repository: CRAN
Date/Publication: 2026-08-21 13:50:02 UTC

tabulergm: Publication-Ready Tables and Summaries for ERGM Models

Description

tabulergm translates models estimated with the ergm framework into publication-ready tables and explanatory summaries.

Author(s)

Maintainer: George Vega Yon g.vegayon@gmail.com (ORCID)

Authors:

See Also

Useful links:


Parse an ERGM Formula

Description

Extracts ERGM term names and attributes from a formula. No fitted model is required. Returns a standardized data frame with metadata from the ERGM term database where available.

Usage

parse_ergm_formula(
  formula,
  directed = NULL,
  override = NULL,
  override.title = NULL,
  override.desc = NULL,
  override.math = NULL,
  override.figure = NULL,
  override.citation = NULL
)

Arguments

formula

An ERGM formula.

directed

Logical or NULL. Whether the network is directed, used to select the matching term metadata (math and figures). When NULL (the default), directedness is inferred from the network on the left-hand side of the formula if it can be evaluated; otherwise the lookup tries undirected metadata first, then directed.

override

A named list keyed by term name, each element a named list of fields to replace, e.g. list(edges = list(title = "Density", citation = "doi:10.1234/x")). Valid fields are title, description (or desc), math, figure, and citation.

override.title, override.desc, override.math, override.figure

Named character vectors keyed by term name, e.g. override.title = c(edges = "Density"). These take precedence over override.

override.citation

A named list keyed by term name whose elements are citation specifications in the same form the YAML ⁠citation:⁠ field accepts: a bare key ("hunter2007"), a prefixed identifier ("doi:10.1016/j.socnet.2006.08.002"), a single entry list (list(key = "hunter2007", doi = "10.1016/j.socnet.2006.08.002")), or a list of such entries.

Details

Metadata is resolved from the same three sources, and with the same precedence, as parse_ergm_model(); see its “Metadata sources” section.

Value

A data frame with columns:

term

Character. The canonical ERGM term name.

attribute

Character or NA. The attribute(s) used in the term, comma-separated when multiple.

estimate

Numeric. Always NA for formula-only parsing.

se

Numeric. Always NA for formula-only parsing.

pvalue

Numeric. Always NA for formula-only parsing.

title

Character or NA. Short one-line label for the term.

description

Character or NA. Prose description of the term.

math

Character or NA. The LaTeX definition of the statistic.

figure

Character or NA. Path to the rendered term figure.

citation

Character or NA. Citation key(s) for the term, comma-separated when several. The corresponding bibliography is attached to the data frame as the "tabulergm_citations" attribute.

See Also

parse_ergm_model() for parsing fitted models, ergm::search.ergmTerms() for the underlying term database.

Examples

library(ergm)
parse_ergm_formula(network ~ edges + nodematch("gender"))

# Directedness can be stated explicitly when the formula has no
# network on its left-hand side
parse_ergm_formula(~ edges + mutual, directed = TRUE)

# Attach a citation to a term that has none in the term dictionary
parse_ergm_formula(
  ~ edges + kstar(2),
  directed = FALSE,
  override.citation = list(
    kstar = list(key = "frank1986", doi = "10.1080/0022250X.1986.9990013")
  )
)

Parse an ERGM Model Object

Description

Extracts terms, coefficients, standard errors, p-values, and term metadata from a fitted ergm object. Returns a standardized data frame for use in publication-ready tables.

Usage

parse_ergm_model(
  object,
  override = NULL,
  override.title = NULL,
  override.desc = NULL,
  override.math = NULL,
  override.figure = NULL,
  override.citation = NULL
)

Arguments

object

A fitted ergm object.

override

A named list keyed by term name, each element a named list of fields to replace, e.g. list(edges = list(title = "Density", citation = "doi:10.1234/x")). Valid fields are title, description (or desc), math, figure, and citation.

override.title, override.desc, override.math, override.figure

Named character vectors keyed by term name, e.g. override.title = c(edges = "Density"). These take precedence over override.

override.citation

A named list keyed by term name whose elements are citation specifications in the same form the YAML ⁠citation:⁠ field accepts: a bare key ("hunter2007"), a prefixed identifier ("doi:10.1016/j.socnet.2006.08.002"), a single entry list (list(key = "hunter2007", doi = "10.1016/j.socnet.2006.08.002")), or a list of such entries.

Details

The coefficient names produced by ergm (which may expand terms into multiple rows, e.g., nodefactor.race.Black) are mapped back to the canonical term names from the formula.

Value

A data frame with columns:

term

Character. The canonical ERGM term name.

coef_name

Character. The full coefficient name from the model.

attribute

Character or NA. The attribute(s) used in the term, comma-separated when multiple.

estimate

Numeric. The coefficient estimate.

se

Numeric. The standard error.

pvalue

Numeric. The p-value.

title

Character or NA. Short one-line label for the term.

description

Character or NA. Prose description of the term.

math

Character or NA. The LaTeX definition of the statistic.

figure

Character or NA. Path to the rendered term figure.

citation

Character or NA. Citation key(s) for the term, comma-separated when several. The corresponding bibliography is attached to the data frame as the "tabulergm_citations" attribute.

Metadata sources

Each metadata field is resolved from three sources, in increasing order of precedence: the ergm term database (via ergm::search.ergmTerms(), which supplies title and description), the YAML term database shipped in ⁠inst/terms/⁠ (which may supply title, description, math, figure, and citation), and the ⁠override*⁠ arguments documented below.

Override names are matched against the term column first and against coef_name second, so an expanded coefficient such as nodefactor.race.Black can be targeted individually. Names matching no row produce a warning.

See Also

parse_ergm_formula() for formula-only parsing, ergm::search.ergmTerms() for the underlying term database.

Examples

library(ergm)
fit <- readRDS(system.file("fits", "fit_nodematch.rds", package = "tabulergm"))
parse_ergm_model(fit)

# Replace the title and description of a single term
parse_ergm_model(
  fit,
  override.title = c(edges = "Density"),
  override.desc  = c(edges = "Baseline propensity to form ties.")
)

Notation and Drawing Standards for Term Definitions

Description

Reference for the conventions used in the YAML term database under ⁠inst/terms/⁠. Follow these standards when adding or editing term definitions so that the math and figures stay consistent across the term dictionary.

Files

Each term lives in ⁠inst/terms/<term>.<directed|undirected>.yml⁠, where ⁠<term>⁠ is the canonical ergm term name as written in a model formula (e.g., gwesp, b1nodematch). Terms available for both directed and undirected networks get one file per variant; bipartite terms use undirected. A file has up to five top-level entries: title and description (text, both optional), citation (optional, see below), plot (the network drawing specification), and math (a LaTeX expression). No parser changes are needed for new terms: files are looked up by term name.

Titles, descriptions, and citations

title is a short label, capitalized like a heading and without a trailing period (e.g. ⁠Uniform homophily⁠). description is one to three sentences of prose saying what the statistic counts and why a modeler would include it; write it as a folded block scalar (⁠>-⁠) so the source stays readable. Both fields are optional: when absent, tabulergm falls back to the title and description recorded in the ergm term database, which are accurate but often too long, too technical, or full of raw LaTeX for a table cell. Prefer writing them here.

citation records the source(s) that introduced the term. Each entry carries a key (the marker shown in the table, conventionally lastnameYEAR) and, where one exists, a machine-readable identifier so readers can pull the full reference into their own bibliography:

citation:
  - key: hunter2007
    doi: 10.1016/j.socnet.2006.08.005

Accepted identifier fields are doi, arxiv, pmid, and url; an entry may also carry free-text text, which is the right choice when no stable identifier can be verified. Always resolve an identifier before committing it (for a DOI, ⁠https://doi.org/<id>⁠); an identifier that points at the wrong paper is worse than none.

tabulergm_table() renders a (key) marker next to the term's description and a matching ⁠[key] identifier⁠ line below the table. Users can replace any of these fields per table through the override arguments of tabulergm_table() without editing the YAML.

Math notation

Always verify a new formula against the ergm term manual (see ergm::ergmTerm and ergm::search.ergmTerms()) and the source literature. When the manual is ambiguous, compare the formula numerically against summary(nw ~ term) on a small test network.

Drawing conventions

The plot entry supports edgelist, vcolor, vshape, vsize, ecolor, elinetype, and layout (with x and y coordinates). Edgelists are chains like "0->1->2, 0->3": each consecutive pair is one edge. Per-vertex vectors follow the node order obtained from the parsed edgelist (unique node ids, all tail nodes first, then head nodes); render the figure to double-check the mapping.

Checklist for a new term

  1. Add the YAML file(s) following the standards above, including a title and description, and a citation when the term has an identifiable source.

  2. Add tinytest coverage in inst/tinytest/test_term_db.R (file lookup, math/figure reading, and formula integration).

  3. Add the term to the dictionary tables in README.qmd and vignettes/ergm-with-tabulergm.Rmd; both contain a hidden coverage-check chunk that fails the render if a term is missing.

  4. Re-render the README, and run the tests and devtools::check().

References

Hunter, D. R. (2007). Curved exponential family models for social networks. Social Networks, 29(2), 216–230. doi:10.1016/j.socnet.2006.08.005

Bomiriya, R. P., Bansal, S., & Hunter, D. R. (2014). Modeling homophily in ERGMs for bipartite networks. (No stable identifier verified; the arXiv id previously given here, 1412.1151, belongs to an unrelated paper.)


Default Plot Function for Term Figures

Description

Draws a network figure using netplot::nplot(). This is the default plot function used by tabulergm when generating term figures.

Usage

tabulergm_default_plotfun(
  netobj,
  layout,
  vcolor,
  vshape,
  vrotation,
  ecolor,
  directed,
  vsize,
  elinetype,
  ...
)

Arguments

netobj

A network::network object.

layout

A two-column numeric matrix of node coordinates (x, y).

vcolor

Character vector of vertex colors.

vshape

Character or numeric vector of vertex shapes, such as "circle"/"square" or polygon side counts.

vrotation

Numeric vector of vertex rotations (in degrees).

ecolor

Character vector of edge colors.

directed

Logical. Whether the network is directed.

vsize

Numeric vector of vertex sizes.

elinetype

Numeric or character vector of edge line types.

...

Additional arguments (currently unused).

Details

A plot function must accept the arguments netobj, layout, vcolor, ecolor, directed, and ..., and draw on the current graphics device.

Value

Called for its side-effect of drawing a plot on the current graphics device. Returns invisibly.

See Also

tabulergm_set_plotfun(), tabulergm_get_plotfun()

Examples

# See the default implementation
tabulergm_default_plotfun

# Use a custom plot function
my_plotfun <- function(netobj, layout, vcolor, ecolor, directed, vshape, vrotation, vsize, ...) {
  netplot::nplot(netobj, vertex.color = vcolor, edge.color = ecolor,
                 layout = layout)
}
old <- tabulergm_get_plotfun()
tabulergm_set_plotfun(my_plotfun)
# Restore the previous plot function
tabulergm_set_plotfun(old)

Get the Current Plot Function

Description

Returns the function currently used to draw term figures. If no custom function has been set, returns tabulergm_default_plotfun().

Usage

tabulergm_get_plotfun()

Value

A function (the active plot function).

See Also

tabulergm_default_plotfun(), tabulergm_set_plotfun()


Save a tabulergm Table and Figure Assets

Description

Saves the table code produced by tabulergm and copies the generated term figures out of the temporary cache into a user-controlled directory. The exported table references the copied image files with relative paths, making the result easier to reuse in another document or project.

Usage

tabulergm_save(object, path, ...)

## S3 method for class 'ergm'
tabulergm_save(
  object,
  path,
  format = c("markdown", "latex"),
  filename = "tabulergm-table",
  images_dir = "figures",
  overwrite = TRUE,
  latex_image_width = "0.7in",
  ...
)

## S3 method for class 'formula'
tabulergm_save(
  object,
  path,
  format = c("markdown", "latex"),
  filename = "tabulergm-table",
  images_dir = "figures",
  overwrite = TRUE,
  latex_image_width = "0.7in",
  ...
)

## S3 method for class 'data.frame'
tabulergm_save(
  object,
  path,
  format = c("markdown", "latex"),
  filename = "tabulergm-table",
  images_dir = "figures",
  overwrite = TRUE,
  latex_image_width = "0.7in",
  ...
)

Arguments

object

A fitted ergm object, an ERGM formula, or a data.frame returned by tabulergm_table().

path

Target directory. It is created recursively when needed.

...

For ergm and formula methods, additional arguments passed to tabulergm_table(). For the data.frame method, additional arguments are ignored.

format

Character vector of output formats to write. Supported values are "markdown" and "latex". By default both are written.

filename

File name stem for the table code files, without extension. Defaults to "tabulergm-table".

images_dir

Directory, relative to path, where copied images should be stored. Use "." to place images directly in path.

overwrite

Logical. Overwrite existing table code and image files? Default TRUE.

latex_image_width

Character width passed to ⁠\\includegraphics⁠ in LaTeX output. Default "0.7in".

Value

Invisibly returns a list with:

path

The normalized export directory.

files

Named character vector of written table code files.

figures

Named character vector of copied figure files, where names are the relative paths used in the table.

table

The data frame used for export, with figure paths rewritten to the copied relative paths.

Methods (by class)

See Also

tabulergm_table()

Examples

if (requireNamespace("knitr", quietly = TRUE)) {
  fit <- readRDS(system.file("fits", "fit_edges.rds", package = "tabulergm"))
  out_dir <- tempfile("tabulergm-export-")
  tabulergm_save(fit, out_dir, include_math = TRUE)
}

Set the Plot Function for Term Figures

Description

Replaces the current plot function used when drawing term figures. The function must accept netobj, layout, vcolor, ecolor, directed, and .... Setting a plot function invalidates previously cached term figures, so subsequent renders use the new function.

Usage

tabulergm_set_plotfun(plotfun)

Arguments

plotfun

A function with signature ⁠function(netobj, layout, vcolor, ecolor, directed, ...)⁠.

Value

Invisibly returns the previous plot function.

See Also

tabulergm_default_plotfun(), tabulergm_get_plotfun()

Examples

my_plotfun <- function(netobj, layout, vcolor, ecolor, directed, ...) {
  netplot::nplot(netobj, vertex.color = vcolor, edge.color = ecolor,
                 layout = layout)
}
old <- tabulergm_get_plotfun()
tabulergm_set_plotfun(my_plotfun)
# Restore the previous plot function
tabulergm_set_plotfun(old)

Generate a Publication-Ready Table from an ERGM Object

Description

S3 generic that dispatches to methods for fitted ergm objects or plain formula objects, calling the internal parsing engine and returning a formatted table.

Usage

tabulergm_table(object, ...)

## S3 method for class 'ergm'
tabulergm_table(
  object,
  include_description = FALSE,
  include_math = FALSE,
  include_attribute = FALSE,
  include_title = FALSE,
  format = c("data.frame", "html", "markdown"),
  figures_dir = NULL,
  override = NULL,
  override.title = NULL,
  override.desc = NULL,
  override.math = NULL,
  override.figure = NULL,
  override.citation = NULL,
  ...
)

## S3 method for class 'formula'
tabulergm_table(
  object,
  format = c("data.frame", "html", "markdown"),
  figures_dir = NULL,
  directed = NULL,
  include_title = FALSE,
  override = NULL,
  override.title = NULL,
  override.desc = NULL,
  override.math = NULL,
  override.figure = NULL,
  override.citation = NULL,
  ...
)

Arguments

object

A fitted ergm object or an ERGM formula.

...

Additional arguments passed to methods.

include_description

Logical. Include the term description column? Default FALSE.

include_math

Logical. Include the mathematical notation column? Default FALSE.

include_attribute

Logical. Include the attribute column? Default FALSE.

include_title

Logical. Include the short term-title column? Default FALSE.

format

Character. Output format: "data.frame" (default), "html", or "markdown". HTML and Markdown output require the knitr package.

figures_dir

Optional directory for figure assets when format = "markdown". When NULL, figures are copied automatically to the active knitr/Quarto/R Markdown figure path during non-interactive document rendering.

override

A named list keyed by term name, each element a named list of fields to replace, e.g. list(edges = list(title = "Density", citation = "doi:10.1234/x")). Valid fields are title, description (or desc), math, figure, and citation.

override.title, override.desc, override.math, override.figure

Named character vectors keyed by term name, e.g. override.title = c(edges = "Density"). These take precedence over override.

override.citation

A named list keyed by term name whose elements are citation specifications in the same form the YAML ⁠citation:⁠ field accepts: a bare key ("hunter2007"), a prefixed identifier ("doi:10.1016/j.socnet.2006.08.002"), a single entry list (list(key = "hunter2007", doi = "10.1016/j.socnet.2006.08.002")), or a list of such entries.

directed

Logical or NULL. Whether the network is directed. Passed to parse_ergm_formula(); when NULL (the default), directedness is inferred from the network on the left-hand side of the formula when possible.

Value

A data.frame (default), or a knitr_kable object when format is "html" or "markdown". When the term figures use drawing conventions (orange for focal attributes, orange/teal for mixing, squares/circles for bipartite modes), an explanatory note is appended below "html" and "markdown" tables. Terms carrying a citation get a (key) marker next to their description, and the matching ⁠[key] identifier⁠ lines are appended below the table.

Methods (by class)

See Also

tabulergm_table.ergm(), tabulergm_table.formula()

Examples

library(ergm)
fit <- readRDS(system.file("fits", "fit_edges.rds", package = "tabulergm"))
tabulergm_table(fit)
tabulergm_table(fit, include_description = TRUE)
tabulergm_table(fit, format = "markdown")

# Replace the shipped title and description for one term
tabulergm_table(
  fit,
  include_title = TRUE,
  include_description = TRUE,
  override.title = c(edges = "Density"),
  override.desc  = c(edges = "Baseline propensity to form ties.")
)
library(ergm)
tabulergm_table(network ~ edges + nodematch("gender"))

View an ERGM Table in the RStudio Viewer or System Browser

Description

Builds a self-contained HTML page containing the formatted ERGM table (with MathJax for LaTeX math rendering) and opens it in the RStudio viewer pane when available, falling back to utils::browseURL().

Usage

tabulergm_view(object, ...)

## S3 method for class 'ergm'
tabulergm_view(object, ...)

## S3 method for class 'formula'
tabulergm_view(object, ...)

Arguments

object

A fitted ergm object or an ERGM formula.

...

Additional arguments passed to tabulergm_table().

Value

Invisibly returns the path to the temporary HTML file.

Methods (by class)

See Also

tabulergm_table()

Examples

library(ergm)
fit <- readRDS(system.file("fits", "fit_edges.rds", package = "tabulergm"))
tabulergm_view(fit)

# Also works with a formula (shows term metadata only)
tabulergm_view(network ~ edges + triangle)