aiask: Ask an AI service to interpret summarized statistical results

View source: R/ai.R

aiaskR Documentation

Ask an AI service to interpret summarized statistical results

Description

Automatically discovers compact statistical result components inside an object, sends only the most useful results to the selected AI service, and attaches the returned interpretation. Lists receive an ai element; other objects receive an "ai" attribute.

Usage

aiask(x, ai = TRUE, prompt = NULL)

Arguments

x

An R4VN result object, summarized list, result table/matrix, or character result. aiask() automatically finds useful result components.

ai

TRUE to use the default configuration or a configuration name.

prompt

Optional request specific to the current result. It is added after the permanent prompt stored by aisetup().

Details

The extractor is generic rather than command-specific. It recursively searches for result-like tables, matrices, estimates, tests, diagnostics, and short statistical context while ignoring raw data, HTML/CSS, formatting objects, plots, model internals, residuals, and other high-volume noise. The same aiask() can therefore be used for R4VN descriptive, multivariable, longitudinal, survival, diagnostic, scale, regression, and future result objects without a separate prepare function for every command.

A conservative input budget is applied before the API call. If the provider reports that the request is too large, aiask() automatically compacts the results further and retries up to two times.

Value

Invisibly returns x with an attached AI result. Successful results contain status, name, model, comment, input, optional usage, and created. API failures are attached with status = "error" and do not destroy x.

Examples

## Not run: 
aisetup(
  name = "gpt",
  provider = "openai",
  model = "gpt-5.6",
  tokenenv = "OPENAI_API_KEY",
  default = TRUE
)

result <- list(
  analysis = "Logistic regression",
  outcome = "Hypertension",
  estimates = data.frame(
    variable = c("Age", "Smoking"),
    OR = c(1.05, 1.82),
    lower = c(1.02, 1.15),
    upper = c(1.08, 2.88),
    p = c(0.001, 0.011)
  )
)

result <- aiask(result)
result$ai$comment

result <- aiask(
  result,
  ai = "gpt",
  prompt = "Focus on modifiable risk factors."
)

## End(Not run)


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.