Search in sources :

Example 21 with ICovarianceMatrix

use of edu.cmu.tetrad.data.ICovarianceMatrix 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)

Example 22 with ICovarianceMatrix

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

the class CovCellEditor method deleteSelected.

public void deleteSelected() {
    CovMatrixTable model = (CovMatrixTable) getModel();
    ICovarianceMatrix cov = model.getCovMatrix();
    java.util.List<String> selected = cov.getSelectedVariableNames();
    java.util.List<String> remaining = cov.getVariableNames();
    remaining.removeAll(selected);
    cov.removeVariables(remaining);
    firePropertyChange("modelChanged", null, null);
    model.fireTableDataChanged();
}
Also used : ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix)

Example 23 with ICovarianceMatrix

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

the class SemEstimatorWrapper method setParams.

private boolean setParams(SemPmWrapper semPmWrapper, DataModelList dataModel) {
    for (DataModel model : dataModel) {
        if (model instanceof DataSet) {
            DataSet dataSet = (DataSet) model;
            SemPm semPm = semPmWrapper.getSemPm();
            this.semPm = semPm;
            SemEstimator estimator = new SemEstimator(dataSet, semPm, getOptimizer());
            estimator.setNumRestarts(getParams().getInt("numRestarts", 1));
            estimator.setScoreType((ScoreType) getParams().get("scoreType", ScoreType.Fgls));
            if (!degreesOfFreedomCheck(semPm)) {
                return true;
            }
            estimator.estimate();
            getMultipleResultList().add(estimator);
        } else if (model instanceof ICovarianceMatrix) {
            ICovarianceMatrix covMatrix = new CovarianceMatrix((ICovarianceMatrix) model);
            SemPm semPm = semPmWrapper.getSemPm();
            this.semPm = semPm;
            SemEstimator estimator = new SemEstimator(covMatrix, semPm, getOptimizer());
            estimator.setNumRestarts(getParams().getInt("numRestarts", 1));
            estimator.setScoreType((ScoreType) getParams().get("scoreType", ScoreType.Fgls));
            if (!degreesOfFreedomCheck(semPm)) {
                return true;
            }
            estimator.estimate();
            getMultipleResultList().add(estimator);
        } else {
            throw new IllegalArgumentException("Data must consist of continuous data sets or covariance matrices.");
        }
    }
    return false;
}
Also used : ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) DataModel(edu.cmu.tetrad.data.DataModel) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) ScoreType(edu.cmu.tetrad.sem.ScoreType) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix)

Example 24 with ICovarianceMatrix

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

the class CovMatrixAverageWrapper method calcAverage.

private void calcAverage(List<DataWrapper> wrappers) {
    List<TetradMatrix> cov = new ArrayList<>();
    for (int i = 0; i < wrappers.size(); i++) {
        DataModel selectedDataModel = wrappers.get(i).getSelectedDataModel();
        if (!(selectedDataModel instanceof ICovarianceMatrix)) {
            throw new IllegalArgumentException("Sorry, this is an average only over covariance matrices.");
        }
        cov.add(((ICovarianceMatrix) selectedDataModel).getMatrix());
    }
    TetradMatrix cov3 = new TetradMatrix(cov.get(0).rows(), cov.get(0).rows());
    for (int i = 0; i < cov.get(0).rows(); i++) {
        for (int j = 0; j < cov.get(0).rows(); j++) {
            double c = 0.0;
            for (int k = 0; k < cov.size(); k++) {
                c += cov.get(k).get(i, j);
            }
            c /= cov.size();
            cov3.set(i, j, c);
            cov3.set(j, i, c);
        }
    }
    DataModel m = wrappers.get(0).getSelectedDataModel();
    ICovarianceMatrix _cov = (ICovarianceMatrix) m;
    List<Node> nodes = _cov.getVariables();
    int n = _cov.getSampleSize();
    ICovarianceMatrix covWrapper = new CovarianceMatrix(nodes, cov3, n);
    setDataModel(covWrapper);
}
Also used : DataModel(edu.cmu.tetrad.data.DataModel) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix)

Example 25 with ICovarianceMatrix

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

the class TestSem method testEstimation.

/**
 * The point of this test is to try to detect if the function of the
 * estimation ever changes for perhaps extraneous reasons.
 */
@Test
public void testEstimation() {
    Graph graph = constructGraph1();
    SemPm semPm = new SemPm(graph);
    ICovarianceMatrix covMatrix = constructCovMatrix1();
    SemEstimator estimator = new SemEstimator(covMatrix, semPm);
    estimator.estimate();
    SemIm semIm2 = estimator.getEstimatedSem();
    double[][] edgeCoef = semIm2.getEdgeCoef().toArray();
    double[][] _edgeCoef = { { 0.0000, 0.7750, 0.0000, 1.3192, 0.0000 }, { 0.0000, 0.0000, 1.0756, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000, 0.9639, 0.0000 }, { 0.0000, 0.0000, 0.0000, 0.0000, 0.5198 }, { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000 } };
    for (int i = 0; i < edgeCoef.length; i++) {
        for (int j = 0; j < edgeCoef[i].length; j++) {
            assertEquals(edgeCoef[i][j], _edgeCoef[i][j], .001);
        }
    }
    double[][] errCovar = semIm2.getErrCovar().toArray();
    double[][] _errCovar = { { 1.0439, 0.0000, 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.9293, 0.0000, 0.0000, 0.0000 }, { 0.0000, 0.0000, 1.0756, 0.0000, 0.0000 }, { 0.0000, 0.0000, 0.0000, 1.0233, 0.0000 }, { 0.0000, 0.0000, 0.0000, 0.0000, 1.0465 } };
    for (int i = 0; i < edgeCoef.length; i++) {
        for (int j = 0; j < edgeCoef[i].length; j++) {
            assertEquals(errCovar[i][j], _errCovar[i][j], .001);
        }
    }
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Test(org.junit.Test)

Aggregations

ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)30 DataSet (edu.cmu.tetrad.data.DataSet)12 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)10 ArrayList (java.util.ArrayList)9 DataModel (edu.cmu.tetrad.data.DataModel)8 Node (edu.cmu.tetrad.graph.Node)8 Graph (edu.cmu.tetrad.graph.Graph)7 SemPm (edu.cmu.tetrad.sem.SemPm)5 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)5 Test (org.junit.Test)5 Parameters (edu.cmu.tetrad.util.Parameters)4 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)3 SemIm (edu.cmu.tetrad.sem.SemIm)3 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)2 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)2 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)2 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)2 Algebra (cern.colt.matrix.linalg.Algebra)2 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)2