R/felm_helpers.R

Defines functions get_score_matrix_felm_

# srr_stats
# {G1.0} Provides modular helper functions for computations in linear models with fixed effects.

#' @title Get score matrix
#' @description Computes the score matrix
#' @param object Result list
#' @noRd
get_score_matrix_felm_ <- function(object) {
  # Update weights and dependent variable
  y <- object[["data"]][[1L]]

  # Center regressor matrix (if required)
  if (object[["control"]][["keep_tx"]]) {
    tx <- object[["tx"]]
  } else {
    # Generate flat FE codes to project out the fixed effects
    k_list <- get_index_list_(object[["nms_fe"]], object[["data"]])

    # Extract regressor matrix
    X <- model.matrix(object[["formula"]], object[["data"]], rhs = 1L)[, -1L, drop = FALSE]
    nms_sp <- attr(X, "dimnames")[[2L]]
    attr(X, "dimnames") <- NULL

    # Center variables
    tx <- center_variables_(
      X, object[["weights"]], k_list[["codes"]],
      object[["control"]][["center_tol"]],
      object[["control"]][["iter_center_max"]]
    )
    colnames(tx) <- nms_sp
  }

  # Return score matrix
  tx * (y * object[["weights"]])
}

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.