optim_model_space_params: Finds MLE parameters for each model in the given model space

View source: R/model_space.R

optim_model_space_paramsR Documentation

Finds MLE parameters for each model in the given model space

Description

Given a dataset and a generator of starting values, initializes a model space by drawing a starting point for each model. Then for each model performs a numerical optimization and finds parameters which maximize the likelihood.

Usage

optim_model_space_params(
  df,
  timestamp_col,
  entity_col,
  dep_var_col,
  init_value,
  nested,
  exact_value = FALSE,
  cl = NULL,
  control = list(trace = 0, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05),
  max_restarts = 5,
  restart_tol = 0.001,
  max_reoptimizations = 5,
  max_init_attempts = 100
)

Arguments

df

Data frame with data for the analysis.

timestamp_col

The name of the column with time stamps.

entity_col

Column with entities (e.g. countries).

dep_var_col

Column with the dependent variable.

init_value

Function of one argument n returning n starting values for the numerical optimization (e.g. function(n) runif(n, 0.1, 1)). It is called separately for every model, so every model gets its own starting point; with a random generator this turns the estimation into a randomized multi-start experiment. A single number is also accepted and is used as the starting value for every parameter, so that init_value = 0.5 is equivalent to function(n) rep(0.5, n). Starting values must be non-zero, because zeros encode excluded parameters.

nested

Logical. If TRUE (default), compute approximate standard deviations using the nested-model approach via nested_std_dev_from_params(). If FALSE, use the non-nested approach via non_nested_std_dev_from_params(). The choice affects which approximation routine is used for each model in params.

exact_value

Whether the exact value of the likelihood should be computed (TRUE) or just the proportional part (FALSE). Check sem_likelihood for details.

cl

An optional cluster object. If supplied, the function will use this cluster for parallel processing. If NULL (the default), pbapply::pblapply will run sequentially.

control

a list of control parameters for the optimization which are passed to optim. Default is list(trace = 0, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05).

max_restarts

Maximum number of times the BFGS optimization is restarted from its previous solution for a single model. A restart resets the internal curvature approximation of BFGS, which often makes further progress on ill-conditioned likelihood ridges where a single run stalls. Default is 5.

restart_tol

Log-likelihood improvement between restarts below which the optimization is considered converged. Improvements of this size are immaterial for posterior model probabilities. Default is 1e-3.

max_reoptimizations

Maximum number of times a model is re-optimized from a fresh starting point drawn from init_value, when the solution reached turns out to be one no standard errors can be computed from. BFGS stops wherever the gradient vanishes, which need not be a maximum, and from a randomly drawn starting point a model occasionally ends up in a degenerate region instead - hundreds of log-likelihood units below its maximum, with an observed information matrix so rank-deficient that inverting it into standard errors fails with ‘system is computationally singular’. Such a solution is therefore discarded and the whole optimization repeated from a new starting point. A model whose every attempt ends this way keeps the likeliest of them and is reported with converged = 0; no error is raised, so a single model cannot bring down the estimation of the whole model space. Default is 5.

max_init_attempts

Maximum number of starting points drawn from init_value for a single model. Not every parameter vector is a usable starting point: at some of them the covariance matrix implied by the parameters is not positive definite, so the likelihood is undefined there and the optimization cannot even start (taping the likelihood for automatic differentiation fails on the Cholesky factorization with an error such as ‘the leading minor of order 13 is not positive’). Whether a starting point is usable depends on the model, so it can only be checked after the point has been drawn: unusable draws are discarded and replaced by fresh draws from init_value, and only after max_init_attempts unsuccessful draws is an error raised. The number of draws each model actually needed is reported in the n_init_draws row of the convergence diagnostics. Default is 100.

Value

List (or matrix) of parameters describing analyzed models. The returned matrix carries a "convergence" attribute: a matrix with one column per model and rows converged (1 if the likelihood value stalled at a solution standard errors can be computed from, 0 if the restart budget was exhausted while still improving or every re-optimization ended in a degenerate region), optim_code (the optim convergence code of the final run), n_restarts, max_abs_gradient and n_init_draws (the number of starting points drawn from init_value before one at which the likelihood is defined was found). A large final gradient with converged = 1 indicates parameters on a degenerate (nearly collinear) likelihood ridge rather than a failed optimization.


badp documentation built on Aug. 20, 2026, 9:08 a.m.