Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 93 additions & 2 deletions lib/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -398,6 +488,7 @@
};
},


// Calculates the power regression line for the data set.
//
// Returns an object with the coefficient, base, and correlation
Expand Down Expand Up @@ -941,4 +1032,4 @@
// Just write to window (or whatever is the root object)
root.stats = stats;
}
}(this));
}(this));