/// <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 = anova; this.Source = source; this.SumOfSquares = sumOfSquares; this.DegreesOfFreedom = degreesOfFreedom; this.Significance = test; }
private void initialize(double[][] samples) { DFb = groupCount - 1; DFw = totalSize - groupCount; DFt = totalSize - 1; // Step 1. Calculate the mean within each group means = Statistics.Tools.Mean(samples, 1); // Step 2. Calculate the overall mean totalMean = 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); }
public void FTestConstructorTest2() { // The following example has been based on the page "F-Test for Equality // of Two Variances", from NIST/SEMATECH e-Handbook of Statistical Methods: // // http://www.itl.nist.gov/div898/handbook/eda/section3/eda359.htm // // Consider a data set containing 480 ceramic strength // measurements for two batches of material. The summary // statistics for each batch are shown below: // BATCH 1: int numberOfObservations1 = 240; // double mean1 = 688.9987; double stdDev1 = 65.54909; double var1 = stdDev1 * stdDev1; // BATCH 2: int numberOfObservations2 = 240; // double mean2 = 611.1559; double stdDev2 = 61.85425; double var2 = stdDev2 * stdDev2; // Here, we will be testing the null hypothesis that // the variances for the two batches are equal. int degreesOfFreedom1 = numberOfObservations1 - 1; int degreesOfFreedom2 = numberOfObservations2 - 1; // Now we can create a F-Test to test the difference between variances var ftest = new FTest(var1, var2, degreesOfFreedom1, degreesOfFreedom2); double statistic = ftest.Statistic; // 1.123037 double pvalue = ftest.PValue; // 0.185191 bool significant = ftest.Significant; // false // The F test indicates that there is not enough evidence // to reject the null hypothesis that the two batch variances // are equal at the 0.05 significance level. Assert.AreEqual(1.1230374607443194, statistic); Assert.AreEqual(0.18519157993853722, pvalue); Assert.IsFalse(significant); }
public void FTestConstructorTest() { double var1 = 1.05766555271071; double var2 = 1.16570834301777; int d1 = 49; int d2 = 49; FTest oneGreater = new FTest(var1, var2, d1, d2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond); FTest oneSmaller = new FTest(var1, var2, d1, d2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond); FTest twoTail = new FTest(var1, var2, d1, d2, TwoSampleHypothesis.ValuesAreDifferent); Assert.AreEqual(0.632, oneGreater.PValue, 1e-3); Assert.AreEqual(0.367, oneSmaller.PValue, 1e-3); Assert.AreEqual(0.734, twoTail.PValue, 1e-3); Assert.IsFalse(Double.IsNaN(oneGreater.PValue)); Assert.IsFalse(Double.IsNaN(oneSmaller.PValue)); Assert.IsFalse(Double.IsNaN(twoTail.PValue)); }
private void initialize(double[][] samples) { DFb = groupCount - 1; DFw = totalSize - groupCount; DFt = totalSize - 1; // Step 1. Calculate the mean within each group means = Statistics.Tools.Mean(samples, 1); // Step 2. Calculate the overall mean totalMean = 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); }
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] = Measures.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); }
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] = Measures.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); }
/// <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) { }