Skip to main content

r2rs_stats/regression/glm/
test.rs

1// "Whatever you do, work at it with all your heart, as working for the Lord,
2// not for human masters, since you know that you will receive an inheritance
3// from the Lord as a reward. It is the Lord Christ you are serving."
4// (Col 3:23-24)
5
6use std::error::Error;
7
8use strafe_trait::{
9    Assumption, Concept, Conclusion, InsignificantCoefficient, NoSignificantFeature,
10    NormalResiduals, SignificantCoefficient, SomeSignficantFeature, StatisticalTest,
11};
12
13use crate::{
14    regression::glm::{family::core::Family, link::core::Link, GeneralizedLinearRegression},
15    tests::{
16        NormalResidualStatistic, NormalResidualTest, SignificanceOfRegressionStatistic,
17        SignificanceOfRegressionTest, ZCoefficient, ZCoefficientBuilder,
18    },
19};
20
21pub struct GeneralizedRegressionTest {
22    pub residual_test: NormalResidualStatistic,
23    pub significance_test: SignificanceOfRegressionStatistic,
24    pub significant_coef_tests: Vec<ZCoefficient>,
25}
26
27impl<L: 'static + Link, F: 'static + Family<L> + Clone> StatisticalTest
28    for GeneralizedLinearRegression<L, F>
29{
30    type Input = ();
31    type Output = Result<GeneralizedRegressionTest, Box<dyn Error>>;
32
33    fn assumptions() -> Vec<Box<dyn Assumption>> {
34        vec![Box::new(NormalResiduals::new())]
35    }
36
37    fn null_hypotheses() -> Vec<Box<dyn Conclusion>> {
38        vec![
39            Box::new(NoSignificantFeature::new()),
40            Box::new(InsignificantCoefficient::new()),
41        ]
42    }
43
44    fn alternate_hypotheses() -> Vec<Box<dyn Conclusion>> {
45        vec![
46            Box::new(SomeSignficantFeature::new()),
47            Box::new(SignificantCoefficient::new()),
48        ]
49    }
50
51    // TODO: Weights cannot be negative
52    // TODO: Cannot be NAN's
53    fn test(&mut self, _: &Self::Input) -> Self::Output {
54        let residual_test = NormalResidualTest::new()
55            .with_alpha(self.alpha)
56            .test(&self.model_data)?;
57
58        let significance_test = SignificanceOfRegressionTest::new()
59            .with_alpha(self.alpha)
60            .test(&self.model_data)?;
61
62        let significant_coef_tests = ZCoefficientBuilder::new()
63            .with_alpha(self.alpha)
64            .test(&self.model_data)?;
65
66        Ok(GeneralizedRegressionTest {
67            residual_test,
68            significance_test,
69            significant_coef_tests,
70        })
71    }
72}