Search in sources :

Example 96 with DataSet

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

the class EmBayesProperties method logProbDataGivenStructure.

// =========================================PRIVATE METHODS===================================//
private double logProbDataGivenStructure() {
    BayesIm bayesIm = this.estimator.estimate(bayesPm, dataSet);
    BayesImProbs probs = new BayesImProbs(bayesIm);
    List<Node> variables = bayesIm.getVariables();
    System.out.println("E1 bayesIm : " + variables);
    System.out.println("E2 data set : " + dataSet.getVariables());
    DataSet reorderedDataSet = dataSet.subsetColumns(variables);
    int n = reorderedDataSet.getNumRows();
    int m = reorderedDataSet.getNumColumns();
    double score = 0.0;
    int[] _case = new int[m];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            _case[j] = reorderedDataSet.getInt(i, j);
        }
        score += Math.log(probs.getCellProb(_case));
    }
    return score;
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node)

Example 97 with DataSet

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

the class DirichletBayesIm method simulateData.

/**
 * Simulates a random sample with the number of cases equal to
 * <code>sampleSize</code>.
 *
 * @param sampleSize      the sample size.
 * @param seed            the random number generator seed allows you
 *                        recreate the simulated data by passing in the same
 *                        seed (so you don't have to store the sample data
 * @param latentDataSaved true iff data for latent variables should be
 *                        included in the simulated data set.
 * @return the simulated sample as a DataSet.
 */
public DataSet simulateData(int sampleSize, long seed, boolean latentDataSaved) {
    RandomUtil random = RandomUtil.getInstance();
    long _seed = random.getSeed();
    random.setSeed(seed);
    DataSet dataSet = simulateData(sampleSize, latentDataSaved);
    random.revertSeed(_seed);
    return dataSet;
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet)

Example 98 with DataSet

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

the class DirichletBayesIm method simulateDataHelper.

/**
 * Simulates a sample with the given sample size.
 *
 * @param sampleSize      the sample size.
 * @param randomUtil      optional random number generator to use when
 *                        creating the data
 * @param latentDataSaved true iff data for latent variables should be
 *                        saved.
 * @return the simulated sample as a DataSet.
 */
private DataSet simulateDataHelper(int sampleSize, RandomUtil randomUtil, boolean latentDataSaved) {
    int numMeasured = 0;
    int[] map = new int[nodes.length];
    List<Node> variables = new LinkedList<>();
    for (int j = 0; j < nodes.length; j++) {
        if (!latentDataSaved && nodes[j].getNodeType() != NodeType.MEASURED) {
            continue;
        }
        int numCategories = bayesPm.getNumCategories(nodes[j]);
        List<String> categories = new LinkedList<>();
        for (int k = 0; k < numCategories; k++) {
            categories.add(bayesPm.getCategory(nodes[j], k));
        }
        DiscreteVariable var = new DiscreteVariable(nodes[j].getName(), categories);
        variables.add(var);
        int index = ++numMeasured - 1;
        map[index] = j;
    }
    DataSet dataSet = new ColtDataSet(sampleSize, variables);
    constructSample(sampleSize, randomUtil, numMeasured, dataSet, map);
    return dataSet;
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node)

Example 99 with DataSet

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

the class DataCellRenderer method checkValueAt.

/**
 * @return true iff the given token is a legitimate value for the cell at
 * (row, col) in the table.
 */
public boolean checkValueAt(String token, int col) {
    if (col < getNumLeadingCols()) {
        throw new IllegalArgumentException();
    }
    DataSet dataSet = getDataSet();
    int dataCol = col - getNumLeadingCols();
    if (dataCol < dataSet.getNumColumns()) {
        Node variable = dataSet.getVariable(dataCol);
        return ((Variable) variable).checkValue(token);
    } else {
        return true;
    }
}
Also used : Variable(edu.cmu.tetrad.data.Variable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node)

Example 100 with DataSet

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

the class DataCellRenderer method clearSelected.

public void clearSelected() {
    TabularDataTable model = (TabularDataTable) getModel();
    DataSet dataSet = model.getDataSet();
    if (!getRowSelectionAllowed()) {
        int[] selectedCols = getSelectedColumns();
        TableCellEditor editor = getCellEditor();
        if (editor != null) {
            editor.stopCellEditing();
        }
        for (int i = selectedCols.length - 1; i >= 0; i--) {
            if (selectedCols[i] < getNumLeadingCols()) {
                continue;
            }
            int dataCol = selectedCols[i] - getNumLeadingCols();
            if (dataCol >= dataSet.getNumColumns()) {
                continue;
            }
            Node variable = dataSet.getVariable(dataCol);
            Object missingValue = ((Variable) variable).getMissingValueMarker();
            for (int j = 0; j < dataSet.getNumRows(); j++) {
                dataSet.setObject(j, dataCol, missingValue);
            }
        }
    } else if (!getColumnSelectionAllowed()) {
        int[] selectedRows = getSelectedRows();
        TableCellEditor editor = getCellEditor();
        if (editor != null) {
            editor.stopCellEditing();
        }
        for (int i = getColumnCount() - 1; i >= 0; i--) {
            if (i < getNumLeadingCols()) {
                continue;
            }
            String colName = (String) (getValueAt(1, i));
            if (colName == null) {
                continue;
            }
            int dataCol = i - getNumLeadingCols();
            Node variable = dataSet.getVariable(dataCol);
            Object missingValue = ((Variable) variable).getMissingValueMarker();
            for (int j = selectedRows.length - 1; j >= 0; j--) {
                if (selectedRows[j] < 2) {
                    continue;
                }
                if (selectedRows[j] > dataSet.getNumRows() + 1) {
                    continue;
                }
                dataSet.setObject(selectedRows[j] - 2, dataCol, missingValue);
            }
        }
    } else {
        int[] selectedRows = getSelectedRows();
        int[] selectedCols = getSelectedColumns();
        TableCellEditor editor = getCellEditor();
        if (editor != null) {
            editor.stopCellEditing();
        }
        for (int i = selectedCols.length - 1; i >= 0; i--) {
            if (selectedCols[i] < getNumLeadingCols()) {
                continue;
            }
            int dataCol = selectedCols[i] - getNumLeadingCols();
            if (dataCol >= dataSet.getNumColumns()) {
                continue;
            }
            String colName = (String) (getValueAt(1, selectedCols[i]));
            if (colName == null) {
                continue;
            }
            Node variable = dataSet.getVariable(dataCol);
            Object missingValue = ((Variable) variable).getMissingValueMarker();
            for (int j = selectedRows.length - 1; j >= 0; j--) {
                if (selectedRows[j] < 2) {
                    continue;
                }
                if (selectedRows[j] > dataSet.getNumRows() + 1) {
                    continue;
                }
                dataSet.setObject(selectedRows[j] - 2, dataCol, missingValue);
            }
        }
    }
    firePropertyChange("modelChanged", null, null);
    model.fireTableDataChanged();
}
Also used : Variable(edu.cmu.tetrad.data.Variable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) EventObject(java.util.EventObject) TableCellEditor(javax.swing.table.TableCellEditor)

Aggregations

DataSet (edu.cmu.tetrad.data.DataSet)216 Test (org.junit.Test)65 Graph (edu.cmu.tetrad.graph.Graph)64 Node (edu.cmu.tetrad.graph.Node)60 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)48 ArrayList (java.util.ArrayList)45 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)36 GeneralBootstrapTest (edu.pitt.dbmi.algo.bootstrap.GeneralBootstrapTest)32 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)29 SemIm (edu.cmu.tetrad.sem.SemIm)28 SemPm (edu.cmu.tetrad.sem.SemPm)28 BootstrapEdgeEnsemble (edu.pitt.dbmi.algo.bootstrap.BootstrapEdgeEnsemble)26 DataModel (edu.cmu.tetrad.data.DataModel)22 Parameters (edu.cmu.tetrad.util.Parameters)22 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)20 File (java.io.File)16 ParseException (java.text.ParseException)16 LinkedList (java.util.LinkedList)14 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)13 DMSearch (edu.cmu.tetrad.search.DMSearch)10