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 raw 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 set of arrays: testvar# (for assigning values of selected
variables), 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. Select the variables for the test, assign their values to an array, and calculate
sample statistics}
If choice = 1 then begin
If SelectVariables1 ('Select variable for the test', 1, 1, Selected, Count1, 'Variable
for One-sample mean test:') = 0 then abort;
Redim testvar1 (ncases);
For i:=1 to Ncases do testvar1(i):=data(i,Selected);
ValMean (Testvar1, 1, Ncases, mean);
ValStDeviation (Testvar1, 1, NCases, stdv);
ValStError (Testvar1, 1, NCases, sterr);
{1.2. Specify input parameter (H0 mean) for test, significance level, and check validity
of input}
Dim Test1(2);
Testspecs1:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Mean for Null
Hypothesis H0:|Significance level alpha (between 0.0 and 1.0):', Test1) = 0 then abort;
If (test1(2)>=1) or (test1(2) <= 0) 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}
alpha:=test1(2);
nu:=ncases-1;
statistic:=((mean-test1(1))/(stdv/sqrt(ncases)));
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 (9);
Results1(1):=Test1(1);
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", Varname(selected), ?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. Select the variable for the test, assign its values to an array, and calculate
sample statistics}
If choice = 2 then begin
If SelectVariables1 ('Select variable for the test', 1, 1, Selected2, Count1, 'Variable
for One-sample mean test:') = 0 then abort;
Redim testvar2 (ncases);
For i:=1 to Ncases do testvar2(i):=data(i,Selected2);
ValMean (Testvar2, 1, Ncases, mean);
ValStError (Testvar2, 1, NCases, sterr);
{2.2. Specify input parameters (H0 mean, std.dev.) for test, significance level, and
check validity of input}
Dim Test2(3);
Testspecs2:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Mean for Null
Hypothesis H0:|Standard deviation:|Significance level alpha (between 0.0 and 1.0):',
Test2) = 0 then abort;
If (test2(3)<=0) OR (test2(3)>=1) OR (test2(2)<=0) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid test parameter(s)!', 'Try again or
abort program.');
If Error1 = IDRETRY then Goto Testspecs2 else abort;
end;
{2.3. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypmean:=test2(1);
stdv:=test2(2);
alpha:=test2(3);
size:=ncases;
statistic:=((mean-hypmean)/(stdv/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",
Varname(Selected2), ?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. Select the variable for the test, assign its values to an array, and calculate
sample statistics}
If choice = 3 then begin
If SelectVariables1 ('Select variable for the test', 1, 1, Selected3, Count1, 'Variable
for One-sample variance test:') = 0 then abort;
Redim testvar3 (ncases);
For i:=1 to Ncases do testvar3(i):=data(i,Selected3);
ValVariance (Testvar3, 1, NCases, variance);
{3.2. Specify input parameter (H0 variance) and significance level for test, and check
validity of input}
Dim Test3(2);
Testspecs3:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Variance for Null
Hypothesis H0:|Significance level alpha (between 0.0 and 1.0):', Test3) = 0 then abort;
If (test3(1)<=0) OR (test3(2)<=0) OR (test3(2)>=1) then begin
Error1:=DisplayMessageBox (MB_RETRYCANCEL, 'Invalid parameter(s)!', 'Try again or abort
program.');
If Error1 = IDRETRY then Goto Testspecs3 else abort;
end;
{3.3. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypvariance:=test3(1);
alpha:=test3(2);
size:=ncases;
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';
LCL2:=((size-1)*variance)/cscu1;
UCL2:=((size-1)*variance)/cscl1;
Dim Results3 (12);
Results3(1):=hypvariance;
Results3(2):=variance;
Results3(3):=LCL2;
Results3(4):=UCL2;
Results3(5):=statistic;
Results3(6):=nu;
Results3(8):=plevell;
Results3(9):=plevelu;
{3.4. 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|Chi-square distribution",
Varname(selected3), ?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.5. End of compound instruction for third menu item - see first line of code in
section 3.1.}
end;
{4. TEST FOR PROPORTION}
{4.1. Select the variable for the test, assign its values to an array, and calculate
sample statistics}
If choice = 4 then begin
If SelectVariables1 ('Select the variable for the test', 1, 1, Selected4, Count1,
'Variable for One-sample proportion test:') = 0 then abort;
Redim testvar4 (ncases);
For i:=1 to Ncases do testvar4(i):=data(i,Selected4);
{4.2. Specify input parameters (H0 proportion, numeric code) and significance level for
test, and check validity of input}
Dim Test4(3);
Testspecs4:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'H0 proportion (between
0.0 and 1.0):|Numeric code for attribute x (for p = x/n):|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)-trunc(test4(2))<>0) OR
(test4(3)>=1) OR (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.3. Compute test statistic, confidence limits, etc. and assign values to results
array}
hypp:=test4(1);
alpha:=test4(3);
X:=0;
For i:=1 to Ncases do begin
If testvar4(i)=test4(2) then X:=X+1;
end;
p:=X/Ncases;
size:=ncases;
{If the condition for normal approximation is not met a warning will be issued}
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.');
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.4. 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!)",Varname(Selected4), ?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.5. End of compound instruction for fourth menu item - see first line of code in
section 4.1.}
end;
{5. TEST FOR VARIANCE}
{5.1. Select the pair of variables for the test, assign its values to an array, and
calculate sample correlation}
If choice = 5 then begin
If SelectVariables2 ('Select the pair of variables for the correlation:', 1, 1, XVar,
Count1, 'X variable:', 1, 1, YVar, Count2, 'Y variable:') = 0 then abort;
Redim pair(ncases,2);
For i:=1 to Ncases do begin
pair(i,1):=data(i,XVar);
pair(i,2):=data(i,YVar);
end;
ReDim Corrs (2,2);
MatrixCorrelations (pair, 1, corrs);
{5.2. Specify input parameter (H0 correlation) and significance level for test, and
check validity of input}
Dim Test5(2);
Testspecs5:
If DisplayNumericInputBox ('Enter the parameters for the test:', 'Correlation for Null
Hypothesis H0 (between -1.0 and 1.0):|Significance level alpha (between 0.0 and 1.0):',
Test5) = 0 then abort;
If (test5(1)<=-1) OR (test5(1)>=1) OR (test5(2)<=0) OR (test5(2)>=1) 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:=corrs(1,2);
size:=ncases;
alpha:=test5(2);
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.5. 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]](../../../images/sssmall.gif)
2300 East 14th Street, Tulsa, OK 74104
Phone: (918) 749-1119; Fax: (918) 749-2217
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.