//////////////////////////////////////////////////////////////////////
// Filename:	TTestMean2Sample.OGS
// Purpose:		Event handlers for TTestMean2Sample dialog box
// Creation:	by GLane, 3/22/01
// Taken from:  wksStat.OGS by CP Yang, 8/2/95
//////////////////////////////////////////////////////////////////////

[OnOpen]
	dialog.start(OStat.DLL,TTestMean2Sample,TTestMean2Sample.OPS);
	return 0;

[OnInit]
	define DocEnd {run.section(Origin.OGS,DocEnd);};
	TTestMean2Sample!TestTypeRB=1;
	TTestMean2Sample!SampleSizeCHKBX$=$tTest.TotalSS;	
	%A="TTestMean2Sample!Sample1DataCMBX";
	%B="TTestMean2Sample!Sample2DataCMBX";
	run.section(DBRoutines,Pop2CMBXSwNonTextData,"%A" "%B");
	TTestMean2Sample!TestMeanEBX.v1=0;
	TTestMean2Sample!AlternateRB.choice=1;
	run.section(,UpdateAlternateHypotheses);
	TTestMean2Sample!SignificanceLevelEBX.v1$=$(5/100,.2);
	TTestMean2Sample!ConfidenceIntervalsEBX.v1$=90,95,99;
	TTestMean2Sample!PowerAnalysisEBX.v1$=TTestMean2Sample!SignificanceLevelEBX.v1$;
	TTestMean2Sample!SampleSizeEBX.v1$=50,100,200;
	TTestMean2Sample!SampleSizeEBX.Enable=TTestMean2Sample!PowerAnalysisCHKBX &
										  TTestMean2Sample!SampleSizeCHKBX;
	return 0;

[TestTypeRB.OnClick]
	if(TTestMean2Sample!TestTypeRB==1)
		TTestMean2Sample!SampleSizeCHKBX$=$tTest.TotalSS;
	else
		TTestMean2Sample!SampleSizeCHKBX$=$tTest.IndividualSS;
	return 0;
		
[Sample1DataCMBX.OnSelChange]
	%A=TTestMean2Sample!Sample1DataCMBX.v$(TTestMean2Sample!Sample1DataCMBX.Choice)$;
	%B=TTestMean2Sample!Sample2DataCMBX.v$(TTestMean2Sample!Sample2DataCMBX.Choice)$;
	if("%A"=="%B")
		type -b;
	return 0;

[Sample2DataCMBX.OnSelChange]
	%A=TTestMean2Sample!Sample1DataCMBX.v$(TTestMean2Sample!Sample1DataCMBX.Choice)$;
	%B=TTestMean2Sample!Sample2DataCMBX.v$(TTestMean2Sample!Sample2DataCMBX.Choice)$;
	if("%A"=="%B")
		type -b;
	return 0;

[TestMeanEBX.OnEditChange]
	run.section(,UpdateAlternateHypotheses);
	return 0;

[UpdateAlternateHypotheses]
	%W=TTestMean2Sample!TestMeanEBX.v1$;
	if(%[%W] > 8) %W=%[%W,1:8];
	TTestMean2Sample!AlternateRB.l1$=$tTest.Mean2SampleHA1;
	TTestMean2Sample!AlternateRB.l2$=$tTest.Mean2SampleHA2;
	TTestMean2Sample!AlternateRB.l3$=$tTest.Mean2SampleHA3;
	return 0;

[AlternateRB.OnClick]
	switch(TTestMean2Sample!AlternateRB.choice)
	{
		case 1:
			TTestMean2Sample!NullHypothesisTXT$=$tTest.Mean2SampleH01;
			break;
		case 2:
			TTestMean2Sample!NullHypothesisTXT$=$tTest.Mean2SampleH02;
			break;
		case 3:
			TTestMean2Sample!NullHypothesisTXT$=$tTest.Mean2SampleH03;
			break;
	}
	return 0;

[SignificanceLevelEBX.OnEditChange]
	TTestMean2Sample!PowerAnalysisEBX.v1$=TTestMean2Sample!SignificanceLevelEBX.v1$;
	return 0;

[SignificanceLevelEBX.OnKillFocus]
	delete _CI;
	%W=TTestMean2Sample!SignificanceLevelEBX.v1$;
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,0 0 1 (%W))!=0)
		return 1;
	else
	{
		alpha=%W;
		alpha=(1-alpha)*100;
		%W=$(alpha);
		alpha=%W;
		%V=TTestMean2Sample!ConfidenceIntervalsEBX.v1$;
		if(%[%V]==0)
		{		
			%V=$(alpha);
		}
		else
		{
			_CI={%V};
			get _CI -e jj;
			loop(ii,1,jj)
			{
				if(_CI[ii]==alpha) return 1;  // if value already in list
			}
			_CI[jj+1]=alpha;
			_CI=sort(_CI);
			%V=""; 
			loop(ii,1,jj)
			{
				if(_CI[ii]==0/0)
					continue;
				else
					%V=%V$(_CI[ii]);
				if(_CI[ii+1]!=0/0) %V=%V,;
			}
			if(_CI[jj+1]!=0/0) %V=%V$(_CI[jj+1]);
		}
		TTestMean2Sample!ConfidenceIntervalsEBX.v1$=%V;
	}
	return 0;

[PowerAnalysisCHKBX.OnClick]
	TTestMean2Sample!SampleSizeEBX.Enable=TTestMean2Sample!PowerAnalysisCHKBX &
										  TTestMean2Sample!SampleSizeCHKBX;
	return 0;

[SampleSizeCHKBX.OnClick]
	TTestMean2Sample!SampleSizeEBX.Enable=TTestMean2Sample!SampleSizeCHKBX;
	return 0;

[ComputeBTN.OnClick]
	run.section(DBRoutines.OGS,SaveAndSetAtOC);
	if(run.section(,CheckSettings)==0)
	{
		run.section(,OpenAndDirectOutputToResultsLog);
		run.section(,ComputeTandP);
		if(run.section(,DisplayTandP)==0)
		{
			if(TTestMean2Sample!ConfidenceIntervalsCHKBX==1)
			{
				run.section(,ComputeConfidenceIntervals);
				run.section(,DisplayConfidenceIntervals);
			}
			if(TTestMean2Sample!PowerAnalysisCHKBX==1)
			{
				run.section(,ComputeActualPower);
				if(TTestMean2Sample!SampleSizeCHKBX==1)
					run.section(,ComputeHypotheticalPowers);
				run.section(,DisplayPower);
			}
		}
		run.section(,CloseAndRedirectOutput);
		run.section(,CleanUp);
	}
	run.section(DBRoutines.OGS,RestoreAtOC);
	dialog.SaveSettingsToOPS(OStat.DLL,TTestMean2Sample);
	return 0;

[CheckSettings]
	%V=TTestMean2Sample!Sample1DataCMBX.v$(TTestMean2Sample!Sample1DataCMBX.Choice)$;
	%W=$OStatCommon.NoSample1Dataset;
	if(run.section(DBRoutines.OGS,CheckDatasetExists,"%W" "%V")!=0)
		return 1;
	
	%V=TTestMean2Sample!Sample2DataCMBX.v$(TTestMean2Sample!Sample2DataCMBX.Choice)$;
	%W=$OStatCommon.NoSample2Dataset;
	if(run.section(DBRoutines.OGS,CheckDatasetExists,"%W" "%V")!=0)
		return 1;
	
	%V=TTestMean2Sample!TestMeanEBX.v1$;
	%W=$tTest.D0SingleNumeric;
	if(run.section(DBRoutines.OGS,CheckSingleNumeric,"%W" (%V))!=0)
		return 1;
	
	%V=TTestMean2Sample!SignificanceLevelEBX.v1$;	
	%W=$OStatCommon.SigLevelGT0LT1; 
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%W" 0 1 (%V))!=0)
		return 1;
	
	%V=TTestMean2Sample!ConfidenceIntervalsEBX.v1$;
	%W=$OStatCommon.ConLevelCommaSepList;
	if(run.section(DBRoutines.OGS,CheckMultipleNumericsGTandLT,"%W" 0 100 "%V")!=0)
		return 1;
	
	%V=TTestMean2Sample!PowerAnalysisEBX.v1$;
	%W=$OStatCommon.PowerSingleNumeric; 
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%W" 0 1 (%V))!=0)
		return 1;
	
	%V=TTestMean2Sample!SampleSizeEBX.v1$;
	%W=$OStatCommon.SSCommaSepList;
	if(run.section(DBRoutines.OGS,CheckMultiplePositiveIntegers,"%W" "%V")!=0)
		return 1;
	
	%A=TTestMean2Sample!Sample1DataCMBX.v$(TTestMean2Sample!Sample1DataCMBX.Choice)$;
	/// GJL 4/11/02 QA70-1404 UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
	%B=%[%A,'_'];
	if(%B!page.extInfo==1) // If data set is in Excel workbook update Origin
	{
		win -o %B
		{
			layer -s;
			set %B -ui;
		}
	}
	/// GJL 4/11/02 QA70-1404 End UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
	sum(%A);ave1=sum.mean;sd1=sum.sd;n1=sum.n;se1=sum.sd/sqrt(n1);var1=sd1^2;
	if(n1<1)
	{
		run.section(DBRoutines.OGS,ReportError,$OStatCommon.TooFewDataPts);
		return 1;
	}

	%A=TTestMean2Sample!Sample2DataCMBX.v$(TTestMean2Sample!Sample2DataCMBX.Choice)$;
	/// GJL 4/11/02 QA70-1404 UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
	%B=%[%A,'_'];
	if(%B!page.extInfo==1) // If data set is in Excel workbook update Origin
	{
		win -o %B
		{
			layer -s;
			set %B -ui;
		}
	}
	/// GJL 4/11/02 QA70-1404 End UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
	sum(%A);ave2=sum.mean;sd2=sum.sd;n2=sum.n;se2=sum.sd/sqrt(n2);var2=sd2^2;
	if(n2<1)
	{
		run.section(DBRoutines.OGS,ReportError,$OStatCommon.TooFewDataPts);
		return 1;
	}
	
	if(TTestMean2Sample!TestTypeRB==2 && n1!=n2)
	{
		run.section(DBRoutines.OGS,ReportError,$tTest.tTestFailed);
		return 1;
	}
	
	if(run.section(DBRoutines.OGS,LoadOStatWorkspace)!=0)
		return 1;
	
	return 0;

[OpenAndDirectOutputToResultsLog]	
	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 v6.0148 REPORT_TO_OUTPUTLOG
	%A=TTestMean2Sample!Sample1DataCMBX.v$(TTestMean2Sample!Sample1DataCMBX.Choice)$;
	%W=%[%A,'_'];
	win -o  %W {type.BeginResults()}; // RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
	return 0;
	
[ComputeTandP]
	%A=TTestMean2Sample!Sample1DataCMBX.v$(TTestMean2Sample!Sample1DataCMBX.Choice)$;
	%B=TTestMean2Sample!Sample2DataCMBX.v$(TTestMean2Sample!Sample2DataCMBX.Choice)$;
	d0=TTestMean2Sample!TestMeanEBX.v1;
	tTestLevel=TTestMean2Sample!SignificanceLevelEBX.v1;
	if(TTestMean2Sample!TestTypeRB==1) 
	{ 				/* independent */
		df=n1+n2-2;
		svar=((n1-1)*var1+(n2-1)*var2)/df;
		tval=(ave1-ave2-d0)/sqrt(svar*(1/n1+1/n2));
	} 
	else 
	{ 				/* paired, assume n2==n1 */
		df=n1-1;
		SaveSysMaskedVal=@MV;  // Save @MV system property to reset
		@MV=1;                 // Make cov() function ignore masked values
		covar=cov(%A,%B,ave1,ave2);
		@MV=SaveSysMaskedVal;  // Restore @MV system property
		sd=sqrt((var1+var2-2*covar)/n1);
		if(sd==0) return 1;
		tval=(ave1-ave2-d0)/sd;
	}
	P=incbeta(df/(df+tval*tval),df/2,1/2);
	switch(TTestMean2Sample!AlternateRB.choice)
	{
		case 1:
			break;
		case 2:
			if(tval < 0)
				P=1-P/2;
			else
				P=P/2;
			break;
		case 3:
			if(tval < 0)
				P=P/2;
			else
				P=1-P/2;
			break;
	}
	return 0;

[DisplayTandP]
	// Output Two Sample t-Test Summary Statistics (test=TTestMean2Sample!TestTypeRB and nData=2)
	osSummary_Stat_Output_to_ResultsLog(TTestMean2Sample!TestTypeRB,2);
	
	%V=TTestMean2Sample!NullHypothesisTXT$;
	if(TTestMean2Sample!AlternateRB.choice!=1) %V=%[%V,'2']2 %[%V,>'2'];
	%W=TTestMean2Sample!TestMeanEBX.v1$;
	%V=%V   %W;
	
	%W=TTestMean2Sample!AlternateRB.l$(TTestMean2Sample!AlternateRB.choice)$;
	if(TTestMean2Sample!AlternateRB.choice==1) %W=%[%W,'2']2 %[%W,>'2'];

	// Output the Two Sample t-Test Table
	if(osTTest_Table_Output_to_ResultsLog(TTestMean2Sample!TestTypeRB,sd,"%V","%W",tval,df,P,TTestMean2Sample!AlternateRB.choice,tTestLevel,d0))
		return 1;
	
	return 0;

[ComputeConfidenceIntervals]
	%W=TTestMean2Sample!ConfidenceIntervalsEBX$;
	_CI={%W};
	_LLim=1-(1-(_CI/100))/2;
	get _LLim -e jj;
	loop(ii,1,jj)
	{
		alpha=_LLim[ii];
		_LLim[ii]=tTable(alpha,df);
	}
	if(TTestMean2Sample!TestTypeRB==1)
	{
		_LLim=_LLim*sqrt(svar*(1/n1+1/n2));
		_ULim=ave1-ave2+_LLim;
		_LLim=ave1-ave2-_LLim;
	}
	else
	{
		_LLim=_LLim*sd;
		_ULim=ave1-ave2+_LLim;
		_LLim=ave1-ave2-_LLim;
	}
	return 0;

[DisplayConfidenceIntervals]
	osTTest_ConIntervals_Output_to_ResultsLog(1,"_CI","_LLim","_ULim");	
	return 0;	
	
[ComputeActualPower]
	switch(TTestMean2Sample!AlternateRB.choice)
	{
		case 1:
			alpha=1-TTestMean2Sample!PowerAnalysisEBX.v1/2;
			percentile=tTable(alpha,df)-tval;
			power=1-osTTest_ProbT(percentile,df);
			percentile=-tTable(alpha,df)-tval;
			power=power+osTTest_ProbT(percentile,df);
			break;
		case 2:
			alpha=1-TTestMean2Sample!PowerAnalysisEBX.v1;
			percentile=tTable(alpha,df)-tval;
			power=1-osTTest_ProbT(percentile,df);
			break;
		case 3:
			alpha=1-TTestMean2Sample!PowerAnalysisEBX.v1;
			percentile=-tTable(alpha,df)-tval;
			power=osTTest_ProbT(percentile,df);
			break;
	}
	return 0;

[ComputeHypotheticalPowers]
	%W=TTestMean2Sample!SampleSizeEBX$;
	_SS={%W};
	_SSPOW={%W};
	get _SS -e jj;
	loop(ii,1,jj)
	{
		if(TTestMean2Sample!TestTypeRB==1) 
		{
			// independent
			ssn1=int((n1/(n1+n2))*_SS[ii]+1/2);
			ssn2=_SS[ii]-ssn1;
			ssdf=ssn1+ssn2-2;
			sssvar=((ssn1-1)*var1+(ssn2-1)*var2)/ssdf;
			sst=(ave1-ave2-d0)/sqrt(sssvar*(1/ssn1+1/ssn2));
		}
		else
		{
			// paired
			ssn1=_SS[ii];
			ssdf=ssn1-1;
			sssd=sd*sqrt(n1)/sqrt(ssn1);
			sst=(ave1-ave2-d0)/sssd;
		}
		switch(TTestMean2Sample!AlternateRB.choice)
		{
			case 1:
				alpha=1-TTestMean2Sample!PowerAnalysisEBX.v1/2;
				percentile=tTable(alpha,ssdf)-sst;
				_SSPOW[ii]=1-osTTest_ProbT(percentile,ssdf);
				percentile=-tTable(alpha,ssdf)-sst;
				_SSPOW[ii]=_SSPOW[ii]+osTTest_ProbT(percentile,ssdf);
				break;
			case 2:
				alpha=1-TTestMean2Sample!PowerAnalysisEBX.v1;
				percentile=tTable(alpha,ssdf)-sst;
				_SSPOW[ii]=1-osTTest_ProbT(percentile,ssdf);
				break;
			case 3:
				alpha=1-TTestMean2Sample!PowerAnalysisEBX.v1;
				percentile=-tTable(alpha,ssdf)-sst;
				_SSPOW[ii]=osTTest_ProbT(percentile,ssdf);
				break;
		}
	}
	return 0;

[DisplayPower]
	iPowerFlag=TTestMean2Sample!PowerAnalysisCHKBX+2*TTestMean2Sample!SampleSizeCHKBX;
	alpha=TTestMean2Sample!PowerAnalysisEBX.v1;
	if(TTestMean2Sample!TestTypeRB==1) // If Independent Test use total sample size
		actualSS=n1+n2;
	else  // Else Paired Test use individual sample size
		actualSS=sum.n;
	osPower_Output_to_ResultsLog(iPowerFlag,alpha,TTestMean2Sample!TestTypeRB-1,actualSS,power,"_SS","_SSPOW");	
	return 0;

[CloseAndRedirectOutput]
	type.EndResults();
	type.Redirection=SaveRedirection; // restore previous redirection.
	return 0;

[OnCancel]
	run.section(,CleanUp);
	return 0;
	
[CleanUp]
	delete -v SaveSysMaskedVal;
	delete -v SaveRedirection;
	delete -v tTestLevel;
	delete -v d0;
	delete -v df;
	delete -v tval;
	delete -v P;
	delete -v ii;
	delete -v jj;
	delete -v alpha;
	delete -v power;
	delete -v percentile;
	delete -v n1;
	delete -v n2;
	delete -v ave1;
	delete -v ave2;
	delete -v sd;
	delete -v sd1;
	delete -v sd2;
	delete -v se1;
	delete -v se2;
	delete -v var1;
	delete -v var2;
	delete -v svar;
	delete -v covar;
	delete -v ssdf;
	delete -v sssd;
	delete -v sst;
	delete -v ssn1;
	delete -v ssn2;
	delete -v sssvar;
	delete -v iPowerFlag;
	delete -v actualSS;	
	delete _CI;
	delete _LLim;
	delete _ULim;
	delete _SS;
	delete _SSPOW;
	%A="";%B="";%V="";%W="";
	return 0;