@letalis Actually he's misunderstood the core logic and then mocked his own misunderstanding. I didn't want to be drawn into re-explaining it, so I just pointed out that what he's being sarcastic about isn't I'm suggesting. Hopefully he'll go back and re-read the original post.
@bustr My apologies, I hope this helps. This uses the extreme stats library in C#. Obviously it's a generic routine and would have to be adjusted for the specifics of Aces High.
using System;
using System.Data;
using System.IO;
using Extreme.Statistics;
namespace Extreme.Numerics.QuickStart.C Sharp
{
using Extreme.Mathematics;
using Extreme.Mathematics.LinearAlg ebra.IO;
class LogisticRegression
{
static void Main(string[] args)
{
DataTable dataTable = ReadData();
VariableCollection data = new VariableCollection(dataTable);
// We need indicator variables for the race. We use the
// CreateIndicatorVariable method:
NumericalVariable race = (NumericalVariable)data["RACE"];
NumericalVariable race2 = race.CreateIndicatorVariable(2.0);
data.Add(race2);
NumericalVariable race3 = race.CreateIndicatorVariable(3.0);
data.Add(race3);
Console.WriteLine("Variable Value Std.Error t-stat p-Value");
foreach (Parameter parameter in model.Parameters)
Console.WriteLine("{0,-20}{1,10:F5}{2,10:F5}{3,8:F2} {4,7:F4}",
parameter.Name,
parameter.Value,
Console.WriteLine("Log-likelihood: {0:F4}", model.GetLogLikelihood());
Extreme.Statistics.Tests.Simp leHypothesisTest lrt = model.GetLikelihoodRatioTest();
Console.WriteLine("Likelihood-ratio test: chi-squared={0:F4}, p={1:F4}", lrt.Statistic, lrt.PValue);
Console.WriteLine();
LogisticRegressionModel model2 = new LogisticRegressionModel(data, "LOW", new string[] { "LWT", "RACE(2)", "RACE(3)" });
model2.Compute();
Console.WriteLine("Variable Value Std.Error t-stat p-Value");
foreach (Parameter parameter in model2.Parameters)
Console.WriteLine("{0,-20}{1,10:F5}{2,10:F5}{3,8:F2} {4,7:F4}",
parameter.Name, parameter.Value, parameter.StandardError, parameter.Statistic, parameter.PValue);
Console.WriteLine("Log-likelihood: {0:F4}", model.GetLogLikelihood());
lrt = model.GetLikelihoodRatioTest(model2);
Console.WriteLine("Likelihood-ratio test: chi-squared={0:F4}, p={1:F4}", lrt.Statistic, lrt.PValue);
Console.WriteLine();
FixedWidthMatrixReader reader = new FixedWidthMatrixReader(
File.OpenText(@"..\..\..\..\Data\mlogit.txt"),
0, new int[] { 5, 10, 15, 20, 25, 32, 37, 42, 47 },
System.Globalization.NumberSt yles.Integer, null);
Matrix m = reader.ReadMatrix();
CategoricalVariable duration =
new NumericalVariable("duration", m.GetColumn(1)).ToCategoricalVariable();
NumericalVariable nutritio = new NumericalVariable("nutritio", m.GetColumn(5));
NumericalVariable agecat1 = new NumericalVariable("agecat1", m.GetColumn(6));
NumericalVariable agecat3 = new NumericalVariable("agecat3", m.GetColumn(7));
NumericalVariable alcohol = new NumericalVariable("alcohol", m.GetColumn(8));
NumericalVariable smoking = new NumericalVariable("smoking", m.GetColumn(9));
LogisticRegressionModel model3 = new LogisticRegressionModel(duration,
new NumericalVariable[] { nutritio, agecat1, agecat3, alcohol, smoking },
LogisticRegressionMethod.Nomi nal);
model3.Compute();
foreach (Parameter p in model3.Parameters) {
Console.WriteLine(p.ToString());
}
Console.WriteLine("Log likelihood: {0:F4}", model3.GetLogLikelihood());
lrt = model3.GetLikelihoodRatioTest();
Console.WriteLine("Test that all slopes are zero: chi-squared={0:F4}, p={1:F4}", lrt.Statistic, lrt.PValue);
Console.Write("Press any key to exit.");
Console.ReadLine();
}
private static DataTable ReadData()
{
DataTable data = new DataTable("LowBirthWeight");
char[] whitespace = new char[] { ' ', '\t' };
StreamReader sr = new StreamReader(@"..\..\..\..\Data\lowbwt.txt");
String line = sr.ReadLine();
int pos = 0;
int pos2;
do
{
while (char.IsWhiteSpace(line[pos]))
pos++;
pos2 = line.IndexOfAny(whitespace, pos);
if (pos2 < 0)
{
data.Columns.Add(line.Substring(pos), typeof(double));
break;
}
else
data.Columns.Add(line.Substring(pos, pos2 - pos), typeof(double));
pos = pos2;
}
while (pos >= 0);
object[] rowData = new object[data.Columns.Count];
line = sr.ReadLine();
while (line != null && line.Length > 0)
{
int column = 0;
pos = 0;
do
{
string field;
while (char.IsWhiteSpace(line[pos]))
pos++;
pos2 = line.IndexOfAny(whitespace, pos);
if (pos2 < 0)
field = line.Substring(pos);
else
field = line.Substring(pos, pos2 - pos);
if (column == 0)
rowData[column++] = field;
else
rowData[column++] = double.Parse(field);
pos = pos2;
}
while (pos >= 0 && column < data.Columns.Count);
data.Rows.Add(rowData);
line = sr.ReadLine();
}
return data;
}
}
}