X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FLinestFormulaFunction.java;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FLinestFormulaFunction.java;h=dd037e9eaae010927dee651aadca7a5474d19014;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=dc15b99b1dcf03634dbd2f37b1ca6fecf5c049fb;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/LinestFormulaFunction.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/LinestFormulaFunction.java index dc15b99b1..dd037e9ea 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/LinestFormulaFunction.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/LinestFormulaFunction.java @@ -1,77 +1,77 @@ -package org.simantics.spreadsheet.graph.formula; - -import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; -import org.simantics.spreadsheet.graph.CellFormulaFunction; -import org.simantics.spreadsheet.graph.CellValueVisitor; -import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils; -import org.simantics.spreadsheet.graph.SpreadsheetMatrix; -import org.simantics.spreadsheet.graph.parser.ast.AstArgList; - -public class LinestFormulaFunction implements CellFormulaFunction { - - @Override - public SpreadsheetMatrix evaluate(CellValueVisitor visitor, AstArgList args) { - if (args.values.size() != 4) - throw new IllegalStateException(); - - Object ys = args.values.get(0).accept(visitor); - Object xs = args.values.get(1).accept(visitor); - - if (xs instanceof SpreadsheetMatrix) { - - OLSMultipleLinearRegression reg = new OLSMultipleLinearRegression(); - - SpreadsheetMatrix xsm = (SpreadsheetMatrix) xs; - SpreadsheetMatrix ysm = (SpreadsheetMatrix) ys; - - if (xsm.getWidth() > 1) { - - double y[] = new double[ysm.values.length]; - double x[][] = new double[ysm.values.length][xsm.getWidth()]; - - for (int i = 0; i < ysm.values.length; i++) { - y[i] = SpreadsheetGraphUtils.asNumber(ysm.values[i]); - for (int j = 0; j < xsm.getWidth(); j++) { - x[i][j] = SpreadsheetGraphUtils.asNumber(xsm.get(i, j)); - } - } - - reg.newSampleData(y, x); - - int width = xsm.getWidth() + 1; - SpreadsheetMatrix result = new SpreadsheetMatrix(width, 5); - double[] pars = reg.estimateRegressionParameters(); - for (int i = 0; i < width; i++) - result.set(0, width - i - 1, pars[i]); - double[] errs = reg.estimateRegressionParametersStandardErrors(); - for (int i = 0; i < width; i++) - result.set(1, width - i - 1, errs[i]); - - double sstotal = reg.calculateTotalSumOfSquares(); - double ssresid = reg.calculateResidualSumOfSquares(); - double ssreg = sstotal - ssresid; - double F = 0; - double r2 = ssreg / sstotal; - double sey = reg.estimateRegressionStandardError(); - double df = width; - - result.set(2, 0, r2); - result.set(2, 1, sey); - result.set(3, 0, F); - result.set(3, 1, df); - result.set(4, 0, ssreg); - result.set(4, 1, ssresid); - result.set(2,2, FormulaError2.NA.getString()); - result.set(3,2, FormulaError2.NA.getString()); - result.set(4,2, FormulaError2.NA.getString()); - - return result; - - } - - } - - return null; - } - -} +package org.simantics.spreadsheet.graph.formula; + +import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; +import org.simantics.spreadsheet.graph.CellFormulaFunction; +import org.simantics.spreadsheet.graph.CellValueVisitor; +import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils; +import org.simantics.spreadsheet.graph.SpreadsheetMatrix; +import org.simantics.spreadsheet.graph.parser.ast.AstArgList; + +public class LinestFormulaFunction implements CellFormulaFunction { + + @Override + public SpreadsheetMatrix evaluate(CellValueVisitor visitor, AstArgList args) { + if (args.values.size() != 4) + throw new IllegalStateException(); + + Object ys = args.values.get(0).accept(visitor); + Object xs = args.values.get(1).accept(visitor); + + if (xs instanceof SpreadsheetMatrix) { + + OLSMultipleLinearRegression reg = new OLSMultipleLinearRegression(); + + SpreadsheetMatrix xsm = (SpreadsheetMatrix) xs; + SpreadsheetMatrix ysm = (SpreadsheetMatrix) ys; + + if (xsm.getWidth() > 1) { + + double y[] = new double[ysm.values.length]; + double x[][] = new double[ysm.values.length][xsm.getWidth()]; + + for (int i = 0; i < ysm.values.length; i++) { + y[i] = SpreadsheetGraphUtils.asNumber(ysm.values[i]); + for (int j = 0; j < xsm.getWidth(); j++) { + x[i][j] = SpreadsheetGraphUtils.asNumber(xsm.get(i, j)); + } + } + + reg.newSampleData(y, x); + + int width = xsm.getWidth() + 1; + SpreadsheetMatrix result = new SpreadsheetMatrix(width, 5); + double[] pars = reg.estimateRegressionParameters(); + for (int i = 0; i < width; i++) + result.set(0, width - i - 1, pars[i]); + double[] errs = reg.estimateRegressionParametersStandardErrors(); + for (int i = 0; i < width; i++) + result.set(1, width - i - 1, errs[i]); + + double sstotal = reg.calculateTotalSumOfSquares(); + double ssresid = reg.calculateResidualSumOfSquares(); + double ssreg = sstotal - ssresid; + double F = 0; + double r2 = ssreg / sstotal; + double sey = reg.estimateRegressionStandardError(); + double df = width; + + result.set(2, 0, r2); + result.set(2, 1, sey); + result.set(3, 0, F); + result.set(3, 1, df); + result.set(4, 0, ssreg); + result.set(4, 1, ssresid); + result.set(2,2, FormulaError2.NA.getString()); + result.set(3,2, FormulaError2.NA.getString()); + result.set(4,2, FormulaError2.NA.getString()); + + return result; + + } + + } + + return null; + } + +}