📄 ostat.c
字号:
vector<string> vDatasetNames;
vDatasetNames.SetSize( nData );
// Vector containing the number of datapoints in each selected dataset
vector<int> vNPTS;
vNPTS.SetSize( nData );
// Vector containing the mean of each selected dataset
vector<double> vMEAN;
vMEAN.SetSize( nData );
// Vector containing the standard deviation of each selected dataset
vector<double> vSD;
vSD.SetSize( nData );
// Vector containing the standard error of each selected dataset
vector<double> vSE;
vSE.SetSize( nData );
// *** Construct All Test Specific Strings and Vectors needed for Summary Table ***
// Default value of case 2
strMainHDR = TT2_PAIRED_SUMMARY_MAIN_HDR;
switch( iTest )
{
case 0:
// One Sample tTest...get summary statistics for One Sample t-Test
strMainHDR = TT1_SUMMARY_MAIN_HDR;
LT_get_str( "%B", szTemp, 40 ); strDatasetName = szTemp;
vDatasetNames[0] = strDatasetName;
LT_evaluate( "sum.n", &dGetVar ); vNPTS[0] = (int) dGetVar;
LT_evaluate( "sum.mean", &dGetVar ); vMEAN[0] = dGetVar;
LT_evaluate( "sum.sd", &dGetVar ); vSD[0] = dGetVar;
LT_evaluate( "sum.sd/sqrt(sum.n)", &dGetVar ); vSE[0] = dGetVar;
break;
case 1:
// Two Sample Independent tTest...continue on through case 2
strMainHDR = TT2_INDEPENDENT_SUMMARY_MAIN_HDR;
case 2:
// Two Sample Paired tTest...code common with case 1
// Get summary statistics for the first selected dataset of the Two Sample t-Test
LT_get_str( "%A", szTemp, 40 ); strDatasetName = szTemp;
vDatasetNames[0] = TT2_SAMPLE1 + strDatasetName;
LT_get_var( "n1", &dGetVar ); vNPTS[0] = (int) dGetVar;
LT_get_var( "ave1", &dGetVar ); vMEAN[0] = dGetVar;
LT_get_var( "sd1", &dGetVar ); vSD[0] = dGetVar;
LT_get_var( "se1", &dGetVar ); vSE[0] = dGetVar;
// Get summary statistics for the second selected dataset of the Two Sample t-Test
LT_get_str( "%B", szTemp, 40 ); strDatasetName = szTemp;
vDatasetNames[1] = TT2_SAMPLE2 + strDatasetName;
LT_get_var( "n2", &dGetVar ); vNPTS[1] = (int) dGetVar;
LT_get_var( "ave2", &dGetVar ); vMEAN[1] = dGetVar;
LT_get_var( "sd2", &dGetVar ); vSD[1] = dGetVar;
LT_get_var( "se2", &dGetVar ); vSE[1] = dGetVar;
break;
case 3:
// One-Way ANOVA
strMainHDR = AN1_SUMMARY_MAIN_HDR;
// Get summary statistics for all selected ANOVA1 datasets
Dataset dsNPTS( "_NPTS" ); vNPTS = dsNPTS; dsNPTS.Detach();
Dataset dsMEAN( "_MEAN" ); vMEAN = dsMEAN; dsMEAN.Detach();
Dataset dsSD( "_SD" ); vSD = dsSD; dsSD.Detach();
double dNpts;
string strDB;
// Read the names of all selected datasets...
for( ii = 0; ii < nData; ii++ )
{
dNpts = (double) vNPTS[ii];
vSE[ii] = vSD[ii] / sqrt( dNpts );
strDB.Format( "%%A=ANOVA1Way!SelectedDataLBX.v%d$", ii + 1 );
LT_execute( strDB );
LT_get_str( "%A", szTemp, 40 );
strDatasetName = szTemp;
vDatasetNames[ii] = strDatasetName;
}
strDatasetName = AN1_DATASET;
break;
}
int iRow, iCol;
string strTablePath;
string strTableName;
Worksheet wksSummaryTable;
// Create and attach to a temporary Summary Statistics output worksheet
strTablePath = GetFullPath( "osSummaryStat.OGW", OSTAT_OGW_SUBFOLDER, TRUE );
if( !strTablePath.IsFile() )
{
Type_ErrorMsg1( OSTAT_FILE_NOT_FOUND_ERROR_MSG, strTablePath );
return OSTAT_ABORT_NO_ERROR_MSG;
}
wksSummaryTable.Open( strTablePath, CREATE_TEMP );
strTableName = wksSummaryTable.GetPage().GetName();
// All row and column numbers are indexed from 0
// *** Change Main Header ***
// Change main header in row 1 and column 1
iRow = 0;
iCol = 0;
wksSummaryTable.SetCell( iRow, iCol, strMainHDR );
if( iTest==3 ) // If One-Way ANOVA...
{
// Change "Sample" to "Dataset" in row 5 column 2
iRow = 4;
iCol = 1;
wksSummaryTable.SetCell( iRow, iCol, strDatasetName );
}
// *** Add nData Summary Rows and Output Summary Statistics ***
// Insert nData blank lines in summary table at row 7
iRow = 6;
Type_Insert_Blank_Lines_in_Wks( wksSummaryTable, iRow, nData );
// Loop on samples/selected datasets starting in row 7
for( ii = 0; ii < nData; iRow++, ii++ )
{
// Output sample or selected dataset name in column 2
iCol = 1;
strDatasetName = vDatasetNames[ii];
wksSummaryTable.SetCell( iRow, iCol, strDatasetName );
// Output number of data points in column 3
iCol++;
wksSummaryTable.SetCell( iRow, iCol, vNPTS[ii] );
// Output mean in column 4
iCol++;
wksSummaryTable.SetCell( iRow, iCol, vMEAN[ii] );
// Output standard deviation in column 5
iCol++;
wksSummaryTable.SetCell( iRow, iCol, vSD[ii] );
// Output standard error in column 6
iCol++;
wksSummaryTable.SetCell( iRow, iCol, vSE[ii] );
}
if( mod( iTest, 3 ) != 0 ) // If Two Sample t-Test (iTest=1 or 2)...
{
// Add blank row after separator table
iRow++;
Type_Insert_Blank_Lines_in_Wks( wksSummaryTable, iRow );
// Output difference of sample means after separator table
iCol = 1;
wksSummaryTable.SetCell( iRow, iCol, TT2_DIF_OF_MEANS );
iCol = 3;
wksSummaryTable.SetCell( iRow, iCol, vMEAN[0] - vMEAN[1] );
}
// GJL 11/14/02 v7.0434 USE_OC_WKS_TYPE_NOT_LT
// Send output Summary Statistics worksheet to Results Log
wksSummaryTable.Type( TYPETARGET_OUTPUTLOG, NULL, 7 );
return OSTAT_NO_ERROR;
}
/**
Function to output to the Results Log Actual and Hypothetical Power values for the
One and Two Sample t-Test and the One-Way ANOVA dialog boxes.
Example:
See the [DisplayPower] section in TTestMean1Sample.OGS, TTestMean2Sample.OGS, and
ANOVA1Way.OGS for sample calls.
Parameters:
iPower=Bitwise flag indicating whether no power (0), only actual power (1), or actual and
hypothetical powers (3) are to be output
dAlpha=Alpha used in power computations
iSSType=Flag for sample size type:0 for "Total" or 1 for "Individual"
iActualSS=Actual sample size
dActualPower=Actual power value
strHypotSSDataName=Name of dataset containing hypothetical sample sizes
strHypotPowerDataName=Name of dataset containing hypothetical powers
Return:
Outputs Actual and Hypothetical Power values to Results Log for the One and
Two Sample t-Test and the One-Way ANOVA dialog boxes. Returns OSTAT_NO_ERROR
on successful exit.
*/
int osPower_Output_to_ResultsLog( int iPower, double dAlpha, int iSSType, int iActualSS,
double dActualPower, string strHypotSSDataName, string strHypotPowerDataName )
{
int ii, iSize;
int iRow, iCol;
string strTablePath;
string strTableName;
Worksheet wksPowerTable;
// Create and attach to a temporary Power output worksheet
strTablePath = GetFullPath( "osPowerAnalysis.OGW", OSTAT_OGW_SUBFOLDER, TRUE );
if( !strTablePath.IsFile() )
{
Type_ErrorMsg1( OSTAT_FILE_NOT_FOUND_ERROR_MSG, strTablePath );
return OSTAT_ABORT_NO_ERROR_MSG;
}
wksPowerTable.Open( strTablePath, CREATE_TEMP );
strTableName = wksPowerTable.GetPage().GetName();
// All row and column numbers are indexed from 0
// *** Finish Power Header ***
iRow = 2; // Sample size type in row 3
switch( iSSType )
{
case -1:
// If One Sample t-Test then no sample size type row...delete row in *.OGW
wksPowerTable.DeleteRow( iRow );
iRow--;
break;
case 0:
// If Two Sample Independent t-Test then sample size type is Total (most common)
// so leave alone...Total is in *.OGW
break;
case 1:
// If Two Sample Paired t-Test then sample size type is Individual so overwrite
// Total with Individual
iCol = 2; // Output "Individual" in column 3
wksPowerTable.SetCell( iRow, iCol, TT2_INDIVIDUAL );
break;
}
// *** Output Actual Power Row ***
// Insert one blank line in power table for actual power
iRow += 3; // Jump to row for actual power
Type_Insert_Blank_Lines_in_Wks( wksPowerTable, iRow );
// Output actual alpha in column 2
iCol = 1;
wksPowerTable.SetCell( iRow, iCol, dAlpha );
// Output actual sample size in column 3
iCol++;
wksPowerTable.SetCell( iRow, iCol, iActualSS );
// Output actual power in column 4
iCol++;
wksPowerTable.SetCell( iRow, iCol, dActualPower );
// Output "(actual)" in column 5
iCol++;
wksPowerTable.SetCell( iRow, iCol, OSTAT_POWER_ACTUAL );
// *** Output Hypothetical Power Rows ***
if( iPower & 2 ) // If hypothetical powers were computed...
{
// Get hypothetical power dataset
Dataset dsSSPOW( strHypotPowerDataName );
iSize = dsSSPOW.GetSize();
vector<double> vSSPOW;
vSSPOW.SetSize( iSize );
vSSPOW = dsSSPOW;
dsSSPOW.Detach();
// Get hypothetical sample size dataset
Dataset dsSS( strHypotSSDataName );
iSize = dsSS.GetSize(); // Get number of hypothetical sample sizes
vector<int> vSS;
vSS.SetSize( iSize );
vSS = dsSS;
dsSS.Detach();
// Insert one blank line in power table for each hypothetical power
iRow++;
Type_Insert_Blank_Lines_in_Wks( wksPowerTable, iRow, iSize );
// Loop on hypothetical sample sizes starting in row 7
for( ii = 0; ii < iSize; iRow++, ii++ )
{
// Output alpha in column 2
iCol = 1;
wksPowerTable.SetCell( iRow, iCol, dAlpha );
// Output hypothetical sample size in column 3
iCol++;
wksPowerTable.SetCell( iRow, iCol, vSS[ii] );
// Output hypothetical power in column 4
iCol++;
wksPowerTable.SetCell( iRow, iCol, vSSPOW[ii] );
}
}
// GJL 11/14/02 v7.0434 USE_OC_WKS_TYPE_NOT_LT
// Send output Power worksheet to Results Log
wksPowerTable.Type( TYPETARGET_OUTPUTLOG, NULL, 5 );
return OSTAT_NO_ERROR;
}
/**
Computes the power of an ANOVA experiment. The computational engine is the
NAG function nag_prob_non_central_f_dist.
Example:
See the [ComputeActualPower] section of ANOVA1Way.OGS and the osANOVA2Way_Compute_Power
function for sample calls.
Parameters:
dFval=Deviate from the F-distribution for given values of alpha, df1, and df2
ddf1=Degrees of freedom for the numerator variance
ddf2=Degrees of freedom for the denominator variance
dnc=Non-centality factor
Return:
Returns the power of an ANOVA experiment on successful exit or NANUM on error.
*/
double osANOVA_Power( double dFval, double ddf1, double ddf2, double dnc )
{
double dtol = AN_POWER_TOLERANCE; // Required accuracy of the solution
int imax_iter = AN_POWER_ITERATIONS; // Maximum number of iterations to be performed
NagError neErr; // NAG error structure
NagError *pneFail = &neErr;
double dPower; // Computed power
// From <NAG\OCN_g01.h>: g01gdc nag_prob_non_central_f_dist
// Computes probabilities for the non-central F-distribution
dPower = 1 - nag_prob_non_central_f_dist( dFval, ddf1, ddf2, dnc, dtol, imax_iter, pneFail );
if( pneFail->code != NE_NOERROR )
{ // If NAG error = NE_REAL_ARG_CONS (non-centrality
if( pneFail->code == NE_REAL_ARG_CONS ) // factor is too large causing nag_prob_non_central_f_dist
dPower = 1; // to effectively return 0) then return 1. Otherwise,
else // return NANUM on NAG error.
dPower = NANUM;
}
return dPower;
}
/**
Computes the standard errors of the differences of the means for the osANOVA1Way_Means_Comparison
and osANOVA2Way_Means_Comparison functions. The returned vector vC is used in place of the C
matrix required by the NAG function nag_anova_confid_interval and consequently is indexed as
if it were a matrix (using row major order).
Example:
See functions osANOVA1Way_Means_Comparison and osANOVA2Way_Means_Comparison.
Parameters:
inData=Number of datasets for which the standard errors of the differences of the means is
computed
dmse=Mean Square Error of ANOVA computation
vNPTS=A vector containing the number of datapoints in each of the inData datasets
vC=The returned vector vC contains standard errors of the differences of the means for an
ANOVA computation
Return:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -