use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestLingamPattern method test1.
@Test
public void test1() {
RandomUtil.getInstance().setSeed(4938492L);
int sampleSize = 1000;
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 6; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 6, 4, 4, 4, false));
List<Distribution> variableDistributions = new ArrayList<>();
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Uniform(-1, 1));
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Normal(0, 1));
SemPm semPm = new SemPm(graph);
SemIm semIm = new SemIm(semPm);
DataSet dataSet = simulateDataNonNormal(semIm, sampleSize, variableDistributions);
Score score = new SemBicScore(new CovarianceMatrixOnTheFly(dataSet));
Graph estPattern = new Fges(score).search();
LingamPattern lingam = new LingamPattern(estPattern, dataSet);
lingam.search();
double[] pvals = lingam.getPValues();
double[] expectedPVals = { 0.18, 0.29, 0.88, 0.00, 0.01, 0.58 };
for (int i = 0; i < pvals.length; i++) {
assertEquals(expectedPVals[i], pvals[i], 0.01);
}
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestSemIm method test5.
@Test
public void test5() {
Graph graph = new EdgeListGraph();
Node x = new GraphNode("X");
Node y = new GraphNode("Y");
Node z = new GraphNode("Z");
graph.addNode(x);
graph.addNode(y);
graph.addNode(z);
Node lx = new GraphNode("LX");
lx.setNodeType(NodeType.LATENT);
Node ly = new GraphNode("LY");
ly.setNodeType(NodeType.LATENT);
Node lz = new GraphNode("LZ");
lz.setNodeType(NodeType.LATENT);
graph.addNode(lx);
graph.addNode(ly);
graph.addNode(lz);
graph.addDirectedEdge(lx, x);
graph.addDirectedEdge(ly, y);
graph.addDirectedEdge(lz, z);
graph.addDirectedEdge(lx, ly);
graph.addDirectedEdge(ly, lz);
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
// DataSet data = im.simulateDataCholesky(1000, true);
DataSet data = im.simulateDataReducedForm(1000, true);
// DataSet data = im.simulateDataRecursive(1000, true);
data.getCovarianceMatrix();
im.getImplCovar(true);
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestSemIm method testCovariancesOfSimulated.
@Test
public void testCovariancesOfSimulated() {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Graph randomGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 8, 30, 15, 15, false));
SemPm semPm1 = new SemPm(randomGraph);
SemIm semIm1 = new SemIm(semPm1);
TetradMatrix implCovarC = semIm1.getImplCovar(true);
implCovarC.toArray();
DataSet dataSet = semIm1.simulateDataRecursive(1000, false);
new CovarianceMatrix(dataSet);
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class LogisticRegressionRunner method execute.
// =================PUBLIC METHODS OVERRIDING ABSTRACT=================//
/**
* Executes the algorithm, producing (at least) a result workbench. Must be
* implemented in the extending class.
*/
public void execute() {
outGraph = new EdgeListGraph();
if (regressorNames == null || regressorNames.isEmpty() || targetName == null) {
report = "Response and predictor variables not set.";
return;
}
if (regressorNames.contains(targetName)) {
report = "Response must not be a predictor.";
return;
}
DataSet regressorsDataSet = dataSets.get(getModelIndex()).copy();
Node target = regressorsDataSet.getVariable(targetName);
regressorsDataSet.removeColumn(target);
List<String> names = regressorsDataSet.getVariableNames();
// Get the list of regressors selected by the user
List<Node> regressorNodes = new ArrayList<>();
for (String s : regressorNames) {
regressorNodes.add(dataSets.get(getModelIndex()).getVariable(s));
}
// If the user selected none, use them all
if (regressorNames.size() > 0) {
for (String name1 : names) {
Node regressorVar = regressorsDataSet.getVariable(name1);
if (!regressorNames.contains(regressorVar.getName())) {
regressorsDataSet.removeColumn(regressorVar);
}
}
}
int ncases = regressorsDataSet.getNumRows();
int nvars = regressorsDataSet.getNumColumns();
double[][] regressors = new double[nvars][ncases];
for (int i = 0; i < nvars; i++) {
for (int j = 0; j < ncases; j++) {
regressors[i][j] = regressorsDataSet.getDouble(j, i);
}
}
LogisticRegression logRegression = new LogisticRegression(dataSets.get(getModelIndex()));
logRegression.setAlpha(alpha);
this.result = logRegression.regress((DiscreteVariable) target, regressorNodes);
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class ConcatenateDatasetsWrapper method construct.
private void construct(DataWrapper... dataWrappers) {
for (DataWrapper wrapper : dataWrappers) {
if (wrapper == null) {
throw new NullPointerException("The given data must not be null");
}
}
List<DataSet> dataSets = new ArrayList<>();
for (DataWrapper wrapper : dataWrappers) {
for (DataModel model : wrapper.getDataModelList()) {
if (!(model instanceof DataSet)) {
throw new IllegalArgumentException("Sorry, I am only willing to concatenate tabular datasets.");
}
DataSet dataSet = (DataSet) model;
dataSets.add(dataSet);
}
}
DataSet concatenated = DataUtils.concatenate(dataSets);
concatenated.setName("Concatenated");
this.setDataModel(concatenated);
LogDataUtils.logDataModelList("Parent data in which constant columns have been removed.", getDataModelList());
}
Aggregations