public void RegressTest()
{
double[,] inputGrouped =
{
{ 1, 4, 5 }, // product 1 has four occurrences of class 1 and five of class 0
{ 2, 1, 3 }, // product 2 has one occurrence of class 1 and three of class 0
};
double[,] inputGroupProb =
{
{ 1, 4.0 / (4 + 5) }, // product 1 has 0.44 probability of belonging to class 1
{ 2, 1.0 / (1 + 3) }, // product 2 has 0.25 probability of belonging to class 1
};
double[,] inputExtended =
{
{ 1, 1 }, // observation of product 1 in class 1
{ 1, 1 }, // observation of product 1 in class 1
{ 1, 1 }, // observation of product 1 in class 1
{ 1, 1 }, // observation of product 1 in class 1
{ 1, 0 }, // observation of product 1 in class 0
{ 1, 0 }, // observation of product 1 in class 0
{ 1, 0 }, // observation of product 1 in class 0
{ 1, 0 }, // observation of product 1 in class 0
{ 1, 0 }, // observation of product 1 in class 0
{ 2, 1 }, // observation of product 2 in class 1
{ 2, 0 }, // observation of product 2 in class 0
{ 2, 0 }, // observation of product 2 in class 0
{ 2, 0 }, // observation of product 2 in class 0
};
// Fit using extended data
double[][] inputs = Matrix.ColumnVector(inputExtended.GetColumn(0)).ToJagged();
double[] outputs = inputExtended.GetColumn(1);
LogisticRegression target = new LogisticRegression(1);
IterativeReweightedLeastSquares irls = new IterativeReweightedLeastSquares(target);
irls.Run(inputs, outputs);
// Fit using grouped data
double[][] inputs2 = Matrix.ColumnVector(inputGroupProb.GetColumn(0)).ToJagged();
double[] outputs2 = inputGroupProb.GetColumn(1);
LogisticRegression target2 = new LogisticRegression(1);
IterativeReweightedLeastSquares irls2 = new IterativeReweightedLeastSquares(target2);
irls2.Run(inputs2, outputs2);
Assert.IsTrue(Matrix.IsEqual(target.Coefficients, target2.Coefficients, 0.000001));
double[,] data = new double[,]
{
{ 1, 0 },
{ 2, 0 },
{ 3, 0 },
{ 4, 0 },
{ 5, 1 },
{ 6, 0 },
{ 7, 1 },
{ 8, 0 },
{ 9, 1 },
{ 10, 1 }
};
double[][] inputs3 = Matrix.ColumnVector(data.GetColumn(0)).ToJagged();
double[] outputs3 = data.GetColumn(1);
LogisticRegressionAnalysis analysis = new LogisticRegressionAnalysis(inputs3, outputs3);
analysis.Compute();
Assert.IsFalse(double.IsNaN(analysis.Deviance));
Assert.IsFalse(double.IsNaN(analysis.ChiSquare.PValue));
Assert.AreEqual(analysis.Deviance, 8.6202, 0.0005);
Assert.AreEqual(analysis.ChiSquare.PValue, 0.0278, 0.0005);
// Check intercept
Assert.IsFalse(double.IsNaN(analysis.Coefficients[0].Value));
Assert.AreEqual(analysis.Coefficients[0].Value, -4.3578, 0.0005);
// Check coefficients
Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].Value));
Assert.AreEqual(analysis.Coefficients[1].Value, 0.6622, 0.0005);
// Check statistics
Assert.AreEqual(analysis.Coefficients[1].StandardError, 0.4001, 0.0005);
Assert.AreEqual(analysis.Coefficients[1].Wald.PValue, 0.0979, 0.0005);
Assert.AreEqual(analysis.Coefficients[1].OddsRatio, 1.9391, 0.0005);
Assert.AreEqual(analysis.Coefficients[1].ConfidenceLower, 0.8852, 0.0005);
Assert.AreEqual(analysis.Coefficients[1].ConfidenceUpper, 4.2478, 0.0005);
Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].Wald.PValue));
Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].StandardError));
Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].OddsRatio));
Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].ConfidenceLower));
Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].ConfidenceUpper));
}