use of edu.cmu.tetrad.regression.Regression in project tetrad by cmu-phil.
the class Lofs2 method regressionCoef.
private double regressionCoef(double[] xValues, double[] yValues) {
List<Node> v = new ArrayList<>();
v.add(new GraphNode("x"));
v.add(new GraphNode("y"));
TetradMatrix bothData = new TetradMatrix(xValues.length, 2);
for (int i = 0; i < xValues.length; i++) {
bothData.set(i, 0, xValues[i]);
bothData.set(i, 1, yValues[i]);
}
Regression regression2 = new RegressionDataset(bothData, v);
RegressionResult result;
try {
result = regression2.regress(v.get(0), v.get(1));
} catch (Exception e) {
return Double.NaN;
}
return result.getCoef()[1];
}
use of edu.cmu.tetrad.regression.Regression in project tetrad by cmu-phil.
the class TimeSeriesUtils method getSelfLoopCoefs.
public static double[] getSelfLoopCoefs(DataSet timeSeries) {
DataSet timeLags = createLagData(timeSeries, 1);
double[] coefs = new double[timeSeries.getNumColumns()];
for (int j = 0; j < timeSeries.getNumColumns(); j++) {
Node target = timeLags.getVariable(j);
Node selfLoop = timeLags.getVariable(j + timeSeries.getNumColumns());
List<Node> regressors = Collections.singletonList(selfLoop);
Regression regression = new RegressionDataset(timeLags);
RegressionResult result = regression.regress(target, regressors);
coefs[j] = result.getCoef()[1];
}
return coefs;
}
use of edu.cmu.tetrad.regression.Regression in project tetrad by cmu-phil.
the class TimeSeriesUtils method ar.
/**
* @return the VAR residuals of the given time series with the given number
* of lags. That is, every variable at the model lag is regressed onto every
* variable at previous lags, up to the given number of lags, and the
* residuals of these regressions for each variable are returned.
*/
public static DataSet ar(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);
// Regression regression = new RegressionDatasetGeneralized(timeLags);
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
RegressionResult result = regression.regress(target, regressors);
TetradVector residualsColumn = result.getResiduals();
// residuals.viewColumn(i).assign(residualsColumn);
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 TimeSeriesUtils method structuralVar.
public static VarResult structuralVar(DataSet timeSeries, int numLags) {
DataSet timeLags = TimeSeriesUtils.createLagData(timeSeries, numLags);
IKnowledge knowledge = timeLags.getKnowledge().copy();
for (int i = 0; i <= numLags; i++) {
knowledge.setTierForbiddenWithin(i, true);
}
Score score;
if (timeLags.isDiscrete()) {
score = new BDeuScore(timeLags);
} else if (timeLags.isContinuous()) {
SemBicScore semBicScore = new SemBicScore(new CovarianceMatrixOnTheFly(timeLags));
semBicScore.setPenaltyDiscount(2.0);
score = semBicScore;
} else {
throw new IllegalArgumentException("Mixed data set");
}
Fges search = new Fges(score);
search.setKnowledge(knowledge);
Graph graph = search.search();
// want to collapse graph here...
Graph collapsedVarGraph = new EdgeListGraph(timeSeries.getVariables());
for (Edge edge : graph.getEdges()) {
String node1_before = edge.getNode1().getName();
String node2_before = edge.getNode2().getName();
String node1_after = node1_before.substring(0, node1_before.indexOf("."));
String node2_after = node2_before.substring(0, node2_before.indexOf("."));
Node node1 = collapsedVarGraph.getNode(node1_after);
Node node2 = collapsedVarGraph.getNode(node2_after);
Edge _edge = new Edge(node1, node2, edge.getEndpoint1(), edge.getEndpoint2());
if (!collapsedVarGraph.containsEdge(_edge)) {
collapsedVarGraph.addEdge(_edge);
}
}
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
Regression regression = new RegressionDataset(timeLags);
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
List<Node> regressors = new ArrayList<>();
// timelags.getVariable(i).
for (int j = 0; j <= 0; /*numLags*/
j++) {
Node variable = timeLags.getVariable(i + j * timeSeries.getNumColumns());
regressors.addAll(graph.getParents(variable));
}
RegressionResult result = regression.regress(target, regressors);
TetradVector residualsColumn = result.getResiduals();
// residuals.viewColumn(i).assign(residualsColumn);
residuals.assignColumn(i, residualsColumn);
}
return new VarResult(ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals), collapsedVarGraph);
}
use of edu.cmu.tetrad.regression.Regression 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);
}
Aggregations