Search in sources :

Example 41 with ContinuousVariable

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

the class NormalityTestAction method createNormalityTestDialog.

/**
 * Creates a dialog that is showing the histogram for the given node (if null
 * one is selected for you)
 */
private JPanel createNormalityTestDialog(Node selected) {
    DataSet dataSet = (DataSet) dataEditor.getSelectedDataModel();
    QQPlot qqPlot = new QQPlot(dataSet, selected);
    NormalityTestEditorPanel editorPanel = new NormalityTestEditorPanel(qqPlot, dataSet);
    JTextArea display = new JTextArea(NormalityTests.runNormalityTests(dataSet, (ContinuousVariable) qqPlot.getSelectedVariable()), 20, 65);
    display.setEditable(false);
    editorPanel.addPropertyChangeListener(new NormalityTestListener(display));
    Box box = Box.createHorizontalBox();
    box.add(display);
    box.add(Box.createHorizontalStrut(3));
    box.add(editorPanel);
    box.add(Box.createHorizontalStrut(5));
    box.add(Box.createHorizontalGlue());
    Box vBox = Box.createVerticalBox();
    vBox.add(Box.createVerticalStrut(15));
    vBox.add(box);
    vBox.add(Box.createVerticalStrut(5));
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(vBox, BorderLayout.CENTER);
    return panel;
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DataSet(edu.cmu.tetrad.data.DataSet)

Example 42 with ContinuousVariable

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

the class ScatterPlot method addConditioningVariable.

// ========================================PUBLIC METHODS=================================//
/**
 * Adds a continuous conditioning variables, conditioning on a range of values.
 *
 * @param variable The name of the variable in the data set.
 * @param low      The low end of the conditioning range.
 * @param high     The high end of the conditioning range.
 */
public void addConditioningVariable(String variable, double low, double high) {
    if (!(low < high))
        throw new IllegalArgumentException("Low must be less than high: " + low + " >= " + high);
    Node node = dataSet.getVariable(variable);
    if (!(node instanceof ContinuousVariable))
        throw new IllegalArgumentException("Variable must be continuous.");
    if (continuousIntervals.containsKey(node))
        throw new IllegalArgumentException("Please remove conditioning variable first.");
    continuousIntervals.put(node, new double[] { low, high });
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Node(edu.cmu.tetrad.graph.Node)

Example 43 with ContinuousVariable

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

the class QQPlot method testPlot.

// ============================ Private Methods =======================//
/**
 * Used to test this class.
 *
 * Generates a continuous test variable and q-q plots it.
 */
private void testPlot() {
    ContinuousVariable c = new ContinuousVariable("test");
    if (dataSet.getVariable("test") == null)
        dataSet.addVariable(c);
    ContinuousVariable c2 = new ContinuousVariable("test2");
    if (dataSet.getVariable("test2") == null)
        dataSet.addVariable(c2);
    this.selectedVariable = c;
    int columnIndex = dataSet.getColumn(c);
    Normal g = new Normal(1, 1);
    Exponential e = new Exponential(1);
    double mean = 0.0;
    double sd = 0.0;
    this.minData = 10000000000000.0;
    this.maxData = 0.0;
    this.minComparison = 1000000000000.0;
    this.maxComparison = 0.0;
    for (int i = 0; i < dataSet.getNumRows(); i++) {
        double value = g.nextRandom();
        double value2 = e.nextRandom();
        dataSet.setDouble(i, columnIndex, value);
        dataSet.setDouble(i, columnIndex + 1, value2);
        mean += value;
        if (value < this.minData)
            this.minData = value;
        if (value > this.maxData)
            this.maxData = value;
    // System.out.println(value);
    // System.out.println(mean);
    }
    // System.out.println(this.dataSet.getNumRows());
    NormalityTests.kolmogorovSmirnov(dataSet, c2);
    // sort the dataset
    for (int i = 0; i < dataSet.getNumRows(); i++) {
        for (int k = i; k < dataSet.getNumRows(); k++) {
            if (dataSet.getDouble(i, columnIndex) > dataSet.getDouble(k, columnIndex)) {
                double temp = dataSet.getDouble(i, columnIndex);
                dataSet.setDouble(i, columnIndex, dataSet.getDouble(k, columnIndex));
                dataSet.setDouble(k, columnIndex, temp);
            }
        }
    }
    if (mean == 0.0)
        mean = 1.0;
    else
        mean /= dataSet.getNumRows();
    for (int i = 0; i < dataSet.getNumRows(); i++) {
        sd += (dataSet.getDouble(i, columnIndex) - mean) * (dataSet.getDouble(i, columnIndex) - mean);
    // System.out.println(dataSet.getDouble(i, columnIndex));
    // System.out.println(sd);
    }
    if (sd == 0.0) {
        sd = 1.0;
    } else {
        sd /= dataSet.getNumRows() - 1.0;
        sd = Math.sqrt(sd);
    }
    // System.out.println("Mean: " + mean + " SD: " + sd + " Min: " + this.minData + " Max: " + this.maxData);
    this.comparison = new cern.jet.random.Normal(mean, sd, new MersenneTwister());
    calculateComparisonSet(this.comparison, this.dataSet);
    if (this.minData < this.minComparison)
        this.min = this.minData;
    else
        this.min = this.minComparison;
    if (this.maxData > this.maxComparison)
        this.max = this.maxData;
    else
        this.max = this.maxComparison;
// end test code
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Exponential(edu.cmu.tetrad.util.dist.Exponential) Normal(edu.cmu.tetrad.util.dist.Normal) MersenneTwister(cern.jet.random.engine.MersenneTwister)

Example 44 with ContinuousVariable

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

the class IndTestMultinomialLogisticRegressionWald 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)

Example 45 with ContinuousVariable

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

the class FactorAnalysisRunner method execute.

// ===================PUBLIC METHODS OVERRIDING ABSTRACT================//
public void execute() {
    DataSet selectedModel = (DataSet) getDataModel();
    if (selectedModel == null) {
        throw new NullPointerException("Data not specified.");
    }
    FactorAnalysis analysis = new FactorAnalysis(selectedModel);
    threshold = .2;
    TetradMatrix unrotatedSolution = analysis.successiveResidual();
    rotatedSolution = analysis.successiveFactorVarimax(unrotatedSolution);
    NumberFormat nf = NumberFormatUtil.getInstance().getNumberFormat();
    output = "Unrotated Factor Loading Matrix:\n";
    output += tableString(unrotatedSolution, nf, Double.POSITIVE_INFINITY);
    if (unrotatedSolution.columns() != 1) {
        output += "\n\nRotated Matrix (using sequential varimax):\n";
        output += tableString(rotatedSolution, nf, threshold);
    }
    SemGraph graph = new SemGraph();
    Vector<Node> observedVariables = new Vector<>();
    for (Node a : selectedModel.getVariables()) {
        graph.addNode(a);
        observedVariables.add(a);
    }
    Vector<Node> factors = new Vector<>();
    for (int i = 0; i < getRotatedSolution().columns(); i++) {
        ContinuousVariable factor = new ContinuousVariable("Factor" + (i + 1));
        factor.setNodeType(NodeType.LATENT);
        graph.addNode(factor);
        factors.add(factor);
    }
    for (int i = 0; i < getRotatedSolution().rows(); i++) {
        for (int j = 0; j < getRotatedSolution().columns(); j++) {
            if (Math.abs(getRotatedSolution().get(i, j)) > getThreshold()) {
                graph.addDirectedEdge(factors.get(j), observedVariables.get(i));
            }
        }
    }
    setResultGraph(graph);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) FactorAnalysis(edu.cmu.tetrad.search.FactorAnalysis) DataSet(edu.cmu.tetrad.data.DataSet) Vector(java.util.Vector) NumberFormat(java.text.NumberFormat)

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