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

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

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

[DatasetNameCMBX.OnSelChange]
	%B=TTestMean1Sample!DatasetNameCMBX.v$(TTestMean1Sample!DatasetNameCMBX.Choice)$;
	return 0;

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

[UpdateAlternateHypotheses]
	%A=TTestMean1Sample!TestMeanEBX.v1$;
	if(%[%A] > 8) %A=%[%A,1:8];
	TTestMean1Sample!AlternateRB.l1$=$tTest.Mean1SampleHA1;
	TTestMean1Sample!AlternateRB.l2$=$tTest.Mean1SampleHA2;
	TTestMean1Sample!AlternateRB.l3$=$tTest.Mean1SampleHA3;
	return 0;

[AlternateRB.OnClick]
	switch(TTestMean1Sample!AlternateRB.choice)
	{
		case 1:
			TTestMean1Sample!NullHypothesisTXT$=$tTest.Mean1SampleH01;
			break;
		case 2:
			TTestMean1Sample!NullHypothesisTXT$=$tTest.Mean1SampleH02;
			break;
		case 3:
			TTestMean1Sample!NullHypothesisTXT$=$tTest.Mean1SampleH03;
			break;
	}
	return 0;

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

[SignificanceLevelEBX.OnKillFocus]
	delete _CI;
	%W=TTestMean1Sample!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;
		%A=TTestMean1Sample!ConfidenceIntervalsEBX.v1$;
		if(%[%A]==0)
		{		
			%A=$(alpha);
		}
		else
		{
			_CI={%A};
			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);
			%A=""; 
			loop(ii,1,jj)
			{
				if(_CI[ii]==0/0)
					continue;
				else
					%A=%A$(_CI[ii]);
				if(_CI[ii+1]!=0/0) %A=%A,;
			}
			if(_CI[jj+1]!=0/0) %A=%A$(_CI[jj+1]);
		}
		TTestMean1Sample!ConfidenceIntervalsEBX.v1$=%A;
	}
	return 0;
	
[PowerAnalysisCHKBX.OnClick]
	TTestMean1Sample!SampleSizeEBX.Enable=TTestMean1Sample!PowerAnalysisCHKBX &
										  TTestMean1Sample!SampleSizeCHKBX;
	return 0;

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

[CheckSettings]
	%A=TTestMean1Sample!DatasetNameCMBX.v$(TTestMean1Sample!DatasetNameCMBX.Choice)$;
	%W=$tTest.NoSampleDataset;
	if(run.section(DBRoutines.OGS,CheckDatasetExists,"%W" "%A")!=0)
		return 1;

	/// 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;
		}
	}
	sum(%A);nPts=sum.n; // Don't count masked or missing values
	/// GJL 4/11/02 QA70-1404 End UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
	if(nPts<1)
	{
		run.section(DBRoutines.OGS,ReportError,$OStatCommon.TooFewDataPts);
		return 1;
	}
	
	%A=TTestMean1Sample!TestMeanEBX.v1$;
	%W=$tTest.MeanSingleNumeric;
	if(run.section(DBRoutines.OGS,CheckSingleNumeric,"%W" (%A))!=0)
		return 1;

	%A=TTestMean1Sample!SignificanceLevelEBX.v1$;	
	%W=$OStatCommon.SigLevelGT0LT1; 
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%W" 0 1 (%A))!=0)
		return 1;
	
	%A=TTestMean1Sample!ConfidenceIntervalsEBX.v1$;
	%W=$OStatCommon.ConLevelCommaSepList;
	if(run.section(DBRoutines.OGS,CheckMultipleNumericsGTandLT,"%W" 0 100 "%A")!=0)
		return 1;
	
	%A=TTestMean1Sample!PowerAnalysisEBX.v1$;
	%W=$OStatCommon.PowerSingleNumeric; 
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%W" 0 1 (%A))!=0)
		return 1;
	
	%A=TTestMean1Sample!SampleSizeEBX.v1$;
	%W=$OStatCommon.SSCommaSepList;
	if(run.section(DBRoutines.OGS,CheckMultiplePositiveIntegers,"%W" "%A")!=0)
		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
	%B=TTestMean1Sample!DatasetNameCMBX.v$(TTestMean1Sample!DatasetNameCMBX.Choice)$;
	%W=%[%B,'_'];
	win -o %W {type.BeginResults()}; // RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
	return 0;

[ComputeTandP]
	%B=TTestMean1Sample!DatasetNameCMBX.v$(TTestMean1Sample!DatasetNameCMBX.Choice)$;
	sum(%B);
	tTestMean=TTestMean1Sample!TestMeanEBX.v1;
	tTestLevel=TTestMean1Sample!SignificanceLevelEBX.v1;
	df=sum.n-1;
	tval=(sum.mean-tTestMean)*sqrt(sum.n)/sum.sd;
	P=incbeta(df/(df+tval*tval),df/2,1/2);
	
	switch(TTestMean1Sample!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 One Sample t-Test Summary Statistics (test=0 and nData=1)
	osSummary_Stat_Output_to_ResultsLog(0,1);

	%V=TTestMean1Sample!NullHypothesisTXT$;
	if(TTestMean1Sample!AlternateRB.choice!=1) %V=%[%V,' ']  %[%V,>' '];
	%W=TTestMean1Sample!TestMeanEBX.v1$;
	%V=%V   %W;
	
	%W=TTestMean1Sample!AlternateRB.l$(TTestMean1Sample!AlternateRB.choice)$;
	if(TTestMean1Sample!AlternateRB.choice==1) %W=%[%W,' ']  %[%W,>' '];

	// Output the One Sample t-Test Table...arg1 is test type 0 for One Sample and arg2 is place
	// holder for variance of Two Sample Paired t-Test
	osTTest_Table_Output_to_ResultsLog(0,1,%V,%W,tval,df,P,TTestMean1Sample!AlternateRB.choice,tTestLevel,tTestMean);

	return 0;

[ComputeConfidenceIntervals]
	%A=TTestMean1Sample!ConfidenceIntervalsEBX$;
	_CI={%A};
	_LLim=1-(1-(_CI/100))/2;
	get _LLim -e jj;
	loop(ii,1,jj)
	{
		alpha=_LLim[ii];
		_LLim[ii]=tTable(alpha,df);
	}
	_LLim=_LLim*sum.sd/sqrt(sum.n);
	_ULim=sum.mean+_LLim;
	_LLim=sum.mean-_LLim;
	return 0;

[DisplayConfidenceIntervals]
	osTTest_ConIntervals_Output_to_ResultsLog(0,"_CI","_LLim","_ULim");	
	return 0;
	
[ComputeActualPower]
	switch(TTestMean1Sample!AlternateRB.choice)
	{
		case 1:
			alpha=1-TTestMean1Sample!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-TTestMean1Sample!PowerAnalysisEBX.v1;
			percentile=tTable(alpha,df)-tval;
			power=1-osTTest_ProbT(percentile,df);
			break;
		case 3:
			alpha=1-TTestMean1Sample!PowerAnalysisEBX.v1;
			percentile=-tTable(alpha,df)-tval;
			power=osTTest_ProbT(percentile,df);
			break;
	}
	return 0;

[ComputeHypotheticalPowers]
	%A=TTestMean1Sample!SampleSizeEBX$;
	_SS={%A};
	_SSPOW={%A};
	get _SS -e jj;
	loop(ii,1,jj)
	{
		ssdf=_SS[ii]-1;
		sst=tval*sqrt(ssdf+1)/sqrt(sum.n);
		switch(TTestMean1Sample!AlternateRB.choice)
		{
			case 1:
				alpha=1-TTestMean1Sample!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-TTestMean1Sample!PowerAnalysisEBX.v1;
				percentile=tTable(alpha,ssdf)-sst;
				_SSPOW[ii]=1-osTTest_ProbT(percentile,ssdf);
				break;
			case 3:
				alpha=1-TTestMean1Sample!PowerAnalysisEBX.v1;
				percentile=-tTable(alpha,ssdf)-sst;
				_SSPOW[ii]=osTTest_ProbT(percentile,ssdf);
				break;
		}
	}
	return 0;

[DisplayPower]
	iPowerFlag=TTestMean1Sample!PowerAnalysisCHKBX+2*TTestMean1Sample!SampleSizeCHKBX;
	alpha=TTestMean1Sample!PowerAnalysisEBX.v1;
	osPower_Output_to_ResultsLog(iPowerFlag,alpha,-1,sum.n,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 SaveRedirection;
	delete -v tTestMean;
	delete -v tTestLevel;
	delete -v df;
	delete -v tval;
	delete -v P;
	delete -v nPts;
	delete -v ii;
	delete -v jj;
	delete -v alpha;
	delete -v iPowerFlag;
	delete -v power;
	delete -v ssdf;
	delete -v percentile;
	delete -v sst;
	delete _CI;
	delete _LLim;
	delete _ULim;
	delete _SS;
	delete _SSPOW;
	%A="";%B="";%V="";%W="";
	return 0;