From 35435019b6f73134ae59bd894c3d64395404ac4b Mon Sep 17 00:00:00 2001 From: Alex Linden Levy Date: Tue, 27 Aug 2013 11:15:16 -0600 Subject: [PATCH] added eExpReg: uses a*e^(x*b) form of exponential and lnReg: logarithmic form a + b*ln(a) --- lib/stats.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/lib/stats.js b/lib/stats.js index c875a34..6b4dead 100644 --- a/lib/stats.js +++ b/lib/stats.js @@ -361,10 +361,100 @@ }; }, - // Calculates the exponential regression line for the data set. + // A true exponential form -- lindenle@gmail.com + // y = a* e ^ bx + // ln y = ln a + ln e^ bx + // ln y = ln a + bx + + // y' = ln y + // m' = b + // b' = ln a + + // y' = b' + m'x + + eExpReg: function() { + // Get y coordinates + var yCoords = this.pluck('y'); + + + // Do a semi-log transformation of the coordinates + // y' = ln y + yCoords.map(function(num) { + return Math.log(num); + }); + + // Get a new stats object to work with that has the transformed data + var nums = this.clone().map(function(coord, index) { + return { + x: coord.x, + y: yCoords.get(index) + }; + }); + + // Calculate the linear regression for the linearized data + var linReg = nums.linReg(); + + // Calculate the coefficient for the exponential equation + // a = e ^ b' + var coefficient = Math.pow(Math.E, linReg.yIntercept); + + // Calculate the base for the exponential equation + // b = m' + var weight = linReg.slope; + + return { + coefficient: coefficient, + weight: weight, + r: linReg.r + }; + }, + //------------- + // Logarithmic + //-------------- + // y = a + b ln x + // x' = ln(x) + // y = a+bx' + lnReg: function() { + // Get y coordinates + var xCoords = this.pluck('x'); + + + // Do a semi-log transformation of the coordinates + // x' = ln x + xCoords.map(function(num) { + return Math.log(num); + }); + + // Get a new stats object to work with that has the transformed data + var nums = this.clone().map(function(coord, index) { + return { + x: xCoords.get(index), + y: coord.y + }; + }); + + // Calculate the linear regression for the linearized data + var linReg = nums.linReg(); + + // Calculate the coefficient for the exponential equation + // a = e ^ b' + var coefficient = linReg.slope; + + // Calculate the base for the exponential equation + // b = m' + var offset = linReg.yIntercept; + + return { + coefficient: coefficient, + offset: offset, + r: linReg.r + }; + }, + // Calculates the exponential regression line for the data set. // // Returns an object with the coefficient, base, and correlation // coefficient for the linearized data. + expReg: function() { // Get y coordinates var yCoords = this.pluck('y'); @@ -398,6 +488,7 @@ }; }, + // Calculates the power regression line for the data set. // // Returns an object with the coefficient, base, and correlation @@ -941,4 +1032,4 @@ // Just write to window (or whatever is the root object) root.stats = stats; } -}(this)); \ No newline at end of file +}(this));