Search in sources :

Example 11 with CovarianceMatrix

use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.

the class CovMatrixAverageWrapper method calcAverage.

private void calcAverage(List<DataWrapper> wrappers) {
    List<TetradMatrix> cov = new ArrayList<>();
    for (int i = 0; i < wrappers.size(); i++) {
        DataModel selectedDataModel = wrappers.get(i).getSelectedDataModel();
        if (!(selectedDataModel instanceof ICovarianceMatrix)) {
            throw new IllegalArgumentException("Sorry, this is an average only over covariance matrices.");
        }
        cov.add(((ICovarianceMatrix) selectedDataModel).getMatrix());
    }
    TetradMatrix cov3 = new TetradMatrix(cov.get(0).rows(), cov.get(0).rows());
    for (int i = 0; i < cov.get(0).rows(); i++) {
        for (int j = 0; j < cov.get(0).rows(); j++) {
            double c = 0.0;
            for (int k = 0; k < cov.size(); k++) {
                c += cov.get(k).get(i, j);
            }
            c /= cov.size();
            cov3.set(i, j, c);
            cov3.set(j, i, c);
        }
    }
    DataModel m = wrappers.get(0).getSelectedDataModel();
    ICovarianceMatrix _cov = (ICovarianceMatrix) m;
    List<Node> nodes = _cov.getVariables();
    int n = _cov.getSampleSize();
    ICovarianceMatrix covWrapper = new CovarianceMatrix(nodes, cov3, n);
    setDataModel(covWrapper);
}
Also used : DataModel(edu.cmu.tetrad.data.DataModel) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix)

Example 12 with CovarianceMatrix

use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.

the class TestDeltaTetradTest method test3.

@Test
public void test3() {
    SemPm pm = makePm();
    DataSet data = new SemIm(pm).simulateData(1000, false);
    CovarianceMatrix cov = new CovarianceMatrix(data);
    List<Node> variables = data.getVariables();
    Node x1 = variables.get(0);
    Node x2 = variables.get(1);
    Node x3 = variables.get(2);
    Node x4 = variables.get(3);
    Node x5 = variables.get(4);
    Tetrad t1234 = new Tetrad(x1, x2, x3, x4);
    Tetrad t1342 = new Tetrad(x1, x3, x4, x2);
    Tetrad t1423 = new Tetrad(x1, x4, x2, x3);
    DeltaTetradTest test1 = new DeltaTetradTest(data);
    // DeltaTetradTest test = new DeltaTetradTest(new CorrelationMatrix(cov));
    double chiSq1 = test1.calcChiSquare(t1234, t1342);
    DeltaTetradTest test2 = new DeltaTetradTest(cov);
    // DeltaTetradTest test = new DeltaTetradTest(new CorrelationMatrix(cov));
    double chiSq2 = test2.calcChiSquare(t1234, t1342);
}
Also used : DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) DataSet(edu.cmu.tetrad.data.DataSet) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Tetrad(edu.cmu.tetrad.search.Tetrad) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) Test(org.junit.Test)

Example 13 with CovarianceMatrix

use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.

the class TestDeltaTetradTest method getBollenExample2Data.

private CovarianceMatrix getBollenExample2Data() {
    // Union sentiment.
    double[][] d = new double[][] { { 14.610 }, { -5.250, 11.017 }, { -8.057, 11.087, 31.971 }, { -0.482, 0.677, 1.559, 1.021 }, { -18.857, 17.861, 28.250, 7.139, 215.662 } };
    Node y1 = new ContinuousVariable("y1");
    Node y2 = new ContinuousVariable("y2");
    Node y3 = new ContinuousVariable("y3");
    Node x1 = new ContinuousVariable("x1");
    Node x2 = new ContinuousVariable("x2");
    List<Node> nodes = new ArrayList<>();
    nodes.add(y1);
    nodes.add(y2);
    nodes.add(y3);
    nodes.add(x1);
    nodes.add(x2);
    TetradMatrix matrix = new TetradMatrix(5, 5);
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i; j++) {
            matrix.set(i, j, d[i][j]);
            matrix.set(j, i, d[i][j]);
        }
    }
    return new CovarianceMatrix(nodes, matrix, 173);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix)

Example 14 with CovarianceMatrix

use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.

the class TestDeltaTetradTest method getBollenExample1Data.

private CovarianceMatrix getBollenExample1Data() {
    // Sympathy and anger, p. 164.
    double[][] d = new double[][] { { 6.982 }, { 4.686, 6.047 }, { 4.335, 3.307, 5.037 }, { -2.294, -1.453, -1.979, 5.569 }, { -2.209, -1.262, -1.738, 3.931, 5.328 }, { -1.671, -1.401, -1.564, 3.915, 3.601, 4.977 } };
    Node v1 = new ContinuousVariable("v1");
    Node v2 = new ContinuousVariable("v2");
    Node v3 = new ContinuousVariable("v3");
    Node v4 = new ContinuousVariable("v4");
    Node v5 = new ContinuousVariable("v5");
    Node v6 = new ContinuousVariable("v6");
    List<Node> nodes = new ArrayList<>();
    nodes.add(v1);
    nodes.add(v2);
    nodes.add(v3);
    nodes.add(v4);
    nodes.add(v5);
    nodes.add(v6);
    TetradMatrix matrix = new TetradMatrix(6, 6);
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j <= i; j++) {
            matrix.set(i, j, d[i][j]);
            matrix.set(j, i, d[i][j]);
        }
    }
    return new CovarianceMatrix(nodes, matrix, 138);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix)

Example 15 with CovarianceMatrix

use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.

the class TestDeltaTetradTest method testBollenSimulationExample.

// Bollen 2000 A Tetrad Test for Causal Indicators, p. 13.
@Test
public void testBollenSimulationExample() {
    CovarianceMatrix cov = getBollenSimulationExampleData();
    List<Node> variables = cov.getVariables();
    Node y1 = variables.get(0);
    Node y2 = variables.get(1);
    Node y3 = variables.get(2);
    Node y4 = variables.get(3);
    Node y5 = variables.get(4);
    Tetrad t1 = new Tetrad(y1, y2, y3, y4);
    Tetrad t2 = new Tetrad(y1, y2, y4, y3);
    Tetrad t3 = new Tetrad(y1, y3, y4, y2);
    Tetrad t4 = new Tetrad(y1, y2, y3, y5);
    Tetrad t5 = new Tetrad(y1, y2, y5, y3);
    Tetrad t6 = new Tetrad(y1, y3, y5, y2);
    Tetrad t7 = new Tetrad(y1, y2, y4, y5);
    Tetrad t8 = new Tetrad(y1, y2, y5, y4);
    Tetrad t9 = new Tetrad(y1, y4, y5, y2);
    Tetrad t10 = new Tetrad(y1, y3, y4, y5);
    Tetrad t11 = new Tetrad(y1, y3, y5, y4);
    Tetrad t12 = new Tetrad(y1, y4, y5, y3);
    Tetrad t13 = new Tetrad(y2, y3, y4, y5);
    Tetrad t14 = new Tetrad(y2, y3, y5, y4);
    Tetrad t15 = new Tetrad(y2, y4, y5, y3);
    Tetrad[] tetrads = new Tetrad[] { t1, t2, t3, t4 };
    DeltaTetradTest test = new DeltaTetradTest(cov);
    double chiSq = test.calcChiSquare(tetrads[0]);
    double pValue = test.getPValue();
    assertEquals(58.1, chiSq, 0.1);
    assertEquals(2.46E-14, pValue, .1E-14);
    Tetrad[] independentTetrads = new Tetrad[] { t1, t2, t4, t6, t10 };
    chiSq = test.calcChiSquare(independentTetrads[0]);
    pValue = test.getPValue();
    assertEquals(58.1, chiSq, 0.1);
    assertEquals(2.46E-14, pValue, 0.1E-14);
    {
        chiSq = test.calcChiSquare(independentTetrads);
        pValue = test.getPValue();
        assertEquals(89.34, chiSq, 0.01);
        assertEquals(0.0, pValue, 0.01);
    }
// They get chsq = 64.13 and so do I.
}
Also used : DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) Tetrad(edu.cmu.tetrad.search.Tetrad) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) Test(org.junit.Test)

Aggregations

CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)20 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)11 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)10 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 DataSet (edu.cmu.tetrad.data.DataSet)6 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)5 DeltaTetradTest (edu.cmu.tetrad.search.DeltaTetradTest)4 Tetrad (edu.cmu.tetrad.search.Tetrad)4 SemPm (edu.cmu.tetrad.sem.SemPm)4 SemIm (edu.cmu.tetrad.sem.SemIm)3 DataModel (edu.cmu.tetrad.data.DataModel)2 Node (edu.cmu.tetrad.graph.Node)2 List (java.util.List)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 Graph (edu.cmu.tetrad.graph.Graph)1 SemGraph (edu.cmu.tetrad.graph.SemGraph)1 Regression (edu.cmu.tetrad.regression.Regression)1