Search in sources :

Example 6 with SemEstimator

use of edu.cmu.tetrad.sem.SemEstimator in project tetrad by cmu-phil.

the class TestSemVarMeans method testMeansRecursive.

@Test
public void testMeansRecursive() {
    Graph graph = constructGraph1();
    SemPm semPm1 = new SemPm(graph);
    List<Parameter> parameters = semPm1.getParameters();
    for (Parameter p : parameters) {
        p.setInitializedRandomly(false);
    }
    SemIm semIm1 = new SemIm(semPm1);
    double[] means = { 5.0, 4.0, 3.0, 2.0, 1.0 };
    RandomUtil.getInstance().setSeed(-379467L);
    for (int i = 0; i < semIm1.getVariableNodes().size(); i++) {
        Node node = semIm1.getVariableNodes().get(i);
        semIm1.setMean(node, means[i]);
    }
    DataSet dataSet = semIm1.simulateDataRecursive(1000, false);
    SemEstimator semEst = new SemEstimator(dataSet, semPm1);
    semEst.estimate();
    SemIm estSemIm = semEst.getEstimatedSem();
    List<Node> nodes = semPm1.getVariableNodes();
    for (Node node : nodes) {
        double mean = semIm1.getMean(node);
        assertEquals(mean, estSemIm.getMean(node), 0.5);
    }
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) DataSet(edu.cmu.tetrad.data.DataSet) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) SemPm(edu.cmu.tetrad.sem.SemPm) Parameter(edu.cmu.tetrad.sem.Parameter) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Example 7 with SemEstimator

use of edu.cmu.tetrad.sem.SemEstimator in project tetrad by cmu-phil.

the class TestStandardizedSem method rtest8.

public void rtest8() {
    // RandomUtil.getInstance().setSeed(2958442283L);
    SemGraph graph = new SemGraph();
    Node x = new ContinuousVariable("X");
    Node y = new ContinuousVariable("Y");
    Node z = new ContinuousVariable("Z");
    graph.addNode(x);
    graph.addNode(y);
    graph.addNode(z);
    graph.addDirectedEdge(x, y);
    graph.addBidirectedEdge(x, y);
    graph.addDirectedEdge(x, z);
    graph.addDirectedEdge(y, z);
    graph.setShowErrorTerms(true);
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    StandardizedSemIm sem = new StandardizedSemIm(semIm, StandardizedSemIm.Initialization.CALCULATE_FROM_SEM);
    DataSet data = semIm.simulateDataCholesky(1000, false);
    data = ColtDataSet.makeContinuousData(data.getVariables(), DataUtils.standardizeData(data.getDoubleData()));
    SemEstimator estimator = new SemEstimator(data, semPm);
    semIm = estimator.estimate();
    DataSet data2 = semIm.simulateDataReducedForm(1000, false);
    DataSet data3 = sem.simulateDataReducedForm(1000, false);
    StandardizedSemIm.ParameterRange range2 = sem.getCovarianceRange(x, y);
    double high = range2.getHigh();
    double low = range2.getLow();
    if (high == Double.POSITIVE_INFINITY)
        high = 1000;
    if (low == Double.NEGATIVE_INFINITY)
        low = -1000;
    double coef = low + RandomUtil.getInstance().nextDouble() * (high - low);
    assertTrue(sem.setErrorCovariance(x, y, coef));
    assertTrue(isStandardized(sem));
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm)

Example 8 with SemEstimator

use of edu.cmu.tetrad.sem.SemEstimator in project tetrad by cmu-phil.

the class TestStandardizedSem method test5.

@Test
public void test5() {
    RandomUtil.getInstance().setSeed(582374923L);
    SemGraph graph = new SemGraph();
    graph.setShowErrorTerms(true);
    Node x1 = new ContinuousVariable("X1");
    Node x2 = new ContinuousVariable("X2");
    Node x3 = new ContinuousVariable("X3");
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.setShowErrorTerms(true);
    Node ex1 = graph.getExogenous(x1);
    Node ex2 = graph.getExogenous(x2);
    Node ex3 = graph.getExogenous(x3);
    graph.addDirectedEdge(x1, x3);
    graph.addDirectedEdge(x2, x3);
    // graph.addDirectedEdge(x1, x2);
    // graph.addBidirectedEdge(ex1, ex2);
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet dataSet = im.simulateDataRecursive(1000, false);
    TetradMatrix _dataSet = dataSet.getDoubleData();
    _dataSet = DataUtils.standardizeData(_dataSet);
    DataSet dataSetStandardized = ColtDataSet.makeData(dataSet.getVariables(), _dataSet);
    SemEstimator estimator = new SemEstimator(dataSetStandardized, im.getSemPm());
    SemIm imStandardized = estimator.estimate();
    StandardizedSemIm sem = new StandardizedSemIm(im);
    // sem.setErrorCovariance(ex1, ex2, -.24);
    assertTrue(isStandardized(sem));
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 9 with SemEstimator

use of edu.cmu.tetrad.sem.SemEstimator in project tetrad by cmu-phil.

the class TestStandardizedSem method test6.

@Test
public void test6() {
    // RandomUtil.getInstance().setSeed(582374923L);
    SemGraph graph = new SemGraph();
    graph.setShowErrorTerms(true);
    Node x1 = new ContinuousVariable("X1");
    Node x2 = new ContinuousVariable("X2");
    Node x3 = new ContinuousVariable("X3");
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.setShowErrorTerms(true);
    Node ex1 = graph.getExogenous(x1);
    Node ex2 = graph.getExogenous(x2);
    Node ex3 = graph.getExogenous(x3);
    graph.addDirectedEdge(x1, x3);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x1, x2);
    graph.addBidirectedEdge(ex1, ex2);
    // List<List<Node>> treks = DataGraphUtils.treksIncludingBidirected(graph, x1, x3);
    // 
    // for (List<Node> trek : treks) {
    // System.out.println(trek);
    // }
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet dataSet = im.simulateDataRecursive(1000, false);
    TetradMatrix _dataSet = dataSet.getDoubleData();
    _dataSet = DataUtils.standardizeData(_dataSet);
    DataSet dataSetStandardized = ColtDataSet.makeData(dataSet.getVariables(), _dataSet);
    SemEstimator estimator = new SemEstimator(dataSetStandardized, im.getSemPm());
    SemIm imStandardized = estimator.estimate();
    StandardizedSemIm sem = new StandardizedSemIm(im);
    assertTrue(isStandardized(sem));
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 10 with SemEstimator

use of edu.cmu.tetrad.sem.SemEstimator in project tetrad by cmu-phil.

the class PcStableLocalSearchEditor method reportIfContinuous.

private String reportIfContinuous(Graph dag, DataSet dataSet) {
    SemPm semPm = new SemPm(dag);
    SemEstimator estimator = new SemEstimator(dataSet, semPm);
    estimator.estimate();
    SemIm semIm = estimator.getEstimatedSem();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    StringBuilder buf = new StringBuilder();
    buf.append("\nDegrees of Freedom = ").append(semPm.getDof()).append("Chi-Square = ").append(nf.format(semIm.getChiSquare())).append("\nP Value = ").append(nf.format(semIm.getPValue())).append("\nBIC Score = ").append(nf.format(semIm.getBicScore()));
    buf.append("\n\nThe above chi square test assumes that the maximum " + "likelihood function over the measured variables has been " + "maximized. Under that assumption, the null hypothesis for " + "the test is that the population covariance matrix over all " + "of the measured variables is equal to the estimated covariance " + "matrix over all of the measured variables written as a function " + "of the free model parameters--that is, the unfixed parameters " + "for each directed edge (the linear coefficient for that edge), " + "each exogenous variable (the variance for the error term for " + "that variable), and each bidirected edge (the covariance for " + "the exogenous variables it connects).  The model is explained " + "in Bollen, Structural Equations with Latent Variable, 110. ");
    return buf.toString();
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) NumberFormat(java.text.NumberFormat)

Aggregations

SemEstimator (edu.cmu.tetrad.sem.SemEstimator)18 SemPm (edu.cmu.tetrad.sem.SemPm)16 SemIm (edu.cmu.tetrad.sem.SemIm)15 DataSet (edu.cmu.tetrad.data.DataSet)6 Test (org.junit.Test)6 Graph (edu.cmu.tetrad.graph.Graph)4 Node (edu.cmu.tetrad.graph.Node)4 StandardizedSemIm (edu.cmu.tetrad.sem.StandardizedSemIm)4 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)3 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)3 GraphNode (edu.cmu.tetrad.graph.GraphNode)3 Parameter (edu.cmu.tetrad.sem.Parameter)3 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)3 NumberFormat (java.text.NumberFormat)3 ArrayList (java.util.ArrayList)2 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)1 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)1 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)1 DataModel (edu.cmu.tetrad.data.DataModel)1 Dag (edu.cmu.tetrad.graph.Dag)1