/// <summary> /// Constructs a new power analysis for a one-sample test. /// </summary> /// protected BaseOneSamplePowerAnalysis(DistributionTail tail) { Tail = tail; Size = 0.05; Power = 0.8; Samples = 2; }
/// <summary> /// Computes the Wilcoxon Signed-Rank test. /// </summary> /// protected void Compute(double statistic, double[] ranks, DistributionTail tail) { this.Statistic = statistic; this.StatisticDistribution = new WilcoxonDistribution(ranks); this.Tail = tail; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Computes the Wilcoxon Signed-Rank test. /// </summary> /// protected void Compute(int[] signs, double[] diffs, DistributionTail tail) { Signs = signs; Delta = diffs; Ranks = Accord.Statistics.Tools.Rank(Delta); double W = WilcoxonDistribution.WPositive(Signs, Ranks); this.Compute(W, Ranks, tail); }
/// <summary> /// Computes the Wilcoxon Signed-Rank test. /// </summary> /// protected void Compute(int[] signs, double[] diffs, DistributionTail tail) { Signs = signs; Delta = diffs; Ranks = Delta.Rank(); double W = WilcoxonDistribution.WPositive(Signs, Ranks); this.Compute(W, Ranks, tail); }
/// <summary> /// Computes the Wilcoxon Signed-Rank test. /// </summary> /// protected void Compute(int[] signs, double[] diffs, DistributionTail tail, bool?exact, bool adjustForTies) { this.Signs = signs; this.Delta = diffs; this.Ranks = Delta.Rank(adjustForTies: adjustForTies); double W = WilcoxonDistribution.WPositive(Signs, Ranks); this.Compute(W, Ranks, tail, exact); }
/// <summary> /// Computes the Wilcoxon Signed-Rank test. /// </summary> /// protected void Compute(double statistic, double[] ranks, DistributionTail tail, bool?exact) { this.Statistic = statistic; this.Tail = tail; this.StatisticDistribution = new WilcoxonDistribution(ranks, exact) { Correction = (Tail == DistributionTail.TwoTail) ? ContinuityCorrection.Midpoint : ContinuityCorrection.KeepInside }; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="t">The value of the test statistic.</param> /// <param name="type">The tail of the test distribution.</param> /// <param name="distribution">The test distribution.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public static double StatisticToPValue(double t, TDistribution distribution, DistributionTail type) { double p; switch (type) { case DistributionTail.TwoTail: p = 2.0 * distribution.ComplementaryDistributionFunction(Math.Abs(t)); break; case DistributionTail.OneUpper: p = distribution.ComplementaryDistributionFunction(t); break; case DistributionTail.OneLower: p = distribution.DistributionFunction(t); break; default: throw new InvalidOperationException(); } return(p); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="z">The value of the test statistic.</param> /// <param name="type">The tail of the test distribution.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public static double StatisticToPValue(double z, DistributionTail type) { double p; switch (type) { case DistributionTail.TwoTail: p = 2.0 * NormalDistribution.Standard.ComplementaryDistributionFunction(Math.Abs(z)); break; case DistributionTail.OneUpper: p = NormalDistribution.Standard.ComplementaryDistributionFunction(z); break; case DistributionTail.OneLower: p = NormalDistribution.Standard.DistributionFunction(z); break; default: throw new InvalidOperationException(); } return(p); }
/// <summary> /// Computes the Wilcoxon Signed-Rank test. /// </summary> /// protected void Compute(int[] signs, double[] diffs, DistributionTail tail, bool?exact, bool adjustForTies) { int[] nonZeros = diffs.Find(x => x != 0); this.HasZeros = nonZeros.Length != diffs.Length; if (HasZeros) { // It is actually necessary to discard zeros before the test procedure // https://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test#Test_procedure // http://vassarstats.net/textbook/ch12a.html signs = signs.Get(nonZeros); diffs = diffs.Get(nonZeros); } this.Signs = signs; this.Delta = diffs; this.Ranks = Delta.Rank(hasTies: out hasTies, adjustForTies: adjustForTies); double W = WilcoxonDistribution.WPositive(Signs, Ranks); this.Compute(W, Ranks, tail, exact); }
/// <summary> /// Computes the Wilcoxon Signed-Rank test. /// </summary> /// protected void Compute(double statistic, double[] ranks, DistributionTail tail, bool?exact) { if (this.HasZeros) { if (!exact.HasValue) { exact = false; } if (exact == true) { throw new ArgumentException("An exact test cannot be computed when there are zeros in the samples (or when paired samples are the same in a paired test)."); } } this.Statistic = statistic; this.Tail = tail; if (ranks.Length != 0) { this.StatisticDistribution = new WilcoxonDistribution(ranks, exact) { Correction = (Tail == DistributionTail.TwoTail) ? ContinuityCorrection.Midpoint : ContinuityCorrection.KeepInside }; this.IsExact = this.StatisticDistribution.Exact; } else { this.StatisticDistribution = null; this.IsExact = exact.GetValueOrDefault(false); } this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Converts a given p-value to a test statistic. /// </summary> /// /// <param name="p">The p-value.</param> /// <param name="type">The tail of the test distribution.</param> /// <param name="distribution">The test distribution.</param> /// /// <returns>The test statistic which would generate the given p-value.</returns> /// public static double PValueToStatistic(double p, TDistribution distribution, DistributionTail type) { double t; switch (type) { case DistributionTail.OneLower: t = distribution.InverseDistributionFunction(p); break; case DistributionTail.OneUpper: t = distribution.InverseDistributionFunction(1.0 - p); break; case DistributionTail.TwoTail: t = distribution.InverseDistributionFunction(1.0 - p / 2.0); break; default: throw new InvalidOperationException(); } return(t); }
/// <summary> /// Converts a given p-value to a test statistic. /// </summary> /// /// <param name="p">The p-value.</param> /// <param name="type">The tail of the test distribution.</param> /// /// <returns>The test statistic which would generate the given p-value.</returns> /// public static double PValueToStatistic(double p, DistributionTail type) { double z; switch (type) { case DistributionTail.OneLower: z = NormalDistribution.Standard.InverseDistributionFunction(p); break; case DistributionTail.OneUpper: z = NormalDistribution.Standard.InverseDistributionFunction(1.0 - p); break; case DistributionTail.TwoTail: z = NormalDistribution.Standard.InverseDistributionFunction(1.0 - p / 2.0); break; default: throw new InvalidOperationException(); } return(z); }
/// <summary> /// Creates a new Wilcoxon's W+ test. /// </summary> /// /// <param name="signs">The signs for the sample differences.</param> /// <param name="diffs">The differences between samples.</param> /// <param name="tail">The distribution tail to test.</param> /// public WilcoxonTest(int[] signs, double[] diffs, DistributionTail tail) { Compute(signs, diffs, tail, null, adjustForTies: true); }
/// <summary> /// Creates a new Wilcoxon's W+ test. /// </summary> /// /// <param name="signs">The signs for the sample differences.</param> /// <param name="diffs">The differences between samples.</param> /// <param name="tail">The distribution tail to test.</param> /// public WilcoxonTest(int[] signs, double[] diffs, DistributionTail tail) { Compute(signs, diffs, tail); }