Skip to contents

This is the main function to perform calculations using a Rydra setup. It loads the configuration, applies transformations, evaluates conditions, computes a model score, and applies an output transformation.

Usage

rydra_calculate(
  config_path,
  data,
  model_name = NULL,
  transformations = NULL,
  constants_key = "constants"
)

Arguments

config_path

Path to the YAML configuration file.

data

A list or a single-row data frame representing the input data for which the calculation is to be performed.

model_name

The name of the model configuration block in the YAML. If NULL, the function requires 'model_name' to be specified at the YAML root; otherwise it errors.

transformations

A named list of custom transformation functions. Defaults to internal list including center_variable, square_variable, log_transform, exp_transform, multiply_by, add_value, and truncate_variable. Provide your own list to override defaults. Provide list() to disable defaults; only functions explicitly provided will be allowed for both transformations and output transformation.

constants_key

Root key name for global constants injection. Default "constants". Legacy configs using centering are supported automatically.

Value

The final calculated result after applying all steps.

Examples

if (FALSE) { # \dontrun{
# Basic example using the package's built-in example configuration
config_path <- system.file("extdata", "example_config.yaml", package = "Rydra")

# Fallback for development when package is not installed
if (config_path == "" && file.exists("inst/extdata/example_config.yaml")) {
  config_path <- "inst/extdata/example_config.yaml"
}

if (file.exists(config_path)) {
  input_data <- list(
    biochemical_ga    = 12, # weeks
    weight            = 70, # kg
    age               = 30, # years
    plgf_machine      = 1,  # Corresponds to 'Delfia' in example config
    race              = 1,  # e.g., Caucasian/Other
    smoking           = 0,  # 0 for No, 1 for Yes
    diabetes_type_i   = 0,  # 0 for No, 1 for Yes
    diabetes_type_ii  = 0,  # 0 for No, 1 for Yes
    conception        = 1,  # e.g., 1 for Spontaneous, 3 for IVF
    previous          = 0   # e.g., 0 for Nulliparous
  )
  result <- rydra_calculate(config_path = config_path, data = input_data)
  print(paste("Calculated result:", result))

  # Example with custom transformations (overriding defaults)
  # This example defines a new transformation and uses only that one.
  # Note: The example_config.yaml might not use 'custom_doubler'.
  # This is for illustration of the 'transformations' parameter.
  my_transforms <- list(custom_doubler = function(x) x * 2)
  # If your config expects 'ga_centered', this example might need adjustment
  # or a config that uses 'custom_doubler(some_variable)'.
  # For this to run meaningfully, you'd typically align custom functions
  # with what your specific YAML configuration expects.

  # Example using NO base transformations, relying only on globally defined ones
  # (if your YAML refers to functions like 'log' directly and they are available)
  # result_no_base <- rydra_calculate(config_path = config_path,
  #                                   data = input_data,
  #                                   transformations = list())
  # print(paste("Calculated result (no base transformations):", result_no_base))

} else {
  print("Could not find example_config.yaml for rydra_calculate examples.")
}
} # }