Search in sources :

Example 11 with ContinuousVariable

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

the class TestGeneralizedSem method test7.

@Test
public void test7() {
    RandomUtil.getInstance().setSeed(29999483L);
    List<Node> nodes = new ArrayList<>();
    int numVars = 10;
    for (int i = 0; i < numVars; i++) nodes.add(new ContinuousVariable("X" + (i + 1)));
    Graph graph = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, numVars, 30, 15, 15, false, true);
    GeneralizedSemPm pm = new GeneralizedSemPm(graph);
    GeneralizedSemIm im = new GeneralizedSemIm(pm);
    print(im);
    DataSet data = im.simulateDataRecursive(1000, false);
    GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
    GeneralizedSemIm estIm = estimator.estimate(pm, data);
    print(estIm);
    print(estimator.getReport());
    double aSquaredStar = estimator.getaSquaredStar();
    assertEquals(0.67, aSquaredStar, 0.01);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DataSet(edu.cmu.tetrad.data.DataSet) Test(org.junit.Test)

Example 12 with ContinuousVariable

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

the class TestGeneralizedSem method makeTypicalPm.

private GeneralizedSemPm makeTypicalPm() {
    List<Node> variableNodes = new ArrayList<>();
    ContinuousVariable x1 = new ContinuousVariable("X1");
    ContinuousVariable x2 = new ContinuousVariable("X2");
    ContinuousVariable x3 = new ContinuousVariable("X3");
    ContinuousVariable x4 = new ContinuousVariable("X4");
    ContinuousVariable x5 = new ContinuousVariable("X5");
    variableNodes.add(x1);
    variableNodes.add(x2);
    variableNodes.add(x3);
    variableNodes.add(x4);
    variableNodes.add(x5);
    Graph _graph = new EdgeListGraph(variableNodes);
    SemGraph graph = new SemGraph(_graph);
    graph.addDirectedEdge(x1, x3);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x3, x4);
    graph.addDirectedEdge(x2, x4);
    graph.addDirectedEdge(x4, x5);
    graph.addDirectedEdge(x2, x5);
    return new GeneralizedSemPm(graph);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable)

Example 13 with ContinuousVariable

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

the class TestDeltaTetradTest method getBollenSimulationExampleData.

private CovarianceMatrix getBollenSimulationExampleData() {
    double[][] d = new double[][] { { 2.034 }, { 0.113, 1.281 }, { 0.510, 0.093, 1.572 }, { 0.105, 0.857, 0.447, 1.708 }, { 0.998, 0.228, 0.170, 0.345, 1.651 } };
    Node y1 = new ContinuousVariable("y1");
    Node y2 = new ContinuousVariable("y2");
    Node y3 = new ContinuousVariable("y3");
    Node y4 = new ContinuousVariable("y4");
    Node y5 = new ContinuousVariable("y5");
    List<Node> nodes = new ArrayList<>();
    nodes.add(y1);
    nodes.add(y2);
    nodes.add(y3);
    nodes.add(y4);
    nodes.add(y5);
    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, 1000);
}
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 ContinuousVariable

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

the class Mgm method search.

@Override
public Graph search(DataModel ds, Parameters parameters) {
    // Notify the user that you need at least one continuous and one discrete variable to run MGM
    List<Node> variables = ds.getVariables();
    boolean hasContinuous = false;
    boolean hasDiscrete = false;
    for (Node node : variables) {
        if (node instanceof ContinuousVariable) {
            hasContinuous = true;
        }
        if (node instanceof DiscreteVariable) {
            hasDiscrete = true;
        }
    }
    if (!hasContinuous || !hasDiscrete) {
        throw new IllegalArgumentException("You need at least one continuous and one discrete variable to run MGM.");
    }
    if (parameters.getInt("bootstrapSampleSize") < 1) {
        DataSet _ds = DataUtils.getMixedDataSet(ds);
        double mgmParam1 = parameters.getDouble("mgmParam1");
        double mgmParam2 = parameters.getDouble("mgmParam2");
        double mgmParam3 = parameters.getDouble("mgmParam3");
        double[] lambda = { mgmParam1, mgmParam2, mgmParam3 };
        MGM m = new MGM(_ds, lambda);
        return m.search();
    } else {
        Mgm algorithm = new Mgm();
        DataSet data = (DataSet) ds;
        GeneralBootstrapTest search = new GeneralBootstrapTest(data, algorithm, parameters.getInt("bootstrapSampleSize"));
        BootstrapEdgeEnsemble edgeEnsemble = BootstrapEdgeEnsemble.Highest;
        switch(parameters.getInt("bootstrapEnsemble", 1)) {
            case 0:
                edgeEnsemble = BootstrapEdgeEnsemble.Preserved;
                break;
            case 1:
                edgeEnsemble = BootstrapEdgeEnsemble.Highest;
                break;
            case 2:
                edgeEnsemble = BootstrapEdgeEnsemble.Majority;
        }
        search.setEdgeEnsemble(edgeEnsemble);
        search.setParameters(parameters);
        search.setVerbose(parameters.getBoolean("verbose"));
        return search.search();
    }
}
Also used : GeneralBootstrapTest(edu.pitt.dbmi.algo.bootstrap.GeneralBootstrapTest) BootstrapEdgeEnsemble(edu.pitt.dbmi.algo.bootstrap.BootstrapEdgeEnsemble) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) MGM(edu.pitt.csb.mgm.MGM)

Example 15 with ContinuousVariable

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

the class IndTestMultinomialLogisticRegression method expandVariable.

private List<Node> expandVariable(DataSet dataSet, Node node) {
    if (node instanceof ContinuousVariable) {
        return Collections.singletonList(node);
    }
    if (node instanceof DiscreteVariable && ((DiscreteVariable) node).getNumCategories() < 3) {
        return Collections.singletonList(node);
    }
    if (!(node instanceof DiscreteVariable)) {
        throw new IllegalArgumentException();
    }
    List<String> varCats = new ArrayList<>(((DiscreteVariable) node).getCategories());
    varCats.remove(0);
    List<Node> variables = new ArrayList<>();
    for (String cat : varCats) {
        Node newVar;
        do {
            String newVarName = node.getName() + "MULTINOM" + "." + cat;
            newVar = new DiscreteVariable(newVarName, 2);
        } while (dataSet.getVariable(newVar.getName()) != null);
        variables.add(newVar);
        dataSet.addVariable(newVar);
        int newVarIndex = dataSet.getColumn(newVar);
        int numCases = dataSet.getNumRows();
        for (int l = 0; l < numCases; l++) {
            Object dataCell = dataSet.getObject(l, dataSet.getColumn(node));
            int dataCellIndex = ((DiscreteVariable) node).getIndex(dataCell.toString());
            if (dataCellIndex == ((DiscreteVariable) node).getIndex(cat))
                dataSet.setInt(l, newVarIndex, 1);
            else
                dataSet.setInt(l, newVarIndex, 0);
        }
    }
    return variables;
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) Node(edu.cmu.tetrad.graph.Node)

Aggregations

ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)91 DataSet (edu.cmu.tetrad.data.DataSet)48 Node (edu.cmu.tetrad.graph.Node)46 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)28 Graph (edu.cmu.tetrad.graph.Graph)22 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)19 SemPm (edu.cmu.tetrad.sem.SemPm)18 SemIm (edu.cmu.tetrad.sem.SemIm)16 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)15 LinkedList (java.util.LinkedList)13 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)12 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)8 DMSearch (edu.cmu.tetrad.search.DMSearch)7 Dag (edu.cmu.tetrad.graph.Dag)6 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)5 RandomUtil (edu.cmu.tetrad.util.RandomUtil)5 ParseException (java.text.ParseException)4 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)3 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)3