STATISTICA







STATISTICA BASIC Program BasicStatsByGroup.stb

{ This program produces multiple statistics for a measurement variable by a grouping variable. The statistics produced combine those available in the Descriptive Statistics option of Basic Statistics and Tables with statistics from the Ordinal Descriptive Statistics option of Nonparametric Statistics. The benefit of this program is its ability to produce a single Scrollsheet of output which gives statistics by the levels of the grouping variable. This is an alternative to the Descriptives By option in Quick Basic Stats which can handle several measurement variables, but separates each level of the grouping variable into different Scrollsheets. It allows a choice of the Percentage Confidence Limit for the Mean and also can display user specified Percentiles, for example request the 70th Percentile and the 30th Percentile will also be calculated automatically.

Program written, modified, or edited at StatSoft, Inc.}


RandomAccess;
NoDataFileVariableNames;

{create vectors to hold data}
ReDim GroupData(NCases), MeasureData(NCases), ExtractDetails(NCases), NumericInput(2);

{request the variables and percentages to use}
NumericInput(1) := 95;
NumericInput(2) := 90;

if 0 = SelectVariables2 ('Select Variables', 1, 1, MeasureVar, Count2,
	'Variable for the analysis', 1, 1, GroupVar, Count1, 'by... (Grouping Var.)') then
STOP;
if 0 = DisplayNumericInputBox ('Select Percentages','Enter Confidence Limit for Mean|'
	+'Upper Percentile to Display', NumericInput) then STOP;

{verify inputs}
MeanPct := NumericInput(1);
Pctile := NumericInput(2);

if (Pctile <= 50) or (Pctile >= 100) then begin
	DisplayMessageBox (MB_OK, 'Error', 'Invalid Numeric Entry, Reset to Default');
	Pctile := 90;
end;
if (MeanPct <= 0) or (MeanPct >= 100) then begin
	DisplayMessageBox (MB_OK, 'Error', 'Invalid Numeric Entry, Reset to Default');
	MeanPct := 95;
end;

MeanPct$ := Str (MeanPct, 4, 1);
LowPctile$ := Str ((100 - Pctile), 3, 0);
HighPctile$ := Str (Pctile, 3, 0);

{put the requested data in vectors}
MatrixGetColumn (Data, GroupVar, GroupData);
MatrixGetColumn (Data, MeasureVar, MeasureData);

{sort both vectors by group number}
VectorDualSort (GroupData, MeasureData, SORT_ASCENDING);

{get the number of distinct group values and the first (sorted) case number for each}
ValidCases := NCases;

for i := 1 to NCases do begin
	if i = 1 then begin
		ExtractDetails(1) := 1;
		DistinctGroups := 1;
	end else
		if (Valid (GroupData(i))) and (GroupData(i) > GroupData(i - 1)) then begin
			DistinctGroups := DistinctGroups + 1;
			ExtractDetails(DistinctGroups) := i;
		end;
	if Valid (GroupData(i)) = 0 then ValidCases := ValidCases - 1;
end;

{create matrices to hold the statistics}
ReDim Output (DistinctGroups,19);
ReDim Qrtls (3);

{loop through all the group levels}
for i:= 1 to DistinctGroups do begin

{extract a temporary vector of data for each group}
	if i < DistinctGroups
		then NoToObtain := ExtractDetails(i+1) - ExtractDetails(i)
		else NoToObtain := ValidCases - ExtractDetails(i) + 1;

	ReDim TempData(NoToObtain);
	MatrixExtract (MeasureData, ExtractDetails(i), 1, NoToObtain, 1, TempData);

{get statistics}
	Qrtls(1) := 1/0; Qrtls(2) := 1/0; Qrtls(3) := 1/0;
	ValQuartiles (TempData, 1, 0, Qrtls);

	Output(i,1) := ValCount (TempData, 1, 0);
	if Output(i,1) = 0 then Output(i,2) := 1/0
		else ValMean (TempData, 1, 0, Output(i,2));
	Output(i,5) := Qrtls(2);
	if Output(i,1) = 0 then Output(i,6) := 1/0
		else ValSum (TempData, 1, 0, Output(i,6));
	if Output(i,1) = 0 then Output(i,7) := 1/0
		else ValMin (TempData, 1, 0, Output(i,7));
	if Output(i,1) = 0 then Output(i,8) := 1/0
		else ValMax (TempData, 1, 0, Output(i,8));
	Output(i,9) := Qrtls(1);
	Output(i,10) := Qrtls(3);
	Output(i,11) := Output(i,8) - Output(i,7);
	Output(i,12) := Qrtls(3) - Qrtls(1);
	if Output(i,1) = 0 then Output(i,13) := 1/0
		else ValVariance (TempData, 1, 0, Output(i,13));
	if Output(i,1) = 0 then Output(i,14) := 1/0
		else ValStDeviation (TempData, 1, 0, Output(i,14));
	if Output(i,1) = 0 then Output(i,15) := 1/0
		else ValStError (TempData, 1, 0, Output(i,15));

	tvalue := VStudent (((100-((100-MeanPct)/2))/100), (Output(i,1) - 1));
	Output(i,3) := Output(i,2) - (tvalue * (Output(i,15)));
	Output(i,4) := Output(i,2) + (tvalue * (Output(i,15)));

	if Output(i,1) > 1 then begin
		ValPercentile (TempData, 1, 0, (100-Pctile), Output(i,16));
		ValPercentile (TempData, 1, 0, Pctile, Output(i,17));
	end else begin
		Output(i,16) := 1/0;
		Output(i,17) := 1/0;
	end;
	ModeVal := 1/0;
	ValMode (TempData, 1, 0, ModeVal);

	if (Output(i,1) < NoToObtain) and (Output(i,1) > 0) then begin
		VectorSort (TempData, SORT_ASCENDING);
		ReDim TempData(Output(i,1)), ModeVector(Output(i,1));
	end else ReDim ModeVector(NoToObtain);

	if Valid (ModeVal) then begin
		Output(i,18) := ModeVal;
		MatrixFill (ModeVal, ModeVector, 1, 1, 0, 1);
		MatrixIsEqual (TempData, ModeVector, TempData);
		ValSum (TempData, 1, 0, Output(i,19));
	end else begin
		Output(i,18) := 1/0;
		Output(i,19) := 1/0;
	end;
end;

{make a Scrollsheet}
ColumnText$ := 'Valid N|Mean|L%|U%|Median|Sum|Minimum|Maximum|LQ|UQ|Range|QR|' +
	'Variance|Std.Dev.|SE|' + LowPctile$ + '% ile|' + HighPctile$ + '% ile|Mode|FM';

scroll := NewScrollsheet (DistinctGroups, 19, Output, 'Statistics for '
	+ VarName (MeasureVar) + ' by ' + VarName (GroupVar), ?RowNames$, ColumnText$);

ScrollsheetSetColumnName (scroll, 3, 'Confid.', '-' + MeanPct$ + '%');
ScrollsheetSetColumnName (scroll, 4, 'Confid.', '+' + MeanPct$ + '%');
ScrollsheetSetColumnName (scroll, 9, 'Lower', 'Quartile');
ScrollsheetSetColumnName (scroll, 10, 'Upper', 'Quartile');
ScrollsheetSetColumnName (scroll, 12, 'Quartile', 'Range');
ScrollsheetSetColumnName (scroll, 15, 'Standard', 'Error');
ScrollsheetSetColumnName (scroll, 19, 'Freqncy', 'of Mode');

{add text labels to the Scrollsheet when available}
ScrollsheetSetRowNameWidth (scroll, 8);
ScrollsheetSetColumnFormat (scroll, 1, SCF_INTEGER, 8);

for i := 1 to DistinctGroups do begin
	GroupVal := GroupData(ExtractDetails(i));
	GroupText$ := GetText (GroupVar, GroupVal);
	if GroupText$ <> ''
		then ScrollsheetSetRowName (scroll, i, GroupText$)
		else ScrollsheetSetRowName (scroll, i, Str (GroupVal, 8, 0));
end;
Back to List of Programs



[StatSoft]
2300 East 14th Street, Tulsa, OK 74104
Phone: (918) 749-1119; Fax: (918) 749-2217

[StatSoft]e-mail: info@statsoft.com

©Copyright StatSoft, Inc., 1984-2004.
StatSoft, StatSoft logo, STATISTICA, SEWSS, SEDAS, Data Miner, SEPATH and GTrees are trademarks of StatSoft, Inc.