R/np.formula.subset.R

Defines functions .np_formula_validate_syntax .np_formula_validate_terms .np_formula_term_names .np_formula_frame_promises .np_missing_na_dots .np_na_promise_missing .np_formula_promise_expression .np_reject_fit_subset .np_fit_na_preflight

# Unsupported NA controls must be rejected before dots are materialized. The
# few supported controls remain owned by the existing formula/prediction code.
.np_fit_na_preflight <- function(bws, expressions, frame, family,
                                 native = FALSE, data = NULL, newdata = NULL,
                                 native.eval = FALSE) {
  if (!("na.action" %in% names(expressions)) ||
      !is.list(bws) || inherits(bws, "formula") ||
      length(.np_missing_na_dots(expressions, frame)))
    return(invisible(NULL))
  value <- function(name) {
    index <- match(name, names(expressions))
    if (is.na(index)) return(NULL)
    eval(substitute(...elt(INDEX), list(INDEX = index)), envir = frame)
  }
  # The generic dispatches to formula only in the absence of native training.
  training <- if (family %in% c("npudens", "npudist")) "tdat" else "txdat"
  formula <- !native && !is.null(bws[["formula", exact = TRUE]]) &&
    is.null(value(training))
  if (formula) {
    if (!is.null(value(".np.formula.state")) ||
        !is.null(data) || !is.null(value("data")) ||
        (family %in% c("npreg", "npindex", "npcdens", "npcdist",
                       "npqreg", "npconmode", "npscoef", "npplreg") &&
         "tydat" %in% names(expressions)))
      return(invisible(NULL))
    external <- !is.null(newdata) || !is.null(value("newdata"))
    native.names <- if (family %in% c("npudens", "npudist")) "edat" else
      if (family %in% c("npcdens", "npcdist")) c("exdat", "eydat") else
        if (family %in% c("npscoef", "npplreg")) c("exdat", "ezdat") else "exdat"
    formula.eval <- external && !any(native.names %in% names(expressions))
    if (family == "npindex" && formula.eval)
      return(invisible(NULL))
  }
  # Regression prediction already restores omitted evaluation rows for this
  # exact policy. Resolve its value, not the callback; do not broaden it to
  # arbitrary functions or apply it to retained training.
  if (family == "npreg" &&
      (native.eval || "exdat" %in% names(expressions) ||
       !is.null(newdata) || !is.null(value("newdata")))) {
    action <- value("na.action")
    if (identical(action, stats::na.exclude) || identical(action, "na.exclude"))
      return(invisible(NULL))
  }
  stop("na.action override is not supported for this stored-fit route; use the retained omission policy, preprocess replacement native data, or supply replacement formula data",
       call. = FALSE)
}

# A stored fit owns its training sample. Inspect supplied syntax only: unused
# replacement subsets must never be forced outside the original data mask.
.np_reject_fit_subset <- function(expressions) {
  if ("subset" %in% names(expressions))
    stop("replacement subset is not supported with this stored bandwidth or fit; subset the input data explicitly or select new bandwidths with a formula subset",
         call. = FALSE)
  invisible(NULL)
}

# Reconstructed calls must consume the original promise, not replay its
# expression. Keep this reference transient: public calls retain their original
# syntax and the model-frame owner still controls subset/NA evaluation order.
.np_formula_promise_expression <- function(expression, frame) {
  substitute(base::evalq(EXPRESSION, FRAME), list(EXPRESSION = expression, FRAME = frame))
}

# missing() must inspect the original formal/dots promise. Evaluating its value
# or its substituted expression would lose omission or run a policy factory.
.np_na_promise_missing <- function(name, frame) {
  eval(substitute(base::missing(ARG), list(ARG = as.name(name))), envir = frame)
}

.np_missing_na_dots <- function(expressions, frame) {
  indices <- which(names(expressions) == "na.action")
  # Duplicate names keep their existing argument-admission failure.
  if (length(indices) != 1L || !exists("...", frame, inherits = FALSE))
    return(integer())
  indices[vapply(indices, function(i)
    .np_na_promise_missing(paste0("..", i), frame), logical(1L))]
}

.np_formula_frame_promises <- function(args, frame,
                                        map = c(data = "data", na.action = "na.action")) {
  for (name in intersect(names(map), names(args))) {
    if (identical(name, "na.action") &&
        .np_na_promise_missing(map[[name]], frame)) {
      args[[name]] <- NULL
      next
    }
    args[[name]] <- .np_formula_promise_expression(as.name(map[[name]]), frame)
  }
  args
}

# terms() quotes non-syntactic symbols; model.frame() uses their actual names.
# Resolve only a simple symbol. Never evaluate a term or rewrite a transform,
# interaction, formula, predvars expression, or the user's data-frame names.
.np_formula_term_names <- function(labels) {
  quoted <- which(startsWith(labels, "`"))
  if (!length(quoted)) return(labels)
  for (i in quoted) {
    term <- str2lang(labels[[i]])
    if (is.symbol(term)) labels[[i]] <- as.character(term)
  }
  labels
}

# Inspect terms, not text: offset as a variable/response or inside an ordinary
# transformation is not an offset special and retains its existing semantics.
.np_formula_validate_terms <- function(tt) {
  if (length(attr(tt, "offset")))
    stop("offset() terms are not supported in this estimator formula; remove the offset term or use explicitly prepared data",
         call. = FALSE)
  tt
}

# Call only after the existing owner has resolved its formula promise. Never
# evaluate a construction call or a response/covariate expression here.
.np_formula_validate_syntax <- function(formula, conditional.response = FALSE) {
  if (conditional.response && length(formula) == 3L) {
    named.response <- function(expr) {
      if (is.symbol(expr)) return(TRUE)
      if (!is.call(expr)) return(FALSE)
      if (identical(expr[[1L]], quote(`(`)) && length(expr) == 2L)
        return(named.response(expr[[2L]]))
      if (identical(expr[[1L]], quote(`+`)) && length(expr) %in% c(2L, 3L))
        return(all(vapply(as.list(expr)[-1L], named.response, logical(1L))))
      FALSE
    }
    if (!named.response(formula[[2L]]))
      stop("conditional formula responses must be variable names separated by '+'; create an explicit transformed variable in data or use the native data interface",
           call. = FALSE)
  }
  parts <- function(expr) {
    if (is.call(expr) && identical(expr[[1L]], quote(`|`)) &&
        length(expr) == 3L)
      return(c(parts(expr[[2L]]), parts(expr[[3L]])))
    list(expr)
  }
  for (rhs in parts(formula[[length(formula)]])) {
    role <- as.call(list(quote(`~`), rhs))
    class(role) <- "formula"
    environment(role) <- environment(formula)
    .np_formula_validate_terms(terms(role, allowDotAsName = TRUE))
  }
  invisible(formula)
}

# Formula-owned subset is non-standard evaluation: dispatch must inspect other
# arguments without forcing it outside the data mask. Read retained arguments
# from their original promises, never by re-evaluating substituted expressions.
.np_formula_subset_indices <- function(method, expressions) {
  dot.names <- names(expressions)
  indices <- if (is.null(dot.names)) integer() else
    which(!is.na(pmatch(dot.names, "subset")))
  if (!is.null(method) && length(expressions)) {
    markers <- lapply(seq_along(expressions), function(i)
      as.name(paste0(".np_formula_dispatch_dot_", i)))
    names(markers) <- dot.names
    synthetic <- as.call(c(list(as.name(".np_formula_dispatch")),
      list(bws = as.name(".np_formula_dispatch_bws")), markers))
    matched <- tryCatch(match.call(definition = method, call = synthetic,
                                  expand.dots = FALSE),
                        error = function(e) NULL)
    if (!is.null(matched) && "subset" %in% names(matched))
      indices <- union(indices, which(vapply(markers, identical, logical(1L),
                                             matched[["subset"]])))
  }
  indices
}

.np_omit_missing_na_call <- function(call, frame) {
  if (!"na.action" %in% c(names(call), names(call[["..."]]))) return(call)
  dots <- eval(quote(substitute(list(...))), envir = frame)[-1L]
  if (!length(.np_missing_na_dots(dots, frame))) return(call)
  if ("na.action" %in% names(call)) call[["na.action"]] <- NULL
  if ("na.action" %in% names(call[["..."]])) {
    dots <- as.list(call[["..."]])
    dots <- dots[names(dots) != "na.action"]
    call[["..."]] <- if (length(dots)) as.pairlist(dots) else NULL
  }
  call
}

.np_formula_dispatch_args <- function(method, expressions, promise.frame,
                                      data.roles = character(), call = NULL,
                                      caller = NULL, omit.subset = TRUE) {
  omitted <- if (omit.subset) .np_formula_subset_indices(method, expressions) else integer()
  omitted <- union(omitted, .np_missing_na_dots(expressions, promise.frame))
  diagnose <- length(data.roles) && "data" %in% names(expressions)
  if (!length(omitted) && !diagnose)
    return(eval(quote(list(...)), envir = promise.frame))
  indices <- setdiff(seq_along(expressions), omitted)
  args <- lapply(indices, function(i) {
    argument <- names(expressions)[i]
    if (diagnose && length(argument) && argument %in% data.roles)
      .np_data_argument_preflight(expressions[[i]], argument, call, caller,
        value = eval(substitute(...elt(index), list(index = i)), envir = promise.frame))
    eval(substitute(...elt(index), list(index = i)), envir = promise.frame)
  })
  if (!is.null(names(expressions)))
    names(args) <- names(expressions)[indices]
  args
}

# Only materialized option values are inspected here. An ordinary caller
# expression has already evaluated in its original owner; it is not syntax
# for us to evaluate again. Structural formulas/subset/NA policies are handled
# separately. Inert lists and expression vectors are deliberately not walked.
.np_formula_options <- function(args, where) {
  unsafe <- vapply(args, function(value)
    is.call(value) || is.symbol(value) || typeof(value) == "bytecode", logical(1L))
  # Named formula construction can retain the structural formula in dots.
  # Accept only a real tilde form, never an executable call with a spoofed class.
  for (i in which(names(args) == "formula")) {
    value <- args[[i]]
    if (inherits(value, "formula") && is.call(value) &&
        length(value) %in% c(2L, 3L) && identical(value[[1L]], quote(`~`)))
      unsafe[i] <- FALSE
  }
  if (any(unsafe)) {
    labels <- names(args)
    if (is.null(labels)) labels <- rep.int("", length(args))
    empty <- is.na(labels) | !nzchar(labels)
    labels[empty] <- paste0("#", which(empty))
    stop(paste0(where, ": executable option value(s) ",
      paste(shQuote(labels[unsafe]), collapse = ", "),
      " cannot be evaluated again inside a formula handoff; evaluate these ",
      "options in the caller and pass their resulting values instead."),
      call. = FALSE)
  }
  args
}

# Existing value-list constructor calls still need the original subset syntax.
# The constructor's model.frame, not this helper, evaluates that expression.
.np_formula_dispatch_call <- function(fun, args, expressions, envir) {
  indices <- .np_formula_subset_indices(NULL, expressions)
  # envir is the activation owning these dots, not its parent. Keep the
  # promise reference so the model-frame owner can recover both expression
  # and original scope instead of splicing syntax into the formula's scope.
  subsets <- stats::setNames(lapply(indices, function(i)
    as.name(paste0("..", i))), names(expressions)[indices])
  # This helper is used only for formula entry, before the model-frame owner.
  # Keep structural syntax and supported NA policies out of option admission.
  structural <- names(args) %in% c("formula", "bws", "data", "newdata", "na.action")
  if (!length(structural)) structural <- rep.int(FALSE, length(args))
  .np_formula_options(args[!structural], "formula dispatch")
  do.call(fun, c(args, subsets), envir = envir)
}

# Evaluate expression values once, align them transiently, and retain portable
# prediction expressions (including makepredictcall metadata), never data in
# those prediction expressions. Bandwidth owners separately retain the frame.
.np_formula_unwrap_prediction <- function(tt) {
  prediction <- attr(tt, "predvars")
  variables <- attr(tt, "variables")
  owned <- is.call(prediction) && length(prediction) == 2L &&
    is.call(prediction[[1L]]) && length(prediction[[1L]]) == 3L &&
    identical(prediction[[1L]][[1L]], quote(utils::getFromNamespace)) &&
    identical(prediction[[1L]][[2L]], ".np_formula_align_values") &&
    is.character(prediction[[1L]][[3L]]) &&
    prediction[[1L]][[3L]] %in% c("np", "npRmpi")
  if (owned) prediction <- prediction[[2L]]
  # Older conditional objects used base-R alignment scaffolding. Recognize
  # only exact reconstructions from the stored variable expressions: arbitrary
  # user prediction calls and their trained metadata are not ours to replace.
  legacy <- FALSE
  if (is.call(prediction) && identical(prediction[[1L]], quote(as.data.frame))) {
    expected <- as.call(list(quote(as.data.frame),
      as.call(c(list(quote(ts.intersect)), as.list(variables)[-1L]))))
    legacy <- identical(prediction, expected)
  } else if (is.call(prediction) && identical(prediction[[1L]], quote(`[`)) &&
             length(prediction) == 4L && is.call(prediction[[2L]]) &&
             identical(prediction[[2L]][[1L]], quote(cbind)) &&
             length(prediction[[2L]]) >= 2L) {
    frame <- prediction[[2L]][[2L]]
    if (is.call(frame) && identical(frame[[1L]], quote(as.data.frame)) &&
        length(frame) == 2L && is.call(frame[[2L]]) &&
        identical(frame[[2L]][[1L]], quote(ts.intersect))) {
      arguments <- as.list(variables)[-1L]
      series <- as.list(frame[[2L]])[-1L]
      is.ts <- vapply(arguments, function(x)
        any(vapply(series, identical, logical(1L), x)), logical(1L))
      if (any(is.ts) && any(!is.ts) && sum(is.ts) == length(series)) {
        combined <- as.call(c(list(quote(cbind),
          as.call(list(quote(as.data.frame),
            as.call(c(list(quote(ts.intersect)), arguments[is.ts]))))),
          arguments[!is.ts], list(check.rows = TRUE)))
        index <- order(c(which(is.ts), which(!is.ts)))
        expected <- substitute(COMBINED[, INDEX], list(COMBINED = combined, INDEX = index))
        legacy <- identical(prediction, expected)
      }
    }
  }
  if (legacy) prediction <- variables
  list(prediction = if (is.null(prediction)) attr(tt, "variables") else prediction,
       makepredictcall = is.null(attr(tt, "predvars")) || owned || legacy)
}

.np_formula_model_frame <- function(formula, data = NULL, subset, na.action,
                                    drop.unused.levels = FALSE, xlev = NULL, ...,
                                    .np.capture = NULL, .np.auxiliary = list(),
                                    .np.subset = NULL) {
  if (!is.data.frame(data) && !is.environment(data) && !is.null(attr(data, "class")))
    data <- as.data.frame(data)
  tt <- if (inherits(formula, "terms")) formula else terms(formula, data = data)
  .np_formula_validate_terms(tt)
  prepared <- .np_formula_unwrap_prediction(tt)
  prediction <- prepared$prediction
  values <- eval(prediction, data, environment(tt))
  if (prepared$makepredictcall) {
    variables <- attr(tt, "variables")
    for (i in seq_along(values))
      prediction[[i + 1L]] <- stats::makepredictcall(values[[i]], variables[[i + 1L]])
  }
  # Observation-level auxiliaries share the formula's time intersection,
  # subset and NA action. They are model-frame extras, never predictors.
  aligned <- .np_formula_align_values(c(values, .np.auxiliary))
  # I(numeric_vector) protects formula syntax, not a distinct kernel data type.
  # Keep matrices and all other explicit classes subject to existing policy.
  for (i in seq_along(values)) {
    value <- aligned[[i]]
    if (identical(attr(value, "class"), "AsIs") &&
        is.numeric(value) && is.null(dim(value))) {
      class(value) <- NULL
      aligned[[i]] <- value
    }
  }
  attr(tt, "predvars") <- aligned[seq_along(values)]
  auxiliary <- aligned[length(values) + seq_along(.np.auxiliary)]
  selected <- if (length(auxiliary))
    vapply(auxiliary, NROW, integer(1L)) != NROW(aligned[[1L]]) else logical()
  frame.call <- match.call()
  frame.call[[1L]] <- quote(stats::model.frame)
  # Preserve already prepared column values when evaluating the terms object.
  # Use the base primitive so caller bindings cannot replace this value handoff.
  frame.call[["formula"]] <- as.call(list(base::quote, tt))
  frame.call["data"] <- list(data)
  frame.call$.np.capture <- NULL
  frame.call$.np.auxiliary <- NULL
  frame.call$.np.subset <- NULL
  if (length(auxiliary))
    frame.call[names(auxiliary)[!selected]] <- auxiliary[!selected]
  if (!is.null(.np.capture)) {
    # Freeze the resolved policy, not its caller-side expression. Force the
    # promise once, and let model.frame invoke the policy exactly once.
    policy <- if (!missing(na.action)) na.action else {
      action <- attr(data, "na.action")
      if (!is.null(action) && mode(action) != "numeric") action else
        getOption("na.action", stats::na.fail)
    }
    if (is.character(policy))
      policy <- get(policy, envir = asNamespace("stats"), mode = "function")
    .np.capture$na.action <- policy
    frame.call["na.action"] <- list(policy)
  }
  # Keep syntax and its owner together; a saved ..k is not a reusable call.
  subset.spec <- .np.subset
  if (is.null(subset.spec) && !missing(subset))
    subset.spec <- .np_formula_subset_inputs(data, match.call()[["subset"]],
                                            parent.frame(), resolve.only = TRUE)
  if (!is.null(subset.spec))
    frame.call["subset"] <- list(eval(subset.spec$expression, data,
                                      subset.spec$envir))
  frame <- eval(frame.call, parent.frame())
  # Preserve unambiguously preselected legacy auxiliaries. Full-length inputs
  # always belong to the original sample, even when subset only reorders it.
  for (name in names(auxiliary)[selected]) {
    value <- auxiliary[[name]]
    if (NROW(value) != nrow(frame))
      stop(sprintf("'%s' must match the original or selected formula sample", name),
           call. = FALSE)
    frame[[paste0("(", name, ")")]] <- value
  }
  retained <- attr(frame, "terms")
  attr(retained, "predvars") <- prediction
  attr(frame, "terms") <- retained
  if (!is.null(.np.capture)) frame <- .np_formula_complete_training_frame(frame)
  if (!is.null(.np.capture)) attr(frame, ".np.subset") <- subset.spec
  .np_formula_share_prepared_columns(frame, aligned)
}

# Native training owners use complete cases even when a model-frame policy
# leaves missing values. Record that existing exclusion before formula owners
# freeze the sample, rather than overwriting native omission bookkeeping later.
.np_formula_complete_training_frame <- function(frame) {
  missing.rows <- which(!stats::complete.cases(frame))
  if (!length(missing.rows)) return(frame)
  previous <- attr(frame, "na.action")
  kept <- seq_len(nrow(frame) + length(previous))
  if (length(previous)) kept <- kept[-as.integer(previous)]
  omitted <- c(as.integer(previous), kept[missing.rows])
  labels <- c(names(previous), row.names(frame)[missing.rows])
  order <- order(omitted)
  action <- structure(omitted[order], names = labels[order],
    class = if (inherits(previous, "exclude")) "exclude" else "omit")
  result <- frame[-missing.rows, , drop = FALSE]
  attr(result, "terms") <- attr(frame, "terms")
  attr(result, "na.action") <- action
  result
}

# An automatic constructor/fit transaction may hand off its frame exactly once.
# The context never travels to native/MPI leaves or remains in a stored call.
.np_formula_frame_store <- function(state, frame) {
  if (is.null(state)) return(invisible(NULL))
  if (!is.environment(state) || !identical(parent.env(state), emptyenv()) ||
      exists("frame", envir = state, inherits = FALSE))
    stop("invalid formula preparation context", call. = FALSE)
  validate <- state[["validate.training"]]
  if (!is.null(validate)) {
    if (!is.function(validate))
      stop("invalid formula training validator", call. = FALSE)
    rm("validate.training", envir = state)
    validate(frame)
  }
  state$frame <- frame
  invisible(NULL)
}

.np_formula_frame_take <- function(state) {
  if (!is.environment(state) || !exists("frame", envir = state, inherits = FALSE))
    stop("formula preparation context has no training frame", call. = FALSE)
  frame <- state$frame
  rm("frame", envir = state)
  frame
}

.np_formula_call_public <- function(mc) {
  mc$.np.formula.state <- NULL
  dots <- mc[["...", exact = TRUE]]
  if (!is.null(dots) && !is.null(names(dots))) {
    keep <- is.na(names(dots)) | names(dots) != ".np.formula.state"
    mc[["..."]] <- dots[keep]
  }
  mc
}

# Resolve formula subsets before transport or model-frame replay. Value-only
# consumers evaluate once; retained formula frames also need syntax and scope.
.np_formula_subset_inputs <- function(data, subset.expr, caller,
                                      resolve.only = FALSE) {
  # match.call expands forwarded dots to ..n references without discarding
  # their owner. Follow only that invocation's actual parent chain; never
  # search arbitrary frames for a same-named variable. substitute(list(...))
  # alone would return the final expression but lose its evaluation frame.
  while (is.symbol(subset.expr) &&
         grepl("^\\.\\.[0-9]+$", as.character(subset.expr))) {
    # ..k resolves lexically. An lapply/local callback may inherit dots from
    # an enclosing invocation rather than own the dynamically preceding call.
    while (!exists("...", envir = caller, inherits = FALSE) &&
           !identical(caller, emptyenv()))
      caller <- parent.env(caller)
    frames <- sys.frames()
    position <- which(vapply(frames, identical, logical(1L), caller))
    if (!length(position))
      stop("cannot resolve forwarded subset owner", call. = FALSE)
    # eval() can add later frames using the same environment. The first
    # activation is the function that owns these dots.
    position <- position[[1L]]
    parent <- sys.parents()[[position]]
    origin <- sys.frame(parent)
    matched <- match.call(sys.function(position), sys.call(position),
                          expand.dots = FALSE, envir = origin)
    index <- as.integer(substring(as.character(subset.expr), 3L))
    dots <- matched[["..."]]
    if (index < 1L || index > length(dots))
      stop("invalid forwarded subset position", call. = FALSE)
    subset.expr <- dots[[index]]
    caller <- origin
  }
  if (resolve.only) return(list(expression = subset.expr, envir = caller))
  # Formula variables retain their formula environment. Subset is evaluated
  # once in its data mask and original invocation environment, then supplied
  # to model.frame as a value so it cannot bind to an unrelated global.
  list(data = data, subset = eval(subset.expr, envir = data, enclos = caller))
}

.np_bws_retain_formula_training <- function(bws, frame, na.action) {
  attr(frame, ".np.na.policy") <- NULL
  subset.spec <- attr(frame, ".np.subset", exact = TRUE)
  attr(frame, ".np.subset") <- NULL
  previous <- bws[[".np.formula.training", exact = TRUE]]
  if (is.null(subset.spec)) subset.spec <- previous[["subset", exact = TRUE]]
  bws[[".np.formula.training"]] <- list(frame = frame, na.action = na.action,
                                         subset = subset.spec)
  if (!is.null(subset.spec)) bws$call["subset"] <- list(subset.spec$expression)
  bws[[".np.native.training"]] <- NULL
  call.env <- environment(bws$call)
  # An internal constructor activation is not the owner of a user's formula.
  # Retained training values make this transient frame unnecessary; preserve
  # the original formula environment for transforms and explicit-data refits.
  environment(bws$call) <- .np_call_owner_environment(
    call.env, replacement = environment(bws$formula))
  bws
}

# Project stored formula metadata, never formula expressions, into native roles.
.np_bws_formula_roles <- function(bws) {
  tt <- bws[["terms", exact = TRUE]]
  if (inherits(bws, c("bandwidth", "dbandwidth")))
    return(list(dat = .np_formula_term_names(attr(tt, "term.labels"))))
  if (inherits(bws, c("conbandwidth", "condbandwidth")))
    return(list(xdat = bws$variableNames[["terms"]],
                ydat = bws$variableNames[["response"]]))
  # A transformed scalar response has its full term label, not all.vars().
  variable <- attr(tt, "variables")[[2L]]
  response <- if (is.symbol(variable)) as.character(variable) else
    paste(deparse(variable, width.cutoff = 500L), collapse = "")
  chromoly <- bws[["chromoly", exact = TRUE]]
  if (inherits(bws, c("plbandwidth", "scbandwidth"))) {
    out <- list(xdat = .np_formula_term_names(chromoly[[2L]]), ydat = response)
    if (length(chromoly) == 3L) out$zdat <- .np_formula_term_names(chromoly[[3L]])
    return(out)
  }
  list(xdat = .np_formula_term_names(attr(tt, "term.labels")), ydat = response)
}

.np_bws_retain_formula_roles <- function(bws, training) {
  roles <- .np_bws_formula_roles(bws)
  if (!setequal(names(roles), names(training)))
    stop("incomplete retained formula training roles", call. = FALSE)
  retained <- bws[[".np.formula.training", exact = TRUE]]
  old <- retained[["frame", exact = TRUE]]
  columns <- list()
  rows <- NULL
  for (role in names(roles)) {
    values <- toFrame(training[[role]])
    if (ncol(values) != length(roles[[role]]))
      stop("retained formula training role has incompatible columns", call. = FALSE)
    if (is.null(rows)) rows <- row.names(values)
    if (nrow(values) != length(rows))
      stop("retained formula training roles have incompatible rows", call. = FALSE)
    for (j in seq_along(roles[[role]])) {
      name <- roles[[role]][[j]]
      if (name %in% names(columns) && !identical(columns[[name]], values[[j]]))
        stop("overlapping formula training roles disagree", call. = FALSE)
      columns[[name]] <- values[[j]]
    }
  }
  if (!is.null(old) && setequal(names(old), names(columns)) &&
      identical(row.names(old), rows) &&
      all(vapply(names(old), function(name) identical(old[[name]], columns[[name]]), logical(1L)))) {
    bws[[".np.native.training"]] <- NULL
    return(bws)
  }
  tt <- if (inherits(bws, "plbandwidth")) .np_plreg_formula_terms(bws) else bws$terms
  column.order <- if (!is.null(old)) names(old) else
    vapply(as.list(attr(tt, "variables"))[-1L], function(x)
      paste(deparse(x, width.cutoff = 500L), collapse = ""), character(1L))
  if (!setequal(column.order, names(columns)))
    stop("retained formula training columns do not match terms", call. = FALSE)
  columns <- columns[column.order]
  frame <- as.data.frame(columns, optional = TRUE, row.names = rows)
  # Native replacement inputs already represent the formula's evaluated terms.
  # Retain their complete cases without replaying old subset/NA expressions.
  attr(frame, "terms") <- tt
  frame <- stats::na.omit(frame)
  policy <- if (is.null(retained)) stats::na.omit else retained[["na.action", exact = TRUE]]
  .np_bws_retain_formula_training(bws, frame, policy)
}

# A partial response replacement conditions on the already-selected design.
# Its rows therefore index the retained frame, never the original data before
# subset/NA selection. Do not replay expressions or infer ownership from length.
.np_formula_replace_response <- function(frame, bws, response, overrides,
                                         data.override = FALSE,
                                         matrix.response = FALSE) {
  if (data.override)
    stop("partial response replacement cannot be combined with 'data'; put the replacement response in 'data' instead",
         call. = FALSE)
  roles <- .np_bws_formula_roles(bws)
  columns <- roles[["ydat"]]
  if (!length(columns) || !all(columns %in% names(frame)))
    stop("partial response replacement requires retained response columns", call. = FALSE)
  if (length(intersect(columns, unlist(roles[names(roles) != "ydat"], use.names = FALSE))))
    stop("a response used as a predictor requires full 'data' replacement", call. = FALSE)
  if (NROW(response) != nrow(frame))
    stop("replacement response must have one row per retained training observation, in retained-sample order; use 'data' to replace the original sample",
         call. = FALSE)
  if (isTRUE(matrix.response) && length(columns) == 1L && is.matrix(response)) {
    frame[[columns]] <- I(response)
  } else {
    values <- toFrame(response)
    if (ncol(values) != length(columns))
      stop("replacement response has an incompatible number of columns", call. = FALSE)
    for (j in seq_along(columns)) frame[[columns[[j]]]] <- values[[j]]
  }
  retained <- bws[[".np.formula.training", exact = TRUE]]
  policy <- if ("na.action" %in% names(overrides)) overrides[["na.action"]] else
    if (!is.null(retained)) retained[["na.action", exact = TRUE]] else {
      effective <- attr(frame, ".np.na.policy", exact = TRUE)
      if (!is.null(effective)) effective[[1L]] else getOption("na.action", stats::na.omit)
    }
  if (is.character(policy))
    policy <- get(policy, envir = asNamespace("stats"), mode = "function")
  tt <- attr(frame, "terms")
  attr(frame, "na.action") <- NULL
  if (!is.null(policy)) frame <- policy(frame)
  attr(frame, "terms") <- tt
  frame <- .np_formula_complete_training_frame(frame)
  attr(frame, ".np.na.policy") <- list(policy)
  frame
}

.np_bws_retain_fit_frame <- function(bws, frame) {
  retained <- bws[[".np.formula.training", exact = TRUE]]
  effective <- attr(frame, ".np.na.policy", exact = TRUE)
  policy <- if (!is.null(effective)) effective[[1L]] else if (is.null(retained)) getOption("na.action", stats::na.omit) else
    retained[["na.action", exact = TRUE]]
  .np_bws_retain_formula_training(bws, frame, policy)
}

.np_formula_default_call <- function(call, definition, caller, required.training = NULL) {
  # Name the bandwidth before changing the callee. Keep the remaining call
  # shape: an unnamed formula beside bws= belongs to the formula dispatcher,
  # not to an explicitly named native training argument.
  call <- .np_formula_expand_call(call, caller)
  matched <- match.call(definition = definition, call = call, expand.dots = TRUE)
  # Progress inspection and selector replay must use the first realization of
  # ordinary controls. Data/formula roles keep their dedicated handoff owners.
  controls <- setdiff(names(matched), c("", "bws", "formula", "data", "subset",
    "call", "na.action", "tdat", "txdat", "tydat", "tzdat", "dat", "xdat",
    "ydat", "zdat", "newdata", "exdat", "eydat", "ezdat", "edat"))
  if (length(controls)) {
    tagged <- call
    for (i in seq.int(2L, length(tagged))) tagged[[i]] <- i
    positions <- match.call(definition, tagged, expand.dots = TRUE)
    owner <- parent.frame()
    dots <- eval(quote(substitute(list(...))), envir = owner)[-1L]
    for (name in controls) {
      if (name %in% names(formals(definition))) {
        value <- get(name, envir = owner, inherits = FALSE)
      } else {
        index <- match(name, names(dots), nomatch = 0L)
        if (!index) next
        value <- eval(substitute(...elt(I), list(I = index)), envir = owner)
      }
      call[positions[[name]]] <- list(if (is.language(value))
        substitute(quote(VALUE), list(VALUE = value)) else value)
    }
  }
  if (!"bws" %in% names(matched) || "bws" %in% names(call)) return(call)
  labels <- names(call)
  if (is.null(labels)) labels <- rep.int("", length(call))
  labels[is.na(labels)] <- ""
  partial <- which(nzchar(labels) & startsWith("bws", labels))
  if (!length(partial)) {
    # These defaults also accept data-first calls (e.g. npudens(x) or
    # npreg(x, y)). Matching the fitter's first formal does not by itself
    # establish bandwidth ownership: its selector has a data-first NULL
    # method. Preserve that syntax unless the training slots establish bws.
    training <- if (is.null(required.training))
      grep("^t(x|y|z)?dat$", names(formals(definition)), value = TRUE) else required.training
    first <- training[[1L]]
    first.named <- any(nzchar(labels) &
      (startsWith(first, labels) | labels == substring(first, 2L)))
    complete <- all(training %in% names(matched))
    formula.after <- !first.named && !complete && first %in% names(matched) &&
      inherits(get(first, envir = parent.frame(), inherits = FALSE), "formula")
    if (!first.named && !complete && !formula.after && !"formula" %in% labels)
      return(call)
  }
  index <- if (length(partial)) partial[[1L]] else which(labels[-1L] == "")[[1L]] + 1L
  labels[[index]] <- "bws"
  names(call) <- labels
  call
}

.np_bws_formula_model_frame <- function(bws, mf.args, data.override = FALSE,
                                        overrides = list()) {
  if (inherits(bws, c("conbandwidth", "condbandwidth")))
    .np_formula_validate_syntax(bws$formula, conditional.response = TRUE)
  training <- bws[[".np.formula.training", exact = TRUE]]
  if (!is.null(training)) {
    if (!is.list(training) || !is.data.frame(training[["frame", exact = TRUE]]) ||
        !("na.action" %in% names(training)))
      stop("invalid retained formula training state", call. = FALSE)
    if (!data.override)
      return(.np_formula_complete_training_frame(training[["frame", exact = TRUE]]))
    mf.args["na.action"] <- training["na.action"]
    mf.args$.np.subset <- training[["subset", exact = TRUE]]
  }
  # Compatibility for objects saved before training frames were retained.
  # Their original values cannot be reconstructed after caller rebinding.
  call.env <- environment(bws$call)
  if (is.null(training) && is.environment(call.env)) {
    # These are the value arguments extracted from the saved model-frame call.
    # Resolve them lazily in their original owner, including forwarded ..n
    # promises. Formula variables and subset still use the formula/data mask.
    # eval is necessary because do.call below runs in the formula environment,
    # which need not contain the constructor's data or NA-action bindings.
    owned <- intersect(c(if (!data.override) "data", "na.action"), names(mf.args))
    for (name in owned) {
      if (is.language(mf.args[[name]]))
        mf.args[[name]] <- substitute(base::eval(quote(EXPR), envir = OWNER),
                                     list(EXPR = mf.args[[name]], OWNER = call.env))
    }
  }
  if (data.override && "na.action" %in% names(overrides))
    mf.args["na.action"] <- overrides["na.action"]
  capture <- new.env(parent = emptyenv())
  mf.args$.np.capture <- capture
  frame <- do.call(.np_formula_model_frame, mf.args,
                  envir = environment(mf.args[["formula"]]))
  # Invocation-local metadata carries even an explicit NULL policy. The
  # retention owner strips this attribute from the stored model frame.
  attr(frame, ".np.na.policy") <- list(capture$na.action)
  frame
}

# Partially linear formulas have two retained role terms but one observation
# set. Keep the joint frame invocation-local and project trained metadata back
# into the existing terms/xterms fields.
.np_plreg_formula_spec <- function(formula, chromoly = NULL) {
  if (is.null(chromoly))
    chromoly <- explodePipe(formula, env = environment(formula))
  if (length(chromoly) != 3L)
    stop("invoked with improper formula, please see npplregbw documentation for proper use")
  bronze <- vapply(chromoly, paste, character(1L), collapse = " + ")
  make <- function(text) terms(as.formula(text, env = environment(formula)))
  list(chromoly = chromoly,
       terms = make(paste(bronze[[1L]], "~", bronze[[3L]])),
       xterms = make(paste("~", bronze[[2L]])),
       joint = make(paste(bronze[[1L]], "~", bronze[[2L]], "+", bronze[[3L]])))
}

.np_plreg_formula_indices <- function(role, joint) {
  variables <- as.list(attr(role, "variables"))[-1L]
  combined <- as.list(attr(joint, "variables"))[-1L]
  vapply(variables, function(variable) {
    index <- which(vapply(combined, identical, logical(1L), variable))
    if (length(index) != 1L)
      stop("inconsistent partially linear formula variables", call. = FALSE)
    index
  }, integer(1L))
}

.np_plreg_formula_prediction <- function(role, joint) {
  prediction <- .np_formula_unwrap_prediction(role)$prediction
  variables <- attr(role, "variables")
  if (is.call(prediction) && identical(prediction[[1L]], quote(list)) &&
      length(prediction) == length(variables))
    return(list(prediction = prediction, scaffold = NULL))

  # Old plreg objects wrapped the full joint alignment in an exact role
  # subset. Recognize that package-owned shape only; user prediction calls
  # and trained transform expressions are never stripped heuristically.
  if (is.call(prediction) && length(prediction) == 5L &&
      identical(prediction[[1L]], quote(`[`))) {
    inner <- prediction[[2L]]
    indices <- .np_plreg_formula_indices(role, joint)
    expected <- substitute(INNER[, INDEX, drop = FALSE],
                           list(INNER = inner, INDEX = indices))
    if (identical(prediction, expected)) {
      aligned <- inner
      if (is.call(aligned) && length(aligned) == 2L &&
          identical(aligned[[1L]], quote(`(`)))
        aligned <- aligned[[2L]]
      temporary <- joint
      attr(temporary, "predvars") <- aligned
      unwrapped <- .np_formula_unwrap_prediction(temporary)$prediction
      if (!identical(aligned, unwrapped) &&
          identical(unwrapped, attr(joint, "variables")))
        return(list(prediction = variables, scaffold = inner))
    }
  }
  stop("unsupported partially linear prediction metadata", call. = FALSE)
}

.np_plreg_formula_terms <- function(bws) {
  spec <- .np_plreg_formula_spec(bws$formula, bws$chromoly)
  joint <- spec$joint
  roles <- list(bws$terms, bws$xterms)
  predictions <- lapply(roles, .np_plreg_formula_prediction, joint = joint)
  if (!is.null(predictions[[1L]]$scaffold) &&
      !is.null(predictions[[2L]]$scaffold) &&
      !identical(predictions[[1L]]$scaffold, predictions[[2L]]$scaffold))
    stop("inconsistent partially linear alignment metadata", call. = FALSE)
  combined <- attr(joint, "variables")
  seen <- rep_len(FALSE, length(combined) - 1L)
  for (i in seq_along(roles)) {
    indices <- .np_plreg_formula_indices(roles[[i]], joint)
    prediction <- predictions[[i]]$prediction
    for (j in seq_along(indices)) {
      index <- indices[[j]]
      if (seen[[index]] && !identical(combined[[index + 1L]], prediction[[j + 1L]]))
        stop("inconsistent partially linear trained prediction terms", call. = FALSE)
      combined[[index + 1L]] <- prediction[[j + 1L]]
      seen[[index]] <- TRUE
    }
  }
  if (!all(seen))
    stop("incomplete partially linear formula metadata", call. = FALSE)
  attr(joint, "predvars") <- combined
  joint
}

.np_plreg_formula_split <- function(frame, terms, xterms) {
  joint <- attr(frame, "terms")
  split.role <- function(role) {
    indices <- .np_plreg_formula_indices(role, joint)
    out <- frame[, indices, drop = FALSE]
    attr(role, "predvars") <- as.call(c(list(quote(list)),
      as.list(attr(joint, "predvars"))[indices + 1L]))
    classes <- attr(joint, "dataClasses")
    if (!is.null(classes)) attr(role, "dataClasses") <- classes[indices]
    attr(out, "terms") <- role
    attr(out, "na.action") <- attr(frame, "na.action")
    out
  }
  list(yz = split.role(terms), x = split.role(xterms))
}

.np_plreg_formula_frame <- function(bws, data = NULL, overrides = list()) {
  m <- match(c("formula", "data", "subset", "na.action"),
             names(bws$call), nomatch = 0L)
  args <- as.list(bws$call[c(1L, m)])[-1L]
  args$formula <- .np_plreg_formula_terms(bws)
  if (!is.null(data)) args$data <- data
  .np_bws_formula_model_frame(bws, args, data.override = !is.null(data), overrides = overrides)
}

.np_plreg_formula_training <- function(bws, data = NULL) {
  frame <- .np_plreg_formula_frame(bws, data)
  roles <- .np_plreg_formula_split(frame, bws$terms, bws$xterms)
  list(txdat = roles$x, tydat = model.response(roles$yz),
       tzdat = roles$yz[, .np_formula_term_names(bws$chromoly[[3L]]), drop = FALSE])
}

# Stored public calls are syntax, not a second evaluation of user arguments.
.np_public_replay_call <- function(call, generic, frame = parent.frame()) {
  call <- .np_formula_call_public(call)
  if ("na.action" %in% c(names(call), names(call[["..."]]))) {
    if ("na.action" %in% names(call) &&
        exists("na.action", envir = frame, inherits = FALSE) &&
        .np_na_promise_missing("na.action", frame))
      call[["na.action"]] <- NULL
    call <- .np_omit_missing_na_call(call, frame)
  }
  parts <- as.list(call)
  index <- match("...", names(parts), nomatch = 0L)
  if (index) {
    parts <- c(parts[seq_len(index - 1L)], as.list(parts[[index]]),
      if (index < length(parts)) parts[seq.int(index + 1L, length(parts))])
  }
  parts[[1L]] <- as.call(list(as.name("::"), as.name("np"),
                             as.name(generic)))
  out <- as.call(parts)
  for (name in setdiff(names(attributes(call)), "names"))
    attr(out, name) <- attr(call, name, exact = TRUE)
  out
}

Try the np package in your browser

Any scripts or data that you put into this service are public.

np documentation built on Nov. 25, 2026, 1:06 a.m.