Search in sources :

Example 31 with RegressionResult

use of edu.cmu.tetrad.regression.RegressionResult in project tetrad by cmu-phil.

the class IndTestMultinomialLogisticRegression method isIndependentRegression.

private boolean isIndependentRegression(Node x, Node y, List<Node> z) {
    if (!variablesPerNode.containsKey(x)) {
        throw new IllegalArgumentException("Unrecogized node: " + x);
    }
    if (!variablesPerNode.containsKey(y)) {
        throw new IllegalArgumentException("Unrecogized node: " + y);
    }
    for (Node node : z) {
        if (!variablesPerNode.containsKey(x)) {
            throw new IllegalArgumentException("Unrecogized node: " + node);
        }
    }
    List<Node> regressors = new ArrayList<>();
    regressors.add(internalData.getVariable(y.getName()));
    for (Node _z : z) {
        regressors.addAll(variablesPerNode.get(_z));
    }
    int[] _rows = getNonMissingRows(x, y, z);
    regression.setRows(_rows);
    RegressionResult result;
    try {
        result = regression.regress(x, regressors);
    } catch (Exception e) {
        return false;
    }
    double p = result.getP()[1];
    this.lastP = p;
    boolean indep = p > alpha;
    if (verbose) {
        if (indep) {
            TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(x, y, z, p));
        } else {
            TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(x, y, z, p));
        }
    }
    return indep;
}
Also used : Node(edu.cmu.tetrad.graph.Node) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 32 with RegressionResult

use of edu.cmu.tetrad.regression.RegressionResult in project tetrad by cmu-phil.

the class IndTestRegressionAD method isIndependent.

/**
 * Determines whether variable x is independent of variable y given a list of conditioning variables z.
 *
 * @param xVar  the one variable being compared.
 * @param yVar  the second variable being compared.
 * @param zList the list of conditioning variables.
 * @return true iff x _||_ y | z.
 * @throws RuntimeException if a matrix singularity is encountered.
 */
public boolean isIndependent(Node xVar, Node yVar, List<Node> zList) {
    if (zList == null) {
        throw new NullPointerException();
    }
    for (Node node : zList) {
        if (node == null) {
            throw new NullPointerException();
        }
    }
    TetradVector v1, v2;
    try {
        List<Node> regressors = new ArrayList<>();
        regressors.add(dataSet.getVariable(yVar.getName()));
        for (Node zVar : zList) {
            regressors.add(dataSet.getVariable(zVar.getName()));
        }
        RegressionDataset regression = new RegressionDataset(dataSet);
        RegressionResult result = regression.regress(xVar, regressors);
        v1 = result.getResiduals();
        v2 = regression.getResidualsWithoutFirstRegressor();
    // regressors.remove(dataSet.getVariable(yVar.getNode()));
    // regression = new RegressionDataset(dataSet);
    // result = regression.regress(xVar, regressors);
    // v2 = result.getResiduals();
    } catch (Exception e) {
        throw e;
    }
    List<Double> d1 = new ArrayList<>();
    for (int i = 0; i < v1.size(); i++) d1.add(v1.get(i));
    List<Double> d2 = new ArrayList<>();
    double[] f2 = new double[v2.size()];
    for (int i = 0; i < v2.size(); i++) {
        d2.add(v2.get(i));
        f2[i] = v2.get(i);
    }
    double sd = StatUtils.sd(f2);
    // RealDistribution c2 = new EmpiricalCdf(d2);
    RealDistribution c2 = new NormalDistribution(0, sd);
    GeneralAndersonDarlingTest test = new GeneralAndersonDarlingTest(d1, c2);
    double aSquaredStar = test.getASquaredStar();
    System.out.println("A squared star = " + aSquaredStar + " p = " + test.getP());
    double p = test.getP();
    double aa2 = 1 - tanh(aSquaredStar);
    boolean independent = p > alpha;
    this.pvalue = aa2;
    if (verbose) {
        if (independent) {
            TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(xVar, yVar, zList, 0.));
        } else {
            TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(xVar, yVar, zList, 0.));
        }
    }
    return independent;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) RealDistribution(org.apache.commons.math3.distribution.RealDistribution) RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) GeneralAndersonDarlingTest(edu.cmu.tetrad.data.GeneralAndersonDarlingTest) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 33 with RegressionResult

use of edu.cmu.tetrad.regression.RegressionResult in project tetrad by cmu-phil.

the class IndTestLaggedRegression method isIndependent.

/**
 * Determines whether variable x is independent of variable y given a list of conditioning variables z.
 *
 * @param xVar  the one variable being compared.
 * @param yVar  the second variable being compared.
 * @param zList the list of conditioning variables.
 * @return true iff x _||_ y | z.
 * @throws RuntimeException if a matrix singularity is encountered.
 */
public boolean isIndependent(Node xVar, Node yVar, List<Node> zList) {
    if (zList == null) {
        throw new NullPointerException();
    }
    for (Node node : zList) {
        if (node == null) {
            throw new NullPointerException();
        }
    }
    List<Node> regressors = new ArrayList<>();
    // regressors.add(dataSet.getVariable(yVar.getNode()));
    // 
    // for (Node zVar : zList) {
    // regressors.add(dataSet.getVariable(zVar.getNode()));
    // }
    // TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    xVar = timeLags.getVariable(xVar.getName());
    regressors.add(timeLags.getVariable(yVar.getName()));
    int yIndex = timeLags.getColumn(timeLags.getVariable(yVar.getName()));
    for (int i2 = yIndex + variables.size(); i2 < timeLags.getNumColumns(); i2 += variables.size()) {
        regressors.add(timeLags.getVariable(i2));
    }
    int xIndex = timeLags.getColumn(timeLags.getVariable(xVar.getName()));
    for (int i2 = xIndex + variables.size(); i2 < timeLags.getNumColumns(); i2 += variables.size()) {
        regressors.add(timeLags.getVariable(i2));
    }
    for (Node z : zList) {
        int zIndex = timeLags.getColumn(timeLags.getVariable(z.getName()));
        for (int i2 = zIndex; i2 < timeLags.getNumColumns(); i2 += variables.size()) {
            final List<Node> variables = timeLags.getVariables();
            regressors.add(variables.get(i2));
        }
    }
    System.out.println(xVar + " conditioning on " + regressors);
    // Regression regression = new RegressionDataset(dataSet);
    RegressionResult result = null;
    try {
        result = regression.regress(xVar, regressors);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    double p = result.getP()[1];
    System.out.println("p = " + p);
    boolean independent = p > alpha;
    if (verbose) {
        if (independent) {
            System.out.println(SearchLogUtils.independenceFactMsg(xVar, yVar, zList, p));
        }
    }
    return independent;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Aggregations

RegressionResult (edu.cmu.tetrad.regression.RegressionResult)33 Regression (edu.cmu.tetrad.regression.Regression)17 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)16 ArrayList (java.util.ArrayList)15 DoubleArrayList (cern.colt.list.DoubleArrayList)11 TetradVector (edu.cmu.tetrad.util.TetradVector)9 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)8 Node (edu.cmu.tetrad.graph.Node)8 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)8 RegressionCovariance (edu.cmu.tetrad.regression.RegressionCovariance)3 CombinationGenerator (edu.cmu.tetrad.util.CombinationGenerator)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Test (org.junit.Test)2 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)1 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)1 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)1 GeneralAndersonDarlingTest (edu.cmu.tetrad.data.GeneralAndersonDarlingTest)1 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 Graph (edu.cmu.tetrad.graph.Graph)1