Search in sources :

Example 11 with RegressionDataset

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

the class Lofs2 method getRegressions.

// ==========================PRIVATE=======================================//
private List<Regression> getRegressions() {
    if (this.regressions == null) {
        List<Regression> regressions = new ArrayList<>();
        this.variables = dataSets.get(0).getVariables();
        for (DataSet dataSet : dataSets) {
            regressions.add(new RegressionDataset(dataSet));
        }
        this.regressions = regressions;
    }
    return this.regressions;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) OLSMultipleLinearRegression(org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression) Regression(edu.cmu.tetrad.regression.Regression)

Example 12 with RegressionDataset

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

the class Lofs2 method regressionCoef.

private double regressionCoef(double[] xValues, double[] yValues) {
    List<Node> v = new ArrayList<>();
    v.add(new GraphNode("x"));
    v.add(new GraphNode("y"));
    TetradMatrix bothData = new TetradMatrix(xValues.length, 2);
    for (int i = 0; i < xValues.length; i++) {
        bothData.set(i, 0, xValues[i]);
        bothData.set(i, 1, yValues[i]);
    }
    Regression regression2 = new RegressionDataset(bothData, v);
    RegressionResult result;
    try {
        result = regression2.regress(v.get(0), v.get(1));
    } catch (Exception e) {
        return Double.NaN;
    }
    return result.getCoef()[1];
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) OLSMultipleLinearRegression(org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression) Regression(edu.cmu.tetrad.regression.Regression) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 13 with RegressionDataset

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

the class TimeSeriesUtils method getSelfLoopCoefs.

public static double[] getSelfLoopCoefs(DataSet timeSeries) {
    DataSet timeLags = createLagData(timeSeries, 1);
    double[] coefs = new double[timeSeries.getNumColumns()];
    for (int j = 0; j < timeSeries.getNumColumns(); j++) {
        Node target = timeLags.getVariable(j);
        Node selfLoop = timeLags.getVariable(j + timeSeries.getNumColumns());
        List<Node> regressors = Collections.singletonList(selfLoop);
        Regression regression = new RegressionDataset(timeLags);
        RegressionResult result = regression.regress(target, regressors);
        coefs[j] = result.getCoef()[1];
    }
    return coefs;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) Regression(edu.cmu.tetrad.regression.Regression) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 14 with RegressionDataset

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

the class TimeSeriesUtils method ar.

/**
 * @return the VAR residuals of the given time series with the given number
 * of lags. That is, every variable at the model lag is regressed onto every
 * variable at previous lags, up to the given number of lags, and the
 * residuals of these regressions for each variable are returned.
 */
public static DataSet ar(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);
    // Regression regression = new RegressionDatasetGeneralized(timeLags);
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        RegressionResult result = regression.regress(target, regressors);
        TetradVector residualsColumn = result.getResiduals();
        // residuals.viewColumn(i).assign(residualsColumn);
        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 15 with RegressionDataset

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

the class TimeSeriesUtils method structuralVar.

public static VarResult structuralVar(DataSet timeSeries, int numLags) {
    DataSet timeLags = TimeSeriesUtils.createLagData(timeSeries, numLags);
    IKnowledge knowledge = timeLags.getKnowledge().copy();
    for (int i = 0; i <= numLags; i++) {
        knowledge.setTierForbiddenWithin(i, true);
    }
    Score score;
    if (timeLags.isDiscrete()) {
        score = new BDeuScore(timeLags);
    } else if (timeLags.isContinuous()) {
        SemBicScore semBicScore = new SemBicScore(new CovarianceMatrixOnTheFly(timeLags));
        semBicScore.setPenaltyDiscount(2.0);
        score = semBicScore;
    } else {
        throw new IllegalArgumentException("Mixed data set");
    }
    Fges search = new Fges(score);
    search.setKnowledge(knowledge);
    Graph graph = search.search();
    // want to collapse graph here...
    Graph collapsedVarGraph = new EdgeListGraph(timeSeries.getVariables());
    for (Edge edge : graph.getEdges()) {
        String node1_before = edge.getNode1().getName();
        String node2_before = edge.getNode2().getName();
        String node1_after = node1_before.substring(0, node1_before.indexOf("."));
        String node2_after = node2_before.substring(0, node2_before.indexOf("."));
        Node node1 = collapsedVarGraph.getNode(node1_after);
        Node node2 = collapsedVarGraph.getNode(node2_after);
        Edge _edge = new Edge(node1, node2, edge.getEndpoint1(), edge.getEndpoint2());
        if (!collapsedVarGraph.containsEdge(_edge)) {
            collapsedVarGraph.addEdge(_edge);
        }
    }
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    Regression regression = new RegressionDataset(timeLags);
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        List<Node> regressors = new ArrayList<>();
        // timelags.getVariable(i).
        for (int j = 0; j <= 0; /*numLags*/
        j++) {
            Node variable = timeLags.getVariable(i + j * timeSeries.getNumColumns());
            regressors.addAll(graph.getParents(variable));
        }
        RegressionResult result = regression.regress(target, regressors);
        TetradVector residualsColumn = result.getResiduals();
        // residuals.viewColumn(i).assign(residualsColumn);
        residuals.assignColumn(i, residualsColumn);
    }
    return new VarResult(ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals), collapsedVarGraph);
}
Also used : Regression(edu.cmu.tetrad.regression.Regression) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Aggregations

RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)19 Regression (edu.cmu.tetrad.regression.Regression)16 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)16 ArrayList (java.util.ArrayList)10 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)7 TetradVector (edu.cmu.tetrad.util.TetradVector)7 Node (edu.cmu.tetrad.graph.Node)4 DoubleArrayList (cern.colt.list.DoubleArrayList)3 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)3 CombinationGenerator (edu.cmu.tetrad.util.CombinationGenerator)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 OLSMultipleLinearRegression (org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression)2 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)1 GeneralAndersonDarlingTest (edu.cmu.tetrad.data.GeneralAndersonDarlingTest)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 RegressionCovariance (edu.cmu.tetrad.regression.RegressionCovariance)1 Parameters (edu.cmu.tetrad.util.Parameters)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Vector (java.util.Vector)1 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)1