Search in sources :

Example 1 with FactorAnalysis

use of edu.cmu.tetrad.search.FactorAnalysis 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)

Example 2 with FactorAnalysis

use of edu.cmu.tetrad.search.FactorAnalysis in project tetrad by cmu-phil.

the class FactorAnalysisAction method actionPerformed.

public void actionPerformed(ActionEvent e) {
    DataSet dataSet = (DataSet) dataEditor.getSelectedDataModel();
    if (dataSet == null || dataSet.getNumColumns() == 0) {
        JOptionPane.showMessageDialog(findOwner(), "Cannot perform factor analysis on an empty data set.");
        return;
    }
    FactorAnalysis factorAnalysis = new FactorAnalysis(dataSet);
    // factorAnalysis.centroidUnity();
    JPanel panel = createDialog(factorAnalysis);
    EditorWindow window = new EditorWindow(panel, "Factor Loading Matrices", "Close", false, dataEditor);
    DesktopController.getInstance().addEditorWindow(window, JLayeredPane.PALETTE_LAYER);
    window.setVisible(true);
/*
        EditorWindow window2 = new EditorWindow(new GraphEditor(new DirectedGraph()), "Factor Analysis", "Close", false, dataEditor);
        window2.setLocation(800, 400);
        DesktopController.getInstance().addEditorWindow(window2, JLayeredPane.PALETTE_LAYER);
        window2.setVisible(true);
        */
}
Also used : FactorAnalysis(edu.cmu.tetrad.search.FactorAnalysis) DataSet(edu.cmu.tetrad.data.DataSet)

Example 3 with FactorAnalysis

use of edu.cmu.tetrad.search.FactorAnalysis in project tetrad by cmu-phil.

the class FactorAnalysisAction method main.

public static void main(String[] args) {
    java.util.List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 9; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 9, 30, 15, 15, false));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(500, false);
    ICovarianceMatrix cov = new CovarianceMatrix(data);
    FactorAnalysis factorAnalysis = new FactorAnalysis(cov);
    // factorAnalysis.centroidUnity();
    factorAnalysis.successiveResidual();
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ArrayList(java.util.ArrayList) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) FactorAnalysis(edu.cmu.tetrad.search.FactorAnalysis) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm)

Aggregations

DataSet (edu.cmu.tetrad.data.DataSet)3 FactorAnalysis (edu.cmu.tetrad.search.FactorAnalysis)3 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)2 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)1 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)1 SemIm (edu.cmu.tetrad.sem.SemIm)1 SemPm (edu.cmu.tetrad.sem.SemPm)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1