View source: R/build_network.R
| build_network | R Documentation |
Universal network estimation function that supports both transition networks (relative, frequency, co-occurrence) and association networks (correlation, partial correlation, graphical lasso). Uses the global estimator registry, so custom estimators can also be used.
build_network(
data,
method,
actor = NULL,
action = NULL,
time = NULL,
session = NULL,
order = NULL,
codes = NULL,
group = NULL,
format = "auto",
window_size = 3L,
mode = c("non-overlapping", "overlapping"),
scaling = NULL,
threshold = 0,
level = NULL,
time_threshold = 900,
predictability = TRUE,
params = list(),
...
)
data |
Data frame (sequences or per-observation frequencies) or a square symmetric matrix (correlation or covariance). |
method |
Character. Required. Name of a registered estimator.
Built-in methods: |
actor |
Character. Name of the actor/person ID column for sequence
grouping. Default: |
action |
Character. Name of the action/state column (long format).
Default: |
time |
Character. Name of the time column (long format).
Default: |
session |
Character. Name of the session column. Default: |
order |
Character. Name of the ordering column. Default: |
codes |
Character vector. Column names of one-hot encoded states
(for onehot format). Default: |
group |
Character. Name of a grouping column for per-group networks.
Returns a |
format |
Character. Input format: |
window_size |
Integer. Window size for one-hot windowing.
Default: |
mode |
Character. Windowing mode: |
scaling |
Character vector or NULL. Post-estimation scaling to apply
(in order). Options: |
threshold |
Numeric. Absolute values below this are set to zero in the result matrix. Default: 0 (no thresholding). |
level |
Character or NULL. Multilevel decomposition for association
methods. One of |
time_threshold |
Numeric. Maximum time gap (seconds) for long format
session splitting. Default: |
predictability |
Logical. If |
params |
Named list. Method-specific parameters passed to the estimator
function (e.g. |
... |
Additional arguments passed to the estimator function. |
The function works as follows:
Resolves method aliases to canonical names.
Retrieves the estimator function from the global registry.
For association methods with level specified, decomposes
the data (between-person means or within-person centering).
Calls the estimator: do.call(fn, c(list(data = data), params)).
Applies scaling and thresholding to the result matrix.
Extracts edges and constructs the netobject.
An object of class c("netobject", "cograph_network") containing:
The input data used for estimation, as a data frame.
The estimated network weight matrix.
Data frame with columns id, label, name,
x, y. Node labels are in $nodes$label.
Data frame of non-zero edges with integer from/to
(node IDs) and numeric weight.
Logical. Whether the network is directed.
The resolved method name.
The params list used (for reproducibility).
The scaling applied (or NULL).
The threshold applied.
Number of nodes.
Number of non-zero edges.
Decomposition level used (or NULL).
List with source, layout, and tna metadata
(cograph-compatible).
Node groupings data frame, or NULL.
Named numeric vector of R-squared predictability
values per node (for undirected association methods when
predictability = TRUE). NULL for directed methods.
Method-specific extras (e.g. precision_matrix, cor_matrix,
frequency_matrix, lambda_selected, etc.) are preserved
from the estimator output.
When level = "both", returns an object of class
"netobject_ml" with $between and $within
sub-networks and a $method field.
register_estimator, list_estimators,
bootstrap_network
seqs <- data.frame(V1 = c("A","B","C","A"), V2 = c("B","C","A","B"))
net <- build_network(seqs, method = "relative")
net
# Transition network (relative probabilities)
seqs <- data.frame(
V1 = sample(LETTERS[1:4], 30, TRUE), V2 = sample(LETTERS[1:4], 30, TRUE),
V3 = sample(LETTERS[1:4], 30, TRUE), V4 = sample(LETTERS[1:4], 30, TRUE)
)
net <- build_network(seqs, method = "relative")
print(net)
# Association network (glasso)
freq_data <- convert_sequence_format(seqs, format = "frequency")
net_glasso <- build_network(freq_data, method = "glasso",
params = list(gamma = 0.5, nlambda = 50))
# With scaling
net_scaled <- build_network(seqs, method = "relative",
scaling = c("rank", "minmax"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.