📄 ostat.c
字号:
Returns a vector vC used in place of the C matrix required by the NAG function
nag_anova_confid_interval. The vector vC contains standard errors of the differences of the
means for an ANOVA computation. The jj + ii * inData element contains the standard error
of the difference between the ii^th and jj^th means. Also returns OSTAT_NO_ERROR on successful
exit.
*/
int osANOVA_Compute_StdErr_MeanDiffs( int inData, double dmse, vector<int> &vNPTS, vector<double> &vC )
{
int ii, jj;
// Loop through lower triangle of "matrix" indexing through vector as if it were a matrix
// For inData == 3, Non-zero numbers identify vector index for elements in Lower triangle of matrix:
// index = Col + Row * NumCols: O O O
// 3 O O
// 6 7 O
for( ii = 1; ii < inData; ii++ ) // Loop on rows of "matrix" starting with second row (ii==1)
{
for( jj = 0; jj < ii; jj++ ) // Loop on columns of "matrix"
{
// Compute standard errors of the differences of the means
vC[jj + ii * inData] = sqrt( dmse * ( 1.0 / vNPTS[ii] + 1.0 / vNPTS[jj] ) );
}
}
return OSTAT_NO_ERROR;
}
////////////////////////////////////////////////////////////////////////////////////
/**
Function to output to the Results Log an F Table for the One-Way ANOVA Test or
for the Levene and Brown-Forsythe Tests of Equal Variance. .
Example:
See the [DisplayFandP] section in ANOVA1Way.OGS for a sample call.
Parameters:
strTestType=Test Type: ANOVA on "RawData", or "Levene" or "BrownForsythe" Tests of
Equal Variance
ddf1=Degrees of Freedom of Model row
dssb=Sum of Squares of Model row
dmsb=Mean Square of Model row
dF=F Value
dP=P Value
ddf2=Degrees of Freedom of Error row
dsse=Sum of Squares of Error row
dmse=Mean Square of Error row
dSigLevel=Significance Level
Return:
Outputs an F Table for the One-Way ANOVA Test or for the Levene or Brown-Forsythe
Tests of Equal Variance and returns OSTAT_NO_ERROR on successful exit.
*/
int osANOVA1_Table_Output_to_ResultsLog( string strTestType, double ddf1, double dssb, double dmsb,
double dF, double dP, double ddf2, double dsse, double dmse, double dSigLevel )
{
int ii, iRow, iCol;
string strOut;
string strSigLevel;
string strTablePath;
string strTableName;
Worksheet wksANOVA1Table;
// Create and attach to a temporary ANOVA1 Table output worksheet
strTablePath = GetFullPath( "osANOVA1Table.OGW", OSTAT_OGW_SUBFOLDER, TRUE );
if( !strTablePath.IsFile() )
{
Type_ErrorMsg1( OSTAT_FILE_NOT_FOUND_ERROR_MSG, strTablePath );
return OSTAT_ABORT_NO_ERROR_MSG;
}
wksANOVA1Table.Open( strTablePath, CREATE_TEMP );
strTableName = wksANOVA1Table.GetPage().GetName();
// All row and column numbers are indexed from 0
// *** If Needed Delete Null and Alternative Hypothesis Rows and Change Test Header ***
if( strTestType.Compare( "RawData" ) ) // If test is not "RawData"...
{
// Delete first three rows of temporary worksheet containing hypotheses
iRow = 0;
for( ii = 0; ii < 3; ii++ )
wksANOVA1Table.DeleteRow( iRow );
// Change Test Header in row 1 and column 2
iCol = 1;
if( strTestType.Compare( "Levene" ) )
strOut = AN1_BROWN_FORSYTHE_HDR; // Returns 0 on match...so if "not" "Levene"
else
strOut = AN1_LEVENE_HDR; // Else is "Levene"
wksANOVA1Table.SetCell( iRow, iCol, strOut );
iRow = 5; // Model row number if hypotheses are deleted
}
else // Else test is "RawData" skip to Model row of F table
iRow = 8; // Model row number if hypotheses are not deleted
// *** Output ANOVA Table Values DoF, Sum of Squares, Mean Square, F, and P ***
// Output values in Model row of ANOVA Table
// Output df1 value in column 3
iCol = 2;
wksANOVA1Table.SetCell( iRow, iCol, ddf1 );
// Output ssb in column 4
iCol++;
wksANOVA1Table.SetCell( iRow, iCol, dssb );
// Output msb value in column 5
iCol++;
wksANOVA1Table.SetCell( iRow, iCol, dmsb );
// Output F in column 6
iCol++;
wksANOVA1Table.SetCell( iRow, iCol, dF );
// Output P value in column 7
iCol++;
wksANOVA1Table.SetCell( iRow, iCol, dP );
// Output values in Error row of ANOVA Table
// Output df2 value in next row and column 3
iRow++;
iCol = 2;
wksANOVA1Table.SetCell( iRow, iCol, ddf2 );
// Output sse in column 4
iCol++;
wksANOVA1Table.SetCell( iRow, iCol, dsse );
// Output mse value in column 5
iCol++;
wksANOVA1Table.SetCell( iRow, iCol, dmse );
// *** Output Decision Rule ***
// Localize significance level
strSigLevel = LocalizeDouble( dSigLevel, OSTAT_SIG_CON_LEVEL_FORMAT );
// Output the first phrase of the decision rule three rows after the Model row and in column 2
iRow += 3;
iCol = 1;
strOut.Format( AN1_DECISION_PHRASE1, strSigLevel );
wksANOVA1Table.SetCell( iRow, iCol, strOut );
// Output second phrase of decision rule in next row and in column 2
iRow++;
// .Compare returns 0 on match so if test type is not "RawData" output decision rule for population variances
if( strTestType.Compare( "RawData" ) )
{
if ( dP > dSigLevel )
strOut = AN1_DECISION_PHRASE2_VARS_ARE_NOT_DIFF; // Variances are not different
else
strOut = AN1_DECISION_PHRASE2_VARS_ARE_DIFF; // Variances are different
}
else // Else test type is "RawData" output decision rule for population means
{
if ( dP > dSigLevel )
strOut = AN1_DECISION_PHRASE2_MEANS_ARE_NOT_DIFF; // Means are not different
else
strOut = AN1_DECISION_PHRASE2_MEANS_ARE_DIFF; // Means are different
}
wksANOVA1Table.SetCell( iRow, iCol, strOut ); // Output the second phrase of the decision rule
// GJL 11/14/02 v7.0434 USE_OC_WKS_TYPE_NOT_LT
// Send output ANOVA1 Table worksheet to Results Log
wksANOVA1Table.Type( TYPETARGET_OUTPUTLOG, NULL, 8 );
return OSTAT_NO_ERROR;
}
/**
Function to compute and output the One-Way ANOVA Means Comparison analysis. For each selected
means comparison test (Bonferroni, Scheffe', or Tukey), the mean of each selected dataset is
compared to the mean of every other selected dataset. The computational engine for performing
the means comparison tests is the NAG function nag_anova_confid_interval (from <NAG\OCN_g04.h>:
g04dbc)
Example:
See the [MeansComparison] section in ANOVA1Way.OGS for a sample call.
Parameters:
iTest=Bitwise flag indicating which Means Comparison tests to perform: Bonferroni (Bit 0),
Scheffe' (Bit 1), or Tukey (Bit 2)
inData=Number of selected datasets
strNPTS=Name of dataset holding the number of data points in each selected dataset
ddf2=Degrees of Freedom of the ANOVA Error term
strMEAN=Name of dataset holding the mean of each selected dataset
dmse=Mean Square of the ANOVA Error term
dSigLevel=Significance Level
Return:
For each selected means comparison test (Bonferroni, Scheffe', or Tukey), the mean of each
selected dataset is compared to the mean of every other selected dataset. Returns
OSTAT_NO_ERROR on successful exit.
*/
int osANOVA1Way_Means_Comparison( int iTest, int inData, string strNPTS, double ddf2, string strMEAN,
double dmse, double dSigLevel )
{
int iErr, iSize;
string strTestName; // Name of test to perform
// Read in the NPTS dataset
Dataset dsNPTS( strNPTS );
iSize = dsNPTS.GetSize();
vector<int> vNPTS;
vNPTS.SetSize( iSize );
vNPTS = dsNPTS;
dsNPTS.Detach();
// Read in the MEAN dataset
Dataset dsMEAN( strMEAN );
iSize = dsMEAN.GetSize();
vector<double> vMEAN;
vMEAN.SetSize( iSize );
vMEAN = dsMEAN;
dsMEAN.Detach();
// Vector to hold the standard errors of the differences of the means
vector<double> vC;
iSize = pow( inData, 2 );
vC.SetSize( iSize );
// Function computes the standard errors of the differences of the means
iErr = osANOVA_Compute_StdErr_MeanDiffs( inData, dmse, vNPTS, vC );
if( iErr != OSTAT_NO_ERROR )
return iErr;
double dCLevel;
dCLevel = 1 - dSigLevel; // Confidence level
// Vector to hold the lower limit of the simultaneous confidence interval of the differences of the means
iSize = inData * ( inData - 1 ) / 2;
vector<double> vCIL;
vCIL.SetSize( iSize );
// Vector to hold the upper limit of the simultaneous confidence interval of the differences of the means
vector<double> vCIU;
vCIU.SetSize( iSize );
// Vector to hold Significance decisions: 1=YES and 0=NO
vector<int> vISIG;
vISIG.SetSize( iSize );
// Perform Bonferroni test?
if( iTest & 1 )
{
strTestName = AN_BONFERRONI_TEST;
// From <NAG\OCN_g04.h>: g04dbc nag_anova_confid_interval: Perform Bonferroni test
iErr = nag_anova_confid_interval( Nag_BonferroniInterval, inData, vMEAN, ddf2, vC, inData, dCLevel, vCIL,
vCIU, vISIG );
if( iErr != NE_NOERROR )
return OSTAT_CONF_INT_ERROR1;
// Output the means comparison analysis for the Bonferroni test
iErr = osANOVA1Way_Means_Comparison_Output_to_ResultsLog( strTestName, inData, vMEAN, vCIL,
vCIU, vISIG, dSigLevel );
if( iErr != OSTAT_NO_ERROR )
return iErr;
}
// Perform Scheffe' test?
if( iTest & 2 )
{
strTestName = AN_SCHEFFE_TEST;
// From <NAG\OCN_g04.h>: g04dbc nag_anova_confid_interval: Perform Scheffe' test
iErr = nag_anova_confid_interval( Nag_ScheffeInterval, inData, vMEAN, ddf2, vC, inData, dCLevel, vCIL,
vCIU, vISIG );
if( iErr != NE_NOERROR )
return OSTAT_CONF_INT_ERROR2;
// Output the means comparison analysis for the Scheffe' test
iErr = osANOVA1Way_Means_Comparison_Output_to_ResultsLog( strTestName, inData, vMEAN, vCIL,
vCIU, vISIG, dSigLevel );
if( iErr != OSTAT_NO_ERROR )
return iErr;
}
// Perform Tukey test?
if( iTest & 4 )
{
strTestName = AN_TUKEY_TEST;
// SET_MAX_TUKEY_RESID_DF QA70-688: Limit max value of ddf2 since NAG function nag_anova_confid_interval
// crashes for Tukey test when ddf2 > AN_MAX_TUKEY_RESID_DF. There is little difference in function values
// when ddf2 is large (>120) so set AN_MAX_TUKEY_RESID_DF as max value for ddf2
if( ddf2 > AN_MAX_TUKEY_RESID_DF )
ddf2 = AN_MAX_TUKEY_RESID_DF;
// END SET_MAX_TUKEY_RESID_DF QA70-688
// From <NAG\OCN_g04.h>: g04dbc nag_anova_confid_interval: Perform Tukey test
iErr = nag_anova_confid_interval( Nag_TukeyInterval, inData, vMEAN, ddf2, vC, inData, dCLevel, vCIL,
vCIU, vISIG );
if( iErr != NE_NOERROR )
return OSTAT_CONF_INT_ERROR3;
// Output the means comparison analysis for the Tukey test
iErr = osANOVA1Way_Means_Comparison_Output_to_ResultsLog( strTestName, inData, vMEAN, vCIL,
vCIU, vISIG, dSigLevel );
if( iErr != OSTAT_NO_ERROR )
return iErr;
}
return OSTAT_NO_ERROR;
}
/**
One-Way ANOVA function to output Means Comparison results to the Results Log.
Example:
See function osANOVA1Way_Means_Comparison.
Parameters:
strTestName=Name of Means Comparison test to perform: Bonferroni, Scheffe', or Tukey
inData=Number of selected datasets
vMEAN=Means of selected datasets
vCIL=Lower limit of simultaneous confidence intervals for difference of means
vCIU=Upper limit of simultaneous confidence intervals for difference of means
vISIG=Significance decisions: 1=YES and 0=NO
dSigLevel=Significance Level
Return:
Outputs One-Way ANOVA Means Comparison results to Results Log. Returns OSTAT_NO_ERROR
on successful exit.
*/
int osANOVA1Way_Means_Comparison_Output_to_ResultsLog( string strTestName, int inData, vector<double> &vMEAN,
vector<double> &vCIL, vector<double> &vCIU, vector<int> &vISIG, double dSigLevel )
{
int ii, jj, iv;
int iRow, iCol;
double dMeanDif;
string strDB, strOut;
string strDatasetName, strSigLevel, strSig;
char szTemp[40];
string strTablePath;
string strTableName;
Worksheet wksMeansTable;
// Transpose (negate and swap) vCIU and vCIL to standardize NAG output with references
vector<double> vCILT;
vCILT.SetSize( inData );
vCILT = -1 * vCIU;
vector<double> vCIUT;
vCIUT.SetSize( inData );
vCIUT = -1 * vCIL;
// Create and attach to a temporary One-Way ANOVA Means Comparison output worksheet
strTablePath = GetFullPath( "osANOVA1Means.OGW", OSTAT_OGW_SUBFOLDER, TRUE );
if( !strTablePath.IsFile() )
{
Type_ErrorMsg1( OSTAT_FILE_NOT_FOUND_ERROR_MSG, strTablePath );
return OSTAT_ABORT_NO_ERROR_MSG;
}
wksMeansTable.Open( strTablePath, CREATE_TEMP );
strTableName = wksMeansTable.GetPage().GetName();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -