Package {redeem}


Type: Package
Title: Relational Event and Durational Event Models
Version: 1.0.0
Date: 2026-06-18
Author: Cornelius Fritz [aut, cre]
Maintainer: Cornelius Fritz <corneliusfritz2010@gmail.com>
Description: Model relational and durational events in a counting process framework, with functions for estimating and simulating Relational Event Models (REM) and Durational Event Models (DEM). Includes support for time-varying covariates, windowed statistics, and high-dimensional node-level fixed effects. References include Fritz et al. (2026) "Scalable Durational Event Models: Application to Physical and Digital Interactions" <doi:10.48550/arXiv.2504.00049>.
License: GPL-3
Depends: R (≥ 4.0.0)
Imports: Rcpp (≥ 1.0.12), data.table, MASS, survival, stats, grDevices, graphics, utils, digest
LinkingTo: Rcpp, RcppProgress, RcppArmadillo
Suggests: testthat (≥ 3.0.0), network, knitr, rmarkdown
VignetteBuilder: knitr
Config/testthat/edition: 3
Encoding: UTF-8
URL: https://corneliusfritz.github.io/redeem/
BugReports: https://github.com/corneliusfritz/redeem/issues
Config/roxygen2/version: 8.0.0
NeedsCompilation: yes
Packaged: 2026-06-17 21:52:02 UTC; corneliusfritz
Repository: CRAN
Date/Publication: 2026-06-22 16:20:07 UTC

redeem: Scalable Durational Event Models

Description

The redeem package provides a comprehensive framework for the estimation of Durational Event Models (DEM) and Relational Event Models (REM).

Details

The package is built to handle relational event sequences where interactions have specific start and end times. It implements the scalable block-coordinate ascent algorithm described in Fritz et al. (2026), which allows for the estimation of high-dimensional actor-specific effects and time-varying baseline intensities.

Key functions:

Author(s)

Cornelius Fritz

References

Fritz, C., Rastelli, R., Fop, M., & Caimo, A. (2026). Scalable Durational Event Models: Application to Physical and Digital Interactions. arXiv:2504.00049.

See Also

Useful links:


Initialize redeem Model Terms

Description

This is an internal dispatcher that calls the appropriate term initialization function.

Usage

InitRedeemTerm(
  term_name,
  arglist,
  model_type,
  process,
  n_nodes,
  directed = FALSE
)

Arguments

term_name

The name of the term.

arglist

A list of arguments passed to the term in the formula.

model_type

Either "dem" or "rem".

process

Either "0-1" (incidence) or "1-0" (duration).

n_nodes

Number of nodes in the network.

directed

Logical; if TRUE, the model is directed. Used for term-specific logic.


Internal helper to calculate predictions for gof

Description

Internal helper to calculate predictions for gof

Usage

calculate_predictions_helper(model, data)

Arguments

model

A redeem_result object.

data

A data.table containing the covariates.


Check Arguments for redeem Model Terms

Description

Internal helper to validate arguments and set defaults.

Usage

check.RedeemTerm(
  arglist,
  expected = list(),
  defaults = list(),
  allowed_processes = c("0-1", "1-0"),
  allowed_models = c("dem", "rem"),
  model_type = "dem",
  directed = NULL,
  directed_only = FALSE,
  undirected_only = FALSE
)

Arguments

arglist

List of arguments.

expected

List of expected types/values.

defaults

List of default values.

allowed_processes

Vector of allowed processes ("0-1", "1-0").

model_type

Current model type.


Validate the Structure of a Durational Event List

Description

This function checks the validity of a dyadic interaction matrix by ensuring that each interaction start event has a corresponding end event, that no interactions overlap within a dyad, and that no missing values are present.

Usage

check_matrix(df, return_matrix = FALSE, start_time = NULL)

Arguments

df

A data frame with at least four columns, representing events, where:

Column 1

Event timing or ID (e.g., timestamp).

Column 2

"From" node ID for the dyadic interaction.

Column 3

"To" node ID for the dyadic interaction.

Column 4

Event type (1 for start, 0 for end of interaction).

return_matrix

Logical; if TRUE, returns the (potentially repaired) event matrix. Defaults to FALSE, in which case the function returns TRUE if the matrix is valid.

start_time

Numeric; optional reference time for adding missing start events. Defaults to NULL, in which case the earliest time in the data is used.

Details

The function performs the following checks:

Value

Logical; TRUE if all interactions are valid, FALSE otherwise. If the data contains missing values, the function issues a warning and returns FALSE.

Examples

# Create a valid event matrix with durational events (start=1, end=0)
df <- matrix(c(
  1.0, 1, 2, 1,
  2.0, 1, 2, 0,
  1.5, 3, 4, 1,
  3.0, 3, 4, 0
), ncol = 4, byrow = TRUE)
colnames(df) <- c("time", "from", "to", "type")

# Check if the event matrix is valid
check_matrix(df)

Control Parameters for REDEEM Models

Description

Unified control object to manage estimation parameters for rem and dem functions.

Usage

control.redeem(
  it_max = 100,
  tol = 1e-10,
  accelerated = FALSE,
  verbose = FALSE,
  weighting = TRUE,
  subsample = 1,
  build_time = NULL,
  use_glm = FALSE,
  return_data = FALSE,
  save_hist = TRUE,
  estimate = "Blockwise",
  legacy = FALSE,
  check_matrix = FALSE,
  inf_unidentifiable = TRUE
)

Arguments

it_max

Integer; maximum number of iterations for the algorithm. Defaults to 100.

tol

Numeric; convergence tolerance. Defaults to 1e-10.

accelerated

Logical; if TRUE, uses SQUAREM acceleration for MM updates. Defaults to FALSE.

verbose

Logical; if TRUE, prints progress information. Defaults to FALSE.

weighting

Logical; whether to use weighting to group identical observations. Defaults to TRUE.

subsample

Numeric; proportion of data to subsample for internal GLM checks. Defaults to 1.

build_time

Numeric; time at which to start building the estimation dataset. Events before this time are used to compute statistics but not included as observations. Defaults to NULL, in which case all events are included.

use_glm

Logical; if TRUE, uses standard GLM for updating core coefficients. This is often slower but can yield more robust updates. Defaults to FALSE.

return_data

Logical; whether to return preprocessed data frames in the result. Defaults to FALSE.

save_hist

Logical; whether to save the iteration history of coefficients. Defaults to TRUE.

estimate

Character; estimation method for dem and rem ("Blockwise", "NR", or "GD"). Defaults to "Blockwise".

legacy

Logical; if TRUE, uses a single glm.fit call instead of the iterative loop. Defaults to FALSE.

check_matrix

Logical; whether to apply check_matrix to the event data before estimation. If TRUE, repairs missing events (e.g., adding start events for interactions that only have end events). Defaults to FALSE.

inf_unidentifiable

Logical; whether to set unidentifiable coefficients (e.g., actors with 0 event counts, globally invariant/collinear covariates) to -Inf. Defaults to TRUE.

Value

A list of class "redeem_control" containing the specified parameters.


Durational Event Model (DEM) Estimation

Description

Estimates a Durational Event Model (DEM) for relational event sequences where interactions have a duration.

Usage

dem(
  events,
  training_start = 0,
  exogenous_end = NULL,
  formula_0_1 = NULL,
  formula_1_0 = NULL,
  n_nodes = NULL,
  directed = FALSE,
  estimate_0_1 = NULL,
  estimate_1_0 = NULL,
  coef_0_1 = NULL,
  coef_1_0 = NULL,
  semiparametric = FALSE,
  simultaneous_interactions = TRUE,
  control = control.redeem()
)

Arguments

events

A matrix of events with columns time, from, to, and type (1 for start, 0 for end, 3 for exogenous changes).

training_start

Numeric; the time point at which to start the estimation. Defaults to 0.

exogenous_end

Numeric; the exogenous time point at which the observational period ends. Defaults to NULL, which implies that time when the final event was observed is taken as the end of the observational period.

formula_0_1

A one-sided formula specifying the sufficient statistics for the formation process (0 \rightarrow 1). The right-hand side must be composed of terms from redeem_terms. For example: ~ inertia() + degree. An intercept (~ 1) is the minimal specification. Defaults to NULL.

formula_1_0

A one-sided formula specifying the sufficient statistics for the dissolution process (1 \rightarrow 0). The right-hand side must be composed of terms from redeem_terms. An intercept (~ 1) is the minimal specification. Defaults to NULL.

n_nodes

Integer; the total number of actors in the network. If NULL (default), it is automatically identified based on the actors in the events set.

directed

Logical; whether the interaction events are directed. Defaults to FALSE.

estimate_0_1

Logical; whether to estimate the formation process. Defaults to NULL, in which case it is estimated if formula_0_1 is provided.

estimate_1_0

Logical; whether to estimate the dissolution process. Defaults to NULL, in which case it is estimated if formula_1_0 is provided.

coef_0_1

Numeric vector; initial coefficients for the formation model. If provided, this must be a concatenated vector of:

  1. Core coefficients: values for sufficient statistics in the formula.

  2. Degree coefficients (if degree is in the formula): a vector of length n_nodes (undirected) or 2 * n_nodes (directed, sender effects first then receiver effects).

  3. Baseline coefficients (if temporal changepoints are present): a vector of length equal to the number of baseline intervals (equal to number of changepoints if an intercept/degree is present, or changepoints + 1 if neither is present).

Defaults to NULL, in which case default starting values are automatically computed.

coef_1_0

Numeric vector; initial coefficients for the dissolution model. If provided, this must be a concatenated vector of:

  1. Core coefficients: values for sufficient statistics in the formula.

  2. Degree coefficients (if degree is in the formula): a vector of length n_nodes (undirected) or 2 * n_nodes (directed, sender effects first then receiver effects).

  3. Baseline coefficients (if temporal changepoints are present): a vector of length equal to the number of baseline intervals (equal to number of changepoints if an intercept/degree is present, or changepoints + 1 if neither is present).

Defaults to NULL, in which case default starting values are automatically computed.

semiparametric

Logical; whether to use a semiparametric baseline. Defaults to FALSE. See the 'Semiparametric Baseline' section for details.

simultaneous_interactions

Logical; whether to allow simultaneous interactions (i.e. multiple active events for the same actor or dyad at the same time). Defaults to TRUE.

control

A list of control parameters from control.redeem. Defaults to control.redeem().

Details

The Durational Event Model (DEM) is a general framework for analyzing durational events, extending standard Relational Event Models (REM) by decoupling the modeling of event incidence from event duration. It characterizes the dynamics via two separate continuous-time counting processes:

Formation Process (0 \rightarrow 1)

Counts the number of times that actor pair (i,j) starts an interaction up to time t. The incidence intensity is denoted by \lambda_{i,j}^{0\rightarrow 1}(t | \mathscr{H}_t).

Dissolution Process (1 \rightarrow 0)

Counts the number of times that actor pair (i,j) stops interacting up to time t. The dissolution intensity is denoted by \lambda_{i,j}^{1\rightarrow 0}(t | \mathscr{H}_t).

Under the assumption that the processes are non-homogeneous Poisson processes, the intensities are modeled as:

\lambda_{i,j}^{0\rightarrow 1}(t | \mathscr{H}_t, \theta^{0\rightarrow 1}) = \exp(s_{i,j}^{0\rightarrow 1}(\mathscr{H}_t)^\top \alpha^{0\rightarrow 1} + \beta_i^{0\rightarrow 1} + \beta_j^{0\rightarrow 1} + f(t, \gamma^{0\rightarrow 1}))

\lambda_{i,j}^{1\rightarrow 0}(t | \mathscr{H}_t, \theta^{1\rightarrow 0}) = \exp(s_{i,j}^{1\rightarrow 0}(\mathscr{H}_t)^\top \alpha^{1\rightarrow 0} + \beta_i^{1\rightarrow 0} + \beta_j^{1\rightarrow 0} + f(t, \gamma^{1\rightarrow 0}))

where:

To satisfy the Feller criterion and ensure that the continuous-time counting process remains non-explosive, count-based network statistics (such as inertia or common partners) are typically log-transformed on the \log(x + 1) scale.

Value

An object of class dem_object containing model estimates, log-likelihoods, and preprocessed data. See dem_object for details on the components of the returned object and S3 methods.

Scalable Estimation Algorithm

The likelihood of the model is separable with respect to \theta^{0\rightarrow 1} and \theta^{1\rightarrow 0}, allowing independent estimation of the incidence and duration components. Traditional maximum likelihood estimation via standard Newton-Raphson requires computing and inverting an O(N^2) Hessian matrix, which is computationally prohibitive for larger networks. To bypass this, the redeem package implements a highly scalable block-coordinate ascent algorithm that separates parameter updates:

  1. Step 1: Update covariate parameters \alpha using a standard Newton-Raphson update.

  2. Step 2: Update high-dimensional actor popularity baselines \beta using Minorization-Maximization (MM) steps, avoiding explicit matrix inversion.

  3. Step 3: Update baseline step function parameters \gamma via a closed-form step.

More information is provided in Fritz et at. (2026).

Semiparametric Baseline

When semiparametric = TRUE, the baseline rates for both the formation (0 \rightarrow 1) and dissolution (1 \rightarrow 0) processes are left completely unspecified. Both processes are estimated as separate Cox proportional hazards models using the survival package. In this path:

References

Fritz, C., Rastelli, R., Fop, M., & Caimo, A. (2026). Scalable Durational Event Models: Application to Physical and Digital Interactions. arXiv:2504.00049.

Examples

# Simulate some durational data
n <- 20
events <- matrix(c(
  1.2, 1, 5, 1,
  2.5, 1, 5, 0,
  3.1, 2, 8, 1,
  4.4, 2, 8, 0
), ncol = 4, byrow = TRUE)
colnames(events) <- c("time", "from", "to", "type")

# Estimate a simple DEM
fit <- dem(
  events = events,
  n_nodes = n,
  formula_0_1 = ~1,
  formula_1_0 = ~1,
  control = control.redeem(estimate = "Blockwise")
)
summary(fit)

Simulate events based on specified formulas and coefficients

Description

Simulate events based on specified formulas and coefficients

Usage

dem.simulate(
  events = matrix(0, 0, 4),
  formula_0_1 = NULL,
  formula_1_0 = NULL,
  coef_0_1 = numeric(0),
  coef_1_0 = numeric(0),
  coef_degree_0_1 = 0,
  coef_degree_1_0 = 0,
  n_events = 0,
  time = 0,
  max_events = 4e+05,
  n_nodes,
  verbose = FALSE,
  baseline_0_1 = NULL,
  baseline_1_0 = NULL,
  simultaneous_interactions = TRUE,
  seed = 123,
  directed = FALSE
)

Arguments

events

A matrix representing the initial events with columns time, from, to, and type (1 for start, 0 for end, 3 for exogenous changes). Defaults to an empty 4-column matrix.

formula_0_1

A one-sided formula specifying the sufficient statistics for the formation process (0 \rightarrow 1). The right-hand side must be composed of terms from redeem_terms. For example: ~ inertia() + degree. An intercept (~ 1) is the minimal specification. Defaults to NULL.

formula_1_0

A one-sided formula specifying the sufficient statistics for the dissolution process (1 \rightarrow 0). The right-hand side must be composed of terms from redeem_terms. An intercept (~ 1) is the minimal specification. Defaults to NULL.

coef_0_1

Numeric vector; coefficients for the formation process (0 \rightarrow 1). Defaults to an empty numeric vector.

coef_1_0

Numeric vector; coefficients for the dissolution process (1 \rightarrow 0). Defaults to an empty numeric vector.

coef_degree_0_1

Numeric; degree coefficient for the formation process (0 \rightarrow 1). Defaults to 0.

coef_degree_1_0

Numeric; degree coefficient for the dissolution process (1 \rightarrow 0). Defaults to 0.

n_events

Integer; number of events to simulate. Defaults to 0.

time

Numeric; simulation time limit. Defaults to 0.

max_events

Integer; maximum number of total events. Defaults to 400000.

n_nodes

Integer; the total number of actors in the network.

verbose

Logical; if TRUE, prints progress information. Defaults to FALSE.

baseline_0_1

Numeric vector; baseline for the 0 to 1 transition. If the formula for this process contains an Intercept or a degree term, then baseline_0_1 should be a numeric vector with length equal to the number of changepoints, representing the shifts in the baseline for each interval after the first. If the formula contains neither, then baseline_0_1 must have length equal to the number of changepoints + 1. Defaults to NULL.

baseline_1_0

Numeric vector; baseline for the 1 to 0 transition. Similar to baseline_0_1, its length depends on whether the 1 to 0 formula contains an intercept or degree term. Defaults to NULL.

simultaneous_interactions

Logical; whether to allow simultaneous interactions (i.e. multiple active events for the same actor or dyad at the same time). Defaults to TRUE.

seed

Integer; random seed for simulation. Defaults to 123.

directed

Logical; whether the interaction events are directed. Defaults to FALSE.

Value

A matrix of simulated events.

Note

Multi-stream event models are currently not supported in simulation.

Examples

# Simulate events from a DEM model structure
n <- 10
f_0_1 <- ~ 1 + inertia(transformation = "identity")
f_1_0 <- ~ 1

# Simulating events
evs <- dem.simulate(
  formula_0_1 = f_0_1,
  formula_1_0 = f_1_0,
  n_nodes = n,
  time = 2.0,
  coef_0_1 = c(1.0, 0.5),
  coef_1_0 = c(-0.5),
  seed = 42,
  max_events = 100
)
head(evs)

The dem Object

Description

An object of class dem returned by the dem function, representing a fitted Durational Event Model.

Value

A dem object is a list containing the following components:

Methods (S3)

The following S3 methods are implemented for dem objects:


MM Algorithm for Durational Event Models with Time-Varying Effects

Description

Implementation of the scalable block-coordinate ascent algorithm for DEMs with time-varying baseline intensities. This function performs iterative Minorization-Maximization (MM) updates for degree and temporal effects while using Newton-Raphson for core covariates.

Usage

estimate_mmt(
  data,
  indicators,
  it_max,
  n_nodes,
  tol = 1e-10,
  accelerated = TRUE,
  time_changepoints = NULL,
  labels_changepoints = NULL,
  subsample = 0.2,
  verbose = FALSE,
  est_degree = NULL,
  est_core = NULL,
  est_time = NULL,
  estimate_degree = TRUE,
  directed = FALSE,
  return_data = TRUE,
  save_hist = TRUE,
  use_glm = FALSE,
  inf_unidentifiable = TRUE
)

Arguments

data

Preprocessed data.table.

indicators

Numeric vector of covariate indices.

it_max

Maximum number of iterations.

n_nodes

Number of nodes.

tol

Convergence tolerance.

accelerated

Logical; use SQUAREM acceleration for degree effects.

time_changepoints

Numeric vector of time changepoints.

labels_changepoints

Character vector of labels for time slices.

subsample

Subsampling rate for GLM backup estimation.

verbose

Logical; print progress.

est_degree

Initial degree coefficients.

est_core

Initial core coefficients.

est_time

Initial time effects.

estimate_degree

Logical; estimate degree effects.

directed

Logical; whether the network is directed.

return_data

Logical; whether to return the preprocessed data in the result.

save_hist

Logical; whether to save the iteration history of coefficients.

use_glm

Logical; whether to use GLM-based core updates as fallback or control.

inf_unidentifiable

Logical; if TRUE, unidentifiable parameters are set to -Inf.

Details

The algorithm decomposes the log-likelihood and updates blocks of parameters sequentially. Specifically:

  1. Core effects (\beta) are updated via Newton-Raphson.

  2. Degree effects (\alpha) are updated using an MM step that avoids explicit Hessian inversion for high-dimensional actor sets.

  3. Temporal effects (\gamma) are updated via a similar MM step across defined time changepoints.


Core Estimation Logic for REM and DEM Transitions

Description

This internal helper function encapsulates the estimation routines for a single relational or durational event transition. It is used by both 'rem()' and 'dem()'.

Usage

estimate_transition(
  data,
  formula_original,
  formula_new,
  indicators,
  n_nodes,
  estimate_method,
  it_max,
  tol,
  accelerated,
  subsample,
  verbose,
  estimate_degree,
  directed,
  semiparametric = FALSE,
  labels_changepoints = NULL,
  time_changepoints = NULL,
  coef_init = NULL,
  model_type = "dem",
  process = "0-1",
  return_data = TRUE,
  save_hist = TRUE,
  use_glm = FALSE,
  legacy = FALSE,
  inf_unidentifiable = TRUE,
  events = NULL
)

Preprocess Formulas for Model Terms with Event and Node Information

Description

This function processes two model formulas, each of which can specify transformation and data arguments, and combines the preprocessed results with additional event and node information. It identifies unique coefficient names across both formulas, determining which terms to include based on uniqueness, and returns structured lists for data, transformations, and term names.

Usage

formula_preprocess(
  formula_0_1 = NULL,
  model_type = "dem",
  formula_1_0 = NULL,
  events = matrix(c(0, 0, 0), nrow = 1),
  n_nodes,
  exo_breaks = NULL,
  directed = FALSE,
  simulation = FALSE
)

Arguments

formula_0_1

Optional; an R formula for the '0 -> 1' terms.

model_type

Either "dem" or "rem".

formula_1_0

Optional; an R formula for the '1 -> 0' terms.

events

A data frame or list representing the events.

n_nodes

An integer specifying the number of nodes.

exo_breaks

Optional; a vector or list specifying external breaks.

directed

Logical; if TRUE, the model is directed (defaults to FALSE).

simulation

Logical; if TRUE, the formula is being preprocessed for simulation (defaults to FALSE).

Details

The function first calls 'formula_preprocess_single' on 'formula_1_0' and 'formula_0_1' separately to obtain individual term processing details. It then identifies unique terms across both formulas and combines the term data, transformations, and coefficient names into a single output list, structured for use in further modeling or evaluation.

Value

A list containing the following components:

events

The events input, retained for use in model estimation or evaluation.

n_nodes

The number of nodes specified in the input.

data_list

A combined list of matrices for each term’s data, from both formulas, where each matrix corresponds to the data for a specific term.

transformation_list

A combined character vector of transformation types for each term, with '"identity"' for terms without specified transformations.

coef_names

A character vector of coefficient names for each term, combining terms across both formulas and ensuring uniqueness.

term_names

A character vector of term names, ordered to match 'data_list'.

preprocess_1_0

The output list from 'formula_preprocess_single' applied to 'formula_1_0'.

preprocess_0_1

The output list from 'formula_preprocess_single' applied to 'formula_0_1'.

included_1_0

A logical vector indicating whether each term in 'coef_names' comes from 'formula_1_0'.

included_0_1

A logical vector indicating whether each term in 'coef_names' comes from 'formula_0_1'.

A list containing the preprocessed information.

See Also

formula_preprocess_single


Preprocess a single formula for model terms

Description

This function takes an R formula and extracts the necessary information to build the model's design matrix. It identifies special terms, transformations, and associated data.

Usage

formula_preprocess_single(
  formula,
  n_nodes,
  model_type = "dem",
  process = "0-1",
  directed = FALSE,
  simulation = FALSE
)

Arguments

formula

An R formula object.

n_nodes

Number of nodes.

model_type

Either "dem" or "rem".

process

Either "0-1" (incidence) or "1-0" (duration).

directed

Logical; if TRUE, the model is directed (defaults to FALSE).

simulation

Logical; if TRUE, the formula is being preprocessed for simulation (defaults to FALSE).


Out-of-sample Log-Likelihood (Proper Scoring Rule)

Description

This function computes the out-of-sample log-likelihood (a strictly proper scoring rule) for each test event under a fitted REM or DEM.

Usage

get_oos_likelihood(
  object,
  verbose = FALSE,
  edgelist_test,
  edgelist_train = NULL,
  baseline_method = c("last", "trend", "mean", "beginning"),
  loess_span = 0.75
)

Arguments

object

A redeem object (either rem or dem).

verbose

Logical; if 'TRUE', prints verbose output. Defaults to FALSE.

edgelist_test

A matrix or data frame of test events (timing, from, to, type).

edgelist_train

A matrix or data frame of train events (timing, from, to, type). Defaults to 'NULL', in which case it retrieves the training events from the 'object' or the preprocessed data.

baseline_method

Character; how to compute the fixed log-baseline intensity used for out-of-sample scoring. One of: '"last"' (uses the last estimated baseline value), '"trend"' (extrapolates a LOESS trend), '"mean"', or '"beginning"'. Defaults to '"last"'.

loess_span

Numeric; LOESS span (0, 1] passed to predict_baseline_trend when baseline_method = "trend". Defaults to 0.75.

Value

A numeric vector of log-likelihoods for each test event.

See Also

rem_object and dem_object for details on prediction methods.


Get ranking for test events (Out-of-Sample Goodness-of-Fit)

Description

Evaluates the out-of-sample predictive performance of a fitted model on a test event sequence using a ranking-based Goodness-of-Fit (GoF) procedure.

Usage

get_ranking(
  object,
  verbose = FALSE,
  k_max = 1000,
  edgelist_test,
  edgelist_train = NULL,
  ties.method = c("average", "first", "last", "random", "max", "min"),
  return_probabilities = FALSE,
  baseline_method = c("trend", "mean", "last", "beginning"),
  loess_span = 0.75
)

Arguments

object

A redeem object (either rem or dem).

verbose

Logical; if 'TRUE', prints verbose output. Defaults to FALSE.

k_max

Maximum number of ranked pairs to return. Defaults to 1000.

edgelist_test

A matrix of test events (timing, from, to, type).

edgelist_train

A matrix of train events (timing, from, to, type). Defaults to NULL.

ties.method

Character; the method to handle ties when ranking event intensities, passed directly to rank. Defaults to "average". One of:

"average"

Assigns the average of the ranks of all tied elements to each.

"first"

Breaks ties by the order they appear in the data structure.

"last"

Breaks ties by the reverse order of their appearance.

"random"

Breaks ties randomly, ensuring no systematic bias.

"max"

Assigns the maximum of the ranks of the tied elements to all.

"min"

Assigns the minimum of the ranks of the tied elements to all.

return_probabilities

Logical; if TRUE, returns the predicted probabilities/scores instead of recall curves. Defaults to FALSE.

baseline_method

Character; how to compute the fixed log-baseline intensity used for out-of-sample scoring. Defaults to "trend". One of:

"trend"

Fit a LOESS trend to the estimated piecewise-constant log-baseline (est_time) over training time and extrapolate it to the start of the test period. This mirrors the trend decomposition used in the application plot script and typically yields a better forecast than a fixed mean.

"mean"

Use the simple mean of est_time.

"last"

Use the last estimated log-baseline value (i.e.\ the value from the most recent training interval).

"beginning"

Set the baseline to 0.

loess_span

Numeric; LOESS span (0, 1] passed to predict_baseline_trend when baseline_method = "trend". Defaults to 0.75.

Details

For each event observed in the test period (edgelist_test), the function:

  1. Determines the set of all potential candidate dyads (the risk set) at that event's timestamp.

  2. Computes the predicted event intensities (or probabilities) for all candidate dyads using the fitted model's parameters and the network history up to that moment.

  3. Ranks all candidate dyads in descending order of their predicted intensities.

  4. Determines the rank of the actually observed dyad.

A well-fitting model will consistently assign higher intensities to the dyads that actually interact, ranking them near the top.

The function summarizes the rankings across all test events to compute:

Value

A ranking_redeem data frame with columns:

Cutpoint

Integer value from 0 to k_max.

Recall

The proportion of test events where the true dyad is ranked at or within the cutpoint.

Precision

The precision value at the cutpoint.

Additionally, the returned object has the following attributes:

"mrr"

Mean Reciprocal Rank (MRR) of the true dyads.

"mean_rank"

Mean rank of the true dyads (excluding ranks > k_max).

"median_rank"

Median rank of the true dyads (excluding ranks > k_max).

"hits_summary"

A data frame summarizing Recall, Precision, and F1 values at K = 1, 5, 10, and 50.


Get residuals for model diagnostics (Cox-Snell Residuals)

Description

Computes Cox-Snell residuals for a fitted model to diagnose goodness-of-fit and calibration.

Usage

get_residuals(object, get_0_1 = TRUE, get_1_0 = TRUE, raw = FALSE)

Arguments

object

A redeem object (either rem or dem).

get_0_1

Logical; if 'TRUE', computes residuals for the formation (0 -> 1) process. Defaults to TRUE.

get_1_0

Logical; if 'TRUE', computes residuals for the dissolution (1 -> 0) process. Defaults to TRUE.

raw

Logical; if 'TRUE', returns the raw Cox-Snell residuals. Defaults to FALSE.

Details

Cox-Snell residuals are a standard diagnostic tool for continuous-time survival models and counting processes. Under the true model specification, the integrated cumulative intensity computed up to the exact time of an observed event is distributed as a standard exponential random variable, i.e., \Lambda_{ij}(t_k) \sim Exp(1).

Consequently, if the model is correctly specified:

The function can compute residuals for both the formation/incidence (0 \rightarrow 1) process and the dissolution/duration (1 \rightarrow 0) process.

Value

If 'raw = TRUE', a list containing the raw residuals for the selected process(es). If 'raw = FALSE', a list of data frames containing the Kaplan-Meier coordinates ('time', 'surv') and the corresponding 'theoretical' standard exponential survival values.

References

Cox, D. R., & Snell, E. J. (1968). A general definition of residuals. Journal of the Royal Statistical Society: Series B (Methodological), 30(2), 248-265.


Match user-provided coefficients to internal model names

Description

Match user-provided coefficients to internal model names

Usage

match_coefficients(user_coefs, internal_names, internal_keys = NULL)

Arguments

user_coefs

Named or unnamed vector of coefficients provided by the user.

internal_names

Vector of internal coefficient names (labels).

internal_keys

Vector of internal coefficient keys (e.g., intercept, dyadic_cov_identity).

Value

A numeric vector of the same length as internal_names.


Plot the Estimated Baseline Intensity

Description

Draws a step-function plot of the estimated piecewise-constant baseline intensity against time. The function dispatches to class-specific methods for dem, rem, and redeem_result objects.

Usage

plot_baseline(x, ...)

Arguments

x

A dem, rem, or redeem_result object produced by dem or rem.

...

Additional arguments passed to graphics::plot.

Value

The original object x is returned invisibly. Called primarily for its side effect of producing a plot.


Predict the baseline intensity trend at one or more future time points

Description

Decomposes the estimated piecewise-constant log-baseline (est_time) into a smooth trend component (via LOESS) and a seasonal/residual component, following the same approach used in the application plot script. The fitted trend is then extrapolated to target_times using predict.loess() so that the baseline used for out-of-sample scoring reflects the long-run level of activity rather than any arbitrary fixed value.

Usage

predict_baseline_trend(model, target_times, loess_span = 0.75)

Arguments

model

A redeem_result object with non-null est_time and time_changepoints fields.

target_times

Numeric vector: the times at which to predict the trend (typically the unique timestamps of the test events).

loess_span

Numeric; the span argument passed to stats::loess. Larger values give a smoother (more conservative) trend extrapolation. Defaults to 0.75.

Details

The decomposition mirrors the plot code in the application:

  1. Build a data frame of (time, est_time) using the changepoints stored in model$time_changepoints. The first interval [0, changepoint_1) is given time = 0; each subsequent interval gets the corresponding changepoint value.

  2. Fit LOESS on the log-scale est_time values.

  3. Predict at each target_times; predictions are clamped to the range of the observed est_time to avoid wild extrapolation.

Value

A numeric vector of predicted log-baseline (trend component) values, one per element of target_times. Falls back to mean(est_time) for each time point if there are fewer than 3 observations or if the LOESS fit fails.


Helper to predict single transition process

Description

Helper to predict single transition process

Usage

predict_transition_helper(model, data, type)

Internal helper to prepare residual plot data

Description

Internal helper to prepare residual plot data

Usage

prepare_residual_plot_data(raw_list)

Arguments

raw_list

List of raw residuals


Preprocess Model Terms across Multiple Event Streams

Description

Preprocess Model Terms across Multiple Event Streams

Usage

preprocess_multi_stream(
  preprocessed,
  n_nodes,
  verbose,
  directed,
  simultaneous_interactions,
  build_time = NULL,
  max_time = -1,
  model_type = "dem",
  impute_zero = TRUE,
  omit_na = TRUE
)

Arguments

preprocessed

Standard output from 'formula_preprocess'.

n_nodes

Number of nodes.

verbose

Logical; if TRUE, print progress.

directed

Logical; if TRUE, the model is directed.

simultaneous_interactions

Logical; if TRUE, multiple interactions are allowed.

build_time

Numeric; time at which to start building the dataset.

max_time

Numeric; if positive, events after this time are excluded. Defaults to -1.0 (no upper limit).

model_type

Either "dem" or "rem".

impute_zero

Logical; if TRUE, replace NAs in covariates with 0.

omit_na

Logical; if TRUE, call na.omit() on the final table.

Value

A data.table containing the unified preprocessed data.


Process event actor columns and automatically identify or validate n_nodes

Description

Process event actor columns and automatically identify or validate n_nodes

Usage

process_event_actors(events, n_nodes = NULL, directed = TRUE)

Arguments

events

A matrix or data frame of events with columns from and to (or columns 2 and 3).

n_nodes

Integer; the total number of actors in the network, or NULL.

directed

Logical; whether the interaction events are directed. Defaults to TRUE.

Value

A list containing events (potentially modified) and n_nodes.


redeem Model Terms

Description

The help pages of rem and dem describe the model formulation and estimation details. This page documents all statistics available to be used in the model formulas, characterizing the intensities of event formation and dissolution.

In the redeem framework, models like DEM (fitted via dem) and REM (fitted via rem) are specified using R formulas. The right-hand side of these formulas defines the structural statistics and covariates, where each term must be specified separately as an explicit function call (e.g., ~ inertia() + reciprocity(window = 10)).

All terms support an optional transformation argument f. The available transformations are:

Throughout, N_{i,j}(t) denotes the cumulative number of events from i to j up to (but not including) time t; N_{i,j}^w(t) is the windowed analogue on (t-w,\,t); d_i^{\mathrm{out}}(t) = |\{l: N_{i,l}(t)>0\}| is the historical out-degree of i; and c_i^{\mathrm{out}}(t) = \sum_l N_{i,l}(t) is the total event count sent by i. The superscript \mathrm{act} indicates that the quantity is computed on the currently active DEM network.

The implemented terms are grouped into five categories:

  1. Baseline and Nuisance Terms: Intercept, time-varying baseline, and degree fixed effects.

  2. Endogenous Dyadic Terms: Inertia, reciprocity, interaction duration, and participation shifts.

  3. Triadic Closure and Shared Partners: Common partners and triangle statistics.

  4. Degree and Centrality Statistics: Actor degree and event count statistics.

  5. Exogenous Covariates: Dyadic and monadic covariate terms.

Arguments

K

Numeric; the evaluation point or scaling/saturation factor for the sufficient statistic (default is 1).

transformation

Character; specifies the transformation to apply to the statistic. One of:

  • "identity" (default): f(x) = x

  • "log": f(x) = \log(x+1)

  • "recip": f(x) = 1/(x+1)

  • "bin": f(x) = I(x>0)

  • "sig": sigmoid-like saturation, f(x) = x/(x+K)

event_stream

Optional matrix or data frame; an alternative event stream to use for calculating the statistic. If NULL (default), the modeled stream is used.

window

Numeric; time window for calculating the statistic (default Inf, i.e., use full history).

type

Character; the specific variation of the statistic or triangle type (e.g., "OSP", "ISP", "OTP", "ITP", "out_sender", "sum").

mode

Character; the participation shift mode (e.g., "ABBA", "ABBY").

data

For dyadic_cov, a numeric matrix of dimensions N \times N, a scalar applied globally, or a named list of matrices for time-varying covariates. For monadic_cov, a numeric vector of length N or a named list of vectors for time-varying covariates.

fun

A function taking two arguments fun(v_i, v_j) to generate dyadic values.

change_points

Optional numeric vector; time points for time-varying covariates if data is a list.

changepoints

Numeric vector; time points where the baseline intensity is allowed to change.

labels

Character vector; optional labels for the resulting time intervals.

history

Character; "general" for cumulative history or "current" for currently active events.

count

Logical; if TRUE, uses count-based (weighted) versions of degree statistics (default FALSE).

...

Arguments passed to the underlying initialization function.

Value

A redeem_term object (a list containing structural information about the statistic) to be used inside model formulas.

Multi-Stream Event Covariates

Most endogenous terms support covariates calculated from multiple event streams. By providing an event_stream argument to a term (e.g., inertia(event_stream = other_events)), users can model one event process while accounting for the history of another. The package automatically handles the splintering and union of these timelines.

Baseline and Nuisance Terms

Endogenous Dyadic Terms

Triadic Closure and Shared Partners

Degree and Centrality Statistics

Exogenous Covariates


Relational Event Model (REM) Estimation

Description

Estimates a Relational Event Model (REM) for network data, focusing on the incidence of discrete events between pairs of actors. See dem for the full Durational Event Model, which extends the REM to handle interactions with non-negligible duration.

Usage

rem(
  events,
  training_start = 0,
  exogenous_end = NULL,
  formula = NULL,
  n_nodes = NULL,
  directed = FALSE,
  coef = NULL,
  semiparametric = FALSE,
  control = control.redeem()
)

Arguments

events

A matrix of events with columns time, from, to, and optionally type (1 for start, 3 for exogenous changes).

training_start

Numeric; the time point at which to start the estimation. Defaults to 0.

exogenous_end

Numeric; optional end time for exogenous baseline changes. Defaults to NULL.

formula

A one-sided formula specifying the sufficient statistics to include in the intensity function. The right-hand side must be composed of terms from redeem_terms. For example: ~ inertia() + reciprocity() + degree. An intercept (~ 1) is the minimal specification. Defaults to NULL.

n_nodes

Integer; the total number of actors in the network. If NULL (default), it is automatically identified based on the actors in the events set.

directed

Logical; whether the interaction events are directed. Defaults to FALSE.

coef

Numeric vector; initial coefficients for the model. If provided, this must be a concatenated vector of:

  1. Core coefficients: values for sufficient statistics in the formula.

  2. Degree coefficients (if degree is in the formula): a vector of length n_nodes (undirected) or 2 * n_nodes (directed, sender effects first then receiver effects).

  3. Baseline coefficients (if temporal changepoints are present): a vector of length equal to the number of baseline intervals (equal to number of changepoints if an intercept/degree is present, or changepoints + 1 if neither is present).

Defaults to NULL, in which case default starting values are automatically computed.

semiparametric

Logical; whether to use a semiparametric baseline. Defaults to FALSE. See the 'Semiparametric Baseline' section for details.

control

A list of control parameters from control.redeem. Defaults to control.redeem().

Details

The REM can be viewed as the incidence sub-model of the full dem, corresponding to the formation process \lambda^{0\rightarrow 1}. It uses a counting process approach to estimate the influence of various covariates on the timing and occurrence of events, assuming that events are instantaneous points in time.

Value

An object of class rem_object containing model estimates and log-likelihoods. See rem_object for details on the components of the returned object and S3 methods.

Model Formulation

The Relational Event Model characterizes the instantaneous rate at which actor pair (i,j) initiates an event. Under the log-linear specification, the event intensity at time t is:

\lambda_{i,j}(t \mid \mathscr{H}_t,\, \theta) = \exp\!\bigl(s_{i,j}(\mathscr{H}_t)^\top \alpha + \beta_i + \beta_j + f(t, \gamma)\bigr)

where:

Semiparametric Baseline

When semiparametric = TRUE, the temporal baseline rate of event occurrence is left completely unspecified, and the model parameters are estimated via the Cox partial likelihood using the survival package. In this path:

References

Fritz, C., Rastelli, R., Fop, M., & Caimo, A. (2026). Scalable Durational Event Models: Application to Physical and Digital Interactions. arXiv:2504.00049.

Butts, C. T. (2008). A Relational Event Framework for Social Action. Sociological Methodology, 38(1), 155-200.

Examples

# Simulate some relational event data
n <- 20
events <- matrix(c(
  1.2, 1, 5,
  3.1, 2, 8,
  4.5, 1, 3
), ncol = 3, byrow = TRUE)
colnames(events) <- c("time", "from", "to")

# Estimate a simple REM
fit <- rem(
  events = events,
  n_nodes = n,
  formula = ~1,
  control = control.redeem(it_max = 50)
)
summary(fit)

Simulate a Relational Event Model (REM)

Description

Simulate a Relational Event Model (REM)

Usage

rem.simulate(
  events = matrix(0, 0, 4),
  formula,
  coef = NULL,
  coef_degree = 0,
  n_events = 0,
  time = 0,
  max_events = 4e+05,
  n_nodes,
  verbose = FALSE,
  baseline = NULL,
  seed = 123,
  block = 1,
  directed = FALSE
)

Arguments

events

A matrix representing the initial events with columns time, from, to, and optionally type (1 for start, 3 for exogenous changes). Defaults to an empty 4-column matrix.

formula

A one-sided formula specifying the sufficient statistics to include in the intensity function. The right-hand side must be composed of terms from redeem_terms. For example: ~ inertia() + reciprocity() + degree. An intercept (~ 1) is the minimal specification.

coef

Numeric vector; coefficients for the model. Defaults to NULL.

coef_degree

Numeric; degree coefficient. Defaults to 0.

n_events

Integer; number of events to simulate. Defaults to 0.

time

Numeric; simulation time. Defaults to 0.

max_events

Integer; maximum number of events. Defaults to 400000.

n_nodes

Integer; the total number of actors in the network.

verbose

Logical; if TRUE, prints progress information. Defaults to FALSE.

baseline

Numeric vector; baseline intensity values for intervals defined by changepoints. Defaults to NULL.

seed

Integer; random seed. Defaults to 123.

block

An integer vector of length n_nodes indicating the block/group assignment for each node, or a single value applied to all nodes. Defaults to 1. If multiple blocks are assigned, within-block interactions are suppressed (i.e., their dyadic intensities are set to 0), meaning only events occurring between actors in different blocks are simulated.

directed

Logical; whether the interaction events are directed. Defaults to FALSE.

Details

The block parameter allows the user to specify a partition of the nodes into different groups (blocks). When the vector contains more than one unique block identifier:

Value

A matrix of simulated events.

Note

Multi-stream event models are currently not supported in simulation.

Examples

# Simulate events from a REM model structure
n <- 10
f1 <- ~ 1 + inertia(transformation = "identity")

# Simulating events
evs <- rem.simulate(
  formula = f1,
  n_nodes = n,
  time = 1.0,
  coef = c(1.0, 0.5),
  seed = 42,
  max_events = 100
)
head(evs)

The rem Object

Description

An object of class rem returned by the rem function, representing a fitted Relational Event Model.

Value

A rem object is a list containing the following components:

Methods (S3)

The following S3 methods are implemented for rem objects:


Reconstruct estimation data from a model object

Description

Reconstruct estimation data from a model object

Usage

reproduce_model_data(models, verbose = FALSE)

Arguments

models

A redeem model object (rem or dem).

verbose

Logical.


Convert right-hand side of a formula to a list of term information

Description

Convert right-hand side of a formula to a list of term information

Usage

rhs_terms_as_list(
  formula,
  n_nodes,
  env = NULL,
  evaluate_calls = FALSE,
  model_type = "dem",
  process = "0-1",
  directed = FALSE
)

Arguments

formula

The formula to parse.

n_nodes

Number of nodes.

env

The environment in which to evaluate terms.

evaluate_calls

Logical; if 'TRUE', evaluates the full calls.

model_type

Either "dem" or "rem".

process

Either "0-1" or "1-0".

directed

Logical; if TRUE, the model is directed.

Value

A list of term information, including labels, base names, and data.


Standardize Estimation Output for Redeem Models

Description

Standardize Estimation Output for Redeem Models

Usage

standardize_redeem_result(
  coefficients,
  coef_hist,
  covariance,
  llh_hist,
  data,
  prediction,
  method = "dem.nr",
  n_nodes = NULL,
  directed = FALSE,
  time_changepoints = NULL,
  labels_changepoints = NULL,
  full_baseline = FALSE,
  return_data = TRUE,
  save_hist = TRUE
)

Arguments

coefficients

Joint coefficient vector.

coef_hist

Matrix of coefficient history.

covariance

Covariance matrix for core effects.

llh_hist

Vector of log-likelihood history.

data

Input data.

prediction

Predicted values.

method

Estimation method name (for class).

n_nodes

Number of nodes.

directed

Logical; are the interaction events directed?

time_changepoints

Numeric vector of time changepoints.

labels_changepoints

Character vector of labels for the changepoints.

return_data

Logical; should the estimation dataset be returned?

save_hist

Logical; should the parameter history be returned?


Summary of a redeem_result Model Fit

Description

Computes a summary of a fitted redeem_result object, collecting the estimated fixed effects, log-likelihood, and (if present) degree and temporal baseline effects into a structured list suitable for printing.

Usage

## S3 method for class 'redeem_result'
summary(object, ...)

Arguments

object

A 'redeem_result' object.

...

Additional arguments (currently unused).

Value

An object of class summary.redeem_result, which is a list containing:


Update core coefficients using R (for correctness assessment)

Description

Update core coefficients using R (for correctness assessment)

Usage

update_core_r(
  data,
  covarites,
  prediction,
  est_core,
  identifiable,
  offset_fixed = NULL
)

Arguments

data

Preprocessed data.table.

prediction

Current predicted intensities.

est_core

Initial core coefficients.

identifiable

Logical vector indicating identifiable coefficients.

offset_fixed

Fixed offset (degree + baseline).