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