Search in sources :

Example 1 with RegressionCovariance

use of edu.cmu.tetrad.regression.RegressionCovariance in project tetrad by cmu-phil.

the class TestRegression method testCovariance.

/**
 * Same problem, using the covariance matrix.
 */
@Test
public void testCovariance() {
    setUp();
    RandomUtil.getInstance().setSeed(3848283L);
    ICovarianceMatrix cov = new CovarianceMatrix(data);
    List<Node> nodes = cov.getVariables();
    Node target = nodes.get(0);
    List<Node> regressors = new ArrayList<>();
    for (int i = 1; i < nodes.size(); i++) {
        regressors.add(nodes.get(i));
    }
    Regression regression = new RegressionCovariance(cov);
    RegressionResult result = regression.regress(target, regressors);
    double[] coeffs = result.getCoef();
    assertEquals(0.00, coeffs[0], 0.01);
    assertEquals(-.053, coeffs[1], 0.01);
    assertEquals(0.036, coeffs[2], 0.01);
    assertEquals(.019, coeffs[3], 0.01);
    assertEquals(.007, coeffs[4], 0.01);
}
Also used : ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) RegressionCovariance(edu.cmu.tetrad.regression.RegressionCovariance) RegressionResult(edu.cmu.tetrad.regression.RegressionResult) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Test(org.junit.Test)

Example 2 with RegressionCovariance

use of edu.cmu.tetrad.regression.RegressionCovariance in project tetrad by cmu-phil.

the class SemIm method getStandardError.

public double getStandardError(Parameter parameter, int maxFreeParams) {
    TetradMatrix sampleCovar = getSampleCovar();
    if (sampleCovar == null) {
        return Double.NaN;
    }
    if (getFreeParameters().contains(parameter)) {
        if (getNumFreeParams() <= maxFreeParams) {
            if (parameter.getNodeA() != parameter.getNodeB()) {
                Node nodeA = parameter.getNodeA();
                Node nodeB = parameter.getNodeB();
                Node parent;
                Node child;
                Graph graph = getSemPm().getGraph();
                if (graph.isParentOf(nodeA, nodeB)) {
                    parent = nodeA;
                    child = nodeB;
                } else {
                    parent = nodeB;
                    child = nodeA;
                }
                if (child.getName().startsWith("E_")) {
                    return Double.NaN;
                }
                CovarianceMatrix cov = new CovarianceMatrix(measuredNodes, sampleCovar, sampleSize);
                Regression regression = new RegressionCovariance(cov);
                List<Node> parents = graph.getParents(child);
                for (Node node : new ArrayList<>(parents)) {
                    if (node.getName().startsWith("E_")) {
                        parents.remove(node);
                    }
                }
                if (!(child.getNodeType() == NodeType.LATENT) && !containsLatent(parents)) {
                    RegressionResult result = regression.regress(child, parents);
                    double[] se = result.getSe();
                    return se[parents.indexOf(parent) + 1];
                }
            }
            if (this.sampleCovarC == null) {
                this.standardErrors = null;
                return Double.NaN;
            }
            int index = getFreeParameters().indexOf(parameter);
            double[] doubles = standardErrors();
            if (doubles == null) {
                return Double.NaN;
            }
            return doubles[index];
        } else {
            return Double.NaN;
        }
    } else if (getFixedParameters().contains(parameter)) {
        return 0.0;
    }
    throw new IllegalArgumentException("That is not a parameter of this model: " + parameter);
}
Also used : Regression(edu.cmu.tetrad.regression.Regression) RegressionCovariance(edu.cmu.tetrad.regression.RegressionCovariance) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 3 with RegressionCovariance

use of edu.cmu.tetrad.regression.RegressionCovariance in project tetrad by cmu-phil.

the class RegressionRunner method execute.

// =================PUBLIC METHODS OVERRIDING ABSTRACT=================//
/**
 * Executes the algorithm, producing (at least) a result workbench. Must be
 * implemented in the extending class.
 */
public void execute() {
    if (regressorNames.size() == 0 || targetName == null) {
        outGraph = new EdgeListGraph();
        return;
    }
    if (regressorNames.contains(targetName)) {
        outGraph = new EdgeListGraph();
        return;
    }
    Regression regression;
    Node target;
    List<Node> regressors;
    if (getDataModel() instanceof DataSet) {
        DataSet _dataSet = (DataSet) getDataModel();
        regression = new RegressionDataset(_dataSet);
        target = _dataSet.getVariable(targetName);
        regressors = new LinkedList<>();
        for (String regressorName : regressorNames) {
            regressors.add(_dataSet.getVariable(regressorName));
        }
        double alpha = params.getDouble("alpha", 0.001);
        regression.setAlpha(alpha);
        result = regression.regress(target, regressors);
        outGraph = regression.getGraph();
    } else if (getDataModel() instanceof ICovarianceMatrix) {
        ICovarianceMatrix covariances = (ICovarianceMatrix) getDataModel();
        regression = new RegressionCovariance(covariances);
        target = covariances.getVariable(targetName);
        regressors = new LinkedList<>();
        for (String regressorName : regressorNames) {
            regressors.add(covariances.getVariable(regressorName));
        }
        double alpha = params.getDouble("alpha", 0.001);
        regression.setAlpha(alpha);
        result = regression.regress(target, regressors);
        outGraph = regression.getGraph();
    }
    setResultGraph(outGraph);
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Regression(edu.cmu.tetrad.regression.Regression) RegressionCovariance(edu.cmu.tetrad.regression.RegressionCovariance)

Example 4 with RegressionCovariance

use of edu.cmu.tetrad.regression.RegressionCovariance in project tetrad by cmu-phil.

the class BffBeam method removeZeroEdges.

public Graph removeZeroEdges(Graph bestGraph) {
    boolean changed = true;
    Graph graph = new EdgeListGraph(bestGraph);
    while (changed) {
        changed = false;
        Score score = scoreGraph(graph);
        SemIm estSem = score.getEstimatedSem();
        for (Parameter param : estSem.getSemPm().getParameters()) {
            if (param.getType() != ParamType.COEF) {
                continue;
            }
            Node nodeA = param.getNodeA();
            Node nodeB = param.getNodeB();
            Node parent;
            Node child;
            if (this.graph.isParentOf(nodeA, nodeB)) {
                parent = nodeA;
                child = nodeB;
            } else {
                parent = nodeB;
                child = nodeA;
            }
            Regression regression = new RegressionCovariance(cov);
            List<Node> parents = graph.getParents(child);
            RegressionResult result = regression.regress(child, parents);
            double p = result.getP()[parents.indexOf(parent) + 1];
            if (p > getHighPValueAlpha()) {
                Edge edge = graph.getEdge(param.getNodeA(), param.getNodeB());
                if (getKnowledge().isRequired(edge.getNode1().getName(), edge.getNode2().getName())) {
                    System.out.println("Not removing " + edge + " because it is required.");
                    TetradLogger.getInstance().log("details", "Not removing " + edge + " because it is required.");
                    continue;
                }
                System.out.println("Removing edge " + edge + " because it has p = " + p);
                TetradLogger.getInstance().log("details", "Removing edge " + edge + " because it has p = " + p);
                graph.removeEdge(edge);
                changed = true;
            }
        }
    }
    return graph;
}
Also used : Regression(edu.cmu.tetrad.regression.Regression) RegressionCovariance(edu.cmu.tetrad.regression.RegressionCovariance) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Aggregations

Regression (edu.cmu.tetrad.regression.Regression)4 RegressionCovariance (edu.cmu.tetrad.regression.RegressionCovariance)4 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)3 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)1 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 Node (edu.cmu.tetrad.graph.Node)1 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1