Parses a GAM-style formula and separates the response, all terms, smooth (non‑parametric) terms declared via s(...), and parametric terms. In addition, it extracts per‑term neural network specifications that can be written inline inside each s(...) call (e.g., num_units, activation, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer).

This function uses an abstract syntax tree (AST) walker (no regex) to read the arguments of each s(var, ...). Arguments are evaluated in the caller’s environment. For *_regularizer and *_initializer arguments, proper Keras objects are required (e.g., keras::regularizer_l2(1e-4), keras::initializer_glorot_uniform()).

get_formula_elements(formula)

Arguments

formula

A model formula. Smooth terms must be written as s(var, ...).

Value

A list with the following elements:

y

Character scalar. The response variable name.

terms

Character vector with all variable names on the RHS (both smooth and parametric).

np_terms

Character vector with smooth (non‑parametric) variable names extracted from s(...).

p_terms

Character vector with parametric terms (i.e., terms \\ np_terms).

np_formula

A formula containing only the s(...) terms (or NULL if none).

p_formula

A formula containing only the parametric terms (or NULL if none).

np_architecture

Named list keyed by smooth term (e.g., "x1", "x3"). Each entry is a list of per‑term settings parsed from s(term, ...). Supported keys include: num_units (numeric or numeric vector), activation (character), learning_rate (numeric), kernel_initializer (keras initializer object), bias_initializer (keras initializer object), kernel_regularizer (keras regularizer object), bias_regularizer (keras regularizer object), activity_regularizer (keras regularizer object).

formula

The original formula object.

Details

Inline per‑term configuration in s(...). You can specify neural network hyperparameters per smooth term, e.g.:


  y ~ s(x1, num_units = c(1024, 512),
          activation = "tanh",
          kernel_regularizer = keras::regularizer_l2(1e-4)) +
      x2 +
      s(x3, num_units = 1024,
          bias_initializer = keras::initializer_zeros())

Values are evaluated in the caller’s environment. For regularizers and initializers you must pass actual Keras objects (not character strings).

Supported keys. Only the keys listed in np_architecture above are recognized. Unknown keys are ignored with a warning.

Typical usage. The returned np_terms and np_architecture are consumed by model-building code to construct one neural network per smooth term, applying any per‑term overrides while falling back to global defaults for unspecified keys.

Errors and validation

  • The first argument of each s(...) must be a symbol naming the variable.

  • All additional arguments to s(...) must be named.

  • *_regularizer must be a Keras regularizer object.

  • *_initializer must be a Keras initializer object.

Author

Ines Ortega-Fernandez, Marta Sestelo