public void BaselineHazardTestR()
{
double[,] data =
{
// t c in
{ 8, 0, 13 },
{ 4, 1, 56 },
{ 12, 0, 25 },
{ 6, 0, 64 },
{ 10, 0, 38 },
{ 8, 1, 80 },
{ 5, 0, 0 },
{ 5, 0, 81 },
{ 3, 1, 81 },
{ 14, 1, 38 },
{ 8, 0, 23 },
{ 11, 0, 99 },
{ 7, 0, 12 },
{ 7, 1, 36 },
{ 12, 0, 63 },
{ 8, 0, 92 },
{ 7, 0, 38 },
};
double[] time = data.GetColumn(0);
SurvivalOutcome[] censor = data.GetColumn(1).To<SurvivalOutcome[]>();
double[][] inputs = data.GetColumn(2).ToJagged();
var regression = new ProportionalHazards(1);
var target = new ProportionalHazardsNewtonRaphson(regression);
double error = target.Run(inputs, time, censor);
// Assert.AreEqual(-10.257417973830666, error, 1e-8);
/*
library('survival')
options(digits=17)
time <- c(8, 4, 12, 6, 10, 8, 5, 5, 3, 14, 8, 11, 7, 7, 12, 8, 7)
x <- c(13, 56, 25, 64, 38, 80, 0, 81, 81, 38, 23, 99, 12, 36, 63, 92, 38)
c <- c(0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0)
fit <- coxph(Surv(time, c) ~ x, ties="breslow")
predict(fit,type="risk")
fit$loglik
coef exp(coef) se(coef) z p
x 0.01633097532122 1.016465054586 0.01711960930183 0.9539338797573 0.340117112635
Likelihood ratio test=0.94 on 1 df, p=0.332836850925 n= 17, number of events= 5
*/
// Tested against GNU R
Assert.AreEqual(49.352941176470587, regression.Offsets[0]);
Assert.AreEqual(0.01633097532122, regression.Coefficients[0], 1e-10);
Assert.AreEqual(0.01711960930183, regression.StandardErrors[0], 1e-10);
Assert.AreEqual(0.340117112635, regression.GetWaldTest(0).PValue, 1e-5);
Assert.AreEqual(-10.2879332934202168, regression.GetPartialLogLikelihood(time, censor));
Assert.AreEqual(-9.8190189050165948, regression.GetPartialLogLikelihood(inputs, time, censor));
double[] actual = inputs.Apply(x => regression.Compute(x));
/*
predict(r,type="risk")
[1] 0.55229166964915244 1.11466393245000361 0.67185866444081555 1.27023351821156782 0.83076808526813917 1.64953983529334769 0.44664925161695829 1.67669959872327912
[9] 1.67669959872327912 0.83076808526813917 0.65026895029003673 2.24967304521214029 0.54334545703992021 0.80407192663266613 1.24965783376477391 2.00665280971219540
[17] 0.83076808526813917
*/
double[] expected =
{
0.55229166964915244, 1.11466393245000361, 0.67185866444081555, 1.27023351821156782,
0.83076808526813917, 1.64953983529334769, 0.44664925161695829, 1.67669959872327912,
1.67669959872327912, 0.83076808526813917, 0.65026895029003673, 2.24967304521214029,
0.54334545703992021, 0.80407192663266613, 1.24965783376477391, 2.00665280971219540,
0.83076808526813917
};
for (int i = 0; i < actual.Length; i++)
Assert.AreEqual(expected[i], actual[i], 0.025);
}