Search in sources :

Example 31 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class RegressionDataset method regress.

/**
 * Regresses the target on the given regressors.
 *
 * @param target     The target variable.
 * @param regressors The regressor variables.
 * @return The regression plane, specifying for each regressors its
 * coefficeint, se, t, and p values, and specifying the same for the
 * constant.
 */
public RegressionResult regress(Node target, List<Node> regressors) {
    int n = getRows().length;
    int k = regressors.size() + 1;
    int _target = variables.indexOf(target);
    int[] _regressors = new int[regressors.size()];
    for (int i = 0; i < regressors.size(); i++) {
        _regressors[i] = variables.indexOf(regressors.get(i));
        if (_regressors[i] == -1) {
            System.out.println();
        }
    }
    if (_target == -1) {
        System.out.println();
    }
    TetradMatrix y = data.getSelection(getRows(), new int[] { _target }).copy();
    TetradMatrix xSub = data.getSelection(getRows(), _regressors);
    TetradMatrix x;
    if (regressors.size() > 0) {
        x = new TetradMatrix(xSub.rows(), xSub.columns() + 1);
        for (int i = 0; i < x.rows(); i++) {
            for (int j = 0; j < x.columns(); j++) {
                if (j == 0) {
                    x.set(i, j, 1);
                } else {
                    x.set(i, j, xSub.get(i, j - 1));
                }
            }
        }
    } else {
        x = new TetradMatrix(xSub.rows(), xSub.columns());
        for (int i = 0; i < x.rows(); i++) {
            for (int j = 0; j < x.columns(); j++) {
                x.set(i, j, xSub.get(i, j));
            }
        }
    }
    TetradMatrix xT = x.transpose();
    TetradMatrix xTx = xT.times(x);
    TetradMatrix xTxInv = xTx.inverse();
    TetradMatrix xTy = xT.times(y);
    TetradMatrix b = xTxInv.times(xTy);
    TetradMatrix yHat = x.times(b);
    if (yHat.columns() == 0)
        yHat = y.like();
    // y.copy().assign(yHat, PlusMult.plusMult(-1));
    TetradMatrix res = y.minus(yHat);
    TetradVector _yHat = yHat.getColumn(0);
    TetradVector _res = res.getColumn(0);
    TetradMatrix b2 = b.copy();
    TetradMatrix yHat2 = x.times(b2);
    if (yHat.columns() == 0)
        yHat2 = y.like();
    // y.copy().assign(yHat, PlusMult.plusMult(-1));
    TetradMatrix res2 = y.minus(yHat2);
    this.res2 = res2.getColumn(0);
    double rss = rss(x, y, b);
    double se = Math.sqrt(rss / (n - k));
    double tss = tss(y);
    double r2 = 1.0 - (rss / tss);
    TetradVector sqErr = new TetradVector(x.columns());
    TetradVector t = new TetradVector(x.columns());
    TetradVector p = new TetradVector(x.columns());
    for (int i = 0; i < x.columns(); i++) {
        double _s = se * se * xTxInv.get(i, i);
        double _se = Math.sqrt(_s);
        double _t = b.get(i, 0) / _se;
        double _p = 2 * (1.0 - ProbUtils.tCdf(Math.abs(_t), n - k));
        sqErr.set(i, _se);
        t.set(i, _t);
        p.set(i, _p);
    }
    this.graph = createOutputGraph(target.getName(), x, regressors, p);
    String[] vNames = new String[regressors.size()];
    for (int i = 0; i < regressors.size(); i++) {
        vNames[i] = regressors.get(i).getName();
    }
    double[] bArray = b.columns() == 0 ? new double[0] : b.getColumn(0).toArray();
    double[] tArray = t.toArray();
    double[] pArray = p.toArray();
    double[] seArray = sqErr.toArray();
    return new RegressionResult(regressors.size() == 0, vNames, n, bArray, tArray, pArray, seArray, r2, rss, alpha, _yHat, _res);
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 32 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class RegressionDataset method regress.

public static RegressionResult regress(double[] target, double[][] regressors) {
    int n = target.length;
    int k = regressors.length + 1;
    String[] regressorNames = new String[regressors.length];
    for (int i = 0; i < regressors.length; i++) {
        regressorNames[i] = "X" + (i + 1);
    }
    TetradMatrix y = new TetradMatrix(new double[][] { target }).transpose();
    TetradMatrix x = new TetradMatrix(regressors).transpose();
    TetradMatrix xT = x.transpose();
    TetradMatrix xTx = xT.times(x);
    TetradMatrix xTxInv = xTx.inverse();
    TetradMatrix xTy = xT.times(y);
    TetradMatrix b = xTxInv.times(xTy);
    TetradMatrix yHat = x.times(b);
    if (yHat.columns() == 0)
        yHat = y.like();
    // y.copy().assign(yHat, PlusMult.plusMult(-1));
    TetradMatrix res = y.minus(yHat);
    TetradVector _yHat = yHat.getColumn(0);
    TetradVector _res = res.getColumn(0);
    TetradMatrix b2 = b.copy();
    TetradMatrix yHat2 = x.times(b2);
    if (yHat.columns() == 0)
        yHat2 = y.like();
    // y.copy().assign(yHat, PlusMult.plusMult(-1));
    TetradMatrix _res2 = y.minus(yHat2);
    TetradVector res2 = _res2.getColumn(0);
    double rss = rss(x, y, b);
    double se = Math.sqrt(rss / (n - k));
    double tss = tss(y);
    double r2 = 1.0 - (rss / tss);
    TetradVector sqErr = new TetradVector(x.columns());
    TetradVector t = new TetradVector(x.columns());
    TetradVector p = new TetradVector(x.columns());
    for (int i = 0; i < x.columns(); i++) {
        double _s = se * se * xTxInv.get(i, i);
        double _se = Math.sqrt(_s);
        double _t = b.get(i, 0) / _se;
        double _p = 2 * (1.0 - ProbUtils.tCdf(Math.abs(_t), n - k));
        sqErr.set(i, _se);
        t.set(i, _t);
        p.set(i, _p);
    }
    double[] bArray = b.columns() == 0 ? new double[0] : b.getColumn(0).toArray();
    double[] tArray = t.toArray();
    double[] pArray = p.toArray();
    double[] seArray = sqErr.toArray();
    return new RegressionResult(true, regressorNames, n, bArray, tArray, pArray, seArray, r2, rss, 0.05, _yHat, _res);
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 33 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class TestSemEstimator method constructCovMatrix1.

private ICovarianceMatrix constructCovMatrix1() {
    String[] vars = new String[] { "X1", "X2", "X3", "X4", "X5" };
    double[][] arr = { { 1.04408 }, { 0.80915, 1.55607 }, { 0.89296, 1.67375, 2.87584 }, { 2.23792, 2.68536, 3.94996, 7.78259 }, { 1.17516, 1.36337, 1.99039, 4.04533, 3.14922 } };
    double[][] m = MatrixUtils.convertLowerTriangleToSymmetric(arr);
    TetradMatrix m2 = new TetradMatrix(m);
    return new CovarianceMatrix(DataUtils.createContinuousVariables(vars), m2, 1000);
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 34 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class TestSemEstimator method constructCovMatrix2.

private ICovarianceMatrix constructCovMatrix2() {
    String[] vars = new String[] { "X1", "X2", "X3", "X4", "X5", "X6" };
    double[][] arr = { { 0.915736 }, { 0.636415, 1.446795 }, { 0.596983, 1.289278, 2.202219 }, { -0.004218, -0.012488, 0.017168, 0.979152 }, { 2.106086, 2.864279, 2.696651, 1.334353, 9.705821 }, { 0.029125, -0.027681, -0.043718, 0.679363, 0.886868, 1.495396 } };
    double[][] m = MatrixUtils.convertLowerTriangleToSymmetric(arr);
    // TetradMatrix m2 = TetradMatrix.instance(m);
    return new CovarianceMatrix(DataUtils.createContinuousVariables(vars), new TetradMatrix(m), 1000);
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 35 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class TestRicf method normdiff.

private double normdiff(Ricf.RicfResult ricfResult, double[] shatValues, int rows, int cols) {
    TetradMatrix shat = matrix(shatValues, rows, cols);
    TetradMatrix diff = shat.copy();
    // diff.assign(ricfResult.getShat(), PlusMult.plusMult(-1));
    diff = diff.minus(new TetradMatrix(ricfResult.getShat().toArray()));
    return diff.norm1();
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Aggregations

TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)161 TetradVector (edu.cmu.tetrad.util.TetradVector)46 ArrayList (java.util.ArrayList)43 Node (edu.cmu.tetrad.graph.Node)41 List (java.util.List)12 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)10 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)9 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)9 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)8 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)8 Test (org.junit.Test)8 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 SemIm (edu.cmu.tetrad.sem.SemIm)7 Graph (edu.cmu.tetrad.graph.Graph)6 SemPm (edu.cmu.tetrad.sem.SemPm)6 Vector (java.util.Vector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DataSet (edu.cmu.tetrad.data.DataSet)5 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5