private double getP(List<Integer> cluster, int numRestarts) { if (true) { Node latent = new GraphNode("L"); latent.setNodeType(NodeType.LATENT); Graph g = new EdgeListGraph(); g.addNode(latent); List<Node> measures = variablesForIndices(cluster); for (Node node : measures) { g.addNode(node); g.addDirectedEdge(latent, node); } SemPm pm = new SemPm(g); // pm.fixOneLoadingPerLatent(); SemOptimizerPowell semOptimizer = new SemOptimizerPowell(); semOptimizer.setNumRestarts(numRestarts); SemEstimator est = new SemEstimator(cov, pm, semOptimizer); est.setScoreType(SemIm.ScoreType.Fgls); est.estimate(); return est.getEstimatedSem().getPValue(); } else { double max = Double.NEGATIVE_INFINITY; for (int i = 0; i < numRestarts; i++) { Mimbuild2 mimbuild = new Mimbuild2(); List<List<Node>> clusters1 = new ArrayList<List<Node>>(); clusters1.add(variablesForIndices(new ArrayList<Integer>(cluster))); List<String> names = new ArrayList<String>(); names.add("L"); mimbuild.search(clusters1, names, cov); double c = mimbuild.getpValue(); if (c > max) max = c; } return max; } }
private double getClusterP2(List<Node> c) { Graph g = new EdgeListGraph(c); Node l = new GraphNode("L"); l.setNodeType(NodeType.LATENT); g.addNode(l); for (Node n : c) { g.addDirectedEdge(l, n); } SemPm pm = new SemPm(g); SemEstimator est; if (dataModel instanceof DataSet) { est = new SemEstimator((DataSet) dataModel, pm, new SemOptimizerEm()); } else { est = new SemEstimator((CovarianceMatrix) dataModel, pm, new SemOptimizerEm()); } SemIm estIm = est.estimate(); double pValue = estIm.getPValue(); return pValue == 1 ? Double.NaN : pValue; }
private Graph convertSearchGraphNodes(Set<Set<Node>> clusters) { Graph graph = new EdgeListGraph(variables); List<Node> latents = new ArrayList<Node>(); for (int i = 0; i < clusters.size(); i++) { Node latent = new GraphNode(MimBuild.LATENT_PREFIX + (i + 1)); latent.setNodeType(NodeType.LATENT); latents.add(latent); graph.addNode(latent); } List<Set<Node>> _clusters = new ArrayList<Set<Node>>(clusters); for (int i = 0; i < latents.size(); i++) { for (Node node : _clusters.get(i)) { if (!graph.containsNode(node)) graph.addNode(node); graph.addDirectedEdge(latents.get(i), node); } } return graph; }
@Test public void test1() { GeneralizedSemPm pm = makeTypicalPm(); print(pm); Node x1 = pm.getNode("X1"); Node x2 = pm.getNode("X2"); Node x3 = pm.getNode("X3"); Node x4 = pm.getNode("X4"); Node x5 = pm.getNode("X5"); SemGraph graph = pm.getGraph(); List<Node> variablesNodes = pm.getVariableNodes(); print(variablesNodes); List<Node> errorNodes = pm.getErrorNodes(); print(errorNodes); try { pm.setNodeExpression(x1, "cos(B1) + E_X1"); print(pm); String b1 = "B1"; String b2 = "B2"; String b3 = "B3"; Set<Node> nodes = pm.getReferencingNodes(b1); assertTrue(nodes.contains(x1)); assertTrue(!nodes.contains(x2) && !nodes.contains(x2)); Set<String> referencedParameters = pm.getReferencedParameters(x3); print("Parameters referenced by X3 are: " + referencedParameters); assertTrue(referencedParameters.contains(b1) && referencedParameters.contains(b2)); assertTrue(!(referencedParameters.contains(b1) && referencedParameters.contains(b3))); Node e_x3 = pm.getNode("E_X3"); // for (Node node : pm.getNodes()) { Set<Node> referencingNodes = pm.getReferencingNodes(node); print("Nodes referencing " + node + " are: " + referencingNodes); } for (Node node : pm.getVariableNodes()) { Set<Node> referencingNodes = pm.getReferencedNodes(node); print("Nodes referenced by " + node + " are: " + referencingNodes); } Set<Node> referencingX3 = pm.getReferencingNodes(x3); assertTrue(referencingX3.contains(x4)); assertTrue(!referencingX3.contains(x5)); Set<Node> referencedByX3 = pm.getReferencedNodes(x3); assertTrue( referencedByX3.contains(x1) && referencedByX3.contains(x2) && referencedByX3.contains(e_x3) && !referencedByX3.contains(x4)); pm.setNodeExpression(x5, "a * E^X2 + X4 + E_X5"); Node e_x5 = pm.getErrorNode(x5); graph.setShowErrorTerms(true); assertTrue(e_x5.equals(graph.getExogenous(x5))); pm.setNodeExpression(e_x5, "Beta(3, 5)"); print(pm); assertEquals("Split(-1.5,-.5,.5,1.5)", pm.getParameterExpressionString(b1)); pm.setParameterExpression(b1, "N(0, 2)"); assertEquals("N(0, 2)", pm.getParameterExpressionString(b1)); GeneralizedSemIm im = new GeneralizedSemIm(pm); print(im); DataSet dataSet = im.simulateDataAvoidInfinity(10, false); print(dataSet); } catch (ParseException e) { e.printStackTrace(); } }