#include "ANOVA.h"
#include "ANOVA.h"


ANOVA::ANOVA() {};


void ANOVA::ANOVA1(){

cout << "ONE WAY ANOVA" << endl;


int treatments;
int samples;
float time[treatments][samples];
float subAvg[treatments];
float overallAvg = 0;
float SSb = 0;
float SSw = 0;
float MSb = 0;
float MSw = 0;
float F = 0;
int DFb = 0;
int DFw = 0;
int done = 0;


while(!done){

cout << "Enter number of treatments: ";
cin >> treatments;
cout << "Enter number of samples: ";
cin >> samples;

for(int i = 0; i < treatments; i++){
	for(int j = 0; j < samples; j++){
		cin >> time[i][j];}}

for(int a = 0; a < 2; a++){

for(int i = 0; i < treatments; i++){
	subAvg[i] = 0;}

for(int i = 0; i < treatments; i++){
	for(int j = 0; j < samples; j++){

		overallAvg += time[i][j];
		subAvg[i] += time[i][j];}
		subAvg[i] = subAvg[i]/samples;
		cout << "Col " << i << " average: " <<subAvg[i] <<endl; }
overallAvg = overallAvg/(samples*treatments);

cout << "Overall average: " << overallAvg << endl;

for(int i = 0; i < treatments; i++){
	SSb += pow((subAvg[i] - overallAvg), 2);}
	SSb = SSb * samples;


for(int i = 0; i < treatments; i++){
	for(int j = 0; j < samples; j++){
		SSw += pow((time[i][j] - subAvg[i]), 2);}}

DFb = treatments - 1;
DFw = treatments * (samples - 1);
MSb = SSb/DFb;
MSw = SSw/DFw;
F = MSb/MSw;

cout << "------------------" << endl;
cout << "SSw: " << SSw << endl;
cout << "SSb: " << SSb << endl;
cout << "DFb: " << DFb << endl;
cout << "DFw: " << DFw << endl;
cout << "MSb: " << MSb << endl;
cout << "MSw: " << MSw << endl;
cout << "F: " << F << endl;
cout << "------------------" << endl;

overallAvg = 0;
SSb = 0;
SSw = 0;
MSb = 0;
MSw = 0;
F = 0;
DFb = 0;
DFw = 0;}

cout << "Done? (y=1/n=0): ";
cin >> done;
}//while
}

void ANOVA::ANOVA2(){
cout << "TWO WAY ANOVA" << endl;

int links = 2;
int  cols = 3;
int observations = 7;

float time[links][cols][observations];

for(int i = 0; i < links; i++){
	for(int j = 0; j < cols; j++){
		for(int k = 0; k < observations; k++){
			cin >> time[i][j][k];}}}

float Y = 0;
float alpha[links];
float beta[cols];
float gamma[links][cols];

float SSa, SSb, SSg, SSe;
float MSa, MSb, MSg, MSe;
int DFa, DFb, DFg, DFe;
float Fa, Fb, Fg;
float SStotal = 0;
SSa=SSb=SSg=SSe=MSa=MSb=MSg=MSe=Fa=Fb=Fg=0;
DFa=DFb=DFg=DFe=0;

//initialize arrays
for(int i = 0; i < links; i++){
	alpha[i] = 0;
	for(int j = 0; j < cols; j++){
		beta[j] = 0;
		gamma[i][j] = 0;}}

//get the mean for each of the six cells
//get the overall mean
for(int i = 0; i < links; i++){
	for(int j = 0; j < cols; j++){
		for(int k = 0; k < observations; k++){
			gamma[i][j] += time[i][j][k];
			Y +=time[i][j][k];}}}

	Y = Y/(links*cols*observations);
	cout << "Y: " << Y << endl;

for(int i = 0; i < links; i++){
	for(int j = 0; j < cols; j++){
		gamma[i][j] = gamma[i][j]/observations;
		cout << "gamma[" << i << "][" << j << "]: ";
		cout << gamma[i][j] << endl;}}

//get the link averages independent of columns (two of them)
for(int i = 0; i < links; i++){
	for(int j = 0; j < cols; j++){
		alpha[i] += gamma[i][j];}
	alpha[i] = alpha[i]/cols;
	cout << "alpha[" << i << "]: " << alpha[i] << endl;}
	
//get the column averages independent of links (three of them)
for(int j = 0; j < cols; j++){
	for(int i = 0; i < links; i++){
		beta[j] += gamma[i][j];}
	beta[j] = beta[j]/links;
	cout << "beta[" << j << "]: " << beta[j] << endl;}


for(int i = 0; i < links; i++){
	SSa += pow((alpha[i] - Y), 2);}

for(int j = 0; j < cols; j++){
	SSb += pow((beta[j]- Y), 2);}

for(int i = 0; i < links; i++){
	for(int j = 0; j < cols; j++){
		SSg += pow((gamma[i][j] - alpha[i] - beta[j] + Y), 2);}}

for(int i = 0; i < links; i++){
	for(int j = 0; j < cols; j++){
		for(int k = 0; k < observations; k++){
			SSe += pow((time[i][j][k] - gamma[i][j]), 2);
				SStotal += pow((time[i][j][k]-Y), 2);}}}



	SSa = SSa*cols*observations;
	SSb = SSb*links*observations;
	SSg = SSg*observations;
	
	cout << "SSa: " << SSa << endl;
	cout << "SSb: " << SSb << endl;
	cout << "SSg: " << SSg << endl;
	cout << "SSe: " << SSe << endl;
	cout << "SStotal: " << SStotal << endl;
	cout <<SSa+SSb+SSg+SSe << endl;

DFa = links - 1;
DFb = cols - 1;
DFg = DFa * DFb;
DFe = links*cols*(observations - 1);
MSa = SSa/DFa;
MSb = SSb/DFb;
MSg = SSg/DFg;
MSe = SSe/DFe;
Fa = MSa/MSe;
Fb = MSb/MSe;
Fg = MSg/MSe;

cout << "------------------" << endl;
cout << "DFa: " << DFa << endl;
cout << "DFb: " << DFb << endl;
cout << "DFg: " << DFg << endl;
cout << "DFe: " << DFe << endl;
cout << "MSa: " << MSa << endl;
cout << "MSb: " << MSb << endl;
cout << "MSg: " << MSg << endl;
cout << "MSe: " << MSe << endl;
cout << "Fa: " << Fa << endl;
cout << "Fb: " << Fb << endl;
cout << "Fg: " << Fg << endl;
cout << "------------------" << endl;


}
