STATISTICA







STATISTICA BASIC Program One-SampleTest(Agg).stb

{ This program will perform significance tests and calculate confidence limits for the following statistics taken from one single sample: Mean (with known or unknown standard deviation), Variance, Proportion, and Pearson's Product Moment Correlation Coefficient. It requires aggregated data (e.g. sample mean, sample size etc.) instead of raw data. If you have raw data from which the sample parameters have to be calculated use the STATISTICA BASIC program One-SampleTest(Raw).stb.

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


{SELECT TEST TYPE - let's the user select the particular sample parameter to test}
{The menu items in this dialog are:
1. TEST FOR MEAN WITH UNKNOWN VARIANCE (BASED ON T DISTRIBUTION)
2. TEST FOR MEAN WITH KNOWN VARIANCE (BASED ON Z DISTRIBUTION)
3. TEST FOR VARIANCE
4. TEST FOR PROPORTION
5. TEST FOR CORRELATION}
choice:=DisplayButtonBox ('One-sample-tests for aggregated data', 'Test for mean (SD
unknown)|Test for mean (SD known)|Test for variance|Test for proportion|Test for
correlation');

{Every test has it's own pair of arrays: test# (for input parameters) and results# (for
output parameters) where # is the number of the menu item as listed above}


{1. TEST FOR MEAN WITH UNKNOWN VARIANCE}
{1.1. Specify input parameters for test and check validity of input}
If choice = 1 then begin
Dim Test1(4);
Testspecs1:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Mean for Null
Hypothesis H0:|Sample mean:|Sample standard deviation:|Sample size:', Test1) = 0 then
abort;
If (test1(3)<0) OR (test1(4)<=0) OR (test1(4)-trunc(test1(4))<>0) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid parameter(s)!', 'Try again or abort
program');
If Error1 = IDRETRY then Goto Testspecs1 else abort;
end;

{1.2. Specify significance level for tests and confidence limits}
If DisplayNumericInputBox ('Enter significance level:', 'Alpha: (between 0.0 and 1.0):',
alpha) = 0 then abort;
If (alpha<=0) OR (alpha>=1) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid significance level!', 'Try again or
abort program.');
If Error1 = IDRETRY then Goto Testspecs1 else abort;
end;

{1.3. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypmean:=test1(1);
mean:=test1(2);
stddev:=test1(3);
size:=test1(4);
sterr:=stddev/sqrt(size);
statistic:=((mean-hypmean)/(stddev/sqrt(size)));
nu:=size-1;
abstat:=abs(statistic);
plevelb:=2*(1-istudent(abstat, nu));
plevell:=istudent (statistic, nu);
plevelu:=1-istudent (statistic, nu);
tcl1:=VStudent ((alpha/2), nu);
tcu1:=VStudent ((1-alpha/2),nu);
tcl2:=VStudent (alpha, nu);
tcu2:=VStudent ((1-alpha), nu);
If (statistic>tcl1) and (statistic<tcu1) then bothsides$:='Accpt H0' else
bothsides$:='Rejct H0';
If (statistic<tcl2) then leftside$:='Rejct H0' else leftside$:='Accpt H0';
If (statistic>tcu2) then rightside$:='Rejct H0' else rightside$:='Accpt H0';
LCL:=mean+tcl1*sterr;
UCL:=mean+tcu1*sterr;

Dim Results1 (12);
Results1(1):=hypmean;
Results1(2):=Mean;
Results1(3):=LCL;
Results1(4):=UCL;
Results1(5):=statistic;
Results1(6):=nu;
Results1(7):=plevelb;
Results1(8):=plevell;
Results1(9):=plevelu;


{1.4. Create output table (Scrollsheet), specify title, and assign parameters from
results array}
Scroll:=NewScrollsheet (1, 12, Results1, "One-sample t test for mean (variance
unknown)|Significance tests and confidence limits are based on|Student's t
distribution", 'Test results', ?ColumnNames$);
ScrollsheetSetColumnName (Scroll, 1, 'HO', 'mean');
ScrollsheetSetColumnName (Scroll, 2, 'Sample', 'mean');
cflevel$:=str(100-(alpha*100),5,2)+'%';
ScrollsheetSetColumnName (Scroll, 3, 'LCL', '-'+cflevel$);
ScrollsheetSetColumnName (Scroll, 4, 'UCL', '+'+cflevel$);
ScrollsheetSetColumnName (Scroll, 5, ?Name1$, 't-value');
ScrollsheetSetColumnName (Scroll, 6,?Name1$, 'df');
ScrollsheetSetColumnName (Scroll, 7,'2-sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 8, 'le. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 9, 'ri. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 10, '2-sided', 'test');
ScrollsheetSetColumnName (Scroll, 11, '1-sided', 'left');
ScrollsheetSetColumnName (Scroll, 12, '1-sided', 'right');
ScrollsheetSetTextValue (Scroll, 1, 10, bothsides$);
ScrollsheetSetTextValue (Scroll, 1, 11, leftside$);
ScrollsheetSetTextValue (Scroll, 1, 12, rightside$);

{1.5. End of compound instruction for first menu item - see first line of code in
section 1.1.}
end;


{2. TEST FOR MEAN WITH KNOWN VARIANCE}
{2.1. Specify input parameters for test and check validity of input}
If choice = 2 then begin
Dim Test2(4);
Testspecs2:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Mean for Null
Hypothesis H0:|Sample mean:|Standard deviation:|Sample size:', Test2) = 0 then abort;
If (test2(3)<0) OR (test2(4)<=0) OR (test2(4)-trunc(test2(4))<>0) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid parameter(s)!', 'Try again or abort
program');
If Error1 = IDRETRY then Goto Testspecs2 else abort;
end;

{2.2. Specify significance level for tests and confidence limits}
If DisplayNumericInputBox ('Enter significance level:', 'Alpha: (between 0.0 and 1.0):',
alpha) = 0 then abort;
If (alpha<=0) OR (alpha>=1) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid significance level!', 'Try again or
abort program.');
If Error1 = IDRETRY then Goto Testspecs3 else abort;
end;

{2.3. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypmean:=test2(1);
mean:=test2(2);
stddev:=test2(3);
size:=test2(4);
sterr:=stddev/sqrt(size);

statistic:=((mean-hypmean)/(stddev/sqrt(size)));
abstat:=abs(statistic);
plevelb:=2*(1-inormal(abstat, 0,1));
plevell:=inormal (statistic, 0,1);
plevelu:=1-inormal (statistic, 0,1);
zcl1:=VNormal ((alpha/2), 0,1);
zcu1:=VNormal ((1-alpha/2),0,1);
zcl2:=VNormal (alpha, 0,1);
zcu2:=VNormal ((1-alpha), 0,1);
If (statistic>zcl1) and (statistic<zcu1) then bothsides$:='Accpt H0' else
bothsides$:='Rejct H0';
If (statistic<zcl2) then leftside$:='Rejct H0' else leftside$:='Accpt H0';
If (statistic>zcu2) then rightside$:='Rejct H0' else rightside$:='Accpt H0';
LCL:=mean+zcl1*sterr;
UCL:=mean+zcu1*sterr;

Dim Results2 (11);
Results2(1):=hypmean;
Results2(2):=Mean;
Results2(3):=LCL;
Results2(4):=UCL;
Results2(5):=statistic;
Results2(6):=plevelb;
Results2(7):=plevell;
Results2(8):=plevelu;

{2.4. Create output table (Scrollsheet), specify title, and assign parameters from
results array}
Scroll:=NewScrollsheet (1, 11, Results2, "One-sample t test for mean (variance
known)|Significance tests and confidence limits are based on the|normal distribution",
'Test results', ?ColumnNames$);
ScrollsheetSetColumnName (Scroll, 1, 'HO', 'mean');
ScrollsheetSetColumnName (Scroll, 2, 'Sample', 'mean');
cflevel$:=str(100-(alpha*100),5,2)+'%';
ScrollsheetSetColumnName (Scroll, 3, 'LCL', '-'+cflevel$);
ScrollsheetSetColumnName (Scroll, 4, 'UCL', '+'+cflevel$);
ScrollsheetSetColumnName (Scroll, 5, ?Name1$, 'z-score');
ScrollsheetSetColumnName (Scroll, 6,'2-sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 7, 'le. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 8, 'ri. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 9, '2-sided', 'test');
ScrollsheetSetColumnName (Scroll, 10, '1-sided', 'left');
ScrollsheetSetColumnName (Scroll, 11, '1-sided', 'right');
ScrollsheetSetTextValue (Scroll, 1, 9, bothsides$);
ScrollsheetSetTextValue (Scroll, 1, 10, leftside$);
ScrollsheetSetTextValue (Scroll, 1, 11, rightside$);

{2.5. End of compound instruction for second menu item - see first line of code in
section 2.1.}
End;



{3. TEST FOR VARIANCE}
{3.1. Specify input parameters for test, significance level, and check validity of
input}
If choice = 3 then begin
Dim Test3(4);
Testspecs3:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Variance for Null
Hypothesis H0:|Sample variance:|Sample size:|Significance level alpha (between 0.0 and
1.0):', Test3) = 0 then abort;
If (test3(1)<=0) OR (test3(2)<=0) OR (test3(3)<=0)  OR (test3(3)-trunc(test3(3))<>0)
then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid parameter(s)!', 'Try again or abort
program');
If Error1 = IDRETRY then Goto Testspecs3 else abort;
end;
If (test3(4)<=0) OR (test3(4)>=1) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid significance level!', 'Try again or
abort program.');
If Error1 = IDRETRY then Goto Testspecs3 else abort;
end;

{3.2. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypvariance:=test3(1);
variance:=test3(2);
size:=test3(3);
alpha:=test3(4);
statistic:=((size-1)*variance)/hypvariance;
nu:=size-1;
abstat:=abs(statistic);
plevelb:=(ichi2(statistic, nu))/2 +(1-ichi2(statistic, nu))/2;
plevell:=ichi2 (statistic, nu);
plevelu:=1-ichi2 (statistic, nu);
cscl1:=VChi2 ((alpha/2), nu);
cscu1:=VChi2 ((1-alpha/2),nu);
cscl2:=VChi2 (alpha, nu);
cscu2:=VChi2 ((1-alpha), nu);
If (statistic>cscl1) and (statistic<cscu1) then bothsides$:='Accpt H0' else
bothsides$:='Rejct H0';
If (statistic<cscl2) then leftside$:='Rejct H0' else leftside$:='Accpt H0';
If (statistic>cscu2) then rightside$:='Rejct H0' else rightside$:='Accpt H0';
LCL:=((size-1)*variance)/cscu1;
UCL:=((size-1)*variance)/cscl1;

Dim Results3 (12);
Results3(1):=hypvariance;
Results3(2):=variance;
Results3(3):=LCL;
Results3(4):=UCL;
Results3(5):=statistic;
Results3(6):=nu;
Results3(8):=plevell;
Results3(9):=plevelu;

{3.3. Create output table (Scrollsheet), specify title, and assign parameters from
results array}
Scroll:=NewScrollsheet (1, 12, Results3, "One-sample Chi-Square test for
variance|Significance tests and confidence limits are based on the|Chi-square
distribution", 'Test results', ?ColumnNames$);
ScrollsheetSetColumnName (Scroll, 1, 'HO', 'variance');
ScrollsheetSetColumnName (Scroll, 2, 'Sample', 'variance');
cflevel$:=str(100-(alpha*100),5,2)+'%';
ScrollsheetSetColumnName (Scroll, 3, 'LCL', '-'+cflevel$);
ScrollsheetSetColumnName (Scroll, 4, 'UCL', '+'+cflevel$);
ScrollsheetSetColumnName (Scroll, 5, ?Name1$, 'Chi2');
ScrollsheetSetColumnName (Scroll, 6,?Name1$, 'df');
ScrollsheetSetColumnName (Scroll, 7,'2-sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 8, 'le. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 9, 'ri. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 10, '2-sided', 'test');
ScrollsheetSetColumnName (Scroll, 11, '1-sided', 'left');
ScrollsheetSetColumnName (Scroll, 12, '1-sided', 'right');
ScrollsheetSetTextValue (Scroll, 1, 10, bothsides$);
ScrollsheetSetTextValue (Scroll, 1, 11, leftside$);
ScrollsheetSetTextValue (Scroll, 1, 12, rightside$);
ScrollsheetSetTextValue (Scroll, 1, 7, 'n/a');

{3.4. End of compound instruction for third menu item - see first line of code in
section 3.1.}
end;


{4. TEST FOR PROPORTION}
{4.1. Specify input parameters for test and check validity of input}
If choice = 4 then begin
Dim Test4(4);
Testspecs4:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Proportion for Null
Hypothesis H0 (between 0.0 and 1.0):|Sample proportion (between 0.0 and 1.0):|Sample
size:|Significance level alpha (between 0.0 and 1.0):', Test4) = 0 then abort;
If (test4(1)<=0) OR (test4(1)>=1) OR (test4(2)<=0) OR (test4(2)>=1) OR (test4(1)<=0) OR
(test4(4)>=1) OR (test4(4)<=0) OR (test4(3)-trunc(test4(3))<>0) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid parameter(s)!', 'Try again or abort
program.');
If Error1 = IDRETRY then Goto Testspecs4 else abort;
end;

{4.2. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypp:=test4(1);
p:=test4(2);
size:=test4(3);
alpha:=test4(4);
If size*p*(1-p) < 9 then DisplayMessageBox (MB_OK, 'Warning!', 'The condition for the
normal approximation is not met. You should interpret the following results very
carefully.'); {Normal approximation only valid when this condition is met}
sigmap:=sqrt((hypp*(1-hypp))/(size-1));
statistic:=(p-hypp)/sqrt((hypp*(1-hypp))/size);
abstat:=abs(statistic);
plevelb:=2*(1-inormal(abstat, 0,1));
plevell:=inormal (statistic, 0,1);
plevelu:=1-inormal (statistic, 0,1);
zcl1:=VNormal ((alpha/2), 0,1);
zcu1:=VNormal ((1-alpha/2),0,1);
zcl2:=VNormal (alpha, 0,1);
zcu2:=VNormal ((1-alpha), 0,1);
If (statistic>zcl1) and (statistic<zcu1) then bothsides$:='Accpt H0' else
bothsides$:='Rejct H0';
If (statistic<zcl2) then leftside$:='Rejct H0' else leftside$:='Accpt H0';
If (statistic>zcu2) then rightside$:='Rejct H0' else rightside$:='Accpt H0';
LCL:=p+zcl1*sigmap;
UCL:=p+zcu1*sigmap;
If LCL < 0 then LCL:=0;

Dim Results4 (11);
Results4(1):=hypp;
Results4(2):=p;
Results4(3):=LCL;
Results4(4):=UCL;
Results4(5):=statistic;
Results4(6):=plevelb;
Results4(7):=plevell;
Results4(8):=plevelu;

{4.3. Create output table (Scrollsheet), specify title, and assign parameters from
results array}
Scroll:=NewScrollsheet (1, 11, Results4, "One-sample test for proportion|Significance
tests and confidence limits are based on the|normal distribution (approximation!)",
'Test results', ?ColumnNames$);
ScrollsheetSetColumnName (Scroll, 1, 'HO', 'proport.');
ScrollsheetSetColumnName (Scroll, 2, 'Sample', 'proport.');
cflevel$:=str(100-(alpha*100),5,2)+'%';
ScrollsheetSetColumnName (Scroll, 3, 'LCL', '-'+cflevel$);
ScrollsheetSetColumnName (Scroll, 4, 'UCL', '+'+cflevel$);
ScrollsheetSetColumnName (Scroll, 5, ?Name1$, 'z-score');
ScrollsheetSetColumnName (Scroll, 6,'2-sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 7, 'le. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 8, 'ri. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 9, '2-sided', 'test');
ScrollsheetSetColumnName (Scroll, 10, '1-sided', 'left');
ScrollsheetSetColumnName (Scroll, 11, '1-sided', 'right');
ScrollsheetSetTextValue (Scroll, 1, 9, bothsides$);
ScrollsheetSetTextValue (Scroll, 1, 10, leftside$);
ScrollsheetSetTextValue (Scroll, 1, 11, rightside$);

{4.4. End of compound instruction for fourth menu item - see first line of code in
section 4.1.}
end;

{5. TEST FOR CORRELATION}
{5.1. Specify input parameters for test and check validity of input}
If choice = 5 then begin
Dim Test5(4);
Testspecs5:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Correlation for Null
Hypothesis H0 (between -1.0 and 1.0):|Sample correlation (between -1.0 and 1.0):|Sample
size:|Significance level alpha (between 0.0 and 1.0):', Test5) = 0 then abort;
If (test5(1)<=-1) OR (test5(1)>=1) OR (test5(2)<=-1) OR (test5(2)>=1) OR (test5(3)<=0)
OR (test5(3)-trunc(test5(3))<>0) OR (test5(4)>=1) OR (test5(4)<=0) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid parameter(s)!', 'Try again or abort
program.');
If Error1 = IDRETRY then Goto Testspecs5 else abort;
end;

{5.2. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypr:=test5(1);
r:=test5(2);
size:=test5(3);
alpha:=test5(4);
Fisherz1:=(1/2)*log((1+r)/(1-r));
Fisherz2:=(1/2)*log((1+hypr)/(1-hypr));
sigmaz:=1/sqrt(size-3);
statistic:=(Fisherz1-Fisherz2)*sqrt(size-3);
abstat:=abs(statistic);
plevelb:=2*(1-inormal(abstat, 0,1));
plevell:=inormal (statistic, 0,1);
plevelu:=1-inormal (statistic, 0,1);
zcl1:=VNormal ((alpha/2), 0,1);
zcu1:=VNormal ((1-alpha/2),0,1);
zcl2:=VNormal (alpha, 0,1);
zcu2:=VNormal ((1-alpha), 0,1);
If (statistic>zcl1) and (statistic<zcu1) then bothsides$:='Accpt H0' else
bothsides$:='Rejct H0';
If (statistic<zcl2) then leftside$:='Rejct H0' else leftside$:='Accpt H0';
If (statistic>zcu2) then rightside$:='Rejct H0' else rightside$:='Accpt H0';
LL:=Fisherz1+zcl1*sigmaz;
UL:=Fisherz1+zcu1*sigmaz;
LCL:=tanh(LL);
UCL:=tanh(UL);

Dim Results5 (11);
Results5(1):=hypR;
Results5(2):=R;
Results5(3):=LCL;
Results5(4):=UCL;
Results5(5):=statistic;
Results5(6):=plevelb;
Results5(7):=plevell;
Results5(8):=plevelu;

{5.3. Create output table (Scrollsheet), specify title, and assign parameters from
results array}
Scroll:=NewScrollsheet (1, 11, Results5, "One-sample test for correlation|Significance
tests and confidence limits are based on the|std. normal distribution (via Fisher's z
transformation)", 'Test results', ?ColumnNames$);
ScrollsheetSetColumnName (Scroll, 1, 'HO', 'corr.');
ScrollsheetSetColumnName (Scroll, 2, 'Sample', 'corr.');
cflevel$:=str(100-(alpha*100),5,2)+'%';
ScrollsheetSetColumnName (Scroll, 3, 'LCL', '-'+cflevel$);
ScrollsheetSetColumnName (Scroll, 4, 'UCL', '+'+cflevel$);
ScrollsheetSetColumnName (Scroll, 5, ?Name1$, 'z-score');
ScrollsheetSetColumnName (Scroll, 6,'2-sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 7, 'le. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 8, 'ri. sided', 'p-level');
ScrollsheetSetColumnName (Scroll, 9, '2-sided', 'test');
ScrollsheetSetColumnName (Scroll, 10, '1-sided', 'left');
ScrollsheetSetColumnName (Scroll, 11, '1-sided', 'right');
ScrollsheetSetTextValue (Scroll, 1, 9, bothsides$);
ScrollsheetSetTextValue (Scroll, 1, 10, leftside$);
ScrollsheetSetTextValue (Scroll, 1, 11, rightside$);

{5.4. End of compound instruction for fifth menu item - see first line of code in
section 5.1.}
end;

{END OF OVERALL PROGRAM CODE}
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.