patterncsharpMinor
Finding and generating an Anova table
Viewed 0 times
generatinganovafindingandtable
Problem
I would like to optimize the code to be efficient. Basically the code finds and generates an Anova table with the p-value also computed.
I am inputting a text file with data delimited with commas.
This is the main function that calls the methods in the library file:
```
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;
namespace OneWayAnovaTable
{
public partial class OneWayAnovaTable : Form
{
public OneWayAnovaTable()
{
InitializeComponent();
}
static string TSS, ESS, TotSS, TDF, EDF, TotDF, TMS, EMS, F, p;
private void ReadFile()
{
List> numbers = new List>();
foreach (string line in File.ReadAllLines(@"data.txt"))
{
var list = new List();
foreach (string s in line.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries))
{
double i;
if (Double.TryParse(s, out i))
{
list.Add(i);
}
}
numbers.Add(list);
}
double[] rowTotal = new double[numbers.Count];
double[] squareRowTotal = new double[numbers.Count];
double[] rowMean = new double[numbers.Count];
int totalElements = 0;
int[] totalInRow = new int[numbers.Count()];
double grandTotalMean = 0;
double grandMean = 0;
double grandTotal=0;
for (int row = 0; row v * v).Sum();
rowMean[row] = rowTotal[row] / values.Length;
totalInRow[row] += values.Length;
totalElements += totalInRow[row];
grandTotalMean += rowMean[row];
grandMean += rowMean[row]/numbers.Count;
}
for (int j=0; j<rowTotal.Length; j++)
{
grandTotal += rowTotal[j];
}
double sumOfSquares = OneWayAnovaClas
I am inputting a text file with data delimited with commas.
This is the main function that calls the methods in the library file:
```
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;
namespace OneWayAnovaTable
{
public partial class OneWayAnovaTable : Form
{
public OneWayAnovaTable()
{
InitializeComponent();
}
static string TSS, ESS, TotSS, TDF, EDF, TotDF, TMS, EMS, F, p;
private void ReadFile()
{
List> numbers = new List>();
foreach (string line in File.ReadAllLines(@"data.txt"))
{
var list = new List();
foreach (string s in line.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries))
{
double i;
if (Double.TryParse(s, out i))
{
list.Add(i);
}
}
numbers.Add(list);
}
double[] rowTotal = new double[numbers.Count];
double[] squareRowTotal = new double[numbers.Count];
double[] rowMean = new double[numbers.Count];
int totalElements = 0;
int[] totalInRow = new int[numbers.Count()];
double grandTotalMean = 0;
double grandMean = 0;
double grandTotal=0;
for (int row = 0; row v * v).Sum();
rowMean[row] = rowTotal[row] / values.Length;
totalInRow[row] += values.Length;
totalElements += totalInRow[row];
grandTotalMean += rowMean[row];
grandMean += rowMean[row]/numbers.Count;
}
for (int j=0; j<rowTotal.Length; j++)
{
grandTotal += rowTotal[j];
}
double sumOfSquares = OneWayAnovaClas
Solution
Some advice:
- Replace
File.ReadAllLineswithFile.ReadLines— ReadAllLines will read all file content into memory before the iteration, andforeachis really designed forIEnumerable.
- Definitely replace
for (int row = 0; row
- Not sure that replacing foreach (string line in File.ReadAllLines(@"data.txt"))` will help you, as it is heavily dependent on disc I/O operations.
Context
StackExchange Code Review Q#13993, answer score: 2
Revisions (0)
No revisions yet.