Esempio n. 1
0
 /// <summary>
 ///   Creates a new object representation of a variation source in an ANOVA experiment.
 /// </summary>
 ///
 /// <param name="anova">The associated ANOVA analysis.</param>
 /// <param name="source">The name of the variation source.</param>
 /// <param name="degreesOfFreedom">The degrees of freedom for the source.</param>
 /// <param name="sumOfSquares">The sum of squares of the source.</param>
 /// <param name="meanSquares">The mean sum of squares of the source.</param>
 /// <param name="test">The F-Test containing the F-Statistic for the source.</param>
 ///
 public AnovaVariationSource(IAnova anova, string source, double sumOfSquares,
                             int degreesOfFreedom, double meanSquares, FTest test)
 {
     this.anova            = anova;
     this.Source           = source;
     this.SumOfSquares     = sumOfSquares;
     this.DegreesOfFreedom = degreesOfFreedom;
     this.Significance     = test;
     this.MeanSquares      = meanSquares;
 }
Esempio n. 2
0
        private void initialize(double[][] samples)
        {
            DFb = groupCount - 1;
            DFw = totalSize - groupCount;
            DFt = totalSize - 1;

            // Step 1. Calculate the mean within each group
            means = BestCS.Statistics.Tools.Mean(samples, 1);

            // Step 2. Calculate the overall mean
            totalMean = BestCS.Statistics.Tools.GrandMean(means, sizes);


            // Step 3. Calculate the "between-group" sum of squares
            for (int i = 0; i < samples.Length; i++)
            {
                //  between-group sum of squares
                double u = (means[i] - totalMean);
                SSb += sizes[i] * u * u;
            }


            // Step 4. Calculate the "within-group" sum of squares
            for (int i = 0; i < samples.Length; i++)
            {
                for (int j = 0; j < samples[i].Length; j++)
                {
                    double u = samples[i][j] - means[i];
                    SSw += u * u;
                }
            }

            SSt = SSb + SSw; // total sum of squares


            // Step 5. Calculate the F statistic
            MSb   = SSb / DFb; // between-group mean square
            MSw   = SSw / DFw; // within-group mean square
            FTest = new FTest(MSb / MSw, DFb, DFw);


            // Step 6. Create the ANOVA table
            List <AnovaVariationSource> table = new List <AnovaVariationSource>();

            table.Add(new AnovaVariationSource(this, "Between-Groups", SSb, DFb, FTest));
            table.Add(new AnovaVariationSource(this, "Within-Groups", SSw, DFw, null));
            table.Add(new AnovaVariationSource(this, "Total", SSt, DFt, null));
            this.Table = new AnovaSourceCollection(table);
        }
Esempio n. 3
0
        private void initialize(double[][][] samples, TwoWayAnovaModel type)
        {
            // References:
            // -  http://www.smi.hst.aau.dk/~cdahl/BiostatPhD/ANOVA.pdf

            ModelType    = type;
            Observations = FirstFactorSamples * SecondFactorSamples * Replications;


            // Step 1. Initialize all degrees of freedom
            int cellDegreesOfFreedom  = FirstFactorSamples * SecondFactorSamples - 1;
            int aDegreesOfFreedom     = FirstFactorSamples - 1;
            int bDegreesOfFreedom     = SecondFactorSamples - 1;
            int abDegreesOfFreedom    = cellDegreesOfFreedom - aDegreesOfFreedom - bDegreesOfFreedom;
            int errorDegreesOfFreedom = FirstFactorSamples * SecondFactorSamples * (Replications - 1);
            int totalDegreesOfFreedom = Observations - 1;


            // Step 1. Calculate cell means
            cellMeans = new double[FirstFactorSamples, SecondFactorSamples];

            double sum = 0;

            for (int i = 0; i < samples.Length; i++)
            {
                for (int j = 0; j < samples[i].Length; j++)
                {
                    sum += cellMeans[i, j] = BestCS.Statistics.Tools.Mean(samples[i][j]);
                }
            }


            // Step 2. Calculate the total mean (grand mean)
            totalMean = sum / (FirstFactorSamples * SecondFactorSamples);


            // Step 3. Calculate factor means
            aMean = new double[FirstFactorSamples];
            for (int i = 0; i < samples.Length; i++)
            {
                sum = 0;
                for (int j = 0; j < samples[i].Length; j++)
                {
                    for (int k = 0; k < samples[i][j].Length; k++)
                    {
                        sum += samples[i][j][k];
                    }
                }

                aMean[i] = sum / (SecondFactorSamples * Replications);
            }

            bMean = new double[SecondFactorSamples];
            for (int j = 0; j < samples[0].Length; j++)
            {
                sum = 0;
                for (int i = 0; i < samples.Length; i++)
                {
                    for (int k = 0; k < samples[i][j].Length; k++)
                    {
                        sum += samples[i][j][k];
                    }
                }

                bMean[j] = sum / (FirstFactorSamples * Replications);
            }


            // Step 4. Calculate total sum of squares
            double ssum = 0;

            for (int i = 0; i < samples.Length; i++)
            {
                for (int j = 0; j < samples[i].Length; j++)
                {
                    for (int k = 0; k < samples[i][j].Length; k++)
                    {
                        double u = samples[i][j][k] - totalMean;
                        ssum += u * u;
                    }
                }
            }
            double totalSumOfSquares = ssum;


            // Step 5. Calculate the cell sum of squares
            ssum = 0;
            for (int i = 0; i < FirstFactorSamples; i++)
            {
                for (int j = 0; j < SecondFactorSamples; j++)
                {
                    double u = cellMeans[i, j] - totalMean;
                    ssum += u * u;
                }
            }
            double cellSumOfSquares = ssum * Replications;


            // Step 6. Compute within-cells error sum of squares
            ssum = 0;
            for (int i = 0; i < samples.Length; i++)
            {
                for (int j = 0; j < samples[i].Length; j++)
                {
                    for (int k = 0; k < samples[i][j].Length; k++)
                    {
                        double u = samples[i][j][k] - cellMeans[i, j];
                        ssum += u * u;
                    }
                }
            }
            double errorSumOfSquares = ssum;


            // Step 7. Compute factors sum of squares
            ssum = 0;
            for (int i = 0; i < aMean.Length; i++)
            {
                double u = aMean[i] - totalMean;
                ssum += u * u;
            }
            double aSumOfSquares = ssum * SecondFactorSamples * Replications;

            ssum = 0;
            for (int i = 0; i < bMean.Length; i++)
            {
                double u = bMean[i] - totalMean;
                ssum += u * u;
            }
            double bSumOfSquares = ssum * FirstFactorSamples * Replications;


            // Step 9. Compute interaction sum of squares
            double abSumOfSquares = cellSumOfSquares - aSumOfSquares - bSumOfSquares;

            // Step 10. Compute mean squares
            double aMeanSquares     = aSumOfSquares / aDegreesOfFreedom;
            double bMeanSquares     = bSumOfSquares / bDegreesOfFreedom;
            double abMeanSquares    = abSumOfSquares / abDegreesOfFreedom;
            double errorMeanSquares = errorSumOfSquares / errorDegreesOfFreedom;

            // Step 10. Create the F-Statistics
            FTest aSignificance, bSignificance, abSignificance;

            if (type == TwoWayAnovaModel.Fixed)
            {
                // Model 1: Factors A and B fixed
                aSignificance  = new FTest(aMeanSquares / abMeanSquares, aDegreesOfFreedom, abDegreesOfFreedom);
                bSignificance  = new FTest(bMeanSquares / abMeanSquares, bDegreesOfFreedom, abDegreesOfFreedom);
                abSignificance = new FTest(abMeanSquares / errorMeanSquares, abDegreesOfFreedom, errorDegreesOfFreedom);
            }
            else if (type == TwoWayAnovaModel.Mixed)
            {
                // Model 2: Factors A and B random
                aSignificance  = new FTest(aMeanSquares / errorMeanSquares, aDegreesOfFreedom, errorDegreesOfFreedom);
                bSignificance  = new FTest(bMeanSquares / errorMeanSquares, bDegreesOfFreedom, errorDegreesOfFreedom);
                abSignificance = new FTest(abMeanSquares / errorMeanSquares, abDegreesOfFreedom, errorDegreesOfFreedom);
            }
            else if (type == TwoWayAnovaModel.Random)
            {
                // Model 3: Factor A fixed, factor B random
                aSignificance  = new FTest(aMeanSquares / abMeanSquares, aDegreesOfFreedom, abDegreesOfFreedom);
                bSignificance  = new FTest(bMeanSquares / errorMeanSquares, bDegreesOfFreedom, errorDegreesOfFreedom);
                abSignificance = new FTest(abMeanSquares / errorMeanSquares, abDegreesOfFreedom, errorDegreesOfFreedom);
            }
            else
            {
                throw new ArgumentException("Unhandled analysis type.", "type");
            }


            // Step 11. Create the ANOVA table and sources
            AnovaVariationSource cell  = new AnovaVariationSource(this, "Cells", cellSumOfSquares, cellDegreesOfFreedom);
            AnovaVariationSource a     = new AnovaVariationSource(this, "Factor A", aSumOfSquares, aDegreesOfFreedom, aMeanSquares, aSignificance);
            AnovaVariationSource b     = new AnovaVariationSource(this, "Factor B", bSumOfSquares, bDegreesOfFreedom, bMeanSquares, bSignificance);
            AnovaVariationSource ab    = new AnovaVariationSource(this, "Interaction AxB", abSumOfSquares, abDegreesOfFreedom, abMeanSquares, abSignificance);
            AnovaVariationSource error = new AnovaVariationSource(this, "Within-cells (error)", errorSumOfSquares, errorDegreesOfFreedom, errorMeanSquares);
            AnovaVariationSource total = new AnovaVariationSource(this, "Total", totalSumOfSquares, totalDegreesOfFreedom);

            this.Sources = new TwoWayAnovaVariationSources()
            {
                Cells       = cell,
                FactorA     = a,
                FactorB     = b,
                Interaction = ab,
                Error       = error,
                Total       = total
            };

            this.Table = new AnovaSourceCollection(cell, a, b, ab, error, total);
        }
Esempio n. 4
0
 /// <summary>
 ///   Creates a new object representation of a variation source in an ANOVA experiment.
 /// </summary>
 ///
 /// <param name="anova">The associated ANOVA analysis.</param>
 /// <param name="source">The name of the variation source.</param>
 /// <param name="degreesOfFreedom">The degrees of freedom for the source.</param>
 /// <param name="sumOfSquares">The sum of squares of the source.</param>
 /// <param name="test">The F-Test containing the F-Statistic for the source.</param>
 ///
 public AnovaVariationSource(IAnova anova, string source, double sumOfSquares,
                             int degreesOfFreedom, FTest test)
     : this(anova, source, sumOfSquares, degreesOfFreedom, sumOfSquares / degreesOfFreedom, test)
 {
 }