Model Estimation

REM.jl estimates relational event models by stratified conditional logistic regression — the Cox partial likelihood with one stratum per event, the case dyad against the other dyads at risk. When the risk set is small the strata hold every dyad and the likelihood is exact; when it is not, case-control sampling makes estimation tractable.

Overview

The estimation process follows three steps:

  1. Risk set: For each observed event, the dyads that could have occurred instead — all of them, or a sample of controls
  2. Statistic Computation: Calculate statistics for cases and controls, off the network state as it stands before each event
  3. Maximum Partial Likelihood: Fit the stratified conditional logit on the shared NetworkCore.newton_fit

The running example is the bundled WTC police-calls stream (see Getting Started):

using NetworkCore, REM
using Random

wtc = load_dataset(:wtc_police_calls)
events = [Event(wtc.events[k, 2], wtc.events[k, 3], Float64(wtc.events[k, 1]))
          for k in 1:size(wtc.events, 1)]
seq = EventSequence(events; actors=ActorSet(1:37))
icr = NodeAttribute(:icr, Dict(i => Float64(wtc.is_icr[i]) for i in 1:37))
stats = [Repetition(), Reciprocity(), SenderActivity(), ReceiverPopularity(),
         TransitiveClosure(), SenderAttribute(icr), ReceiverAttribute(icr)]
full = 37 * 36 - 1                  # n_controls that enumerates all 1332 dyads

Why Case-Control Sampling?

For a network with $n$ actors, there are $n(n-1)$ possible directed dyads at each time point. Computing statistics for all dyads at all time points is often computationally infeasible.

Case-control sampling solves this by:

  • Treating observed events as "cases"
  • Sampling a subset of non-events as "controls"
  • Using stratified estimation to obtain consistent parameter estimates

This approach is statistically valid and dramatically reduces computation time. It is also unnecessary when $n(n-1)$ is small: 37 actors are 1332 dyads, and enumerating them all costs well under a second here.

Configuring the Sampler

sampler = CaseControlSampler(
    n_controls = 100,         # Controls per case
    exclude_self_loops = true, # Exclude s→s from risk set
)

The control draw comes from the rng keyword of generate_observations / fit_rem (rng=Xoshiro(42) reproduces it); the sampler itself holds no random state.

Parameters

ParameterDescriptionDefault
n_controlsNumber of controls sampled per caseRequired
exclude_self_loopsWhether to exclude self-eventstrue

Choosing Number of Controls

Controls are sampled without replacement; if fewer distinct dyads exist than n_controls, the sampler enumerates the full risk set (all non-case dyads) and warns once. Passing n_controls = n(n−1) − 1 therefore enumerates it on purpose, silently.

What sampling costs is not only variance. For a correctly specified model the sampled estimates centre on the truth for any n_controls (nested case-control sampling is consistent, and the standard errors of the sampled fit are calibrated — see Standard errors); for a model that is not the true one — every real model — the sampled fit converges to a pseudo-true value that depends on the control draw, and both the draw-to-draw spread and the distance from the full-risk-set fit shrink as n_controls grows. One seed shows one draw; control_draw_cov refits over independent draws and reports their mean and spread. On the WTC calls, ten draws each:

exact = fit_rem(seq, stats; n_controls=full)
println(rpad("n_controls", 12), rpad("repetition (mean ± sd over 10 draws)", 40), "per-draw SE")
for n_controls in (20, 100, 400)
    d = control_draw_cov(seq, stats; n_controls=n_controls, n_draws=10, rng=Xoshiro(1))
    one = fit_rem(seq, stats; n_controls=n_controls, rng=Xoshiro(1))
    println(rpad(n_controls, 12),
            rpad(string(round(d.mean[1], digits=2), " ± ", round(d.sd[1], digits=2)), 40),
            round(stderror(one)[1], digits=2))
end
println(rpad("1331 (full)", 12), rpad(round(coef(exact)[1], digits=2), 40), round(stderror(exact)[1], digits=2))
n_controlsrepetition (mean ± sd over 10 draws)per-draw SEreciprocity (mean ± sd)exact
20−0.50 ± 0.070.080.59 ± 0.08false
100−0.41 ± 0.040.050.43 ± 0.04false
400−0.33 ± 0.020.040.32 ± 0.02false
1331 (full)−0.340.030.33true

Two things are going on, and they are different. The mean over draws moves toward the full-risk-set value as n_controls grows (−0.50 → −0.41 → −0.33 → −0.34): that is the pseudo-true shift of a misspecified model, a bias that only more controls cure. And individual draws scatter around that mean — by about a standard error at 20 controls (Xoshiro(42) alone gives −0.72, nearly two draw-sds out), by half of one at 400. Neither is a deficiency of the standard errors, which are calibrated for the sampled likelihood they belong to; they are the practical reason to use the full risk set when you can, and to check a sampled fit against a larger n_controls (or control_draw_cov) when you cannot.

So: enumerate the full risk set whenever you can afford it (tens of thousands of dyads is fine). When you cannot, use as many controls as you can afford, check the estimates against a larger n_controls, and read the sampling probability the fit reports (NetworkCore.approximations(fit) records that the likelihood was sampled). The rough guidance:

n_controlsUse Case
full risk setWhenever n(n−1) is affordable — the exact ordinal likelihood
100-200Exploratory analysis on large actor sets
400+Final results on large actor sets; verify stability across n_controls
Ordinal likelihood

REM.jl fits the ordinal REM: only event order enters the likelihood; the exact inter-event waiting times are not part of the hazard (unlike relevent::rem.dyad's interval likelihood, which Revel.jl fits: fit_revel(...; model=:timing)). The default standard errors are the inverse observed information on the risk set that was used — sampled or full; se=:sandwich is the misspecification-robust alternative (below).

Tied event times

The likelihood is a likelihood over the order of the events, and the statistics of an event are read off the network state as it stands before it. So two events sharing a timestamp are not a sorting nuisance: whichever is placed first enters the statistics of the one placed second (its Repetition, its Reciprocity, its degrees). Sorting a tie invents the very thing the model is about.

fit_rem therefore refuses tied data by default and the policy is explicit (NetworkCore.TIE_POLICIES, the same vocabulary as Revel.jl's fit_revel):

ties=what it does
:error (default)names the tie and throws
:orderedsequence order, no correction (the pre-0.2 behaviour)
:breslowBreslow correction: the state is frozen across the tie block, each tied event is a stratum, all share one denominator. The one to use with sampled controls: each stratum stays an ordinary nested-case-control stratum
:efronEfron correction: as Breslow, plus the 1 − (j−1)/d denominator weights on the tied cases — the better approximation when ties are heavy, and R's default — with the full risk set only (refused with sampled controls, below)
:batchrefused here: with the state frozen, a simultaneous batch in an ordinal likelihood is Breslow

The WTC clock is ordinal (one event per tick), so it has no ties and the policy never bites; on a coarse clock it does:

# Two events share the timestamp 3.0: refused by default, corrected on request
tied = [Event(1, 2, 1.0), Event(2, 1, 2.0), Event(1, 3, 3.0), Event(3, 2, 3.0),
        Event(2, 3, 4.0), Event(1, 2, 5.0), Event(3, 1, 6.0), Event(2, 1, 7.0)]
tied_seq = EventSequence(tied; actors=ActorSet(1:4))
tied_stats = [Repetition(), Reciprocity()]

try
    fit_rem(tied_seq, tied_stats; n_controls=11)                  # ties=:error — throws
catch err
    println(err.msg[1:60], "…")
end
# 4 actors: 12 dyads, so n_controls=11 is the full risk set
fit = fit_rem(tied_seq, tied_stats; n_controls=11, ties=:efron)   # corrected the way coxph would
NetworkCore.tie_method(fit)             # :efron
breslow = fit_rem(tied_seq, tied_stats; n_controls=5, ties=:breslow, rng=Xoshiro(1))  # sampled

Efron's correction and sampled controls do not mix. In an Efron tie block the other tied cases have to stay in every stratum of the block — with probability one — while the other dyads are sampled with probability m/(N−d). The tied cases are the dyads that just acted, with high linear predictors, so they dominate the sampled denominator, and the estimate is biased toward zero with confident standard errors: for a truth of 1.0 the sampled Efron estimate was 0.63 at 5 controls and 0.88 at the default 100, with 0 % coverage in a simulation study. No reweighting of that design is exact, so fit_rem refuses it:

try
    fit_rem(tied_seq, tied_stats; n_controls=5, ties=:efron)     # sampled: refused
catch err
    println(err.msg[1:80], "…")
end

Breslow's correction is the likelihood of the tied events drawn independently from one frozen risk set, so each of its strata is an ordinary nested-case-control stratum and sampling leaves it unbiased: in the test suite's simulation (truth 1.0, four tied events per tick, 25 actors) the sampled Breslow estimates are 0.995, 0.993 and 0.987 at 5, 10 and 100 controls, with 93–97 % Wald coverage, against 1.000 for Efron on the full risk set.

This is a Cox partial likelihood, so :breslow and :efron are the classical corrections in the classical sense — test/fixtures/rem_ties.toml pins them against survival::coxph(..., ties="breslow"/"efron") on tied data (agreement < 1e-11).

On tie-free data all four policies produce the identical design and the identical fit. NetworkCore.tie_method(fit) reports what actually happened (:none when the data had no ties), and NetworkCore.approximations(fit) carries the caveat when a correction was applied. A fit with ties in it is never is_exact.

Generating Observations

# The case-control design as a DataFrame: one row per case and per control
obs = generate_observations(seq, stats, sampler; rng=Xoshiro(42))
size(obs)                       # (48581, 15): 481 cases + 481 × 100 controls

The resulting DataFrame contains:

ColumnDescription
event_indexIndex of the focal event in the sequence
senderSender ID
receiverReceiver ID
is_eventtrue for cases, false for controls
stratumStratum ID (groups each case with its controls)
risk_set_sizeNumber of dyads in the stratum's risk set (case included)
sampling_probProbability each non-case dyad entered the sample as a control
tie_weightDenominator weight of the row (1.0 except under ties=:efron)
<stat_name>One column per statistic

Options

obs = generate_observations(seq, stats, sampler;
    start_index = 1,           # First event to include
    end_index = length(seq),   # Last event to include
    decay = 0.0,               # Exponential decay rate (eventnet's halflife memory)
    window = nothing,          # Or a sliding window: events older than this stop
                               # counting (mutually exclusive with decay > 0)
    at_risk = nothing,         # Risk set: actor universe, RiskSet,
                               # per-event vector, or callback (see below)
    ties = :error,             # Tied-timestamp policy (above)
    directed = true,           # false: undirected events, unordered pairs at risk
    eventtypes = :error,       # :pool to fit a multi-type sequence as one process
    cases_of = nothing,        # or model the events of some types only
    rng = Random.default_rng() # Source of the control draw
)

Excluding Early Events

The first few events may have unreliable statistics (no history):

# Skip the first 20 events
obs = generate_observations(seq, stats, sampler; start_index=21, rng=Xoshiro(42))

The skipped events still build the network state. A tie block that straddles start_index is treated as a tie: under ties=:error it is refused, and under :breslow/:efron its earlier members do not enter the state before the block's strata are built (they are the block's other tied events), exactly as in a fit of the whole sequence.

The Actor Universe and Risk Sets

The REM likelihood is conditional on the risk set: each event competes against the other dyads that could have occurred instead. Getting the risk set wrong changes the estimand — eligible actors who never happen to send or receive an event (isolates, or actors only observed as receivers) still belong in the denominator, and dropping them biases activity, popularity and covariate effects. Two of the 37 WTC officers never call or are called; they are in the risk set because ActorSet(1:37) was declared.

Declare the actor universe on the sequence:

# A small demonstration sequence: actors 1-3 are observed; 7 and 42 are
# eligible isolates
toy_events = [Event(1, 2, 1.0), Event(2, 1, 2.0), Event(1, 3, 3.0),
              Event(3, 2, 4.0), Event(2, 3, 5.0), Event(1, 2, 6.0)]
toy = EventSequence(toy_events; actors=ActorSet([1, 2, 3, 7, 42]))
toy_stats = [Repetition(), Reciprocity()]

If actors is omitted, the universe falls back to the observed event endpoints ("participants only") and fit_rem warns, because that is a silent change of estimand.

Alternatively, hand the risk set to generate_observations/fit_rem directly. at_risk (alias riskset in fit_rem) accepts:

toy_sampler = CaseControlSampler(n_controls=10)

# 1. A static actor universe (ActorSet, Set{Int} or Vector{Int})
obs = generate_observations(toy, toy_stats, toy_sampler; at_risk=Set(1:10))

# 2. A static RiskSet — senders and receivers may differ (every case must
#    still belong to it: here 7 only ever sends and 42 only ever receives)
rs = RiskSet(0, [1, 2, 3, 7], [1, 2, 3, 42])
obs = generate_observations(toy, toy_stats, toy_sampler; at_risk=rs)

# 3. Per-event risk sets (time-varying membership): one entry per event —
#    here actor k+3 joins the universe at event k
joined = [Set(1:(k + 3)) for k in eachindex(toy)]
obs = generate_observations(toy, toy_stats, toy_sampler; at_risk=joined)

# 4. A callback (event_index, state) -> RiskSet, evaluated against the
#    current network state
obs = generate_observations(toy, toy_stats, toy_sampler;
    at_risk = (i, state) -> RiskSet(i, sort!(collect(state.actors)),
                                    sort!(collect(state.actors))))

# 5. A dyad-level risk set: exactly the listed (sender, receiver) pairs are
#    at risk — one-shot relations, impossible dyads, a structure with holes
allowed = [(1, 2), (2, 1), (1, 3), (3, 2), (2, 3), (7, 42)]
obs = generate_observations(toy, toy_stats, toy_sampler; at_risk=allowed)

toy_result = fit_rem(toy, toy_stats; n_controls=89, at_risk=Set(1:10))   # 10 actors: full risk set

Every case is validated against its own risk set before any fitting happens: if the event's sender or receiver is not in the risk set (the case and its controls would then come from different actor universes), or if the risk set admits no valid control, an ArgumentError is thrown.

Each stratum records its risk-set size and the probability with which each non-case dyad entered the sample as a control, both on the observation DataFrame (risk_set_size, sampling_prob) and on the fitted model (result.risk_set_sizes, result.sampling_probs, aligned with result.strata).

Fitting Models

Direct Fitting (Recommended)

The simplest approach combines sampling and fitting:

result = fit_rem(seq, stats; n_controls=full)              # exact: every dyad at risk
sampled = fit_rem(seq, stats; n_controls=100, rng=Xoshiro(42))   # 100 controls per event

Two-Stage Fitting

For more control over the process:

using DataFrames   # for nrow

# Stage 1: Generate observations
sampler = CaseControlSampler(n_controls=100)
obs = generate_observations(seq, stats, sampler; rng=Xoshiro(42))

# Inspect observations if needed
println("Observations: ", nrow(obs))
println("Cases: ", sum(obs.is_event))
println("Controls: ", sum(.!obs.is_event))

# Stage 2: Fit model
stat_names = [name(s) for s in stats]
two_stage = fit_rem(obs, stat_names)
coef(two_stage) == coef(sampled)          # true: same draw, same design

Fit Options

two_stage = fit_rem(obs, stat_names;
    maxiter = 100,   # Maximum Newton-Raphson iterations (NetworkCore.newton_fit)
    tol = 1e-8       # Convergence: |Δ log-likelihood| < tol and ‖gradient‖ < √tol
)

The optimizer is the ecosystem's one NetworkCore.newton_fit (Newton–Raphson with step halving); REM supplies only its per-stratum conditional-logit kernel. The keyword vocabulary is the family's: maxiter, tol, se, rng (see Reproducibility); control_draw_cov adds n_draws and threaded.

Understanding Results

The REMResult object contains:

FieldTypeDescription
coefficientsVector{Float64}Estimated coefficients
std_errorsVector{Float64}Standard errors
z_valuesVector{Float64}Z-statistics (coef/se)
p_valuesVector{Float64}Two-sided p-values
stat_namesVector{String}Names of statistics
n_eventsIntNumber of events (cases)
n_observationsIntTotal observations
log_likelihoodFloat64Log-likelihood at convergence
convergedBoolWhether optimization converged (an unconverged fit warns, see below)
iterationsIntNewton–Raphson iterations taken
var_covMatrix{Float64}Covariance matching se_type (vcov(result))
se_typeSymbolThe standard-error estimator that was used
tie_typeSymbol:none, or the tie correction that bit
strataVector{Int}Stratum IDs (sorted), indexing the two fields below
risk_set_sizesVector{Int}Risk-set size of each stratum (case included)
sampling_probsVector{Float64}Control sampling probability per stratum
singularBoolThe observed information at the solution is singular (standard errors NaN) — see Convergence Issues
singular_suspectsVector{String}The statistics loading on the null direction (collinear, or constant within every stratum) — see Convergence Issues
separatedVector{String}Coefficients whose maximum is at infinity (separation; z, p and intervals are then NaN) — see Convergence Issues

Accessor Functions

REMResult implements the full StatsAPI surface — the same verbs a GLM or an ERGM answers, each a method of the StatsAPI generic:

coef(result)          # Coefficient vector
stderror(result)      # Standard errors vector (== sqrt.(diag(vcov(result))))
vcov(result)          # Covariance matrix, matching se_method(result)
confint(result)       # Wald 95% intervals (p × 2 matrix); confint(result; level=0.9)
loglikelihood(result) # Log partial likelihood
nobs(result)          # Number of events (one stratum each — survival's `nevent`)
dof(result)           # Number of coefficients
aic(result)           # −2ℓ + 2k
bic(result)           # −2ℓ + k·log(n_events)
coeftable(result)     # NetworkCore.CoefficientTable: index by position or by name

coeftable returns the ecosystem's inspectable NetworkCore.CoefficientTable (tbl.names, tbl.estimates, tbl.std_errors, tbl.z_values, tbl.p_values; tbl["repetition"] is a row) — the same table show(result) prints. Until 0.2.0 it returned a DataFrame; build one from the fields if you need it:

using DataFrames
tbl = coeftable(result)
DataFrame(statistic=tbl.names, coefficient=tbl.estimates, std_error=tbl.std_errors,
          z_value=tbl.z_values, p_value=tbl.p_values)

The result-metadata accessors say what the fit did, so a script can act on it instead of parsing show output:

NetworkCore.is_exact(result)            # true — full risk set, no ties, converged
NetworkCore.is_exact(sampled)           # false
NetworkCore.se_method(sampled)          # :hessian
NetworkCore.tie_method(sampled)         # :none — the data had no ties
NetworkCore.approximations(sampled)     # ["case-control sampling of the risk set …", …]
NetworkCore.fit_metadata(sampled)       # all of the above, printed

Displaying Results

println(sampled)

Output:

Relational Event Model Results
==============================
Events: 481, Observations: 48581
Risk-set size: 1332 dyads, control sampling probability: 0.0751
Log-likelihood: -1230.3128
Converged: true (9 iterations)
Std. errors: inverse Hessian (one control draw)

                     Estimate  Std.Error   z value  Pr(>|z|)
repetition            -0.5086     0.0494  -10.3000    <1e-16 ***
reciprocity            0.5488     0.0520   10.5571    <1e-16 ***
sender_activity        0.0287     0.0027   10.8081    <1e-16 ***
receiver_popularity    0.0254     0.0021   11.9042    <1e-16 ***
transitive_closure     0.1503     0.0227    6.6307   3.3e-11 ***
sender_icr             0.7203     0.1824    3.9497   7.8e-05 ***
receiver_icr           1.3571     0.1681    8.0725   6.9e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Note: the risk set was sampled (100 of 1331 controls per event), so this
partial likelihood approximates the full-risk-set one. If the model is
misspecified the estimates depend on the control draw: refit with a
larger `n_controls` or the full risk set, or measure the draw-to-draw
spread with `control_draw_cov`.

The block is the family convention (ERGM.jl and Revel.jl print the same shape): the header states the design and what was done, the table comes from the shared NetworkCore.print_coeftable, and the note matches the design — with the full risk set there is none. A fit that is wrong rather than approximate — unconverged, singular, separated — prints a Warning: instead (see Convergence Issues).

Standard errors: two estimators, and a diagnostic that is not one

hess = fit_rem(seq, stats; n_controls=100, rng=Xoshiro(42))                 # default
sand = fit_rem(seq, stats; n_controls=100, rng=Xoshiro(42), se=:sandwich)   # Godambe H⁻¹BH⁻¹
coef(hess) == coef(sand)                       # true — only the covariance differs
round.(stderror(hess), digits=3)               # [0.049, 0.052, 0.003, 0.002, 0.023, 0.182, 0.168]
round.(stderror(sand), digits=3)               # [0.069, 0.077, 0.003, 0.002, 0.027, 0.173, 0.149]
se=what it is
:hessian (default)the inverse observed information of the partial likelihood on the risk set that was used. With a sampled risk set this is the information of the sampled likelihood, which nested case-control theory shows to be a consistent estimator of the estimator's variance (Goldstein & Langholz 1992; Borgan, Goldstein & Langholz 1995): the information lost by sampling fewer controls is what makes it larger than the full-risk-set one, and nothing further needs adding. The package pins this by simulation — 95 % Wald coverage over replicate sequences with events and controls redrawn
:sandwichthe event-clustered Godambe sandwich H⁻¹BH⁻¹, robust to misspecification of the within-stratum conditional model. Equals survival::coxph(..., robust=TRUE) with the stratum as the cluster (pinned against R)

Under both, stderror(fit) == sqrt.(diag(vcov(fit))) and NetworkCore.se_method(fit) reports which one ran.

There is deliberately no se=:bootstrap. Redrawing the controls and refitting does not expose a variance component the Hessian misses — combining the between-draw covariance of the refits with the Hessian double-counts (97–99 % coverage in the calibration simulation). What the redraws expose is how far the point estimate of a misspecified model depends on the draw: a pseudo-true value that moves with the controls, which a larger n_controls cures and a wider interval does not. That is a sensitivity diagnostic, and it is offered as one:

draws = control_draw_cov(seq, stats; n_controls=100, n_draws=10, rng=Xoshiro(7))
round.(draws.sd[1:2], digits=3)                # draw-to-draw sd of repetition, reciprocity
round.(draws.mean[1:2], digits=3)              # their mean over the draws
round.(stderror(hess)[1:2], digits=3)          # [0.049, 0.052] — compare, do not add

Read draws.mean against a fit at a larger n_controls (or the full risk set): if it moves, the model's pseudo-true value depends on the sampling. Read draws.sd against stderror(hess): a spread of the order of the standard error says a single draw is not a precise summary of the data. Both point at the same remedy — more controls. The section on choosing the number of controls tabulates it on these data.

Interpreting Coefficients

Log-Hazard Ratios

Coefficients are log-hazard ratios. A coefficient β means:

  • exp(β) is the multiplicative effect on the event rate
  • β > 0 increases the rate
  • β < 0 decreases the rate

Example Interpretations

From the full-risk-set fit above:

StatisticCoefficientexp(β)Interpretation
ReceiverAttribute(icr)1.313.7A coordinator is called at 3.7× the rate of another officer
SenderAttribute(icr)0.611.8Coordinators call at 1.8× the rate
Reciprocity0.331.4Each past call from the callee raises the rate of calling back by 40%
TransitiveClosure0.131.14Each unit of weighted two-path closure raises the rate by 14%
Repetition−0.340.71Given activity and popularity, each earlier call on the dyad lowers the rate of another

Confidence Intervals

Wald intervals come from confint (normal reference, level=0.95 by default); exponentiate for hazard-ratio intervals:

ci = confint(result)                 # 7 × 2: lower, upper
ci90 = confint(result; level=0.90)
hr = exp.(confint(result))           # hazard-ratio intervals
tbl = coeftable(result)
[(n, round(exp(lo), digits=2), round(exp(hi), digits=2))
 for (n, lo, hi) in zip(tbl.names, ci[:, 1], ci[:, 2])]

Computing Statistics Without Sampling

To compute statistics for all events (without controls):

stats_df = compute_statistics(seq, stats; decay=0.0)

Returns a DataFrame with one row per event:

ColumnDescription
senderSender ID
receiverReceiver ID
timeEvent time
<stat_name>Statistic values

Use case: Exploratory analysis, visualization, or exporting statistics.

Model Comparison

Comparing Log-Likelihoods

Nested models fitted on the same risk set (the full one, or the same control draw — the same rng=Xoshiro(k)) can be compared by likelihood ratio and by information criteria:

# Nested models
stats1 = [Repetition(), Reciprocity()]
stats2 = [Repetition(), Reciprocity(), TransitiveClosure()]

result1 = fit_rem(seq, stats1; n_controls=full)
result2 = fit_rem(seq, stats2; n_controls=full)

println("Model 1 LL: ", loglikelihood(result1), "  AIC: ", aic(result1), "  BIC: ", bic(result1))
println("Model 2 LL: ", loglikelihood(result2), "  AIC: ", aic(result2), "  BIC: ", bic(result2))

# Likelihood ratio test
using Distributions
LR = 2 * (loglikelihood(result2) - loglikelihood(result1))
df = dof(result2) - dof(result1)
p_value = ccdf(Chisq(df), LR)
println("LR test p-value: ", p_value)

Multiple control draws

For a sampled fit, compare results across control draws — by hand, or with control_draw_cov, which refits over independent draws and reports their mean and spread:

results = [fit_rem(seq, stats; n_controls=100, rng=Xoshiro(s)) for s in 1:5]

# Check coefficient stability
for (i, r) in enumerate(results)
    println("Draw $i: ", round.(coef(r), digits=3))
end

draws = control_draw_cov(seq, stats; n_controls=100, n_draws=10, rng=Xoshiro(1))
round.(draws.sd, digits=3)           # the same spread, as a number per coefficient

Reproducibility

Every random draw in fit_rem flows through one keyword, rng (an AbstractRNG, default Random.default_rng()) — the control draw, and the per-draw seeds of control_draw_cov — and never through the global RNG behind your back. (During development the draw was seeded with a seed=k keyword; it is rng=Xoshiro(k), which draws the same controls.)

a = fit_rem(seq, stats; n_controls=100, rng=Xoshiro(9))
b = fit_rem(seq, stats; n_controls=100, rng=Xoshiro(9))
coef(a) == coef(b)                   # true — the same rng, the same draw

c = control_draw_cov(seq, stats; n_controls=100, n_draws=10, rng=Xoshiro(7))
d = control_draw_cov(seq, stats; n_controls=100, n_draws=10, rng=Xoshiro(7))
c.cov == d.cov                       # true — every draw's seed comes from `rng`

The refits of control_draw_cov run on all available threads, and the result is independent of the thread count: NetworkCore.bootstrap_cov draws one seed per replicate from rng up front (threaded=false gives the same bits serially — the test suite asserts it).

Convergence Issues

An unconverged fit is loud

fit_rem runs the shared NetworkCore.newton_fit (Newton–Raphson with step halving) and stops when the log-likelihood change falls below tol and the gradient norm below sqrt(tol). If maxiter is exhausted first — or no step improves the likelihood — the fit is returned, but never silently:

  • a warning is emitted naming maxiter, the final gradient norm and tol;
  • result.converged is false and result.iterations says how far it got;
  • NetworkCore.is_exact(result) is false, and NetworkCore.approximations(result) carries the entry "the optimizer did not converge … estimates and standard errors are not maximum-partial-likelihood values";
  • show(result) prints Converged: false (N iterations; see approximations) and a caveat under the coefficient table.
short = fit_rem(seq, stats; n_controls=full, maxiter=2)   # warns
short.converged                     # false
short.iterations                    # 2
NetworkCore.is_exact(short)            # false
NetworkCore.approximations(short)[1]   # "the optimizer did not converge in 2 iterations — …"

So checking the flag is a matter of acting on it, not of discovering it:

if !short.converged
    short = fit_rem(seq, stats; n_controls=full, maxiter=500)
end
short.converged                     # true

Common Causes and Solutions

IssueSymptomWhat the package doesSolution
Perfect separationA coefficient of −20 with a standard error of 18 000warns naming the statistic, lists it in fit.separated, reports converged == false, withholds z, p and intervals (NaN), prints a caveat under the table, is_exact == falseRemove or transform the statistic (on the WTC calls, NodeProduct(icr) separates: no coordinator ever calls another)
MulticollinearityNaN standard errors, Converged: falsewarns naming the suspect statistics, fit.singular == true, fit.singular_suspects, caveat, is_exact == falseRemove one of the collinear statistics (NodeSum(icr) with both SenderAttribute(icr) and ReceiverAttribute(icr) is collinear)
Sparse dataNon-convergencewarns, converged == falseIncrease n_controls, simplify model
Too many parametersSlow convergence—Reduce model complexity

A statistic that is constant across all candidate dyads within every event's risk set cannot be estimated, even if it changes between events. It contributes zero information and is reported in singular_suspects; remove it from the model. Adding a common covariate offset within an event's risk set leaves the conditional likelihood and its standard errors unchanged. The numerical rank check accounts for the number of rows in the largest risk set, so rounding in long covariance sums cannot identify a collinear statistic.

Singular information makes the shared optimizer return converged=false and undefined uncertainty, even when the objective has stopped changing. On a separated design Newton's own stopping rule is satisfied on the asymptote, but the fit follows the ecosystem's separation policy and reports converged=false. The package names the responsible statistics and records both cases in its diagnostics; check singular and separated before interpreting coefficients:

sep = fit_rem(seq, [Repetition(), NodeProduct(icr)]; n_controls=full)   # warns: separation
sep.separated                       # ["product_icr"]
sep.converged, sep.p_values         # (false, [NaN, NaN]): inference withheld
NetworkCore.is_exact(sep)              # false
col = fit_rem(seq, [NodeSum(icr), SenderAttribute(icr), ReceiverAttribute(icr)]; n_controls=full)
col.singular, col.singular_suspects # (true, ["sum_icr", "sender_icr", "receiver_icr"])

Separation is a property of the design, not of where Newton stopped: the partial likelihood has no finite maximum when some combination of the statistics scores the case at least as high as every control in every stratum, and strictly higher in some. NetworkCore.jl's shared verdict (NetworkCore.clogit_separation) decides that exactly, with a linear programme whose separating direction is checked in rational arithmetic, so NodeProduct(icr) is flagged whether the attribute is coded 0/1, 0/1000 or 0/0.001. It is what survival::coxph warns about as "coefficient may be infinite".

Handling Non-Convergence

# Increase iterations
result = fit_rem(seq, stats; n_controls=full, maxiter=500)

# The separation verdict is already on the result
isempty(result.separated) || @warn "separated: $(result.separated)"

Advanced Topics

Is the estimate a property of the draw?

A hand-rolled bootstrap over events is the wrong tool for a sampled fit — resampling events with replacement changes the order and repetition structure the statistics are built from — and a bootstrap over controls is not a standard error either (the sampled likelihood's own information already accounts for the sampling; see above). What redrawing the controls answers is a different question: does the point estimate move with the draw?

draws = control_draw_cov(seq, stats; n_controls=100, n_draws=20, rng=Xoshiro(7))
round.(draws.mean[1:2], digits=2)                 # ≈ [-0.39, 0.40] — vs the full risk set's [-0.34, 0.33]
draws.sd[1:2] .< 2 .* stderror(hess)[1:2]         # true — the draws scatter by about one SE here
draws.n_unconverged                               # 0

n_draws replicates cost n_draws refits (threaded, thread-count-independent). With the full risk set there is nothing to redraw and the spread is exactly zero.

Time-Varying Effects

Two different questions hide under "effects that change over time":

Do the coefficients differ between periods? Split the sequence and fit each period on its own design (start_index/end_index live on generate_observations; fit from the observation DataFrames). The network state still accumulates from the first event, so the late period sees the full history:

mid_point = length(seq) ÷ 2
period_sampler = CaseControlSampler(n_controls=100)

obs_early = generate_observations(seq, stats, period_sampler;
                                  start_index=1, end_index=mid_point, rng=Xoshiro(1))
obs_late = generate_observations(seq, stats, period_sampler;
                                 start_index=mid_point + 1, rng=Xoshiro(2))

result_early = fit_rem(obs_early, [name(s) for s in stats])
result_late = fit_rem(obs_late, [name(s) for s in stats])

# Compare coefficients
println("Early period: ", round.(coef(result_early), digits=3))
println("Late period:  ", round.(coef(result_late), digits=3))

Does only the recent past matter? That is a memory model, not a period split: decay= (eventnet's halflife) or window= (events older than the window stop counting) on fit_rem, applied to every statistic — see Temporal Decay:

recent = fit_rem(seq, stats; n_controls=full, window=50.0)   # the last 50 calls count

Best Practices

  1. Declare the actor universe (actors=): the risk set is the estimand
  2. Enumerate the risk set when affordable, and check a sampled fit against a larger n_controls
  3. Pass an rng (rng=Xoshiro(k)) for a reproducible sampled fit
  4. Check convergence: act on result.converged (the fit has already warned)
  5. Skip early events: Consider excluding first few events with start_index
  6. Avoid multicollinearity: Don't include highly correlated statistics
  7. Scale large counts: Use LogDegree for networks with high-degree hubs
  8. Sufficient events: Aim for at least 10 events per parameter
  9. Compare control draws: Verify stability across control draws (control_draw_cov), and against a larger n_controls