R/beta_ols.R

Defines functions beta_ols

Documented in beta_ols

#' @title Efficient OLS Estimation
#'
#' @description Computes OLS coefficients using \code{.lm.fit} for computational efficiency.
#' If \code{Y} is a matrix, applies OLS column-wise.
#'
#' @param X A numeric matrix of regressors (T × k).
#' @param Y A numeric matrix or vector of responses (T × N or T × 1).
#'
#' @return A numeric matrix of coefficients.
#'
#' @importFrom stats .lm.fit
#'
#' @keywords internal
beta_ols <- function(X, Y) {
  if (is.vector(Y)) {
    return(matrix(stats::.lm.fit(X, Y)$coefficients, ncol = 1))
  } else if (is.matrix(Y)) {
    return(apply(Y, 2, function(y) stats::.lm.fit(X, y)$coefficients))
  } else {
    stop("Y must be a numeric vector or matrix.")
  }
}

Try the FARS package in your browser

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

FARS documentation built on Feb. 17, 2026, 5:06 p.m.