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);
}
}
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));
}
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));
}
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));
}
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();
}
Aggregations