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