⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 survivalanalysis.c

📁 图像处理的压缩算法
💻 C
📖 第 1 页 / 共 4 页
字号:
	string strTablePath;
	string strTableName;

	// *** Output Survival Analysis Main Headers and Summary Table ***
	// Get name of Time dataset from dialog box	
	strDB = "%A=KaplanMeier!TimeDataEBX$";
	LT_execute( strDB );
	LT_get_str( "%A", szTemp, 40 );
	strTimeData = szTemp;

	// Get name of Censor dataset from dialog box
	strDB = "%A=KaplanMeier!CensorDataEBX$";
	LT_execute( strDB );
	LT_get_str( "%A", szTemp, 40 );
	strCensorData = szTemp;

	// Get Censor Value from dialog box
	strDB = "%A=KaplanMeier!CensorValueEBX$";
	LT_execute( strDB );	
	LT_get_str( "%A", szTemp, 40 );
	strCensorValue = szTemp;

	// Get name of dialog box (or feature) from Local.h
	strDB.Format( KM_MAIN_HDR );
	
	// Output survival analysis main headers and summary table for Kaplan-Meier Estimator
	iErr = saSurvival_Analysis_ResultsLog_Summary( strDB, strTimeData, strCensorData, strCensorValue,
		 nPts, dCensoredPts );
	if( iErr != SA_NO_ERROR )
		return iErr;

	// *** Output Survivorship Function and Errors ***
	if( nResults & 1)
	{
		Worksheet wksSurvFuncTable;
		
		// Create and attach to a temporary worksheet 
		strTablePath = GetFullPath( "saKMSurvFunc.OGW", SA_OGW_SUBFOLDER, TRUE );
		if( !strTablePath.IsFile() )
		{
			Type_ErrorMsg1( SA_FILE_NOT_FOUND_ERROR_MSG, strTablePath );
			return SA_ABORT_NO_ERROR_MSG;
		}
		wksSurvFuncTable.Open( strTablePath, CREATE_TEMP );
		strTableName = wksSurvFuncTable.GetPage().GetName();
		
		// Rows and columns are indexed from 0
		
		// Insert a blank line in survivorship function table for each data point + 1
		// (+ 1 for time 0) starting at row 5
		iRow = 4;
		Type_Insert_Blank_Lines_in_Wks( wksSurvFuncTable, iRow, nPts + 1 );
		
		// Output time value = 0 in column 2
		iCol = 1;
		wksSurvFuncTable.SetCell( iRow, iCol, 0.0 );
			
		// Output survivorship function value = 1 in column 3
		iCol++;
		wksSurvFuncTable.SetCell( iRow, iCol, 1.0 );
		
		// Output survivorship function error value = 0 in column 4
		iCol++;
		wksSurvFuncTable.SetCell( iRow, iCol, 0.0 );

		// Output non-zero time values starting in row 5
		iRow++;
		
		// Loop on all non-zero time values
		jj = -1;
		dPrevUncensoredTime = -1;
		for( ii = 0;ii < nPts; iRow++, ii++ )
		{
			// Output time, survivorship function, and survivorship function error values
			// Output time value in column 2
			iCol = 1;
			wksSurvFuncTable.SetCell( iRow, iCol, vTIME[ii] );
			
			if( vCENSOR[ii] == 1 ) // If censored value...
			{
				// Output missing value for survivorship function value in column 3
				iCol++;
				wksSurvFuncTable.SetCell( iRow, iCol, NANUM );
				
				// Output missing value for survivorship function error value in column 4
				iCol++;
				wksSurvFuncTable.SetCell( iRow, iCol, NANUM );
			}
			else // Else, not censored...
			{
				// Output survivorship function and survivorship function error value
				// Must index into vP and vPSIG differently than vTIME because only non-repeated and uncensored
				// survival/error values are in vP/vPSIG while all times are in vTIME 
				if( jj < 0 || vTIME[ii] != dPrevUncensoredTime )
					jj++;
				dPrevUncensoredTime = vTIME[ii];
				
				// Output survivorship function value in column 3
				iCol++;				
				wksSurvFuncTable.SetCell( iRow, iCol, vP[jj] );

				// Output survivorship function error value in column in column 4
				iCol++;				
				wksSurvFuncTable.SetCell( iRow, iCol, vPSIG[jj] );
			}
		}
		
	// GJL 11/14/02 v7.0434 USE_OC_WKS_TYPE_NOT_LT
	// Send output suvivorship function worksheet to Results Log
	wksSurvFuncTable.Type( TYPETARGET_OUTPUTLOG, NULL, 5 );
	
	}
	
	// *** Output Quartile Estimates ***	
	Worksheet wksQuartEstTable;
		
	// Create and attach to a temporary worksheet 
	strTablePath = GetFullPath( "saKMQuartEst.OGW", SA_OGW_SUBFOLDER, TRUE );
	if( !strTablePath.IsFile() )
	{
		Type_ErrorMsg1( SA_FILE_NOT_FOUND_ERROR_MSG, strTablePath );
		return SA_ABORT_NO_ERROR_MSG;
	}
	wksQuartEstTable.Open( strTablePath, CREATE_TEMP );
	strTableName = wksQuartEstTable.GetPage().GetName();
	
	// Rows and columns are indexed from 0
	
	// Output confidence level in row 3 and column 4
	iRow = 2;
	iCol = 3;
	strOut = LocalizeDouble( dConLevel*100, SA_PERCENT_FORMAT ) + KM_QUARTILE_HDR2;
	wksQuartEstTable.SetCell( iRow, iCol, strOut ); 

	// Loop on three percentiles (0.25, 0.50, and 0.75) starting in row 6
	iRow = 5;
	for( ii = 0; ii < 3; iRow++, ii++ )
	{
		// Output the percentile in column 3
		iCol = 2;
		if( ardPercentile[ii] >= 0 )
			wksQuartEstTable.SetCell( iRow, iCol, ardPercentile[ii] ); 

		// Output the percentile lower limit in column 4
		iCol++;
		if( ardPerLLim[ii] >= 0 )
			wksQuartEstTable.SetCell( iRow, iCol, ardPerLLim[ii] ); 
		
		// Output the percentile upper limit in column 5
		iCol++;
		if( ardPerULim[ii] >= 0 )
			wksQuartEstTable.SetCell( iRow, iCol, ardPerULim[ii] ); 
	}

	// GJL 11/14/02 v7.0434 USE_OC_WKS_TYPE_NOT_LT
	// Send output quartile estimates worksheet to Results Log
	wksQuartEstTable.Type( TYPETARGET_OUTPUTLOG, NULL, 6 );
	
	return SA_NO_ERROR;
}

/**
		Outputs the results of the Kaplan-Meier (Product-Limit) Estimator to a worksheet.
	Example:
		See the function saKaplanMeier above for sample call.
	Parameters:
		strSurvivalWKS=Name of output worksheet
		nPts=Number of data points in Time and Censor datasets
		vTIME=Vector of input Time values
		vCENSOR=Vector of input Censor values
		nd=Number of distinct failure times (nd==size of vTP)
		vP=Vector containing Survivorship Function values
		vPSIG=Vector containing Survivorship Function Error (standard deviation) values
		dConLevel=Confidence Level
		ardPercentile=Array containing 25th, 50th, and 75th Percentile values
		ardPerLLim=Array containing Lower Limits for Percentile values
		ardPerULim=Array containing Upper Limits for Percentile values
	Return:
		Outputs the results of the Kaplan-Meier Estimator (the Survivorship Function with Errors table and Upper
		and Lower Confidence Limits, and a Quartile Estimates table) to a worksheet. Returns SA_NO_ERROR on
		successful exit or an SA_ error code.
*/
int saKaplanMeier_Output_to_Worksheet( string strSurvivalWKS, int nPts, vector<double> &vTIME, vector<int> &vCENSOR,
	 int nd, vector<double> &vP, vector<double> &vPSIG, double dConLevel, double *ardPercentile, double *ardPerLLim,
	 double *ardPerULim )
{
	int ii, jj;
	double dZ, dPrevUncensoredTime;
	string strDatasetName;
	NagError neErr;
	NagError *pneFail = &neErr;		// NAG error structure

	// Output Time dataset	
	strDatasetName = strSurvivalWKS + SA_TIME_DATASET_NAME;
	Dataset dsTIME( strDatasetName );
	dsTIME.SetSize( nPts + 1 );
	
	// Output Survivorship Function dataset
	strDatasetName = strSurvivalWKS + SA_SURVIVAL_DATASET_NAME;
	Dataset dsSURVIVAL( strDatasetName );
	dsSURVIVAL.SetSize( nPts + 1 );

	// Output Survivorship Function Error (Standard Deviation) dataset
	strDatasetName = strSurvivalWKS + SA_STDERROR_DATASET_NAME;	
	Dataset dsERROR( strDatasetName );
	dsERROR.SetSize( nPts + 1 );

	// Output Survivorship Function Lower Confidence Limit dataset
	strDatasetName = strSurvivalWKS + KM_SURLLIM_DATASET_NAME;	
	Dataset dsSURLLIM( strDatasetName );
	dsSURLLIM.SetSize( nPts + 1 );
	
	// Output Survivorship Function Upper Confidence Limit dataset
	strDatasetName = strSurvivalWKS + KM_SURULIM_DATASET_NAME;
	Dataset dsSURULIM( strDatasetName );
	dsSURULIM.SetSize( nPts + 1 );
	
	// Output Percentile dataset
	strDatasetName = strSurvivalWKS + KM_PERCENT_DATASET_NAME;
	Dataset dsPERCENT( strDatasetName );
	dsPERCENT.SetSize( 3 );
	
	// Output Survivorship Function Percentile Estimate dataset
	strDatasetName = strSurvivalWKS + KM_ESTIMATE_DATASET_NAME;
	Dataset dsESTIMATE( strDatasetName );
	dsESTIMATE.SetSize( 3 );
	
	// Output Survivorship Function Percentile Estimate Lower Limit dataset
	strDatasetName = strSurvivalWKS + KM_PERLLIM_DATASET_NAME;
	Dataset dsPERLLIM( strDatasetName );
	dsPERLLIM.SetSize( 3 );

	// Output Survivorship Function Percentile Estimate Upper Limit dataset
	strDatasetName = strSurvivalWKS + KM_PERULIM_DATASET_NAME;	
	Dataset dsPERULIM( strDatasetName );
	dsPERULIM.SetSize( 3 );

	// *** Add time=0 to survivorship function, compute confidence limits ( upper and lower limits ) ***
	// *** for survivorship function, and copy all to output datasets in Survival worksheet          ***
	
	// From <NAG\OCN_g01.h>: g01fac nag_deviates_normal: Compute Normal Deviate for Survivorship Function Upper and
	//  Lower Limits
	dZ = nag_deviates_normal( Nag_LowerTail, ( dConLevel+1 )/2, pneFail );
	if( pneFail->code != NE_NOERROR )
		return SA_NORMAL_ERROR;

	dPrevUncensoredTime = -1;
	jj = nd;
	for( ii = nPts; ii > 0; ii-- )      // Loop backward through all input time values
	{
		if( vCENSOR[ii-1] == 1 ) 		// If time value ii-1 was censored and not a failure time...
		{
			dsTIME[ii] = vTIME[ii - 1]; // Output time value anyway 
			dsSURVIVAL[ii] = NANUM;     //  and NANUM for rest
			dsERROR[ii] = NANUM;
			dsSURLLIM[ii] = NANUM;
			dsSURULIM[ii] = NANUM;
		}
		else                            // Else time value not censored (is a failure time)... 
		{
			if( jj == nd || vTIME[ii - 1] != dPrevUncensoredTime ) // If new failure time get index of failure time in vTP (and index of associated  vectors vP and vPSIG)
				jj--;                                              //  vTP only contains distinct failure times (not censored or duplicate
			                                                       //  times) so skip decrementing index when times are censored or repeated 
			dPrevUncensoredTime = vTIME[ii - 1];                   // Set previous uncensored time 
			dsTIME[ii] = vTIME[ii - 1];                            // Output time
			dsSURVIVAL[ii] = vP[jj];                               // Output Survivorship Function value
			dsERROR[ii] = vPSIG[jj];                               // Output Survivorship Function Error (standard deviation) value
			dsSURLLIM[ii] = dsSURVIVAL[ii] - dZ * dsERROR[ii];     // Output Lower Limit of Survivorship Function value
			if( dsSURLLIM[ii] < 0 )                                // Lower Limit has min of 0
				dsSURLLIM[ii] = 0;
			dsSURULIM[ii] = dsSURVIVAL[ii] + dZ * dsERROR[ii];     // Output Upper Limit of Survivorship Function value
			if( dsSURULIM[ii] > 1 )                                // Upper Limit has max of 1
				dsSURULIM[ii] = 1;
		}
	}
	dsTIME[ii] = 0.0;                                              // Output values for time 0 (not computed...just known)
	dsSURVIVAL[ii] = 1.0;
	dsERROR[ii] = 0.0;
	dsSURLLIM[ii] = 1.0;
	dsSURULIM[ii] = 1.0;

	// *** Output Quartile Estimates and Confidence Limits to output datasets in Survival worksheet *** 
	for( jj = 0; jj < 3; jj++ )
	{
		dsPERCENT[jj] = ( jj + 1 ) * 0.25;        // Compute and output decimal percent

		if( ardPercentile[jj] < 0 )               // Output Percentile
			dsESTIMATE[jj] = NANUM;
		else
			dsESTIMATE[jj] = ardPercentile[jj];

		if( ardPerLLim[jj] < 0 )                  // Output Lower Limit of Percentile
			dsPERLLIM[jj] = NANUM;
		else
			dsPERLLIM[jj] = ardPerLLim[jj];

		if( ardPerULim[jj] < 0 )                  // Output Upper Limit of Percentile
			dsPERULIM = NANUM;
		else
			dsPERULIM[jj] = ardPerULim[jj];				
	}

	return SA_NO_ERROR;
}

////////////////////////////////////////////////////////////////////////////////////
/**
		Implements the Cox Proportional Hazards Model. The computational engine is

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -