hpstat/tests/intcox.rs

84 lines
3.9 KiB
Rust
Raw Normal View History

2023-04-17 22:12:07 +10:00
// hpstat: High-performance statistics implementations
// Copyright © 2023 Lee Yingtong Li (RunasSudo)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use indicatif::ProgressBar;
2023-04-21 17:39:24 +10:00
use hpstat::intcox;
2023-04-17 22:12:07 +10:00
#[test]
fn test_intcox_zeng_mao_lin() {
// Compare "Bangkok Metropolitan Administration HIV" data from Zeng, Mao & Lin (2016) with Stata 17 output
2023-04-17 22:12:07 +10:00
2023-04-21 17:39:24 +10:00
let (_indep_names, data_times, data_indep) = intcox::read_data("tests/zeng_mao_lin.csv");
2023-04-17 22:12:07 +10:00
// Fit regression
let progress_bar = ProgressBar::hidden();
2023-04-28 01:02:09 +10:00
let result = intcox::fit_interval_censored_cox(data_times, data_indep, progress_bar, 500, 0.001);
2023-04-17 22:12:07 +10:00
// import delimited "zeng_mao_lin.csv", case(preserve) numericcols(2)
// stintcox Needle Needle2 LogAge GenderM RaceO RaceW GenderM_RaceO GenderM_RaceW, interval(Left_Time Right_Time) full nohr favorspeed lrmodel
// stcurve, cumhaz outfile("cumhaz.dta")
2023-04-17 22:12:07 +10:00
assert!(rel_diff(result.ll_model, -604.82642) < 0.01);
assert!(rel_diff(result.ll_null, -608.64263) < 0.01);
2023-04-17 22:12:07 +10:00
2023-04-28 01:02:09 +10:00
assert!(abs_diff(result.params[0], -0.1869297) < 0.02);
assert!(abs_diff(result.params[1], 0.0808377) < 0.02);
assert!(abs_diff(result.params[2], -0.7088894) < 0.02);
assert!(abs_diff(result.params[3], -0.2296864) < 0.02);
assert!(abs_diff(result.params[4], -0.1408832) < 0.02);
assert!(abs_diff(result.params[5], -0.4397316) < 0.02);
assert!(abs_diff(result.params[6], 0.0642637) < 0.02);
assert!(abs_diff(result.params[7], 0.2110733) < 0.02);
2023-04-17 22:12:07 +10:00
2023-04-28 01:02:09 +10:00
assert!(abs_diff(result.params_se[0], 0.4148436) < 0.01);
assert!(abs_diff(result.params_se[1], 0.1507537) < 0.01);
assert!(abs_diff(result.params_se[2], 0.3653805) < 0.01);
assert!(abs_diff(result.params_se[3], 0.3214563) < 0.01);
assert!(abs_diff(result.params_se[4], 0.3889668) < 0.01);
assert!(abs_diff(result.params_se[5], 0.4165912) < 0.01);
assert!(abs_diff(result.params_se[6], 0.4557368) < 0.01);
assert!(abs_diff(result.params_se[7], 0.4853911) < 0.01);
// Check a few points on the cumulative hazard curve
assert_eq!(result.cumulative_hazard_times[0], 0.0);
assert_eq!(result.cumulative_hazard[0], 0.0);
assert!(abs_diff(result.cumulative_hazard_times[10], 3.43757) < 0.000001);
assert!(rel_diff(result.cumulative_hazard[10], 0.01913) < 0.1);
assert!(abs_diff(result.cumulative_hazard_times[30], 3.710771) < 0.000001);
assert!(rel_diff(result.cumulative_hazard[30], 0.0282363) < 0.1);
assert!(abs_diff(result.cumulative_hazard_times[80], 4.277966) < 0.000001);
assert!(rel_diff(result.cumulative_hazard[80], 0.038723) < 0.1);
assert!(abs_diff(result.cumulative_hazard_times[180], 8.566904) < 0.000001);
assert!(rel_diff(result.cumulative_hazard[180], 0.0564792) < 0.1);
assert!(abs_diff(result.cumulative_hazard_times[380], 19.61333) < 0.00001);
assert!(rel_diff(result.cumulative_hazard[380], 0.1084475) < 0.1);
assert!(abs_diff(result.cumulative_hazard_times[880], 28.87403) < 0.00001);
assert!(rel_diff(result.cumulative_hazard[880], 0.1348967) < 0.1);
2023-04-28 01:02:09 +10:00
// In the ICM algorithm, the final cumulative hazard is quite unstable for this dataset
//assert!(abs_diff(*result.cumulative_hazard_times.last().unwrap(), 42.78283) < 0.00001);
//assert!(rel_diff(*result.cumulative_hazard.last().unwrap(), 0.1638222) < 0.1);
}
fn abs_diff(a: f64, b: f64) -> f64 {
return (a - b).abs();
}
fn rel_diff(a: f64, b: f64) -> f64 {
return ((a - b) / b).abs();
2023-04-17 22:12:07 +10:00
}