Search in sources :

Example 1 with Regression

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

the class ScatterPlot method getRegressionResult.

private RegressionResult getRegressionResult() {
    List<Node> regressors = new ArrayList<>();
    regressors.add(dataSet.getVariable(x));
    Node target = dataSet.getVariable(y);
    Regression regression = new RegressionDataset(dataSet);
    RegressionResult result = regression.regress(target, regressors);
    System.out.println(result);
    return result;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) Node(edu.cmu.tetrad.graph.Node) Regression(edu.cmu.tetrad.regression.Regression) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 2 with Regression

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

the class IndTestRegression 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.getName()));
    for (Node zVar : zList) {
        regressors.add(dataSet.getVariable(zVar.getName()));
    }
    Regression regression = new RegressionDataset(dataSet);
    RegressionResult result = null;
    try {
        result = regression.regress(xVar, regressors);
    } catch (Exception e) {
        return false;
    }
    double p = result.getP()[1];
    boolean independent = p > alpha;
    if (verbose) {
        if (independent) {
            TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(xVar, yVar, zList, p));
        } else {
            TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(xVar, yVar, zList, p));
        }
    }
    return independent;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 3 with Regression

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

the class TimeSeriesUtils method sumOfArCoefficients.

public static double sumOfArCoefficients(DataSet timeSeries, int numLags) {
    DataSet timeLags = createLagData(timeSeries, numLags);
    List<Node> regressors = new ArrayList<>();
    for (int i = timeSeries.getNumColumns(); i < timeLags.getNumColumns(); i++) {
        regressors.add(timeLags.getVariable(i));
    }
    Regression regression = new RegressionDataset(timeLags);
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    double sum = 0.0;
    int n = 0;
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        RegressionResult result = regression.regress(target, regressors);
        double[] coef = result.getCoef();
        for (int k = 0; k < coef.length; k++) {
            sum += coef[k] * coef[k];
            n++;
        }
        TetradVector residualsColumn = result.getResiduals();
        // residuals.viewColumn(i).assign(residualsColumn);
        residuals.assignColumn(i, residualsColumn);
    }
    return sum / n;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 4 with Regression

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

the class TimeSeriesUtils method ar2.

public static DataSet ar2(DataSet timeSeries, int numLags) {
    List<Node> missingVariables = new ArrayList<>();
    for (Node node : timeSeries.getVariables()) {
        int index = timeSeries.getVariables().indexOf(node);
        boolean missing = true;
        for (int i = 0; i < timeSeries.getNumRows(); i++) {
            if (!Double.isNaN(timeSeries.getDouble(i, index))) {
                missing = false;
                break;
            }
        }
        if (missing) {
            missingVariables.add(node);
        }
    }
    DataSet timeLags = createLagData(timeSeries, numLags);
    Regression regression = new RegressionDataset(timeLags);
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        int index = timeSeries.getVariables().indexOf(target);
        if (missingVariables.contains(target)) {
            for (int i2 = 0; i2 < residuals.rows(); i2++) {
                residuals.set(i2, index, Double.NaN);
            }
            continue;
        }
        List<Node> regressors = new ArrayList<>();
        for (int i2 = timeSeries.getNumColumns(); i2 < timeLags.getNumColumns(); i2++) {
            int varIndex = i2 % timeSeries.getNumColumns();
            Node var = timeSeries.getVariable(varIndex);
            if (missingVariables.contains(var)) {
                continue;
            }
            regressors.add(timeLags.getVariable(i2));
        }
        RegressionResult result = regression.regress(target, regressors);
        TetradVector residualsColumn = result.getResiduals();
        residuals.assignColumn(i, residualsColumn);
    }
    return ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals);
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 5 with Regression

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

the class TestRegression method testCovariance.

/**
 * Same problem, using the covariance matrix.
 */
@Test
public void testCovariance() {
    setUp();
    RandomUtil.getInstance().setSeed(3848283L);
    ICovarianceMatrix cov = new CovarianceMatrix(data);
    List<Node> nodes = cov.getVariables();
    Node target = nodes.get(0);
    List<Node> regressors = new ArrayList<>();
    for (int i = 1; i < nodes.size(); i++) {
        regressors.add(nodes.get(i));
    }
    Regression regression = new RegressionCovariance(cov);
    RegressionResult result = regression.regress(target, regressors);
    double[] coeffs = result.getCoef();
    assertEquals(0.00, coeffs[0], 0.01);
    assertEquals(-.053, coeffs[1], 0.01);
    assertEquals(0.036, coeffs[2], 0.01);
    assertEquals(.019, coeffs[3], 0.01);
    assertEquals(.007, coeffs[4], 0.01);
}
Also used : ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) RegressionCovariance(edu.cmu.tetrad.regression.RegressionCovariance) RegressionResult(edu.cmu.tetrad.regression.RegressionResult) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Test(org.junit.Test)

Aggregations

Regression (edu.cmu.tetrad.regression.Regression)19 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)17 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)16 ArrayList (java.util.ArrayList)10 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)7 TetradVector (edu.cmu.tetrad.util.TetradVector)7 RegressionCovariance (edu.cmu.tetrad.regression.RegressionCovariance)4 DoubleArrayList (cern.colt.list.DoubleArrayList)3 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)3 Node (edu.cmu.tetrad.graph.Node)3 CombinationGenerator (edu.cmu.tetrad.util.CombinationGenerator)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 OLSMultipleLinearRegression (org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression)2 Test (org.junit.Test)2 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)1 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1