public bool checkInput(bool rdbAllObservations, bool rdbObservationsInRange, DataSet dataSet, string uiTextBox_StopIndex, string uiTextBox_StartIndex, bool rdbPlotAllObservations, bool rdbPlotObservationsInRange, string uiTextBox_PlotStopIndex, string uiTextBox_PlotStartIndex) { int startindex = Convert.ToInt16(uiTextBox_StartIndex); int stopindex = Convert.ToInt16(uiTextBox_StopIndex); int plotstartindex = Convert.ToInt16(uiTextBox_PlotStartIndex); int plotstopindex = Convert.ToInt16(uiTextBox_PlotStopIndex); if ((rdbAllObservations || rdbObservationsInRange) && (rdbPlotAllObservations || rdbPlotObservationsInRange) && (dataSet != null) && (startindex <= stopindex) && (startindex >= 0 && stopindex >= 0) && (plotstartindex <= plotstopindex) && (plotstartindex >= 0 && plotstopindex >= 0)) { offset = 320; if (rdbAllObservations) { startindex = 0; stopindex = dataSet.amountOfVariables() - 2; } if (rdbObservationsInRange && stopindex >= dataSet.amountOfVariables()) { stopindex = dataSet.amountOfVariables() - 2; } if (rdbPlotAllObservations) { plotstartindex = 0; plotstopindex = dataSet.amountOfVariables() - 2; } if (rdbPlotObservationsInRange && plotstopindex >= dataSet.amountOfVariables()) { plotstopindex = dataSet.amountOfVariables() - 2; } _Worksheet sheet = WorksheetHelper.NewWorksheet("XR Chart"); generateXRChart(startindex, stopindex, plotstartindex, plotstopindex, dataSet, offset, sheet); return(true); } else { MessageBox.Show("Please correct all fields to generate X/R-Chart", "XR-Chart error"); } return(false); }
public void generateProcessCapability(int LSL, int USL, DataSet dataSet, _Worksheet sheet) { int index = 0; int row = 1; int column = 1; double correctionFactor1, correctionFactor2; double[] averages = new double[dataSet.amountOfVariables()]; double[] Rvalues = new double[dataSet.amountOfVariables()]; double[] averageOfAverages = new double[dataSet.amountOfVariables()]; double[] xChartUpperControlLimit = new double[dataSet.amountOfVariables()]; double[] xChartLowerControlLimit = new double[dataSet.amountOfVariables()]; double[] rChartUpperControlLimit = new double[dataSet.amountOfVariables()]; double[] rChartLowerControlLimit = new double[dataSet.amountOfVariables()]; double[] averageOfRvalues = new double[dataSet.amountOfVariables()]; int[] ArrayIndex = new int[dataSet.amountOfVariables()]; double[] constantC4 = new double[25] { 0.0, 0.7979, 0.8862, 0.9213, 0.9400, 0.9515, 0.9594, 0.9650, 0.9693, 0.9727, 0.9754, 0.9776, 0.9794, 0.9810, 0.9823, 0.9835, 0.9845, 0.9854, 0.9862, 0.9869, 0.9876, 0.9882, 0.9887, 0.9892, 0.9896 }; double[] constantD2 = new double[25] { 0.0, 1.128, 1.693, 2.059, 2.326, 2.534, 2.704, 2.847, 2.970, 3.078, 3.173, 3.258, 3.336, 3.407, 3.472, 3.532, 3.588, 3.640, 3.689, 3.735, 3.778, 3.819, 3.858, 3.895, 3.931 }; sheet.Cells[row, column] = "Index"; sheet.Cells[row, column + 1] = "Observation"; sheet.Cells[row, column + 2] = "Average"; sheet.Cells[row, column + 3] = "Max"; sheet.Cells[row, column + 4] = "Min"; sheet.Cells[row, column + 5] = "R"; for (index = 1; index < dataSet.amountOfVariables(); index++) { row++; sheet.Cells[row, column] = index - 1; sheet.Cells[row, column + 1] = dataSet.getVariables()[index].name; sheet.Cells[row, column + 2] = "=AVERAGE(" + dataSet.getWorksheet().Name + "!" + dataSet.getVariables()[index].Range + ")"; sheet.Cells[row, column + 3] = "=MAX(" + dataSet.getWorksheet().Name + "!" + dataSet.getVariables()[index].Range + ")"; sheet.Cells[row, column + 4] = "=MIN(" + dataSet.getWorksheet().Name + "!" + dataSet.getVariables()[index].Range + ")"; sheet.Cells[row, column + 5] = (double)(sheet.Cells[row, column + 3] as Range).Value - (double)(sheet.Cells[row, column + 4] as Range).Value; ArrayIndex[index - 1] = index; var cellValue = (double)(sheet.Cells[row, column + 2] as Range).Value; if (cellValue < -214682680) { cellValue = 0; // if cellValue is the result of a division by 0, set value to 0 } averages[index - 1] = cellValue; cellValue = (double)(sheet.Cells[row, column + 5] as Range).Value; Rvalues[index - 1] = cellValue; } // create variable to limit amount of subsamples in 1 measurement to 25 int safe; if (dataSet.rangeSize() > 24) { safe = 24; } else { safe = dataSet.rangeSize(); } if (dataSet.getVariableNamesInFirstRowOrColumn()) { correctionFactor1 = constantC4[safe - 1]; correctionFactor2 = constantD2[safe - 1]; } else { correctionFactor1 = constantC4[safe]; } correctionFactor2 = constantD2[safe]; double sigma = Rvalues.Average() / correctionFactor2; double Cp = (USL - LSL) / (6 * sigma); double Cpk = Math.Min((USL - averages.Average()) / (3 * sigma), (averages.Average() - LSL) / (3 * sigma)); row = 1; column = 1; sheet.Cells[row, column + 7] = "LSL"; sheet.Cells[row, column + 8] = "USL"; sheet.Cells[row, column + 9] = "Cp"; sheet.Cells[row, column + 10] = "Cpk"; row++; sheet.Cells[row, column + 7] = LSL; sheet.Cells[row, column + 8] = USL; sheet.Cells[row, column + 9] = Cp; sheet.Cells[row, column + 10] = Cpk; }
public void generateProcessCapability(List <Variable> variables, double LSL, double USL, DataSet dataSet, _Worksheet sheet) { int index = 0; int row = 1; int column = 1; double correctionFactor1, correctionFactor2; double[] averages = new double[dataSet.amountOfVariables()]; double[] Rvalues = new double[dataSet.amountOfVariables()]; double[] averageOfAverages = new double[dataSet.amountOfVariables()]; double[] xChartUpperControlLimit = new double[dataSet.amountOfVariables()]; double[] xChartLowerControlLimit = new double[dataSet.amountOfVariables()]; double[] rChartUpperControlLimit = new double[dataSet.amountOfVariables()]; double[] rChartLowerControlLimit = new double[dataSet.amountOfVariables()]; double[] averageOfRvalues = new double[dataSet.amountOfVariables()]; int[] ArrayIndex = new int[dataSet.amountOfVariables()]; double[] constantC4 = new double[25] { 0.0, 0.7979, 0.8862, 0.9213, 0.9400, 0.9515, 0.9594, 0.9650, 0.9693, 0.9727, 0.9754, 0.9776, 0.9794, 0.9810, 0.9823, 0.9835, 0.9845, 0.9854, 0.9862, 0.9869, 0.9876, 0.9882, 0.9887, 0.9892, 0.9896 }; double[] constantD2 = new double[25] { 0.0, 1.128, 1.693, 2.059, 2.326, 2.534, 2.704, 2.847, 2.970, 3.078, 3.173, 3.258, 3.336, 3.407, 3.472, 3.532, 3.588, 3.640, 3.689, 3.735, 3.778, 3.819, 3.858, 3.895, 3.931 }; sheet.Cells[row, column] = "Index"; sheet.Cells[row, column + 1] = "Observation"; sheet.Cells[row, column + 2] = "Average"; sheet.Cells[row, column + 3] = "STD DEV"; sheet.Cells[row, column + 4] = "Max"; sheet.Cells[row, column + 5] = "Min"; sheet.Cells[row, column + 6] = "R"; sheet.Cells[100, 100] = "=ROWS(" + dataSet.getWorksheet().Name + "!" + variables[0].Range + ")"; int length = Convert.ToInt32((sheet.Cells[100, 100] as Range).Value); sheet.Cells[100, 100] = ""; double temp = 0; List <double> allVals = new List <double>(); double[] all = new double[length * variables.Count]; foreach (Variable var in variables) { string ran = var.Range.ToString(); Array arr = dataSet.getWorksheet().Range[ran].Value; double[] vals = new double[length]; int count = 0; foreach (var item in arr) { temp = Convert.ToDouble(item); vals[count] = temp; count++; } allVals.AddRange(vals); } all = allVals.ToArray(); double totMean = 0; double totStd = 0; for (index = 0; index < variables.Count; index++) { row++; sheet.Cells[row, column] = index; sheet.Cells[row, column + 1] = variables[index].name; sheet.Cells[row, column + 2] = "=AVERAGE(" + dataSet.getWorksheet().Name + "!" + variables[index].Range + ")"; sheet.Cells[row, column + 3] = "=STDEV(" + dataSet.getWorksheet().Name + "!" + variables[index].Range + ")"; sheet.Cells[row, column + 4] = "=MAX(" + dataSet.getWorksheet().Name + "!" + variables[index].Range + ")"; sheet.Cells[row, column + 5] = "=MIN(" + dataSet.getWorksheet().Name + "!" + variables[index].Range + ")"; sheet.Cells[row, column + 6] = (double)(sheet.Cells[row, column + 4] as Range).Value - (double)(sheet.Cells[row, column + 5] as Range).Value; totMean = totMean + Convert.ToDouble((sheet.Cells[row, column + 2] as Range).Value); totStd = totStd + (Convert.ToDouble((sheet.Cells[row, column + 3] as Range).Value)); ArrayIndex[index] = index; var cellValue = (double)(sheet.Cells[row, column + 2] as Range).Value; if (cellValue < -214682680) { cellValue = 0; // if cellValue is the result of a division by 0, set value to 0 } averages[index] = cellValue; cellValue = (double)(sheet.Cells[row, column + 6] as Range).Value; Rvalues[index] = cellValue; } totMean = all.Average(); totStd = sheet.Application.WorksheetFunction.StDev(all); //totMean = totMean / variables.Count; //totStd = totStd / variables.Count; if (dataSet.getVariableNamesInFirstRowOrColumn()) { correctionFactor1 = constantC4[variables.Count - 1]; correctionFactor2 = constantD2[variables.Count - 1]; } else { correctionFactor1 = constantC4[variables.Count]; } correctionFactor2 = constantD2[variables.Count]; System.Diagnostics.Debug.WriteLine(totStd); double sigma = totStd; double Cp = (USL - LSL) / (6 * sigma); double Cpk = Math.Min((USL - totMean) / (3 * sigma), (totMean - LSL) / (3 * sigma)); double Pbelow = sheet.Application.WorksheetFunction.NormDist(LSL, totMean, totStd, true); double Pabove = 1.0 - sheet.Application.WorksheetFunction.NormDist(USL, totMean, totStd, true); row = 1; column = 1; sheet.Cells[row, column + 8] = "LSL"; sheet.Cells[row, column + 9] = "USL"; sheet.Cells[row, column + 10] = "Cp"; sheet.Cells[row, column + 11] = "Cpk"; sheet.Cells[row, column + 12] = "P(below LSL)"; sheet.Cells[row, column + 13] = " Per million"; sheet.Cells[row, column + 14] = "P(above USL)"; sheet.Cells[row, column + 15] = " Per million"; row++; sheet.Cells[row, column + 8] = LSL; sheet.Cells[row, column + 9] = USL; sheet.Cells[row, column + 10] = Cp; sheet.Cells[row, column + 11] = Cpk; sheet.Cells[row, column + 12] = Pbelow; sheet.Cells[row, column + 13] = Pbelow * 1000000; sheet.Cells[row, column + 14] = Pabove; sheet.Cells[row, column + 15] = Pabove * 1000000; }
private void generatePChart(int startindex, int stopindex, DataSet dataSet, _Worksheet sheet, int plotstartindex, int plotstopindex) { int index = 0; int row = 1; int column = 1; double[] pValues = new double[dataSet.amountOfVariables() - 1]; double[] averageOfPValues = new double[dataSet.amountOfVariables() - 1]; double[] pValuesInRange = new double[stopindex - startindex + 1]; double[] pChartUpperControlLimit = new double[dataSet.amountOfVariables()]; double[] pChartLowerControlLimit = new double[dataSet.amountOfVariables()]; sheet.Cells[row, column] = "Index"; sheet.Cells[row, column + 1] = "Observation"; sheet.Cells[row, column + 2] = "Average"; for (index = 1; index < dataSet.amountOfVariables(); index++) { row++; sheet.Cells[row, column] = index - 1; sheet.Cells[row, column + 1] = dataSet.getVariables()[index].name; sheet.Cells[row, column + 2] = "=AVERAGE(" + dataSet.getWorksheet().Name + "!" + dataSet.getVariables()[index].Range + ")"; var cellValue = (double)(sheet.Cells[row, column + 2] as Range).Value; if (cellValue < -214682680) { cellValue = 0; // if cellValue is the result of a division by 0, set value to 0 } pValues[index - 1] = cellValue; if (cellValue > 1) { MessageBox.Show("Cannot generate P-Chart; Data in " + dataSet.Name + " contains samples greater than 1"); return; } } for (index = startindex; index <= stopindex; index++) { pValuesInRange[index - startindex] = pValues[index]; } double pChartCorrection = Math.Pow(((pValuesInRange.Average() * (1 - pValuesInRange.Average())) / dataSet.rangeSize()), (0.3333333)); for (index = 0; index < dataSet.amountOfVariables() - 1; index++) { averageOfPValues[index] = pValuesInRange.Average(); pChartUpperControlLimit[index] = pValues.Average() + pChartCorrection; pChartLowerControlLimit[index] = pValues.Average() - pChartCorrection; } // new arrays for subsets of existing arrays but within limits double[] plotpValues = new double[plotstopindex - plotstartindex + 1]; double[] plotaverageOfPValues = new double[plotstopindex - plotstartindex + 1]; double[] plotpChartUpperControlLimit = new double[plotstopindex - plotstartindex + 1]; double[] plotpChartLowerControlLimit = new double[plotstopindex - plotstartindex + 1]; int[] ArrayIndex = new int[plotstopindex - plotstartindex + 1]; for (int i = plotstartindex; i <= plotstopindex; i++) { plotpValues[i - plotstartindex] = pValues[i]; plotaverageOfPValues[i - plotstartindex] = averageOfPValues[i]; plotpChartLowerControlLimit[i - plotstartindex] = pChartLowerControlLimit[i]; plotpChartUpperControlLimit[i - plotstartindex] = pChartUpperControlLimit[i]; ArrayIndex[i - plotstartindex] = i; } var Xcharts = (ChartObjects)sheet.ChartObjects(); var XchartObject = Xcharts.Add(340, 20, 550, 300); var Xchart = XchartObject.Chart; Xchart.ChartType = XlChartType.xlXYScatterLines; Xchart.ChartWizard(Title: "P-Chart " + dataSet.Name, HasLegend: true); var XseriesCollection = (SeriesCollection)Xchart.SeriesCollection(); var pseries = XseriesCollection.NewSeries(); var CLseries = XseriesCollection.NewSeries(); var UCLseries = XseriesCollection.NewSeries(); var LCLseries = XseriesCollection.NewSeries(); pseries.Name = ("Proportion"); CLseries.Name = ("Center line"); UCLseries.Name = ("UCL"); LCLseries.Name = ("LCL"); pseries.Values = plotpValues; CLseries.Values = plotaverageOfPValues; UCLseries.Values = plotpChartUpperControlLimit; LCLseries.Values = plotpChartLowerControlLimit; pseries.XValues = ArrayIndex; CLseries.XValues = ArrayIndex; UCLseries.XValues = ArrayIndex; LCLseries.XValues = ArrayIndex; }
public void createRegression(List <Variable> variablesD, List <Variable> variablesI, DataSet dataSet, DataSet dataSet2, bool[] graphs, List <Variable> variablesPrediction) { _Worksheet sheet = WorksheetHelper.NewWorksheet("Regression"); double confLevel = model.confidenceLevel; sheet.Cells[100, 100] = "=ROWS(" + dataSet.getWorksheet().Name + "!" + variablesI[0].Range + ")"; int length = Convert.ToInt32((sheet.Cells[100, 100] as Range).Value); sheet.Cells[100, 100] = ""; int row = 1; sheet.Cells[1, 1] = "Regression Summary"; sheet.Cells[2, 1] = "R"; sheet.Cells[3, 1] = "R-square"; sheet.Cells[4, 1] = "adjusted R-square"; sheet.Cells[5, 1] = "stErr of Estimate"; sheet.Cells[9, 1] = "ANOVA Table"; sheet.Cells[10, 1] = "Explained"; sheet.Cells[11, 1] = "Unexplained"; sheet.Cells[8, 3] = "Sum of"; sheet.Cells[9, 3] = "Squares"; sheet.Cells[8, 2] = "Degrees of"; sheet.Cells[9, 2] = "Freedom"; sheet.Cells[8, 4] = "Mean"; sheet.Cells[9, 4] = "Squares"; sheet.Cells[9, 5] = "F-Ratio"; sheet.Cells[9, 6] = "p-Value"; sheet.Cells[15, 1] = "Regression Table"; sheet.Cells[16, 1] = "Constant"; row = 17; foreach (Variable var in variablesI) { sheet.Cells[row, 1] = var.name; row++; } lowestDataRow = row; sheet.Cells[15, 2] = "Coefficient"; sheet.Cells[14, 3] = "Standard"; sheet.Cells[15, 3] = "Error"; sheet.Cells[15, 4] = "t-value"; sheet.Cells[15, 5] = "p-value"; sheet.Cells[14, 6] = "Confidence Interval " + confLevel.ToString() + "%"; sheet.Range[sheet.Cells[14, 6], sheet.Cells[14, 7]].Merge(); sheet.Cells[15, 6] = "Lower"; sheet.Cells[15, 7] = "Upper"; sheet.get_Range("B1", "J200").Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter; sheet.get_Range("A1", "B1").Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDouble; sheet.get_Range("A9", "F9").Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDouble; sheet.get_Range("A15", "G15").Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDouble; sheet.get_Range("B3", "B5").NumberFormat = "0.0000"; sheet.get_Range("B18", "B20").NumberFormat = "0.0000"; sheet.get_Range("D18", "E19").NumberFormat = "0.0000"; Globals.ExcelAddIn.Application.ActiveWindow.DisplayGridlines = false; // //calculate values of regression // double[] b = new double[variablesI.Count]; double[] yData = calcYdata(variablesD, dataSet, length); double[,] xData = calcXdata(length, dataSet, variablesI); b = calculateCoefB(yData, xData, b); double a = b[0]; var X = DenseMatrix.OfArray(xData); var B = new DenseVector(b); var Y = new DenseVector(yData); var yh = X.Multiply(B); var er = Y.Subtract(yh); double[] error = er.ToArray(); double[] yhat = yh.ToArray(); double R2 = sheet.Application.WorksheetFunction.Correl(yData, yhat); double R = Math.Pow(R2, 2); double Radj = 1.0 - ((1 - R) * (length - 1) / (length - variablesI.Count - 1)); double err = 0; int i = 0; while (i < error.Length) { err = err + Math.Pow(error[i], 2); i++; } err = Math.Sqrt(err / (length - 4)); double[] anovaResults = calculateAnova(yData, yhat, variablesI.Count(), sheet); multicollinearity(sheet, xData, variablesI.Count); if (model.doDurbinWatson == true) { DurbinWatson(sheet, error); } // //Calculate regressionTable // double MSE = anovaResults[6]; var Xt = X.Transpose(); var tempM = (Xt.Multiply(X)).Inverse(); var variancesM = tempM.Multiply(MSE); double[] variances = variancesM.Diagonal().ToArray(); double[] std = new double[variances.Length]; int index = 0; foreach (double elem in variances) { std[index] = Math.Sqrt(elem); //System.Diagnostics.Debug.WriteLine(std[index]); index++; } double[] tValue = new double[b.Length]; double[] pValue = new double[b.Length]; double[] lowerInt = new double[b.Length]; double[] higherInt = new double[b.Length]; for (index = 0; index < b.Length; index++) { tValue[index] = b[index] / std[index]; pValue[index] = sheet.Application.WorksheetFunction.TDist(Math.Abs(tValue[index]), length - variablesI.Count - 1, 2); lowerInt[index] = b[index] - sheet.Application.WorksheetFunction.TInv(1 - confLevel / 100, length - variablesI.Count - 1) * std[index]; higherInt[index] = b[index] + sheet.Application.WorksheetFunction.TInv(1 - confLevel / 100, length - variablesI.Count - 1) * std[index]; //System.Diagnostics.Debug.WriteLine("{0}, p={1}, lower = {2}, n={3}, inv={4}, std={5}, inv2={6}",real_p, pValue[index], lowerInt[index], length - 1, sheet.Application.WorksheetFunction.TInv(1-0.975, length - 1), std[index], sheet.Application.WorksheetFunction.TInv(1 - 0.95, length - 1)); } // //Prediction // if (model.doPrediction) { double[,] xDataPred = calcXdata(dataSet2.getNrDataRows(), dataSet2, variablesPrediction); int[] rightCell = { dataSet2.getRange().Row, dataSet2.getRange().Column + dataSet2.amountOfVariables() }; //{row, column} prediction(dataSet2.getWorksheet(), xDataPred, b, length, variablesI.Count, X, MSE, rightCell, variablesD[0].name); setPrediction(false); } // //Draw graphs // if (graphs[0]) { drawGraphs(sheet, yData, yhat, "Scatter plot of fitted values vs. actual values"); } if (graphs[1]) { drawGraphs(sheet, yData, error, "Scatter plot of residuals vs. fitted values"); } if (graphs[2]) { int varNumber = 1; foreach (Variable var in variablesI) { double[] xDataTemp = new double[xData.GetLength(0)]; for (int x = 0; x < xData.GetLength(0); x++) { xDataTemp[x] = xData[x, varNumber]; } drawGraphs(sheet, xDataTemp, error, "Scatter plot of residuals vs. " + var.name); varNumber++; } } if (graphs[3]) { int varNumber = 1; foreach (Variable var in variablesI) { double[] xDataTemp = new double[xData.GetLength(0)]; for (int x = 0; x < xData.GetLength(0); x++) { xDataTemp[x] = xData[x, varNumber]; } drawGraphs(sheet, xDataTemp, yData, "Scatter plot of actual Y values vs. " + var.name); varNumber++; } } if (graphs[4]) { int varNumber = 1; foreach (Variable var in variablesI) { double[] xDataTemp = new double[xData.GetLength(0)]; for (int x = 0; x < xData.GetLength(0); x++) { xDataTemp[x] = xData[x, varNumber]; } drawGraphs(sheet, xDataTemp, yhat, "Scatter plot of fitted Y values vs. " + var.name); varNumber++; } } // //print results to excel sheet // sheet.Cells[16, 2] = a; row = 17; foreach (Variable var in variablesI) { sheet.Cells[row, 2] = b[row - 16]; row++; } FillR(sheet, R2, R, Radj, err); FillAnova(sheet, anovaResults); FillRegressionTable(sheet, std, tValue, pValue, lowerInt, higherInt); ((Range)sheet.Cells[1, 1]).EntireColumn.AutoFit(); ((Range)sheet.Cells[1, 2]).EntireColumn.AutoFit(); ((Range)sheet.Cells[1, 3]).EntireColumn.AutoFit(); ((Range)sheet.Cells[1, 4]).EntireColumn.AutoFit(); ((Range)sheet.Cells[1, 5]).EntireColumn.AutoFit(); ((Range)sheet.Cells[1, 6]).EntireColumn.ColumnWidth = 13; ((Range)sheet.Cells[1, 7]).EntireColumn.ColumnWidth = 13; ((Range)sheet.Cells[1, 8]).EntireColumn.ColumnWidth = 13; ((Range)sheet.Cells[1, 9]).EntireColumn.ColumnWidth = 13; ((Range)sheet.Cells[1, 10]).EntireColumn.AutoFit(); }
public void generateXRChart(int startindex, int stopindex, int plotstartindex, int plotstopindex, DataSet dataSet, int offset, _Worksheet sheet) { // declaration of variables int index = 0; int row = 1; int column = 1; double xControlLimitFactor, rControlLimitFactor1, rControlLimitFactor2; double[] averages = new double[dataSet.amountOfVariables()]; double[] Rvalues = new double[dataSet.amountOfVariables()]; double[] averageOfAverages = new double[dataSet.amountOfVariables()]; double[] xChartUpperControlLimit = new double[dataSet.amountOfVariables()]; double[] xChartLowerControlLimit = new double[dataSet.amountOfVariables()]; double[] rChartUpperControlLimit = new double[dataSet.amountOfVariables()]; double[] rChartLowerControlLimit = new double[dataSet.amountOfVariables()]; double[] averageOfRvalues = new double[dataSet.amountOfVariables()]; double[] RvaluesInRange = new double[stopindex - startindex + 1]; double[] averageOfAveragesInRange = new double[stopindex - startindex + 1]; int[] ArrayIndex = new int[plotstopindex - plotstartindex + 1]; double[] xChartConstants = new double[25] { 0.0, 1.880, 1.023, 0.729, 0.577, 0.483, 0.419, 0.373, 0.337, 0.308, 0.285, 0.266, 0.249, 0.235, 0.223, 0.212, 0.203, 0.194, 0.187, 0.180, 0.173, 0.167, 0.162, 0.157, 0.153 }; double[] rChartConstants1 = new double[25] { 0, 0, 0, 0, 0, 0, 0.076, 0.136, 0.184, 0.223, 0.256, 0.283, 0.307, 0.328, 0.347, 0.363, 0.378, 0.391, 0.403, 0.415, 0.425, 0.434, 0.443, 0.451, 0.459 }; double[] rChartConstants2 = new double[25] { 0, 3.267, 2.574, 2.282, 2.114, 2.004, 1.924, 1.864, 1.816, 1.777, 1.744, 1.717, 1.693, 1.672, 1.653, 1.637, 1.662, 1.607, 1.597, 1.585, 1.575, 1.566, 1.557, 1.548, 1.541 }; // write labels to sheet sheet.Cells[row, column] = "Index"; sheet.Cells[row, column + 1] = "Observation"; sheet.Cells[row, column + 2] = "Average"; sheet.Cells[row, column + 3] = "Max"; sheet.Cells[row, column + 4] = "Min"; sheet.Cells[row, column + 5] = "R"; // write values to sheet for (index = 1; index < dataSet.amountOfVariables(); index++) { row++; sheet.Cells[row, column] = index - 1; sheet.Cells[row, column + 1] = dataSet.getVariables()[index].name; sheet.Cells[row, column + 2] = "=AVERAGE(" + dataSet.getWorksheet().Name + "!" + dataSet.getVariables()[index].Range + ")"; sheet.Cells[row, column + 3] = "=MAX(" + dataSet.getWorksheet().Name + "!" + dataSet.getVariables()[index].Range + ")"; sheet.Cells[row, column + 4] = "=MIN(" + dataSet.getWorksheet().Name + "!" + dataSet.getVariables()[index].Range + ")"; sheet.Cells[row, column + 5] = (double)(sheet.Cells[row, column + 3] as Range).Value - (double)(sheet.Cells[row, column + 4] as Range).Value; var cellValue = (double)sheet.Cells[row, column + 2].Value; if (cellValue < -214682680) { cellValue = 0; // if cellValue is the result of a division by 0, set value to 0 } averages[index - 1] = cellValue; cellValue = (double)(sheet.Cells[row, column + 5] as Range).Value; Rvalues[index - 1] = cellValue; } // make arrays for calculation within limits for (index = startindex; index <= stopindex; index++) { RvaluesInRange[index - startindex] = Rvalues[index]; averageOfAveragesInRange[index - startindex] = averages[index]; } // create variable to limit amount of subsamples in 1 measurement to 25 int safe; if (dataSet.rangeSize() > 24) { safe = 24; } else { safe = dataSet.rangeSize(); } if (dataSet.getVariableNamesInFirstRowOrColumn()) { xControlLimitFactor = xChartConstants[safe - 1]; rControlLimitFactor1 = rChartConstants1[safe - 1]; rControlLimitFactor2 = rChartConstants2[safe - 1]; } else { xControlLimitFactor = xChartConstants[safe]; } rControlLimitFactor1 = rChartConstants1[safe]; rControlLimitFactor2 = rChartConstants2[safe]; for (index = 0; index < dataSet.amountOfVariables(); index++) { averageOfAverages[index] = averageOfAveragesInRange.Average(); xChartUpperControlLimit[index] = averageOfAveragesInRange.Average() + (xControlLimitFactor * Rvalues.Average()); xChartLowerControlLimit[index] = averageOfAveragesInRange.Average() - (xControlLimitFactor * Rvalues.Average()); averageOfRvalues[index] = RvaluesInRange.Average(); rChartUpperControlLimit[index] = RvaluesInRange.Average() * rControlLimitFactor2; rChartLowerControlLimit[index] = RvaluesInRange.Average() * rControlLimitFactor1; } // new subsets of arrays for plotting data double[] plotaverages = new double[plotstopindex - plotstartindex + 1]; double[] plotRvalues = new double[plotstopindex - plotstartindex + 1]; double[] plotaverageOfAverages = new double[plotstopindex - plotstartindex + 1]; double[] plotxChartUpperControlLimit = new double[plotstopindex - plotstartindex + 1]; double[] plotxChartLowerControlLimit = new double[plotstopindex - plotstartindex + 1]; double[] plotrChartUpperControlLimit = new double[plotstopindex - plotstartindex + 1]; double[] plotrChartLowerControlLimit = new double[plotstopindex - plotstartindex + 1]; double[] plotaverageOfRvalues = new double[plotstopindex - plotstartindex + 1]; for (int i = plotstartindex; i <= plotstopindex; i++) { ArrayIndex[i - plotstartindex] = i; plotaverages[i - plotstartindex] = averages[i]; plotRvalues[i - plotstartindex] = Rvalues[i]; plotaverageOfAverages[i - plotstartindex] = averageOfAverages[i]; plotxChartUpperControlLimit[i - plotstartindex] = xChartUpperControlLimit[i]; plotxChartLowerControlLimit[i - plotstartindex] = xChartLowerControlLimit[i]; plotrChartUpperControlLimit[i - plotstartindex] = rChartUpperControlLimit[i]; plotrChartLowerControlLimit[i - plotstartindex] = rChartLowerControlLimit[i]; plotaverageOfRvalues[i - plotstartindex] = averageOfRvalues[i]; } var Xcharts = (ChartObjects)sheet.ChartObjects(); var XchartObject = Xcharts.Add(340, 20, 550, 300); var Xchart = XchartObject.Chart; Xchart.ChartType = XlChartType.xlXYScatterLines; Xchart.ChartWizard(Title: "X-Chart " + dataSet.Name, HasLegend: true); var XseriesCollection = (SeriesCollection)Xchart.SeriesCollection(); var avgseries = XseriesCollection.NewSeries(); var avgAvgseries = XseriesCollection.NewSeries(); var UCLseries = XseriesCollection.NewSeries(); var LCLseries = XseriesCollection.NewSeries(); avgseries.Name = ("Observation Averages"); avgAvgseries.Name = ("Center Line"); UCLseries.Name = ("UCL"); LCLseries.Name = ("LCL"); avgseries.Values = plotaverages; avgAvgseries.Values = plotaverageOfAverages; UCLseries.Values = plotxChartUpperControlLimit; LCLseries.Values = plotxChartLowerControlLimit; avgseries.XValues = ArrayIndex; UCLseries.XValues = ArrayIndex; LCLseries.XValues = ArrayIndex; avgAvgseries.XValues = ArrayIndex; var Rcharts = (ChartObjects)sheet.ChartObjects(); var RchartObject = Rcharts.Add(340, 20 + offset, 550, 300); var Rchart = RchartObject.Chart; Rchart.ChartType = XlChartType.xlXYScatterLines; Rchart.ChartWizard(Title: "R-Chart " + dataSet.Name, HasLegend: true); var RseriesCollection = (SeriesCollection)Rchart.SeriesCollection(); var rSeries = RseriesCollection.NewSeries(); var averageRSeries = RseriesCollection.NewSeries(); var UCLSeries = RseriesCollection.NewSeries(); var LCLSeries = RseriesCollection.NewSeries(); rSeries.Name = ("Observation R"); averageRSeries.Name = ("Center Line"); UCLSeries.Name = ("UCL"); LCLSeries.Name = ("LCL"); rSeries.Values = plotRvalues; averageRSeries.Values = plotaverageOfRvalues; UCLSeries.Values = plotrChartUpperControlLimit; LCLSeries.Values = plotrChartLowerControlLimit; rSeries.XValues = ArrayIndex; averageRSeries.XValues = ArrayIndex; UCLSeries.XValues = ArrayIndex; LCLSeries.XValues = ArrayIndex; }