R/orthogonalize_factors.R

Defines functions orthogonalize_factors

Documented in orthogonalize_factors

#' Orthogonalize factors
#' @keywords internal
#' 
#'@importFrom stats lm
#' 
orthogonalize_factors <- function(X) {
  n_factors <- ncol(X)  
  
  X <- Re(X)
  
  # Iterative orthogonalization
  for (i in 1:n_factors) {
    # Regress the i-th factor on all other factors (excluding itself)
    other_factors <- X[, -i, drop = FALSE]  
    model <- lm(X[, i] ~ other_factors)
    X[, i] <- residuals(model)
  }
  
  return(X)
}

Try the FARS package in your browser

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

FARS documentation built on Aug. 8, 2025, 7:33 p.m.