optim_model_space: Calculation of the model_space object

View source: R/model_space.R

optim_model_spaceR Documentation

Calculation of the model_space object

Description

This function calculates model space, values of the maximized likelihood function, BICs, and standard deviations of the parameters that will be used in Bayesian model averaging. Moreover, it provides a vector with the names of the variables for bma function and the number of observations.

Usage

optim_model_space(
  df,
  timestamp_col,
  entity_col,
  dep_var_col,
  init_value,
  nested = TRUE,
  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), but note that scale is used only for adjusting the parscale element added later in the function code.

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 element of the result. Default is 100.

Value

An object of class badp_model_space, which is a list with the following elements:

  1. params - table with parameters of all estimated models

  2. stats - table with the value of maximized likelihood function, BIC, and standard errors for all estimated models

  3. reg_names - vector with the names of the variables

  4. observations_num - number of observations

  5. df - data frame used in estimation

  6. is_nested - logical indicating whether nested approach was used

  7. convergence - matrix of per-model convergence diagnostics with rows converged (1 if the likelihood value stalled across restarts at a solution standard errors can be computed from, 0 if the restart budget was exhausted while the value was 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; the likelihood value is trustworthy but the standard errors of the affected coordinates are not.

Rank of the sandwich covariance

The robust standard errors stored in the statistics, and the columns PSDR and PSDRcon that bma derives from them, come from the sandwich H^{-1} J H^{-1} with J = \sum_{i=1}^{N} s_i s_i' formed from the entity-level score vectors. J depends only on the variation of those scores across entities, so parameters entering the log-likelihood solely through terms common to every entity contribute nothing and J is rank deficient regardless of N; see score_rank. The fraction of parameter directions the scores span is reported by summary.badp_model_space. The standard errors that are reported remain well defined, as the sandwich restricted to the spanned block is the profile sandwich on that block, but the score covariance involving the remaining directions is discarded. The likelihood, the parameter estimates and the Hessian-based standard errors are unaffected.

Methods

Objects of class badp_model_space have the following methods available:

  • print.badp_model_space - Display model space information

Examples


library(magrittr)

data_prepared <- badp::economic_growth[, 1:5] %>%
  badp::feature_standardization(
    excluded_cols = c(country, year, gdp)
  ) %>%
  badp::feature_standardization(
    group_by_col  = year,
    excluded_cols = country,
    scale         = FALSE
  )

optim_model_space(
  df            = data_prepared,
  dep_var_col   = gdp,
  timestamp_col = year,
  entity_col    = country,
  init_value    = function(n) runif(n, 0.1, 1)
)


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