private void SetNoFit(string comment = "Not Fit") { this.ForEach(x => x.UsedInFit = false); ValidDataSet = false; ExpFit = null; LinFit = null; pGrowthRate.GrowthRate = Double.NaN; pGrowthRate.NumPoints = 0; pGrowthRate.R2 = Double.NaN; pGrowthRate.RMSE = Double.NaN; pGrowthRate.DisplayFittingUsed = "Not Fit"; pGrowthRate.Notes = comment; OnPropertyChange(new PropertyChangedEventArgs("FittedValues")); OnPropertyChange(new PropertyChangedEventArgs("FittedLogValues")); }
public static void LinearModelOutlierDetector(GrowthCurve GC) { if (!GC.ExpModelFitted) { foreach (GrowthDataPoint gdp in GC) { if (gdp.UsedInFit) { gdp.OutlierFlag = true; } } } List <double> XvaluesToDrop = new List <double>(); while (true) { bool goAgain = false; double[] xfittedStart = GC.FittedXValues; double[] yfittedStart = GC.FittedYValues; //Flag it all if (xfittedStart.Length < 4) { foreach (GrowthDataPoint gdp in GC) { if (gdp.UsedInFit) { gdp.OutlierFlag = true; } } } for (int i = 0; i < xfittedStart.Length; i++) { var xfit = xfittedStart.ToList(); var yfit = yfittedStart.ToList(); xfit.RemoveAt(i); yfit.RemoveAt(i); double curX = xfittedStart[i]; ExponentialFit EXP = new ExponentialFit(xfit.ToArray(), yfit.ToArray()); double xmean = xfit.Average(); double n = (double)xfit.Count; //Equations from Sleuth p. 190 DoubleArray xs = DoubleArray.From(xfit); DoubleArray ys = DoubleArray.From(yfit); double denom = (n - 1) * xs.Std(); double denom2 = 1 / n; double Difs = xfittedStart[i] - xmean; Difs = Difs * Difs; Difs = Difs / denom; Difs = Difs + denom2 + 1.0; Difs = Math.Sqrt(Difs) * measError; double SEPred = Math.Sqrt(Math.Pow(measError, 2.0) + Difs); ////assume mean is 0 so just divide double predict = EXP.MakePredictionsAtPoints(new double[] { xfittedStart[i] }).First(); ////Leading to the following estimated differences double Error = (predict - yfittedStart[i]) / SEPred; //ArrayMathInPlace.Abs(Error); ////Giving the probability Error = ShoNS.MathFunc.ArrayMath.CdfNorm(DoubleArray.From(new List <double>() { Error }))[0]; if (Error > .99) { var q = from xx in GC where xx.ODValue == curX select xx; q.First().OutlierFlag = true; goAgain = true; break; } } if (!goAgain) { break; } } GC.FitData(); }
/// <summary> /// Function fits data, should be called everytime the fit changes /// </summary> public void FitData() { ValidDataSet = true; double[] XtoFit, YtoFit; XtoFit = FittedXValues; YtoFit = FittedYValues; //MixtureErrorModel = null; ExpFit = null; LinFit = null; if (XtoFit.Length >= 2) { //Linear Fit LinFit = new LinearFit(XtoFit, FittedLogYValues); pGrowthRate.DisplayFittingUsed = "Linear"; pGrowthRate.GrowthRate = LinFit.Slope; pGrowthRate.R2 = LinFit.R2; pGrowthRate.RMSE = LinFit.RMSE; pGrowthRate.FitterUsed = LinFit; //Exponential Fit and others if (XtoFit.Length > 2 && USE_EXPONENTIAL_FITTING) { ExpFit = new ExponentialFit(XtoFit, YtoFit); if (ExpFit.SuccessfulFit) { pGrowthRate.DisplayFittingUsed = "Exponential"; pGrowthRate.GrowthRate = ExpFit.GrowthRate; pGrowthRate.R2 = ExpFit.R2; pGrowthRate.RMSE = ExpFit.RMSE; pGrowthRate.FitterUsed = ExpFit; //(ExpFit.Y.Max() - ExpFit.Y.Min()) > .1 if (ExpFit.Y.Length > 5) { this.OffSetExp = new OffSetExponentialFit(ExpFit.X, ExpFit.Y, this[0].ODValue); #if !MONO this.LogisticModel = new LogisticModel(ExpFit.X, ExpFit.Y); #endif } else { this.OffSetExp = null; } } else { this.OffSetExp = null; } #if !MONO QuadModel = new QuadraticLinearRegression(XtoFit, YtoFit); #endif } //Now Set the Growth Rate pGrowthRate.NumPoints = FittedXValues.Length; OnPropertyChange(new PropertyChangedEventArgs("FittedValues")); OnPropertyChange(new PropertyChangedEventArgs("FittedLogValues")); } else { SetNoFit(); } }