residual_analysis

Function residual_analysis 

Source
pub fn residual_analysis<F, S1, S2, D1, D2>(
    y_true: &ArrayBase<S1, D1>,
    y_pred: &ArrayBase<S2, D2>,
    x: Option<&Array2<F>>,
    hat_matrix: Option<&Array2<F>>,
) -> Result<ResidualAnalysis<F>>
where F: Float + NumCast + Debug + FromPrimitive + 'static, S1: Data<Elem = F>, S2: Data<Elem = F>, D1: Dimension, D2: Dimension,
Expand description

Performs comprehensive residual analysis for a regression model

§Arguments

  • y_true - Ground truth (correct) target values
  • y_pred - Estimated target values
  • x - Optional predictor variables matrix (needed for some diagnostics)
  • hat_matrix - Optional hat/projection matrix (can be provided to avoid recalculation)

§Returns

  • A ResidualAnalysis struct containing various residual diagnostics

§Examples

use scirs2_core::ndarray::{array, Array2};
use scirs2_metrics::regression::residual_analysis;

let y_true = array![3.0, -0.5, 2.0, 7.0, 5.0, 8.0, 1.0, 4.0];
let y_pred = array![2.5, 0.0, 2.0, 8.0, 4.5, 7.5, 1.5, 3.5];

// Create dummy X matrix (features matrix) with 2 predictors
let x = Array2::from_shape_fn((8, 2), |(i, j)| i as f64 + j as f64);

let analysis = residual_analysis(&y_true, &y_pred, Some(&x), None).unwrap();

// Access various diagnostics
println!("Durbin-Watson statistic: {}", analysis.durbin_watson);
println!("Number of residuals: {}", analysis.residuals.len());