libena 0.4.18-dev.9021
QE C++ library
Loading...
Searching...
No Matches
generalized_rotation.hpp File Reference

Generalized Means Rotation (GMR) with Lasso-based covariate adjustment. More...

#include <libqe/linalg_fallback.hpp>
#include <libqe/lasso.hpp>
#include "rotation.hpp"
#include <armadillo>
#include <stdexcept>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

struct  qe::GMRStepResult
 Result of one GMR axis step. More...
 
struct  qe::GeneralizedRotationParams
 Parameters for generalized_means_rotation(). More...
 

Namespaces

namespace  qe
 

Functions

Between-group scatter
arma::mat qe::between_group_scatter (const arma::mat &V, const arma::uvec &labels, arma::uword n_groups)
 Compute the between-group scatter matrix S_B.
 
Rotation direction
arma::vec qe::gmr_direction (const arma::mat &Vx, const arma::vec &target, bool categorical, arma::uword n_groups=0)
 Compute the unit rotation direction from a covariate-adjusted contribution matrix.
 
GMR axis step
GMRStepResult qe::gmr_step (const arma::mat &V, const arma::mat &X_model, const arma::vec &target, const arma::uvec &x1_cols, bool categorical, arma::uword n_groups, const arma::uvec &subset, int n_lambda=50, int k_folds=5, double lasso_eps=0.01)
 Compute the rotation direction for one axis of the Generalized Means Rotation.
 
Generalized Means Rotation
RotationResult qe::generalized_means_rotation (const arma::mat &V, const GeneralizedRotationParams &params)
 Full Generalized Means Rotation (GMR) producing a complete q×q rotation matrix.
 

Detailed Description

Generalized Means Rotation (GMR) with Lasso-based covariate adjustment.

Mirrors rENA's ena.rotate.by.generalized() + gmr() + get_x1_main_effect(). Authoritative reference: commit 46776a1981a90b3a3b2861ed1010e9dbb7acf901 ("Fixed bugs in rotation functions") by Zhiqiang Cai.

Algorithm overview

  1. Compute x_vector: the rotation axis that best represents the target variable's contribution to the ENA point space, after controlling for covariates via Lasso (lasso_x1_contribution).
  2. Deflate V by x_vector → defA = V - V*x*x'.
  3. Optionally compute secondary axis x1: the normalised group-mean difference in the original V space (not defA) for groups 0 vs 1. x1 stays empty when the target is not categorical or there are not exactly 2 groups — there is no SVD fallback.
  4. Compute y_vector from a GMR call on defA, or from SVD of defA. Explicitly orthogonalise y_vector against x_vector, and against x1 when x1 is available.
  5. Double-deflate V by x_vector and y_vector, then call complete_rotation() to fill remaining dimensions from the SVD of the doubly-deflated space.

Caller responsibilities

  • Build the model matrix (X_raw) from the data frame, including dummy variables for categorical predictors and any desired interaction terms. No formula parsing is done here.
  • Identify x1_cols: the 0-based column indices in X_raw that correspond to the target variable (these are unpenalized in the Lasso).
  • Encode categorical targets as 0-based integer codes in x_target.
  • Set x_n_groups to the number of distinct groups when x_categorical=true.

The R wrapper (rENA::ena.rotate.by.generalized) handles these steps and calls libqe::generalized_means_rotation() with the prepared matrices.