Exemplo n.º 1
0
        public void LeveneTestConstructorTest()
        {
            // Example from NIST/SEMATECH e-Handbook of Statistical Methods,
            // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm

            double[][] samples = BartlettTestTest.samples;
            LeveneTest target  = new LeveneTest(samples, median: true);

            Assert.AreEqual(9, target.DegreesOfFreedom1);
            Assert.AreEqual(90, target.DegreesOfFreedom2);
            Assert.AreEqual(1.7059176930008935, target.Statistic, 1e-10);
            Assert.IsFalse(double.IsNaN(target.Statistic));
        }
Exemplo n.º 2
0
        public void LeveneTestConstructorTest2()
        {
            double[][] samples =
            {
                new double[]  { 250, 260, 230, 270 },
                new double[]  { 310, 330, 280, 360 },
                new double[]  { 250, 230, 220, 260 },
                new double[]  { 340, 270, 300, 320 },
                new double[]  { 250, 240, 270, 290 }
            };

            LeveneTest result = new LeveneTest(samples, true);

            Assert.AreEqual(0.7247191011235955, result.Statistic);
            Assert.AreEqual(0.58857793222910693, result.PValue);
        }
Exemplo n.º 3
0
        protected override void EndProcessing()
        {
            var g = _data.GroupBy(CategoryName).ToDoubleJaggedArrayOf(ValueName);

            LeveneTest test;

            if (double.IsNaN(TrimPercent))
            {
                test = new LeveneTest(g, Median);
            }
            else
            {
                test = new LeveneTest(g, TrimPercent);
            }

            test.Size = Size;

            WriteObject(test);
        }
Exemplo n.º 4
0
        public void LeveneTestConstructorTest3()
        {
            double[][] samples =
            {
                new double[]  { 50, 10, 12, 42, 125, 21,  62, 13, 66, 200, 54,  32, 64, 13, 65 },
                new double[]  { 10, 42, 12, 36,  12, 52, 100, 24,  1,  52, 95, 157, 52, 12, 50 }
            };

            double[] expected =
            {
                0.0619, 0.0569, 0.0494,
                0.0498, 0.0535, 0.0519
            };

            for (int i = 0; i < 6; i++)
            {
                double     percent = i / 10.0;
                LeveneTest result  = new LeveneTest(samples, percent);

                Assert.AreEqual(expected[i], result.Statistic, 1e-4);
            }
        }
Exemplo n.º 5
0
        public Statistics CalculateTTest(Physician phys)
        {
            bool   isValid      = false;
            double levenesValue = 0;
            double pValue_E     = 0;
            double pValue_U     = 0;
            double pValue       = 0;

            var statistics = new Statistics
            {
                IsValid = false
            };

            var physData = _context.IndicatorsData
                           .Where(p => p.PayrollID == phys.ID)
                           .Select(p => new { value = p.NumeratorValue });

            if (physData.Count() >= 5)
            {
                #region Build Physician Array
                var physArray = Util.BuildArray(physData);
                #endregion

                #region Build Peers Array
                var peersData = _context.IndicatorsData
                                .Where(p => (p.PayrollID != phys.ID) && (p.OppePhysicianSubGroupID == phys.SubGroupID))
                                .Select(p => new { value = p.NumeratorValue });
                var peersArray = Util.BuildArray(peersData);

                if (peersData.Count() >= 5)
                {
                    statistics.IsValid = true;
                    isValid            = true;
                }
                else
                {
                    statistics.IsValid = false;
                    isValid            = false;
                }
                #endregion


                if (isValid)
                {
                    #region Levene's Test
                    double[][] doubleArray = new double[][]
                    {
                        physArray,
                        peersArray
                    };
                    var levenesTest = new LeveneTest(doubleArray);
                    levenesValue = levenesTest.PValue;
                    #endregion

                    #region T-Test Equal Variances assumed
                    var TTestEqualVariance = new TwoSampleTTest(physArray, peersArray, assumeEqualVariances: true);
                    pValue_E = TTestEqualVariance.PValue;
                    #endregion

                    #region T-Test UnEqual Variances assumed
                    var TTestUnEqualVariance = new TwoSampleTTest(physArray, peersArray, assumeEqualVariances: false);
                    pValue_U = TTestUnEqualVariance.PValue;
                    #endregion
                    pValue = levenesValue > 0.05 ? pValue_E : pValue_U;
                }

                #region Populating Statistics
                statistics.IsValid           = true;
                statistics.PayrollID         = phys.ID;
                statistics.NumeratorSum      = physArray.Sum();
                statistics.DenominatorSum    = physArray.Count();
                statistics.Mean              = Measures.Mean(physArray);
                statistics.StandardDeviation = Measures.StandardDeviation(physArray);
                if (isValid)
                {
                    statistics.PeerNumeratorSum        = peersArray.Sum();
                    statistics.PeerDenominatorSum      = peersArray.Count();
                    statistics.PeerMean                = Measures.Mean(peersArray);
                    statistics.PeerStandardDeviation   = Measures.StandardDeviation(peersArray);
                    statistics.LevenesTest             = levenesValue;
                    statistics.PValue_EqualVariances   = pValue_E;
                    statistics.PValue_UnequalVariances = pValue_U;
                    statistics.PValue = pValue;
                }
                #endregion
            }

            return(statistics);
        }
Exemplo n.º 6
0
        public Statistics CalculateStats(PValueDbContext context, Physician item)
        {
            var phys = context.Indicator_12
                       .Where(p => p.PayrollID == item.ID)
                       .Select(p => new { num = p.NumeratorValue });

            var physCount = phys.Count();

            // string physNum = "";
            double[] x1         = new double[physCount];
            double   physNumVal = 0;
            double   physDenVal = 0;
            double   physMean   = 0;
            int      i          = 0;

            foreach (var phy in phys)
            {
                x1[i] = phy.num;
                // physNum += physNum == "" ? phy.num.ToString() : " " + phy.num.ToString();
                physNumVal += phy.num;
                physDenVal++;
                i++;
            }
            if (physDenVal != 0)
            {
                physMean = physNumVal / physDenVal;
            }

            var peers = context.Indicator_12
                        .Where(p => p.OppePhysicianSubGroupID == item.SubGroupID)
                        .Where(p => p.PayrollID != item.ID)
                        .Select(p => new
            {
                num = p.NumeratorValue
            });

            var peersCount = peers.Count();

            double[] x2 = new double[peersCount];
            // string peersNum = "";
            double peersNumVal = 0;
            double peersDenVal = 0;
            double peersMean   = 0;


            double levenesTest = 0;
            double pValueA     = 0;
            double pValueNA    = 0;

            if (peers.Count() >= 5)
            {
                int j = 0;
                foreach (var peer in peers)
                {
                    x2[j]        = peer.num;
                    peersNumVal += peer.num;
                    peersDenVal++;
                    j++;
                }
                if (peersDenVal != 0)
                {
                    peersMean = peersNumVal / peersDenVal;
                }

                #region Levene's Test
                double[][] X = new double[][]
                {
                    x1,
                    x2
                };

                var levenes = new LeveneTest(X);
                levenesTest = levenes.PValue;
                #endregion

                Console.WriteLine($"Grp = {item.SubGroupID} - Phys = {item.ID}");

                #region T-Test Equal variances assumed
                var ta = new TwoSampleTTest(x1, x2, assumeEqualVariances: true);
                pValueA = ta.PValue;
                #endregion

                #region T-Test Equal variances not assumed
                var tna = new TwoSampleTTest(x1, x2, assumeEqualVariances: false);
                pValueNA = tna.PValue;
                #endregion
            }

            var stats = new Statistics
            {
                Count                   = physCount,
                PayrollID               = item.ID,
                NumeratorSum            = physNumVal,
                DenominatorSum          = physDenVal,
                Mean                    = physMean,
                StandardDeviation       = Measures.StandardDeviation(x1),
                PeerCount               = peersCount,
                PeerNumeratorSum        = peersNumVal,
                PeerDenominatorSum      = peersDenVal,
                PeerMean                = peersMean,
                PeerStandardDeviation   = Measures.StandardDeviation(x2),
                LevenesTest             = levenesTest,
                PValue_EqualVariances   = pValueA,
                PValue_UnequalVariances = pValueNA,
                PValue                  = levenesTest > 0.05 ? pValueA : pValueNA
            };

            return(stats);
        }