示例#1
0
TEST_F(LR_Separable_Test, test_separable) {

        LogisticRegression lr;
        int separableColumn = lr.dataIsSeparable(cov, response);
        ASSERT_EQ( separableColumn, 0);

}
示例#2
0
/**
 * Run a single likelihood ratio test.  Assumes covariates and phenotype are set as global variables.
 * The test is performed on the haps vector.
 *
 * @param haps Haplotype variable.  H_0 is that this vector is independant.
 * @param ones Set of ones.  Precomputed for speed.
 * @return ZaykingStatsInfo should contain all information for this likelihood ratio test.
 */
ZaykinStatsInfo Zaykin::runLikelihoodRatio(const vector<double> &haps, const vector<double> &ones){

	ZaykinStatsInfo stats;
	vector<vector<double> > testVecWithout = cov;
	vector<vector<double> > testVecWith; // wait to fill this one.
	testVecWithout.push_back(ones);

	LogisticRegression lrWith, lrWithout;
	vector<vector<double> > inv_infmatrixWithOut, inv_infmatrixWith;
	vector<double> betasWith, betasWithOut;

	inv_infmatrixWithOut = vecops::getDblVec(testVecWithout.size() , testVecWithout.size());

	int retry = 0;
	double startVal = 0;  // value to start betas with.

	while(retry < 3){
		try{
			betasWithOut = lrWithout.newtonRaphson(testVecWithout, phenotype, inv_infmatrixWithOut, startVal);
			break;
		}catch(NewtonRaphsonFailureEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model in single haplotype test: Newton-Raphson setup failure.");
		}catch(NewtonRaphsonIterationEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model in single haplotype test: max iterations hit.");
		}catch(SingularMatrixEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model in single haplotype test: information matrix was singular.");
		}catch(ConditionNumberEx){
			
			LogisticRegression lr;
			int separableVariable = lr.dataIsSeparable(testVecWithout, phenotype);
			
			string message;
			if (separableVariable < 0){
				// Error: poor conditioning.
				message = "Unable to compute reduced model in single haplotype test: Poor conditioning in information matrix.";
			}else{
				message = "Unable to compute reduced model in single haplotype test: Separable data matrix.";
			}
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;	
		}catch(alglib::ap_error err){
			stringstream ss;
			ss << "Unable to compute reduced model  single haplotype test due to linalg exception: " << err.msg;
			handleException(stats, startVal, retry, ss.str());
			if (retry >= 3) return stats;
		}
	}

	retry = 0;
	startVal = 0;  // value to start betas with.

	testVecWith = testVecWithout;
	testVecWith.push_back(haps);

	inv_infmatrixWith = vecops::getDblVec(testVecWith.size() , testVecWith.size());

	while(retry < 3){
		try{
			betasWith = lrWith.newtonRaphson(testVecWith, phenotype, inv_infmatrixWith, startVal);
			break;
		}catch(NewtonRaphsonFailureEx){
			handleException(stats, startVal, retry, "Unable to compute full model in single haplotype test: Newton-Raphson setup failure.");
		}catch(NewtonRaphsonIterationEx){
			handleException(stats, startVal, retry, "Unable to compute full model in single haplotype test: max iterations hit.");
		}catch(SingularMatrixEx){
			handleException(stats, startVal, retry, "Unable to compute full model in single haplotype test: information matrix was singular.");
		}catch(ConditionNumberEx){
			
			LogisticRegression lr;
			int separableVariable = lr.dataIsSeparable(testVecWith, phenotype);
			
			string message;
			if (separableVariable < 0){
				// Error: poor conditioning.
				message = "Unable to compute full model in single haplotype test: Poor conditioning in information matrix.";
			}else{
				message = "Unable to compute full model in single haplotype test: Separable data matrix.";
			}
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;	
		}catch(alglib::ap_error err){
			stringstream ss;
			ss << "Unable to compute full model single haplotype test due to linalg exception: " << err.msg;
			handleException(stats, startVal, retry, ss.str());
			if (retry >= 3) return stats;
		}
	}

	stats.chiSqStat = lrWith.likelihoodRatio(betasWithOut, testVecWithout, betasWith, testVecWith, phenotype);
	stats.degFree = betasWith.size() - betasWithOut.size();
	double beta = betasWith.at(betasWith.size() - 1);
	double stderr = sqrt(inv_infmatrixWith.at(inv_infmatrixWith.size() - 1).at(inv_infmatrixWith.size() - 1));
	stats.OR = exp(beta);
	stats.LCI = exp(beta - 1.96*stderr);
	stats.UCI = exp(beta + 1.96*stderr);
	return stats;

}
示例#3
0
/**
 * Run the global analysis.  Put all data and covariates in a logistic regression model.
 *
 * Compute likelihood ratio statistic.
 * This uses a likelihood statistic where n-1 haplotypes are tested.
 * 
 * @return pvalue.
 */
ZaykinGlobalStatsResults Zaykin::runGlobal(){

	ZaykinGlobalStatsResults stats;

	vector<vector<double> > haps;
	vector<double> ones;

	prepHaplotypes(ones, haps);

	#if DEBUG_ZAY_PROGRESS
		cout << "Zaykin start LR portion" << endl;
	#endif
	LogisticRegression with(params->getRegressionConditionNumberThreshold()), without(params->getRegressionConditionNumberThreshold());
	/*
	 * Run without the haplotypes:
	 */
	vector<vector<double> > inv_infmatrixWithOut;
	vector<double> betasWithOut;

	vector<vector<double> > inWithout;
	inWithout = cov;
	inWithout.push_back(ones);

	inv_infmatrixWithOut = vecops::getDblVec(inWithout.size() , inWithout.size());

	int retry = 0;
	double startVal = 0;  // value to start betas with.

	while(retry < 3){
		try{
			betasWithOut = without.newtonRaphson(inWithout, phenotype, inv_infmatrixWithOut, startVal);
			break;
		}catch(NewtonRaphsonFailureEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model: Newton-Raphson setup failure.");
		}catch(NewtonRaphsonIterationEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model: max iterations hit.");
		}catch(SingularMatrixEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model: information matrix was singular.");
		}catch(ConditionNumberEx err){
			
			LogisticRegression lr;
			int separableVariable = lr.dataIsSeparable(inWithout, phenotype);
			
			string message;
			if (separableVariable < 0){
				// Error: poor conditioning.
				stringstream ss;
				ss << "Unable to compute reduced model: Poor conditioning in information matrix. ";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
				message = ss.str();
			}else{
				stringstream ss;
				ss << "Unable to compute reduced model: Separable data matrix.";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
				message = ss.str();
			}
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;	
		}catch(ADTException e){
			// This one is generic.
			string message = "Unable to compute reduced model: Newton-Raphson error.";
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;
		}catch(alglib::ap_error err){
			stringstream ss;
			ss << "Unable to compute reduced model due to linalg exception: " << err.msg;
			handleException(stats, startVal, retry, ss.str());
			if (retry >= 3) return stats;
		}
	}

	/*
	 * Run with the haplotypes:
	 */
	vector<vector<double> > inv_infmatrixWith, inWith;
	vector<double> betasWith;

	inWith = inWithout;
	for (unsigned int i=0; i < haps.size()-1; i++){ // NOTE: Don't push the very last haplotype.
		inWith.push_back(haps.at(i));
	}

	inv_infmatrixWith = vecops::getDblVec(inWith.size() , inWith.size());

	retry = 0;
	startVal = 0;  // value to start betas with.

	while(retry < 3){
		try{
			betasWith = with.newtonRaphson(inWith, phenotype, inv_infmatrixWith, startVal);
			break;
		}catch(NewtonRaphsonFailureEx){
			handleException(stats, startVal, retry, "Unable to compute full model: Newton-Raphson setup failure.");
		}catch(NewtonRaphsonIterationEx){
			handleException(stats, startVal, retry, "Unable to compute full model: max iterations hit.");
		}catch(SingularMatrixEx){
			handleException(stats, startVal, retry, "Unable to compute full model: information matrix was singular.");
		}catch(ConditionNumberEx err){
			
			LogisticRegression lr;
			int separableVariable = lr.dataIsSeparable(inWith, phenotype);
			
			stringstream ss;
			if (separableVariable < 0){
				// Error: poor conditioning.
				ss << "Unable to compute reduced model: Poor conditioning in information matrix. ";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
			}else{
				ss << "Unable to compute reduced model: Separable data matrix.";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
			}
			string message = ss.str();
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;	
		}catch(ADTException e){
			string message = "Unable to compute full model: Newton-Raphson error.";
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;
		}catch(alglib::ap_error err){
			stringstream ss;
			ss << "Unable to compute full model due to linalg exception: " << err.msg;
			handleException(stats, startVal, retry,ss.str());
			if (retry >= 3) return stats;
		}
	}

	double likeRatio =  with.likelihoodRatio(betasWithOut, inWithout, betasWith, inWith, phenotype);
	try{
		stats.pvalue = Statistics::chi2prob(likeRatio, betasWith.size() - betasWithOut.size());
		stats.testStat = likeRatio;
		stats.degFreedom = betasWith.size() - betasWithOut.size();
	}catch(...){

		stringstream ss;
		ss << "Zaykin's method: unable to compute chi square: " << likeRatio << " " << betasWith.size() - betasWithOut.size() << endl;
		Logger::Instance()->writeLine(ss.str());

		stats.fillDefault();
		return stats;
	}
	return stats;
}