Search in sources :

Example 6 with RegressionResult

use of edu.cmu.tetrad.regression.RegressionResult 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 7 with RegressionResult

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

the class IndTestMultinomialLogisticRegressionWald 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(node)) {
            throw new IllegalArgumentException("Unrecogized node: " + node);
        }
    }
    List<Node> regressors = new ArrayList<>();
    if (y instanceof ContinuousVariable) {
        regressors.add(internalData.getVariable(y.getName()));
    } else {
        regressors.addAll(variablesPerNode.get(y));
    }
    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 = 1;
    if (y instanceof ContinuousVariable) {
        p = result.getP()[1];
    } else {
        for (int i = 0; i < variablesPerNode.get(y).size(); i++) {
            double val = result.getP()[1 + i];
            if (val < p)
                p = val;
        }
    }
    this.lastP = p;
    boolean indep = p > alpha;
    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 : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Node(edu.cmu.tetrad.graph.Node) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 8 with RegressionResult

use of edu.cmu.tetrad.regression.RegressionResult 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)

Example 9 with RegressionResult

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

the class LingamPattern2 method getScore1.

// =============================PRIVATE METHODS=========================//
private Score getScore1(Graph dag, List<TetradMatrix> data, List<Node> variables) {
    // System.out.println("Scoring DAG: " + dag);
    List<Regression> regressions = new ArrayList<>();
    for (TetradMatrix _data : data) {
        regressions.add(new RegressionDataset(_data, variables));
    }
    int totalSampleSize = 0;
    for (TetradMatrix _data : data) {
        totalSampleSize += _data.rows();
    }
    int numCols = data.get(0).columns();
    List<Node> nodes = dag.getNodes();
    double score = 0.0;
    double[] pValues = new double[nodes.size()];
    TetradMatrix absoluteStandardizedResiduals = new TetradMatrix(totalSampleSize, numCols);
    for (int i = 0; i < nodes.size(); i++) {
        List<Double> _absoluteStandardizedResiduals = new ArrayList<>();
        for (int j = 0; j < data.size(); j++) {
            Node _target = nodes.get(i);
            List<Node> _regressors = dag.getParents(_target);
            Node target = getVariable(variables, _target.getName());
            List<Node> regressors = new ArrayList<>();
            for (Node _regressor : _regressors) {
                Node variable = getVariable(variables, _regressor.getName());
                regressors.add(variable);
            }
            RegressionResult result = regressions.get(j).regress(target, regressors);
            TetradVector residualsColumn = result.getResiduals();
            DoubleArrayList _absoluteStandardizedResidualsColumn = new DoubleArrayList(residualsColumn.toArray());
            double mean = Descriptive.mean(_absoluteStandardizedResidualsColumn);
            double std = Descriptive.standardDeviation(Descriptive.variance(_absoluteStandardizedResidualsColumn.size(), Descriptive.sum(_absoluteStandardizedResidualsColumn), Descriptive.sumOfSquares(_absoluteStandardizedResidualsColumn)));
            for (int i2 = 0; i2 < _absoluteStandardizedResidualsColumn.size(); i2++) {
                _absoluteStandardizedResidualsColumn.set(i2, (_absoluteStandardizedResidualsColumn.get(i2) - mean) / std);
                _absoluteStandardizedResidualsColumn.set(i2, Math.abs(_absoluteStandardizedResidualsColumn.get(i2)));
            }
            for (int k = 0; k < _absoluteStandardizedResidualsColumn.size(); k++) {
                _absoluteStandardizedResiduals.add(_absoluteStandardizedResidualsColumn.get(k));
            }
        }
        DoubleArrayList absoluteStandardResidualsList = new DoubleArrayList(absoluteStandardizedResiduals.getColumn(i).toArray());
        for (int k = 0; k < _absoluteStandardizedResiduals.size(); k++) {
            absoluteStandardizedResiduals.set(k, i, _absoluteStandardizedResiduals.get(k));
        }
        double _mean = Descriptive.mean(absoluteStandardResidualsList);
        double diff = _mean - Math.sqrt(2.0 / Math.PI);
        score += diff * diff;
    }
    for (int j = 0; j < absoluteStandardizedResiduals.columns(); j++) {
        double[] x = absoluteStandardizedResiduals.getColumn(j).toArray();
        double p = new AndersonDarlingTest(x).getP();
        pValues[j] = p;
    }
    return new Score(score, pValues);
}
Also used : Regression(edu.cmu.tetrad.regression.Regression) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) DoubleArrayList(cern.colt.list.DoubleArrayList) RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) AndersonDarlingTest(edu.cmu.tetrad.data.AndersonDarlingTest) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 10 with RegressionResult

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

the class RegressionEditor method runRegression.

// ========================= Private Methods ========================//
/**
 * Runs the regression, resetting the text output and graph output.
 */
private void runRegression() {
    runner.execute();
    Graph graph = runner.getOutGraph();
    GraphUtils.circleLayout(graph, 200, 200, 150);
    GraphUtils.fruchtermanReingoldLayout(graph);
    workbench.setGraph(graph);
    RegressionResult report = runner.getResult();
    reportText.setText(report.toString());
    textWithTable.removeAll();
    textWithTable.setLayout(new BorderLayout());
    textWithTable.add(TextWithTable.component(report.getPreamble(), report.getResultsTable()));
    textWithTable.revalidate();
    textWithTable.repaint();
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) 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