Search in sources :

Example 31 with DiscreteVariable

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

the class BayesUtils method ensureVarsInData.

/**
 * Ensures that the discrete variables in the given list are compatible with
 * the variables in the data set by the same names. If a variable x in the
 * list has a superlist of the categories for x in the data, the data
 * variables is changed.
 *
 * @throws IllegalArgumentException if a variable exists in the list for
 *                                  which no variable occurs in the data by
 *                                  the same name or if the variable in the
 *                                  data by the same name does not have a
 *                                  subset of its categories.
 */
public static void ensureVarsInData(List<Node> pmVars, DataSet dataSet) {
    for (Node pmVar1 : pmVars) {
        DiscreteVariable pmVar = (DiscreteVariable) pmVar1;
        String name = pmVar.getName();
        DiscreteVariable from = (DiscreteVariable) dataSet.getVariable(name);
        if (from == null) {
            throw new IllegalArgumentException("Variable " + pmVar + " was not in the data.");
        }
        List<String> pmCategories = pmVar.getCategories();
        List<String> dataCategories = from.getCategories();
        if (!pmCategories.equals(dataCategories)) {
            if (pmCategories.containsAll(dataCategories)) {
                DiscreteVariable to = new DiscreteVariable(pmVar);
                dataSet.changeVariable(from, to);
            } else {
                throw new IllegalArgumentException("Variable '" + name + "' " + "has more categories in the data than in the model." + "\n\tIn the model, the categories are: " + pmCategories + "." + "\n\tIn the data, the categories are: " + dataCategories + ".");
            }
        }
    }
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) Node(edu.cmu.tetrad.graph.Node)

Example 32 with DiscreteVariable

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

the class TabularDataTable method pasteIntoColumn.

/**
 * The row and column indices are JTable indices.
 */
private void pasteIntoColumn(int row, int col, Object value) {
    int dataRow = row - getNumLeadingRows();
    int dataCol = col - getNumLeadingCols();
    Node variable = dataSet.getVariable(dataCol);
    if (variable instanceof ContinuousVariable && value instanceof Number) {
        dataSet.setObject(dataRow, dataCol, value);
        // dataSet.setDouble(dataRow, dataCol, (Double) value);
        return;
    }
    if ("".equals(value) || value == null) {
        return;
    }
    String valueTrimmed = ((String) value).trim();
    boolean quoted = false;
    if (valueTrimmed.startsWith("\"") && valueTrimmed.endsWith("\"")) {
        value = valueTrimmed.substring(1, valueTrimmed.length() - 1);
        quoted = true;
    }
    if (!(variable instanceof DiscreteVariable) && isEmpty(dataSet, dataCol) && (quoted || !isNumber((String) value))) {
        variable = swapDiscreteColumnForContinuous(col);
    }
    if (value instanceof String && ((String) value).trim().equals("*")) {
        value = ((Variable) variable).getMissingValueMarker();
    }
    dataSet.setObject(dataRow, dataCol, value);
    pcs.firePropertyChange("modelChanged", null, null);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) Node(edu.cmu.tetrad.graph.Node)

Example 33 with DiscreteVariable

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

the class TabularDataTable method getValueAt.

/**
 * @return the value at the given (row, col) coordinates of the table as an
 * Object. If the variable for the col is a DiscreteVariable, the String
 * value (as opposed to the integer index value) is extracted and returned.
 * If the coordinates are out of range of the wrapped table model, 'null' is
 * returned. Otherwise, the value stored in the wrapped table model at the
 * given coordinates is returned.
 */
@Override
public Object getValueAt(int row, int col) {
    int columnIndex = col - getNumLeadingCols();
    int rowIndex = row - 2;
    // else
    if (col >= getNumLeadingCols() && col < dataSet.getNumColumns() + getNumLeadingCols()) {
        Node variable = dataSet.getVariable(columnIndex);
        if (row == 0) {
            boolean discrete = variable instanceof DiscreteVariable;
            return "C" + Integer.toString(columnIndex + 1) + (discrete ? "-T" : "");
        } else if (row == 1) {
            return dataSet.getVariable(columnIndex).getName();
        } else if (rowIndex >= dataSet.getNumRows()) {
            return null;
        } else {
            if (variable instanceof DiscreteVariable) {
                ((DiscreteVariable) variable).setCategoryNamesDisplayed(isCategoryNamesShown());
            }
            Object value = dataSet.getObject(rowIndex, columnIndex);
            if (((Variable) variable).isMissingValue(value)) {
                return "*";
            } else {
                return value;
            }
        }
    } else if (col >= dataSet.getNumColumns() + getNumLeadingCols()) {
        if (row == 0) {
            return "C" + Integer.toString(columnIndex + 1);
        }
    }
    return null;
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Variable(edu.cmu.tetrad.data.Variable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) Node(edu.cmu.tetrad.graph.Node)

Example 34 with DiscreteVariable

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

the class MNLRLikelihood method getDoF.

public double getDoF(int child_index, int[] parents) {
    double dof = 0;
    Node c = variables.get(child_index);
    List<ContinuousVariable> continuous_parents = new ArrayList<>();
    List<DiscreteVariable> discrete_parents = new ArrayList<>();
    for (int p : parents) {
        Node parent = variables.get(p);
        if (parent instanceof ContinuousVariable) {
            continuous_parents.add((ContinuousVariable) parent);
        } else {
            discrete_parents.add((DiscreteVariable) parent);
        }
    }
    int p = continuous_parents.size();
    List<List<Integer>> cells = adTree.getCellLeaves(discrete_parents);
    // List<List<Integer>> cells = partition(discrete_parents, 0).cells;
    int[] continuousCols = new int[p];
    for (int j = 0; j < p; j++) continuousCols[j] = nodesHash.get(continuous_parents.get(j));
    for (List<Integer> cell : cells) {
        int r = cell.size();
        if (r > 0) {
            int degree = fDegree;
            if (fDegree < 1) {
                degree = (int) Math.floor(Math.log(r));
            }
            if (c instanceof ContinuousVariable) {
                dof += degree * continuous_parents.size() + 1;
            } else {
                dof += ((degree * continuous_parents.size()) + 1) * (((DiscreteVariable) c).getNumCategories() - 1);
            }
        }
    }
    return dof;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable)

Example 35 with DiscreteVariable

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

the class LogisticRegressionParamsPanel method actionPerformed.

public void actionPerformed(ActionEvent e) {
    String varName;
    int varSelectionIndex, predictorVarSelectionIndex;
    DefaultListModel varsModel = (DefaultListModel) availableVarsList.getModel();
    DefaultListModel predsModel = (DefaultListModel) predictorVarListbox.getModel();
    int startIndexVars = varsSelModel.getMinSelectionIndex();
    int endIndexVars = varsSelModel.getMaxSelectionIndex();
    int startIndexPreds = predsSelModel.getMinSelectionIndex();
    int endIndexPreds = predsSelModel.getMaxSelectionIndex();
    String[] varSelectedNames = new String[endIndexVars - startIndexVars + 1];
    String[] predSelectedNames = new String[endIndexPreds - startIndexPreds + 1];
    if (varsModel.size() != 0) {
        varName = (String) availableVarsList.getSelectedValue();
    } else {
        varName = "";
    }
    /* include/exclude response variable */
    if (e.getActionCommand().equals(INCLUDE_RESPONSE)) {
        if ((availableVarsList.isSelectionEmpty()) && (responseButton.getIsIncluded())) {
            return;
        }
        if (responseButton.getIsIncluded()) {
            // Make sure response variable is dichotomous
            DataSet cds = (DataSet) dataModel;
            int nrows = cds.getNumRows();
            Node variable = cds.getVariable(varName);
            int varIndex = cds.getVariables().indexOf(variable);
            // make sure the variable is binary.
            if (variable instanceof DiscreteVariable) {
                for (int i = 0; i < nrows; i++) {
                    int value = cds.getInt(i, varIndex);
                    if (value != 0 && value != 1) {
                        JOptionPane.showMessageDialog(this, "The given target was discrete but not binary");
                        return;
                    }
                }
            } else {
                for (int i = 0; i < nrows; i++) {
                    double value = cds.getDouble(i, varIndex);
                    if (value != 0.0d && value != 1.0d) {
                        JOptionPane.showMessageDialog(this, "Target must be a dictotomous variable");
                        return;
                    }
                }
            }
            responseVar.setText(varName);
            varsModel.removeElement(varName);
        } else {
            varsModel.addElement(responseVar.getText());
            responseVar.setText("");
            // Test
            responseButton.toggleInclude();
            // Test
            return;
        }
        responseButton.toggleInclude();
        String newTargetName = responseVar.getText();
        setTargetName(newTargetName);
        params().set("targetName", targetName());
    } else // include predictor variable.
    if (e.getActionCommand().equals(INCLUDE_PREDICTOR)) {
        if (availableVarsList.isSelectionEmpty()) {
            return;
        }
        int intervalSize = endIndexVars - startIndexVars + 1;
        for (int i = 0; i < intervalSize; i++) {
            varSelectedNames[i] = (String) availableVarsList.getSelectedValuesList().get(i);
        }
        for (int i = 0; i < intervalSize; i++) {
            varsModel.removeElement(varSelectedNames[i]);
            predsModel.addElement(varSelectedNames[i]);
        }
    } else // exclude predictor variable.
    if (e.getActionCommand().equals(EXCLUDE_PREDICTOR)) {
        if (predictorVarListbox.isSelectionEmpty()) {
            return;
        }
        int intervalSize = endIndexPreds - startIndexPreds + 1;
        for (int i = 0; i < intervalSize; i++) {
            predSelectedNames[i] = (String) predictorVarListbox.getSelectedValuesList().get(i);
        }
        for (int i = 0; i < intervalSize; i++) {
            predsModel.removeElement(predSelectedNames[i]);
            varsModel.addElement(predSelectedNames[i]);
        }
    } else {
        return;
    }
    // updates expt variables and predictor listbox.
    varSelectionIndex = availableVarsList.getSelectedIndex();
    predictorVarSelectionIndex = predictorVarListbox.getSelectedIndex();
    if (varSelectionIndex > 0) {
        varSelectionIndex--;
    }
    if (varSelectionIndex != -1) {
        availableVarsList.setSelectedIndex(varSelectionIndex);
    }
    if (predictorVarSelectionIndex > 0) {
        predictorVarSelectionIndex--;
    }
    if (predictorVarSelectionIndex != -1) {
        predictorVarListbox.setSelectedIndex(predictorVarSelectionIndex);
    }
    int numPredictors = predsModel.size();
    Object[] predictors = new Object[numPredictors];
    List<String> regNames = new ArrayList<>();
    for (int i = 0; i < numPredictors; i++) {
        predictors[i] = predsModel.getElementAt(i);
        regNames.add((String) predsModel.getElementAt(i));
    }
    setRegressorNames(regNames);
    params().set("regressorNames", regressorNames);
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList)

Aggregations

DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)56 Node (edu.cmu.tetrad.graph.Node)37 DataSet (edu.cmu.tetrad.data.DataSet)18 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)16 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)11 LinkedList (java.util.LinkedList)9 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Dag (edu.cmu.tetrad.graph.Dag)3 NumberFormat (java.text.NumberFormat)3 Element (nu.xom.Element)3 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)2 Graph (edu.cmu.tetrad.graph.Graph)2 LogisticRegression (edu.cmu.tetrad.regression.LogisticRegression)2 List (java.util.List)2 Elements (nu.xom.Elements)2 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 TakesInitialGraph (edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph)1 StoredCellProbs (edu.cmu.tetrad.bayes.StoredCellProbs)1 BoxDataSet (edu.cmu.tetrad.data.BoxDataSet)1