patterncsharpMinor
Two-way ANOVA table
Viewed 0 times
twowaytableanova
Problem
I changed some methods used previously, and I am wondering if I need to do the multiple for loops over and over again. How do I convert my arrays to lists to use in nested
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading.Tasks;
using TwoWayAnova;
namespace TwoWayAnovaTable
{
public partial class TwoWayAnovaTable : Form
{
public TwoWayAnovaTable()
{
InitializeComponent();
}
private static readonly char[] Separators = { ',', ' ' };
private static double _aTreatmentSumOfSquares;
private static double _bTreatmentSumOfSquares;
private static double _interactionSumOfSquares;
private static double _errorSumOfSquares;
private static double _sumOfSquares;
private static double _aMeanTreatmentSumOfSquares;
private static double _bMeanTreatmentSumOfSquares;
private static double _interactionMeanSumOfSquares;
private static double _meanErrorSumOfSquares;
private static double _aTreatmentDegreesOfFreedom;
private static double _bTreatmentDegreesOfFreedom;
private static double _interactionDegreesOfFreedom;
private static double _errorDegreesOfFreedom;
private static double _totalDegreesOfFreedom;
private static double _aTestStatistic;
private static double _bTestStatistic;
private static double _interactionTestStatistic;
private static double _aPValue;
private static double _bPValue;
private static double _interactionPValue;
private static void ProcessFile()
{
var lines = File.ReadLines("Data.csv");
var numbers = ProcessRawNumbers(lines);
var rowTotal = new List();
var squareRowTotal = new List();
var rowMean = new List();
var totalElements = 0;
var totalInRow = new List();
var rowTotalSquareByN = new List();
va
for loops?```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading.Tasks;
using TwoWayAnova;
namespace TwoWayAnovaTable
{
public partial class TwoWayAnovaTable : Form
{
public TwoWayAnovaTable()
{
InitializeComponent();
}
private static readonly char[] Separators = { ',', ' ' };
private static double _aTreatmentSumOfSquares;
private static double _bTreatmentSumOfSquares;
private static double _interactionSumOfSquares;
private static double _errorSumOfSquares;
private static double _sumOfSquares;
private static double _aMeanTreatmentSumOfSquares;
private static double _bMeanTreatmentSumOfSquares;
private static double _interactionMeanSumOfSquares;
private static double _meanErrorSumOfSquares;
private static double _aTreatmentDegreesOfFreedom;
private static double _bTreatmentDegreesOfFreedom;
private static double _interactionDegreesOfFreedom;
private static double _errorDegreesOfFreedom;
private static double _totalDegreesOfFreedom;
private static double _aTestStatistic;
private static double _bTestStatistic;
private static double _interactionTestStatistic;
private static double _aPValue;
private static double _bPValue;
private static double _interactionPValue;
private static void ProcessFile()
{
var lines = File.ReadLines("Data.csv");
var numbers = ProcessRawNumbers(lines);
var rowTotal = new List();
var squareRowTotal = new List();
var rowMean = new List();
var totalElements = 0;
var totalInRow = new List();
var rowTotalSquareByN = new List();
va
Solution
First off, add a
to the top of your partial class. That way you can change all of the calls from the library from:
into:
These assignment could also be changed to go right into your static strings.
This line:
Should be split into a line per variable. You should also rename the variables to something meaningful:
You need to get more consistent with your use of var vs variable type. My suggestion would be to use var anywhere a variable is assigned within a method.
I would also change the array declarations to List declarations. This will allow you to change a couple of the for(...) loops into foreach(...) loops.
In your library, I'm not sure why you are doing this in your methods:
They should be
You also need to store sumOfSquares somewhere that the other methods will be able to access it. Currently, it is only valid in the method that you are calculating it in.
The method names should be changed to start with a capital letter treatmentSumOfSquares would be TreatmentSumOfSquares etc. This is standard C# naming convention.
Anywhere you have an array passed into a method, change it to IEnumerable.
This is a good start, there are a few more things I see, but they are pretty minor. If you want a full clean up, let me know and I'll add it later.
Good luck.
EDIT:
Here is a first kick at the can for a clean up. I have basically used what Jesse posted for the library, but renamed to methods to portray what they are doing. The way you named them before indicated to me that they were Properties
Code partially cleaned up:
```
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OneWayAnovaClassLibrary;
namespace OneWayAnovaTable
{
public class OneWayAnovaTable : Form
{
public OneWayAnovaTable()
{
InitializeComponent();
}
private static readonly char[] Separators = {',', ' '};
// Not sure why these are static
// Leaving them because you probably have a reason.
private static double _treatmentSumOfSquares;
private static double _errorSumOfSquares;
private static double _sumOfSquares;
private static double _meanTreatmentSumOfSquares;
private static double _meanErrorSumOfSquares;
private static double _testStatistic;
// Not sure what these variables represent
// Rename as appropriate.
private static double _tdf;
private static double _edf;
private static double _totDf;
private static double _p;
private static void ProcessFile()
{
var lines = File.ReadLines("Data.csv");
var numbers = ProcessRawNumbers(lines);
var rowTotal = new List();
var squareRowTotal = new List();
var rowMean = new List();
var totalElements = 0;
var totalInRow = new List();
foreach (var values in numbers)
{
var sumOfRow = values.Sum();
rowTotal.Add(sumOfRow);
squareRowTotal.Add(values.Select(v => v*v).Sum());
rowMean.Add(sumOfRow/values.Count);
totalInRow.Add(values.Count);
totalElements += values.Count;
}
var grandTotal = rowTotal.Sum();
_sumOfSquares = OneWayAnova.CalculateTotalSumOfSquares(squareRowTotal, grandTotal, totalElements);
_treatmentSumOfSquares = OneWayAnova.CalculateTreatmentSumOfSquares(rowTotal.ToArray(), totalInRow,
grandTotal,
totalElements);
_errorSumOfSquares = OneWayAnova.CalculateErrorSumOfSquares(_sumOfSquares, _treatmentSumOfSquares);
_meanTreatmentSumOfSquares = OneWayAnova.CalculateMeanTreatmentSumOfSquares(_treatmentSumOfSquares,
totalInRow.ToArray());
_meanErrorSumOfSquares = OneWayAnova.CalculateMeanErrorSumOfSquares(_errorSumOfSquares, (numbers.Count - 1),
(totalElements - 1));
_testStatistic = OneWayAnova.CalculateTestStatistic(_meanTreatmentSumOfSquares, _meanErrorSumOfSquares);
_p = OneWayAnova.CalculatePValue(_testStatistic, (numbers.Count - 1), (totalElements -
using OneWayAnovaClassLibrary;to the top of your partial class. That way you can change all of the calls from the library from:
OneWayAnovaClassLibrary.OneWayAnova(...)into:
OneWayAnova(...)These assignment could also be changed to go right into your static strings.
This line:
static string TSS, ESS, TotSS, TDF, EDF, TotDF, TMS, EMS, F, p;Should be split into a line per variable. You should also rename the variables to something meaningful:
static string TreatmentSumOfSquares;
static string ErrorSumOfSquares;
...You need to get more consistent with your use of var vs variable type. My suggestion would be to use var anywhere a variable is assigned within a method.
I would also change the array declarations to List declarations. This will allow you to change a couple of the for(...) loops into foreach(...) loops.
In your library, I'm not sure why you are doing this in your methods:
double errorSumOfSquares = 0;
return errorSumOfSquares = sumOfSquares - treatmentSumOfSquares;They should be
return sumOfSquares - treatmentSumOfSquares;You also need to store sumOfSquares somewhere that the other methods will be able to access it. Currently, it is only valid in the method that you are calculating it in.
The method names should be changed to start with a capital letter treatmentSumOfSquares would be TreatmentSumOfSquares etc. This is standard C# naming convention.
Anywhere you have an array passed into a method, change it to IEnumerable.
This is a good start, there are a few more things I see, but they are pretty minor. If you want a full clean up, let me know and I'll add it later.
Good luck.
EDIT:
Here is a first kick at the can for a clean up. I have basically used what Jesse posted for the library, but renamed to methods to portray what they are doing. The way you named them before indicated to me that they were Properties
Code partially cleaned up:
```
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OneWayAnovaClassLibrary;
namespace OneWayAnovaTable
{
public class OneWayAnovaTable : Form
{
public OneWayAnovaTable()
{
InitializeComponent();
}
private static readonly char[] Separators = {',', ' '};
// Not sure why these are static
// Leaving them because you probably have a reason.
private static double _treatmentSumOfSquares;
private static double _errorSumOfSquares;
private static double _sumOfSquares;
private static double _meanTreatmentSumOfSquares;
private static double _meanErrorSumOfSquares;
private static double _testStatistic;
// Not sure what these variables represent
// Rename as appropriate.
private static double _tdf;
private static double _edf;
private static double _totDf;
private static double _p;
private static void ProcessFile()
{
var lines = File.ReadLines("Data.csv");
var numbers = ProcessRawNumbers(lines);
var rowTotal = new List();
var squareRowTotal = new List();
var rowMean = new List();
var totalElements = 0;
var totalInRow = new List();
foreach (var values in numbers)
{
var sumOfRow = values.Sum();
rowTotal.Add(sumOfRow);
squareRowTotal.Add(values.Select(v => v*v).Sum());
rowMean.Add(sumOfRow/values.Count);
totalInRow.Add(values.Count);
totalElements += values.Count;
}
var grandTotal = rowTotal.Sum();
_sumOfSquares = OneWayAnova.CalculateTotalSumOfSquares(squareRowTotal, grandTotal, totalElements);
_treatmentSumOfSquares = OneWayAnova.CalculateTreatmentSumOfSquares(rowTotal.ToArray(), totalInRow,
grandTotal,
totalElements);
_errorSumOfSquares = OneWayAnova.CalculateErrorSumOfSquares(_sumOfSquares, _treatmentSumOfSquares);
_meanTreatmentSumOfSquares = OneWayAnova.CalculateMeanTreatmentSumOfSquares(_treatmentSumOfSquares,
totalInRow.ToArray());
_meanErrorSumOfSquares = OneWayAnova.CalculateMeanErrorSumOfSquares(_errorSumOfSquares, (numbers.Count - 1),
(totalElements - 1));
_testStatistic = OneWayAnova.CalculateTestStatistic(_meanTreatmentSumOfSquares, _meanErrorSumOfSquares);
_p = OneWayAnova.CalculatePValue(_testStatistic, (numbers.Count - 1), (totalElements -
Code Snippets
using OneWayAnovaClassLibrary;OneWayAnovaClassLibrary.OneWayAnova(...)OneWayAnova(...)static string TSS, ESS, TotSS, TDF, EDF, TotDF, TMS, EMS, F, p;static string TreatmentSumOfSquares;
static string ErrorSumOfSquares;
...Context
StackExchange Code Review Q#14248, answer score: 6
Revisions (0)
No revisions yet.