STATISTICA







STATISTICA BASIC Program Histres.stb

[Graph] {This program generates a compound graph consisting of (1) a histogram for a selected variable with the expected normal distrbution fit indicated by a line, and (2) a line plot of the deviations of the observed frequencies from the normal expected frequencies at each interval.

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


dim stats(5);

randomaccess;
NoDataFileVariableNames;


if not SelectVariables1 ('Select variable', 1, 1, nv, Count, 'Enter variable:') then
  Exit;


{Determine # of rows and get data vector}
nrows:=ncases;
redim datavec(nrows);
MatrixExtract (Data, 1, nv, nrows, 1, datavec);

display:=DisplayListBox ('Scrollsheet Display', 'Display Scrollsheets |Do not display Scrollsheets', 2);

{Find Maximum value, Minimum value, Range, Mean, and SD of datavec vector}
ValMax (datavec, 1, nrows, stats(1));
ValMin (datavec, 1, nrows, stats(2));
stats(3):=(stats(1))-(stats(2));
ValMean (datavec, 1, nrows, stats(4));
ValStDeviation (datavec, 1, nrows, stats(5));
maxstat:=stats(1);
minstat:=stats(2);
rangestat:=stats(3);
meanstat:=stats(4);
sdstat:=stats(5);

{Ask user to specify # of categories}
ncateg:=12;
DisplayNumericInputBox ('User Input', 'Please enter the number of categories for the histogram', ncateg);
redim freq(ncateg), xval(ncateg), resid(ncateg);

{Determine frequencies for categories}
step:=rangestat/ncateg;	{Note that rangestat is the computed range}
for i:=1 to nrows do
begin
 if datavec(i)=maxstat then
  j:=ncateg
 else
 j:=trunc((datavec(i)-minstat)/step)+1;
 freq(j):=freq(j)+1;
end;

for i:=1 to ncateg do
  xval(i):=minstat+i*step-(.5*step);	{Values for x-axis will be based upon centerpoint of each category}

{Display Max, Min, and Range Scrollsheet and other Scrollsheets}
if display=1 then
Begin
 scrsheet2:=matrixdisplay (stats, 'Max and Min values of data vector');
 ScrollsheetSetRowNameWidth (scrsheet2, 20);
 ScrollsheetSetColumnWidth (scrsheet2, 16, 1);
 ScrollsheetSetRowName (scrsheet2, 1, 'Maximum Value');
 ScrollsheetSetRowName (scrsheet2, 2, 'Minimum Value');
 ScrollsheetSetRowName (scrsheet2, 3, 'Range');
 ScrollsheetSetRowName (scrsheet2, 4, 'Mean');
 ScrollsheetSetRowName (scrsheet2, 5, 'Standard Deviation');
 Pause;
 matrixdisplay (freq, 'Frequencies');
 matrixdisplay (xval,'Values for X-axis');
end;

{Produce Histogram:  First graph}
histogram:=NewGraph (barplot, ?Top_Title$, ?Left_Title$, ?Bottom_Title$, ncateg, XVal, freq);

GraphSetPlot2DLayout (histogram, 1, barplot, ?DataLabels, ?BarStyle, step, ?DevLevel, ?IsRightAxis);

GraphSetScaleTickmarks (histogram, ax_x, tickmarks_inside, 5, tickmarks_off, ?MinorSize, ?MinorCount);
GraphSetScaleTickmarks (histogram, ax_y, tickmarks_inside, 5, tickmarks_off, ?MinorSize, ?MinorCount);
GraphSetScaleTickmarks (histogram, ax_ry, tickmarks_inside, 5, tickmarks_off, ?MinorSize, ?MinorCount);
GraphSetScaleTickmarks (histogram, ax_top, tickmarks_inside, 5, tickmarks_off, ?MinorSize, ?MinorCount);

GraphSetScaling (Histogram, ax_x, scaling_manual, xval(1)-step, xval(ncateg)+step, step);
GraphSetScaleParam (Histogram, ax_x, scaling_manual, 2, ?ShiftScale, ?ScaleBreak, ?ScaleReversed);

fncpar:=step*nrows;
fit$:='y='+Str (fncpar, 12, 4)+' *normal(x, '+str (meanstat, 12, 4)+', '+str (sdstat, 12, 4)+')';
GraphSetPlotFittingFunction (histogram, 1, fit$);

{Calculate residuals between histogram and normal fit}
for i:=1 to ncateg do	{Note that I could calculate a vector of expected normal values...}
 resid(i):=freq(i)-(fncpar*DNormal (xval(i), meanstat, sdstat));

{Produce Line Plot of Residuals}
Lineresid:=NewGraph (lineplot, ?Top_Title$, ?Left_Title$, ?Bottom_Title$, Ncateg, xval, resid);
GraphSetScaling (lineresid, ax_x, scaling_manual, xval(1)-step, xval(ncateg)+step, step);
GraphSetGridlines (lineresid, Gridlines_None);
GraphSetScaleTickmarks (lineresid, ax_x, tickmarks_off, 0, tickmarks_off, ?MinorSize, ?MinorCount);
GraphSetScaleTickmarks (lineresid, ax_y, tickmarks_off, 0, tickmarks_off, ?MinorSize, ?MinorCount);
GraphSetScaleTickmarks (lineresid, ax_ry, tickmarks_off, 0, tickmarks_off, ?MinorSize, ?MinorCount);
GraphSetScaleTickmarks (lineresid, ax_top, tickmarks_off, 0, tickmarks_off, ?MinorSize, ?MinorCount);
GraphSetScaleParam (lineresid, ax_x, scaling_manual, 2, ?ShiftScale, ?ScaleBreak, ?ScaleReversed);
GraphSetPlotFitting (lineresid, 1, Fit_linear);
GraphSetPlotConfidBands (lineresid, 1, ON, .95);

comb:=NewGraph (Ignoredplot, 'Histogram & Residual Plot of '+VarName (nv),
                                 ?Left_Title$, ?Bottom_Title$, ncateg, XVal, freq);

GraphEmbedGraph (comb, histogram, false, false,false, 0, 90, 100, 45, false);
GraphEmbedGraph (comb, lineresid, false, false,false, 0, 45, 100, 0, false);
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.