ergm_preprocess_response: Update the network and the response argument.

View source: R/ergm_response.R

ergm_preprocess_responseR Documentation

Update the network and the response argument.

Description

Update the network and the response argument.

Usage

ergm_preprocess_response(nw, response)

Arguments

nw

a network object.

response

Either a character string, a formula, or NULL (the default), to specify the response attributes and whether the ERGM is binary or valued. Interpreted as follows:

NULL

Model simple presence or absence, via a binary ERGM.

character string

The name of the edge attribute whose value is to be modeled. Type of ERGM will be determined by whether the attribute is logical (TRUE/FALSE) for binary or numeric for valued.

a formula

must be of the form NAME~EXPR|TYPE (with | being literal). EXPR is evaluated in the formula's environment with the network's edge attributes accessible as variables. The optional NAME specifies the name of the edge attribute into which the results should be stored, with the default being a concise version of EXPR. Normally, the type of ERGM is determined by whether the result of evaluating EXPR is logical or numeric, but the optional TYPE can be used to override by specifying a scalar of the type involved (e.g., TRUE for binary and 1 for valued).

Details

  1. If response is NULL or logical, drop all edge attributes except for na and return the network and the response as they are.

  2. If response is a character vector of length 1, drop all edge attributes in nw except for the one corresponding to response.

  3. If response is a formula, construct a name for it and assign to that name (as an edge attribute) the result of evaluating the formula environment; drop all other edge attributes. Return as response the name (possibly with the attribute for the formula attached). If the formula's RHS is of the form a|b use the logicalness of b in Step 4.

  4. Test if the resulting edge attribute is of mode logical. If so set attr(response,'valued') to FALSE, otherwise to TRUE.

If both nw and response are ordinary variables (i.e., not expressions) in the parent frame, nw (whatever it was named) is overwritten with the updated network and response (again, whatever it was named) is deleted. This is for convenience and for making outdated code that relies on response fail immediately rather than introduce subtle bugs. Otherwise, the updated network is returned.

Examples


preproc_check_print <- function(nw, response){
  ergm_preprocess_response(nw, response) 
  str(list(
       valued = is.valued(nw),
       el = head(as.edgelist(nw, attrname=nw%ergmlhs%"response", output="tibble"),3)
  ))
}

data(florentine)
preproc_check_print(flomarriage, NULL)

flomarriage %e% "w" <- runif(network.edgecount(flomarriage))
flomarriage %e% "s" <- rep(c(-1,1), length.out=network.edgecount(flomarriage))

# Edge attribute expression
preproc_check_print(flomarriage, ~w*s)

# Named
preproc_check_print(flomarriage, wsprod~w*s)

# Binary from valued
preproc_check_print(flomarriage, ~s>0)

# Default edge attribute mode is valued
flomarriage[,] <- 0 # Empty network
preproc_check_print(flomarriage, ~w*s)

# Force default edge attribute mode to binary
preproc_check_print(flomarriage, ~w|TRUE)


ergm documentation built on May 31, 2023, 8:04 p.m.