use of edu.cmu.tetrad.regression.Regression in project tetrad by cmu-phil.
the class ScatterPlot method getRegressionResult.
private RegressionResult getRegressionResult() {
List<Node> regressors = new ArrayList<>();
regressors.add(dataSet.getVariable(x));
Node target = dataSet.getVariable(y);
Regression regression = new RegressionDataset(dataSet);
RegressionResult result = regression.regress(target, regressors);
System.out.println(result);
return result;
}
use of edu.cmu.tetrad.regression.Regression in project tetrad by cmu-phil.
the class IndTestRegression method isIndependent.
/**
* Determines whether variable x is independent of variable y given a list of conditioning variables z.
*
* @param xVar the one variable being compared.
* @param yVar the second variable being compared.
* @param zList the list of conditioning variables.
* @return true iff x _||_ y | z.
* @throws RuntimeException if a matrix singularity is encountered.
*/
public boolean isIndependent(Node xVar, Node yVar, List<Node> zList) {
if (zList == null) {
throw new NullPointerException();
}
for (Node node : zList) {
if (node == null) {
throw new NullPointerException();
}
}
List<Node> regressors = new ArrayList<>();
regressors.add(dataSet.getVariable(yVar.getName()));
for (Node zVar : zList) {
regressors.add(dataSet.getVariable(zVar.getName()));
}
Regression regression = new RegressionDataset(dataSet);
RegressionResult result = null;
try {
result = regression.regress(xVar, regressors);
} catch (Exception e) {
return false;
}
double p = result.getP()[1];
boolean independent = p > alpha;
if (verbose) {
if (independent) {
TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(xVar, yVar, zList, p));
} else {
TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(xVar, yVar, zList, p));
}
}
return independent;
}
use of edu.cmu.tetrad.regression.Regression in project tetrad by cmu-phil.
the class TimeSeriesUtils method sumOfArCoefficients.
public static double sumOfArCoefficients(DataSet timeSeries, int numLags) {
DataSet timeLags = createLagData(timeSeries, numLags);
List<Node> regressors = new ArrayList<>();
for (int i = timeSeries.getNumColumns(); i < timeLags.getNumColumns(); i++) {
regressors.add(timeLags.getVariable(i));
}
Regression regression = new RegressionDataset(timeLags);
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
double sum = 0.0;
int n = 0;
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
RegressionResult result = regression.regress(target, regressors);
double[] coef = result.getCoef();
for (int k = 0; k < coef.length; k++) {
sum += coef[k] * coef[k];
n++;
}
TetradVector residualsColumn = result.getResiduals();
// residuals.viewColumn(i).assign(residualsColumn);
residuals.assignColumn(i, residualsColumn);
}
return sum / n;
}
use of edu.cmu.tetrad.regression.Regression in project tetrad by cmu-phil.
the class TimeSeriesUtils method ar2.
public static DataSet ar2(DataSet timeSeries, int numLags) {
List<Node> missingVariables = new ArrayList<>();
for (Node node : timeSeries.getVariables()) {
int index = timeSeries.getVariables().indexOf(node);
boolean missing = true;
for (int i = 0; i < timeSeries.getNumRows(); i++) {
if (!Double.isNaN(timeSeries.getDouble(i, index))) {
missing = false;
break;
}
}
if (missing) {
missingVariables.add(node);
}
}
DataSet timeLags = createLagData(timeSeries, numLags);
Regression regression = new RegressionDataset(timeLags);
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
int index = timeSeries.getVariables().indexOf(target);
if (missingVariables.contains(target)) {
for (int i2 = 0; i2 < residuals.rows(); i2++) {
residuals.set(i2, index, Double.NaN);
}
continue;
}
List<Node> regressors = new ArrayList<>();
for (int i2 = timeSeries.getNumColumns(); i2 < timeLags.getNumColumns(); i2++) {
int varIndex = i2 % timeSeries.getNumColumns();
Node var = timeSeries.getVariable(varIndex);
if (missingVariables.contains(var)) {
continue;
}
regressors.add(timeLags.getVariable(i2));
}
RegressionResult result = regression.regress(target, regressors);
TetradVector residualsColumn = result.getResiduals();
residuals.assignColumn(i, residualsColumn);
}
return ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals);
}
use of edu.cmu.tetrad.regression.Regression 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);
}
Aggregations