/***********************************************************************

	This file is part of JavaNPST, a Java library of NonParametric
	Statistical Tests.

	Copyright (C) 2011
	
	J. Derrac (jderrac@decsai.ugr.es)
	S. Garcia (sglopez@ujaen.es)
	F. Herrera (herrera@decsai.ugr.es)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see http://www.gnu.org/licenses/
  
**********************************************************************/

package javanpst.tests.multiple.partialCorrelationTest;

import java.util.Arrays;

import javanpst.data.structures.dataTable.DataTable;
import javanpst.distributions.tests.PartialCorrelationDistribution;
import javanpst.tests.StatisticalTest;

/**
 * A test based on partial correlation.
 * 
 * This procedure can be applied to test independence of 2 variables, X and Y,
 * when the effect of a third variable, Z, is removed. 
 * 
 * The test statistic is computed by finding the number of concordant (C) and
 * discordant (Q) pairs existing in pairs X-Y, X-Z and Y-Z. Kendall's tau
 * coefficient is also provided.
 * 
 * Note: This test is not recommended for more than 20 samples
 * 
 * @author Joaqu�n Derrac Rus (jderrac@decsai.ugr.es)
 * @version 1.0
 */
public class PartialCorrelationTest extends StatisticalTest{

	/**
	 * Distribution of the test
	 */
	private PartialCorrelationDistribution distribution;
	
	/**
	 * Data to analyze
	 */
	private DataTable data;
	
	/**
	 * Populations samples
	 */
	private double samples [][];
	
	/**
	 * Array of ranks
	 */
	private double ranks [][];
	
	/**
	 * Concordant pairs
	 */
	private double TxyC,TxzC,TyzC;
	
	/**
	 * Discordant pairs
	 */
	private double TxyQ,TxzQ,TyzQ;
	
	/**
	 * T statistics
	 */
	private double Txy,Txz,Tyz;
	
	/**
	 * Kendall's tau coefficient
	 */
	private double tau;
	
	/**
	 * P-value of no association hypothesis
	 */
	private double pValue;
	
	/**
	 * Default builder
	 */
	public PartialCorrelationTest(){

		distribution=PartialCorrelationDistribution.getInstance();
		setReportFormat();
		clearData();
		
	}//end-method
	
	/**
	 * Clears the data stored in the test
	 */
	public void clearData(){
		
		data=new DataTable();
		
		performed=false;
		dataReady=false;
		
		Txy=0.0;
		Txz=0.0;
		Tyz=0.0;

		tau=0.0;
		
		samples=null;
		ranks=null;

		pValue=-1.0;
		
	}//end-method
	
	/**
	 * Builder. Load data to test.
	 * 
	 * @param newData data to test.
	 */
	public PartialCorrelationTest(DataTable newData){
		
		distribution=PartialCorrelationDistribution.getInstance();
		setReportFormat();
		
		data=DataTable.newInstance(newData);
		
		if(data.getColumns()<3){
			System.out.println("Partial correlation test only can be employed with three variables: X, Y and Z");
			clearData();
			return;
		}
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}
		
		samples=new double[data.getRows()][data.getColumns()];
		ranks=new double[data.getRows()][data.getColumns()];
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}
		
		samples=new double[data.getRows()][data.getColumns()];
		ranks=new double[data.getRows()][data.getColumns()];
		
		for(int i=0;iranks[0][j]){
					TxzQ+=1.0;
				}
				else{
					TxzC+=1.0;
				}
			}
		}
		
		//Tyz
		TyzC=0.0;
		TyzQ=0.0;

		for(int i=0;iranks[1][j]){
					TyzQ+=1.0;
				}
				else{
					TyzC+=1.0;
				}
			}
		}
		
		sortRanksY();
		
		//Txy
		TxyC=0.0;
		TxyQ=0.0;

		for(int i=0;iranks[0][j]){
					TxyQ+=1.0;
				}
				else{
					TxyC+=1.0;
				}
			}
		}
		
		//Compute T statistics
		int n=ranks[0].length;
		
		Txy=2*(TxyC-TxyQ)/(double)(n*(n-1));
		Txz=2*(TxzC-TxzQ)/(double)(n*(n-1));
		Tyz=2*(TyzC-TyzQ)/(double)(n*(n-1));
		
		//Compute Kendall's tau
		tau=Txy-(Txz*Tyz);
		
		tau/=Math.sqrt((1.0-(Txz*Txz))*(1.0-(Tyz*Tyz)));
		
		pValue=distribution.computeProbability(n,tau);
		
		performed=true;
		
	}//end-method
	
	/**
	 * Sort ranks according to Z order
	 */
	private void sortRanksZ(){
		
		for(int i=0;i 

Ads help maintain this website.