R/generics_predict.R

Defines functions predict.felm predict.feglm

# srr_stats
# {G1.0} Implements `predict` methods for `feglm` and `felm` objects, similar to base R methods.
# {G2.1a} Ensures input objects are of the expected class (`feglm` or `felm`).
# {G2.3a} Provides options for output type (`link`, `response`, or `terms`) via a standardized `type` argument.
# {G2.3b} Handles missing or invalid new data gracefully with appropriate checks and error messages.
# {G3.1a} Computes predicted values for both new and existing data sets.
# {G3.1b} Supports fixed-effects predictions by accounting for levels in the data.
# {G3.4a} Includes an option for type-specific predictions (e.g., `link` vs. `response`).
# {G5.2a} Tests include validation of predictions against known values and edge cases.
# {G5.4a} Outputs predictions in a format compatible with standard R workflows.
# {RE4.9} The predicted values for the model data or new data are returned as a vector with `predict()`.
# {RE4.16} The fixed effects are passed to the `predict()` function to add the group-specific effects to the predictions.
# {RE5.0} Ensures computational efficiency in handling both `feglm` and `felm` prediction workflows.
# {RE5.2} Integrates seamlessly with user-provided data for generating predictions.
# {RE5.3} Provides predictable and consistent output types for downstream analysis.

#' @title Predict method for 'feglm' objects
#' @description Similar to the 'predict' method for 'glm' objects but returns response predictions as default.
#' @export
#' @noRd
predict.feglm <- function(
  object,
  newdata = NULL,
  type = c("response", "link"),
  ...
) {
  type <- match.arg(type)

  if (!is.null(newdata)) {
    check_data_(newdata)

    # For prediction, we only need the RHS variables, not the response
    # Extract predictor variables and fixed effects
    formula <- object$formula
    k_vars <- attr(terms(formula, rhs = 2L), "term.labels")

    # Fixed effects must have been stored to predict on new data
    if (length(k_vars) > 0 && is.null(object[["fixed_effects"]])) {
      stop(
        "Model has fixed effects but they were not stored. ",
        "Refit with `control = fit_control(return_fe = TRUE)` to enable ",
        "out-of-sample predictions.",
        call. = FALSE
      )
    }

    # Get all variables needed (predictors + fixed effects)
    pred_vars <- attr(terms(formula, rhs = 1L), "term.labels")
    all_vars <- c(pred_vars, k_vars)

    # Extract and prepare data
    data <- newdata[, all_vars, drop = FALSE]

    # Keep track of original row indices for NA handling
    original_rows <- seq_len(nrow(data))

    # Remove rows with NA in any required variable
    complete_cases <- complete.cases(data)
    if (!all(complete_cases)) {
      data <- data[complete_cases, , drop = FALSE]
      original_rows <- original_rows[complete_cases]
    }

    # Transform fixed effects to factors
    data <- transform_fe_(data, formula, k_vars)

    # Create design matrix from predictors only
    # Use fast path when all predictors are simple numeric columns
    if (length(pred_vars) > 0) {
      pred_cols_exist <- pred_vars[pred_vars %in% colnames(data)]

      # Get columns for numeric check
      all_numeric <- length(pred_cols_exist) == length(pred_vars) &&
        all(vapply(data[pred_cols_exist], is.numeric, logical(1)))

      has_special <- any(grepl("factor|poly|ns\\(|bs\\(|strata|I\\(", pred_vars))
      has_interaction <- any(grepl(":", pred_vars, fixed = TRUE))

      if (all_numeric && !has_special && !has_interaction) {
        # Fast path: extract numeric columns directly via vapply
        X <- vapply(pred_vars, function(v) data[[v]], FUN.VALUE = numeric(nrow(data)))
        if (!is.matrix(X)) X <- matrix(X, ncol = 1L)
        nms_sp <- pred_vars
      } else {
        # Slow path: use model.matrix for complex formulas
        pred_formula <- reformulate(pred_vars, response = NULL)
        X <- model.matrix(pred_formula, data = data)[, -1, drop = FALSE]
        nms_sp <- colnames(X)
      }
    } else {
      X <- matrix(0, nrow = nrow(data), ncol = 0)
      nms_sp <- character(0)
    }

    # Check if model has an intercept (no fixed effects case)
    # When there are no FEs, C++ adds an intercept, so coef_table includes "(Intercept)"
    coef_table <- object$coef_table
    has_intercept <- "(Intercept)" %in% rownames(coef_table)
    if (has_intercept) {
      # Add intercept column to X
      X <- cbind(1, X)
    }

    fes <- object[["fixed_effects"]]
    fes_names <- names(fes)

    # Fixed effects are normalized (first level = 0) in get_alpha()
    # C++ fitted values use these normalized FEs via accumulate_fixed_effects()
    # So predictions should also use normalized FEs directly
    # Compute FE contribution directly without storing intermediate vectors
    # This avoids K separate N-length allocations for fes2
    fe_contrib <- numeric(nrow(data))
    for (name in fes_names) {
      fe_vec <- fes[[name]]
      matched_idx <- match(data[[name]], names(fe_vec))
      # Add contribution in-place: missing levels contribute 0
      fe_contrib <- fe_contrib + ifelse(is.na(matched_idx), 0, fe_vec[matched_idx])
    }

    # Replace NA coefficients with 0 for prediction
    coef0 <- coef_table[, 1]
    coef0[is.na(coef0)] <- 0

    if (length(fes) > 0) {
      eta <- X %*% coef0 + fe_contrib
    } else {
      eta <- X %*% coef0
    }

    # Add offset if present in the model
    # Re-evaluate the offset on newdata using the original offset specification
    if (!is.null(object[["offset_spec"]])) {
      offset_spec <- object[["offset_spec"]]
      if (inherits(offset_spec, "formula")) {
        # Evaluate offset formula on newdata
        offset_vars <- attr(terms(offset_spec, data = newdata), "term.labels")
        offset_newdata <- eval(parse(text = offset_vars), envir = newdata)
      } else if (is.numeric(offset_spec)) {
        # If offset was originally a vector, we need it to match newdata length
        if (length(offset_spec) == nrow(data)) {
          offset_newdata <- offset_spec[rownames(data)]
        } else {
          stop(
            "Cannot apply numeric offset to newdata: length mismatch.",
            call. = FALSE
          )
        }
      } else {
        offset_newdata <- rep(0.0, nrow(data))
      }
      eta <- eta + offset_newdata
    }

    # Handle NA values - create result vector with NAs where appropriate
    result_eta <- rep(NA_real_, nrow(newdata))
    result_eta[original_rows] <- as.vector(eta)
    eta <- result_eta
  } else {
    eta <- object[["eta"]]
  }

  if (type == "response") {
    fam <- object[["family"]]
    eta <- fam[["linkinv"]](eta)
  }

  # Convert to vector and assign names
  eta <- as.vector(eta)
  # Prefer row names from the prediction data (or original object), fall back to sequential
  if (!is.null(newdata)) {
    rn <- rownames(newdata) # Use original newdata rownames, not filtered data
  } else {
    # Use stored rownames (works even when keep_data = FALSE)
    rn <- object[[".rownames"]]
    if (is.null(rn) && !is.null(object$data)) {
      rn <- attr(object$data, ".rownames")
      if (is.null(rn)) rn <- rownames(object$data)
    }
  }
  if (!is.null(rn)) {
    names(eta) <- rn
  } else {
    names(eta) <- seq_along(eta)
  }

  eta
}

#' @title Predict method for 'felm' objects
#' @description Similar to the 'predict' method for 'lm' objects
#' @export
#' @noRd
predict.felm <- function(
  object,
  newdata = NULL,
  type = c("response", "terms"),
  ...
) {
  type <- match.arg(type)

  if (!is.null(newdata)) {
    check_data_(newdata)

    # For prediction, we only need the RHS variables, not the response
    # Extract predictor variables and fixed effects
    formula <- object$formula

    # Check if model has fixed effects (rhs = 2)
    has_fe <- length(formula)[2] >= 2

    if (has_fe) {
      fe_names <- attr(terms(formula, rhs = 2L), "term.labels")
    } else {
      fe_names <- character(0)
    }

    # Fixed effects must have been stored to predict on new data
    if (length(fe_names) > 0 && is.null(object[["fixed_effects"]])) {
      stop(
        "Model has fixed effects but they were not stored. ",
        "Refit with `control = fit_control(return_fe = TRUE)` to enable ",
        "out-of-sample predictions.",
        call. = FALSE
      )
    }

    # Get all variables needed (predictors + fixed effects)
    pred_vars <- attr(terms(formula, rhs = 1L), "term.labels")
    all_vars <- c(pred_vars, fe_names)

    # Extract and prepare data
    data <- newdata[, all_vars, drop = FALSE]

    # Keep track of original row indices for NA handling
    original_rows <- seq_len(nrow(data))

    # Remove rows with NA in any required variable
    complete_cases <- complete.cases(data)
    if (!all(complete_cases)) {
      data <- data[complete_cases, , drop = FALSE]
      original_rows <- original_rows[complete_cases]
    }

    # Transform fixed effects to factors
    data <- transform_fe_(data, formula, fe_names)

    # Create design matrix from predictors only
    if (length(pred_vars) > 0) {
      pred_formula <- reformulate(pred_vars, response = NULL)
      X <- model.matrix(pred_formula, data = data)[, -1, drop = FALSE]
    } else {
      X <- matrix(0, nrow = nrow(data), ncol = 0)
    }

    # Check if model has an intercept (no fixed effects case)
    # When there are no FEs, C++ adds an intercept, so coef_table includes "(Intercept)"
    coef_table <- object$coef_table
    has_intercept <- "(Intercept)" %in% rownames(coef_table)
    if (has_intercept) {
      # Add intercept column to X
      X <- cbind(1, X)
    }

    fes <- object[["fixed_effects"]]
    fes_names <- names(fes)

    # Denormalize fixed effects for prediction
    # The FEs were normalized so fe1[1] = 0
    # To match lm() predictions, we don't need to denormalize
    # because lm() also uses one level as reference (sets it to 0)
    # The normalization is already compatible with lm()'s reference level approach

    fes2 <- setNames(
      lapply(fes_names, function(name) {
        # Match values and handle missing levels
        matched_values <- fes[[name]][match(data[[name]], names(fes[[name]]))]
        matched_values[is.na(matched_values)] <- 0 # Set missing levels to 0
        matched_values
      }),
      fes_names
    )

    # Replace NA coefficients with 0 for prediction
    coef0 <- coef_table[, 1]
    coef0[is.na(coef0)] <- 0

    if (length(fes) > 0) {
      y <- X %*% coef0 + Reduce("+", fes2)
    } else {
      y <- X %*% coef0
    }

    # Add offset if present (felm typically doesn't use offsets, but handle for consistency)
    if (!is.null(object[["offset_spec"]])) {
      offset_spec <- object[["offset_spec"]]
      if (inherits(offset_spec, "formula")) {
        offset_vars <- attr(terms(offset_spec, data = newdata), "term.labels")
        offset_newdata <- eval(parse(text = offset_vars), envir = newdata)
        y <- y + offset_newdata
      }
    }

    # Handle NA values - create result vector with NAs where appropriate
    result <- rep(NA_real_, nrow(newdata))
    result[original_rows] <- as.vector(y)
    y <- result
  } else {
    y <- object[["fitted_values"]]
  }

  # Ensure y is a vector and assign names
  y <- as.vector(y)
  # Prefer row names from the prediction data (or original object), fall back to sequential
  if (!is.null(newdata)) {
    rn <- rownames(newdata) # Use original newdata rownames, not filtered data
  } else {
    rn <- attr(object$data, ".rownames")
    if (is.null(rn)) rn <- rownames(object$data)
  }
  if (!is.null(rn)) {
    names(y) <- rn
  } else {
    names(y) <- seq_along(y)
  }

  y
}

Try the capybara package in your browser

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

capybara documentation built on June 29, 2026, 5:07 p.m.