Learn R Programming

iglm (version 1.1)

results.generator: R6 Class for Storing iglm Estimation and Simulation Results

Description

The `results` class stores estimation (`$estimate()`) and simulation (`$simulate()`) results.

This class is primarily intended for internal use within the `iglm` framework but provides structured access to the results via the active bindings of the main `iglm_object`.

Arguments

Active bindings

coefficients_path

(`matrix` or `NULL`) Read-only. The path of all estimated coefficients across iterations.

samples

(`list` or `NULL`) Read-only. A list of simulated `iglm.data` objects (class `iglm.data.list`).

stats

(`matrix` or `NULL`) Read-only. Matrix of summary statistics for simulated samples, which are an `mcmc` obect from `coda`.

var

(`matrix` or `NULL`) Read-only. Estimated variance-covariance matrix for non-popularity coefficients.

fisher_popularity

(`matrix` or `NULL`) Read-only. Fisher information matrix for popularity coefficients.

fisher_nonpopularity

(`matrix` or `NULL`) Read-only. Fisher information matrix for non-popularity coefficients.

score_popularity

(`numeric` or `NULL`) Read-only. Score vector for popularity coefficients.

score_nonpopularity

(`numeric` or `NULL`) Read-only. Score vector for non-popularity coefficients.

llh

(`numeric` or `NULL`) Read-only. Vector of log-likelihood values recorded during estimation.

model_assessment

(`list` or `NULL`) Read-only. Results from model assessment (goodness-of-fit).

estimated

(`logical`) Read-only. Flag indicating if estimation has been completed.

Methods


Method new()

Creates a new `results` object. Initializes internal fields, primarily setting up an empty matrix for the `coefficients_path` based on the expected number of coefficients.

Usage

results.generator$new(size_coef, size_coef_popularity, file)

Arguments

size_coef

(integer) The number of non-popularity (structural) coefficients in the model.

size_coef_popularity

(integer) The number of popularity coefficients in the model (0 if none).

file

(character or `NULL`) If provided, loads the sampler state from the specified .rds file instead of initializing from parameters.

Returns

A new `results` object, initialized to hold results for a model with the specified dimensions.


Method set_model_assessment()

Stores the results object generated by a model assessment (goodness-of-fit) procedure within this `results` container.

Usage

results.generator$set_model_assessment(res)

Arguments

res

An object containing the model assessment results, expected to have the class `iglm_model_assessment`.

Returns

The `results` object itself (`self`), invisibly. Called for its side effect of storing the assessment results.


Method set_prediction()

Stores prediction results.

Usage

results.generator$set_prediction(prediction)

Arguments

prediction

An object containing the prediction results (is a list of class `iglm.prediction`.


Method gather()

Gathers the current state of the `results` object into a list for saving or inspection. This includes all internal fields such as coefficient paths, samples, statistics, variance-covariance matrix, Fisher information, score vectors, log-likelihood values, model assessment results, and estimation status.

Usage

results.generator$gather()

Returns

A list containing all the internal fields of the `results` object.


Method save()

Saves the current state of the `results` object to a specified file path in RDS format. This allows for persisting the results for later retrieval and analysis.

Usage

results.generator$save(file)

Arguments

file

(character) The file path where the results state should be saved. Must be a valid character string.

Returns

The `results` object itself (`self`), invisibly.


Method resize()

Resizes the internal storage for the coefficient paths to accommodate a different number of coefficients. This is useful if the model structure changes and the results object needs to be reset.

Usage

results.generator$resize(size_coef, size_coef_popularity)

Arguments

size_coef

(integer) The new number of non-popularity coefficients.

size_coef_popularity

(integer) The new number of popularity coefficients. @return The `results` object itself (`self`), invisibly.


Method update()

Updates the internal fields of the `results` object with new outputs, typically after an estimation run (`$estimate()`) or simulation run (`$simulate()`). Allows selectively updating components. Appends to `coefficients_path` and `llh` if called multiple times after estimation. Replaces `samples` and `stats`.

Usage

results.generator$update(
  coefficients_path = NULL,
  samples = NULL,
  var = NULL,
  fisher_popularity = NULL,
  fisher_nonpopularity = NULL,
  score_popularity = NULL,
  score_nonpopularity = NULL,
  llh = NULL,
  stats = NULL,
  estimated = FALSE
)

Arguments

coefficients_path

(matrix) A matrix where rows represent iterations and columns represent all coefficients (non-popularity then popularity), showing their values during estimation. If provided, appends to any existing path.

samples

(list) A list of simulated `iglm.data` objects (class `iglm.data.list`). If provided, replaces any existing samples.

var

(matrix) The estimated variance-covariance matrix for the non-popularity coefficients. Replaces existing matrix.

fisher_popularity

(matrix) The Fisher information matrix for popularity coefficients. Replaces existing matrix.

fisher_nonpopularity

(matrix) The Fisher information matrix for non-popularity coefficients. Replaces existing matrix.

score_popularity

(numeric) The score vector for popularity coefficients. Replaces existing vector.

score_nonpopularity

(numeric) The score vector for non-popularity coefficients. Replaces existing vector.

llh

(numeric) Log-likelihood value(s). If provided, appends to the existing vector of log-likelihoods.

stats

(matrix) A matrix of summary statistics from simulations, where rows correspond to simulations and columns to statistics. Replaces or extends the existing matrix and will be turned into a mcmc object from the `coda` package.

estimated

(logical) A flag indicating whether these results come from a completed estimation run. Updates the internal status.

Returns

The `results` object itself (`self`), invisibly. Called for its side effects.


Method remove_samples()

Clears the stored simulation samples (`.samples`) and statistics (`.stats`) from the object, resetting it to an empty list. This might be used to save memory or before running new simulations.

Usage

results.generator$remove_samples()

Returns

The `results` object itself (`self`), invisibly.


Method plot()

Generates diagnostic plots for the estimation results. Currently plots:

  • The log-likelihood path across iterations.

  • The convergence paths for popularity coefficients (if present).

  • The convergence paths for non-popularity coefficients.

Optionally, can also trigger plotting of model assessment results if available.

Usage

results.generator$plot(
  trace = FALSE,
  stats = FALSE,
  model_assessment = FALSE,
  ...
)

Arguments

trace

(logical) If `TRUE` (default), plot the trace plots of the estimation (log-likelihood and coefficient paths). Requires model to be estimated.

stats

(logical) If `TRUE`, plots the normalized statistics from simulations. Default is `FALSE`.

model_assessment

(logical) If `TRUE`, attempts to plot the results stored in the `.model_assessment` field. Requires model assessment to have been run and a suitable `plot` method for `iglm_model_assessment` objects to exist. Default is `FALSE`.

...

Additional outputs

Details

Requires estimation results (`private$.estimated == TRUE`) to plot convergence diagnostics. Requires model assessment results for the model assessment plots.


Method print()

Prints a concise summary of the contents of the `results` object, indicating whether various components (coefficients path, variance matrix, Fisher info, score, samples, stats, etc.) are available.

Usage

results.generator$print(...)

Arguments

...

Additional arguments (currently ignored).

Returns

The `results` object itself (`self`), invisibly.


Method clone()

The objects of this class are cloneable with this method.

Usage

results.generator$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.