// Filename:	wksStat.ogs
// Creation:	by CP Yang, 8/2/95
// Purpose:	Worksheet Analysis Menu Stat related scripts
// RKM 7/25/96	In MultiReg section, changed stat.mr.YData$ to take a column name
//		Also added stat.mr.xdata$ to replace stat.setXname
// RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
// SDB 1/7/99   v6.0148 REPORT_TO_OUTPUTLOG
// GJL 7/23/01  v6.9054 Removed sections TTestOne, TTestTwo, ANOVAOne, and ShowTest
//		which are now in TTestMean1Sample.OGS, TTestMean2Sample.OGS, and ANOVA1Way.OGS
//		per t8951 ADD_DIALOG_BUILDER_STATISTICS_DBS
// GJL 7/23/01  v6.9054 Added section NormalityTest t9347 ADD_NORMALITY_TEST
//////////////////////////////////////////////////////////////////////
//
// Multiple Linear Regression
//
//////////////////////////////////////////////////////////////////////

[MultiReg]
	/// Assume multiple regression on selected section
	/// column 1 assumed to be dependent(Y) 
	/// and the selected columns independent(X) 
	stat.reset();
	if(wks.c1==1 || wks.c2 < 2 || (wks.c2-wks.c1) < 0) 
	{
		type -b $regression.IndependantOnly;
		return 1;
	}

	if (%(%H,@Y#,2)>9 || wks.sel&2!=2)
	{
		type -b $regression.OnetoNineCols;
		return 1;
	}

	type -c $regression.ReadyMR;

	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 v6.0148 REPORT_TO_OUTPUTLOG
	type.BeginResults();		/// RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
	type $regression.PerformMR;
	type;						//Blank Line
	type $regression.MRIndep;
	type $regression.MRDepend;

	//stat.mr.YData$=%(%H,@D,1);			/// dependent dataset
	stat.mr.YData$=%(%H,@C,1);	
	//stat.mr.NumX=wks.c2-wks.c1+1;		/// number of indep cols 
	stat.worksheet$=%H;
	//stat.setXname(wks.c1);				/// c1 is the first indep col number
	wks.cnamemode=64;
	stat.mr.xdata$ #= wks.cname$;
	stat.ChiSqrErr=1;
	stat.mr();

	//MR.A=data(0,0,2*npts+10);
	//dll orgmath MR -W %H MR.A 1 $(selc1) $(selc2);

	type;
	type $regression.LongParaHeader1;
	Separator 5;
	type -l $regression.MRYIntercept;
	if (stat.MR.AP<0.0001) %A="<0.0001"; else %A=$(stat.MR.AP);
	type "\t$(stat.MR.A)\t$(stat.MR.Ase)\t$(stat.MR.A/stat.MR.Ase)\t%A";
	for(i=1;i<=(wks.c2-wks.c1+1);i+=1) 
	{
		if (stat.MR.BP$(i)<0.0001) %A="<0.0001"; else %A=$(stat.MR.BP$(i));
		type "%(%H,@Col,wks.c1+i-1)\t$(stat.MR.B$(i))\t$(stat.MR.Bse$(i))\t$(stat.MR.B$(i)/stat.MR.Bse$(i))\t%A";
	}
	Separator 5;

	type;
	type $regression.MRParaHeader;
	Separator 5;
	type "$(stat.COD)\t$(stat.ADRsq)\t$(stat.SD)";
	Separator 5;
	
	///////////////////////////////////////////////////////////////////////
	// Multiple Regession ANOVA Table
	//////////////////////////////////////////////////////////////////////
	repeat 2 {type};                //Skip some a couple spaces
	
	/* ---------------- Type out the ANOVA Table...---------------- */
	type $regression.AnovaHeader;
	separator 5;
	type $regression.AnovaDesc1;
	type $regression.AnovaDesc2;
	separator 5;
	type -l $regression.model;                       //The word MODEL in current language
	type "\t$(stat.df1)\t$(stat.ssr)\t$(stat.msr)\t$(stat.fvalue)";
	type -l $regression.error;                       //The word ERROR in current language
	type "\t$(stat.df2)\t$(stat.sse)\t$(stat.mse)";
	type -l $regression.total;                       //The word TOTAL in current language
	type "\t$(stat.df3)\t$(stat.SSto)";
	separator 5;
	type;

	type $regression.AnovaDesc3;
	separator 5;
	if (stat.pValue<0.0001) %A="<0.0001"; else %A=$(stat.pValue);
	type  "%A";
	separator 5;
	type;
	type.EndResults();
	type.Redirection=SaveRedirection; // restore previous redirection.
	delete -v SaveRedirection;
	return 0;

//////////////////////////////////////////////////////////////////////
// Code to implement Shapiro-Wilk Normality Test menu item.
// Normality Test t9347 ADD_NORMALITY_TEST
//////////////////////////////////////////////////////////////////////

[NormalityTest]
	// Must select one or more entire columns in a single worksheet
	if(!wks.sel&2) 
	{
		run.section(DBRoutines.OGS,ReportError,$NormalityTest.OneOrMoreEntireCols);
		return 1;
	}

	// Remember active worksheet	
	%W="%H";

	// Set default significance level if not defined; Users can customize by assigning
	//  desired value (between 0 and 100) to LabTalk variable ONormTestSL prior
	//  to executing menu item
	if(ONormTestSL==0/0)
		ONormTestSL=5;
	NormTestSL=ONormTestSL/100;
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,0 0 1 ($(NormTestSL)))!=0)
		NormTestSL=5/100;
	
	// Load and compile OStat workspace
	run.section(DBRoutines.OGS,SaveAndSetAtOC);
	if(run.section(DBRoutines.OGS,LoadOStatWorkspace)!=0)
		return 1;

	// Save pre-existing redirection
	SaveRedirection=type.Redirection;
	
	// Loop on all selected columns in active worksheet
	%Z="";
	loop(ii,1,%W!wks.iscolsel())
	{
		// Get selected column name
		jj=%W!wks.c1 + %(%W,@O,ii-1);
		%A=%(%W,@Col,jj);
		%Z=%Z %W_%A;
	}
	jj=osNormalityTest("%Z", NormTestSL);	// Computations and output for all datasets
	// errors are handled by the OriginC function

	type.Redirection=SaveRedirection; // restore previous redirection.

	// Clean up used variables
	run.section(DBRoutines.OGS,RestoreAtOC);
	delete -v SaveRedirection;
	delete -v NormTestSL;
	delete -v ii;
	delete -v jj;
	%A="";%W="";%Z="";

	return 0;

