Skip to main content

predict_simple_intervals

Function predict_simple_intervals 

Source
pub fn predict_simple_intervals(
    model: &SimpleRegressionResult,
    x_new: &[f64],
    level: f64,
) -> Option<Vec<RegressionPrediction>>
Expand description

Predictions of a simple regression at x_new, each with the confidence interval of the mean response and the prediction interval of a new observation at two-sided level (Student’s t on n − 2 degrees of freedom).

Var(ŷ₀) = SE(β₀)² + x₀² SE(β₁)² + 2 x₀ Cov(β₀, β₁) = MSE (1/n + (x₀ − x̄)²/Sxx).

§Returns

None when level is not strictly between 0 and 1, or an x_new value is not finite.

§References

Montgomery, Peck & Vining (2012). “Introduction to Linear Regression Analysis”, 5th edition, §2.4.2 and §2.5.

§Examples

use u_analytics::regression::{predict_simple_intervals, simple_linear_regression};

let x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let y = [2.2, 3.9, 6.1, 8.2, 9.8, 12.1];
let model = simple_linear_regression(&x, &y).unwrap();
let p = predict_simple_intervals(&model, &[3.5, 10.0], 0.95).unwrap();
// Wider for a new observation than for the mean, and wider away from x̄.
assert!(p[0].prediction_upper - p[0].prediction_lower > p[0].confidence_upper - p[0].confidence_lower);
assert!(p[1].se_fit > p[0].se_fit);