diff --git a/TODO.md b/TODO.md index c3f2cf5..488b1c9 100644 --- a/TODO.md +++ b/TODO.md @@ -12,12 +12,9 @@ TODO - [jarque-bera test](http://en.wikipedia.org/wiki/Jarque%E2%80%93Bera_test) - [condition number](http://en.wikipedia.org/wiki/Condition_number) - p-value - - log likelihood - estimate std error - t-value - t-prob - - aic - - bic - skew - kurt - influential observations diff --git a/lib/adjustedrsquared.js b/lib/adjustedrsquared.js new file mode 100644 index 0000000..d8d6d76 --- /dev/null +++ b/lib/adjustedrsquared.js @@ -0,0 +1,55 @@ +/** +* +* ADJUSTED COEFFICIENT OF DETERMINATION +* +* +* DESCRIPTION: +* - Computes the adjusted coefficient of determination (adjusted R squared). +* +* NOTES: +* [1] +* +* +* TODO: +* [1] +* +* +* LICENSE: +* MIT +* +* Copyright (c) 2015. Athan Reines. +* +* +* AUTHOR: +* Athan Reines. kgryte@gmail.com. 2015. +* +*/ + +'use strict'; + +// MODULES // + +var rsq = require( './rsquared.js' ); + + +// ADJUSTED R SQUARED // + +/** +* FUNCTION: arsq( mss, tss, mdf, rdf ) +* Computes the adjusted coefficient of determination. +* +* @param {Number} mss - model sum of squares +* @param {Number} tss - total sum of squares +* @param {Number} mdf - model degrees of freedom +* @param {Number} rdf - residual degrees of freedom +* @returns {Number} adjusted coefficient of determination +*/ +function arsq( mss, tss, mdf, rdf ) { + var r2 = rsq( mss, tss ); + return r2 - (1-r2)*(mdf/rdf); +} // end FUNCTION arsq() + + +// EXPORTS // + +module.exports = arsq; diff --git a/lib/aic.js b/lib/aic.js new file mode 100644 index 0000000..1d6d930 --- /dev/null +++ b/lib/aic.js @@ -0,0 +1,45 @@ +/** +* +* AIC +* +* +* DESCRIPTION: +* - Computes the Akaike information criterion. +* +* NOTES: +* [1] +* +* +* TODO: +* [1] +* +* +* LICENSE: +* MIT +* +* Copyright (c) 2015. Athan Reines. +* +* +* AUTHOR: +* Athan Reines. kgryte@gmail.com. 2015. +* +*/ + +'use strict'; + +/** +* FUNCTION: aic( ll, k ) +* Computes the Akaike information criterion. +* +* @param {Number} ll - log likelihood +* @param {Number} k - number of parameters +* @returns {Number} Akaike information criterion +*/ +function aic( ll, k ) { + return 2 * ( k - ll ); +} // end FUNCTION aic() + + +// EXPORTS // + +module.exports = aic; diff --git a/lib/aicc.js b/lib/aicc.js new file mode 100644 index 0000000..77095e2 --- /dev/null +++ b/lib/aicc.js @@ -0,0 +1,53 @@ +/** +* +* AIC +* +* +* DESCRIPTION: +* - Computes the corrected Akaike information criterion for finite sample sizes. +* +* NOTES: +* [1] +* +* +* TODO: +* [1] +* +* +* LICENSE: +* MIT +* +* Copyright (c) 2015. Athan Reines. +* +* +* AUTHOR: +* Athan Reines. kgryte@gmail.com. 2015. +* +*/ + +'use strict'; + +// MODULES // + +var aic = require( './aic.js' ); + + +// CORRECTED AIC // + +/** +* FUNCTION: aicc( ll, N, k ) +* Computes the corrected Akaike information criterion for finite sample sizes. +* +* @param {Number} ll - log likelihood +* @param {Number} N - sample size +* @param {Number} k - number of parameters +* @returns {Number} Akaike information criterion +*/ +function aicc( ll, N, k ) { + return aic( ll, k ) + (2*k*(k+1)) / (N-k-1); +} // end FUNCTION aicc() + + +// EXPORTS // + +module.exports = aicc; diff --git a/lib/bic.js b/lib/bic.js new file mode 100644 index 0000000..f862ff6 --- /dev/null +++ b/lib/bic.js @@ -0,0 +1,46 @@ +/** +* +* BIC +* +* +* DESCRIPTION: +* - Computes the Bayesian information criterion. +* +* NOTES: +* [1] +* +* +* TODO: +* [1] +* +* +* LICENSE: +* MIT +* +* Copyright (c) 2015. Athan Reines. +* +* +* AUTHOR: +* Athan Reines. kgryte@gmail.com. 2015. +* +*/ + +'use strict'; + +/** +* FUNCTION: bic( ll, N, k ) +* Computes the Bayesian information criterion. +* +* @param {Number} ll - log likelihood +* @param {Number} N - number of observations +* @param {Number} k - number of parameters +* @returns {Number} Bayesian information criterion +*/ +function bic( ll, N, k ) { + return -2*ll + k*Math.log( N ); +} // end FUNCTION bic() + + +// EXPORTS // + +module.exports = bic; diff --git a/lib/loglikelihood.js b/lib/loglikelihood.js new file mode 100644 index 0000000..3dc065a --- /dev/null +++ b/lib/loglikelihood.js @@ -0,0 +1,45 @@ +/** +* +* LOG LIKELIHOOD +* +* +* DESCRIPTION: +* - Computes the log likelihood. +* +* NOTES: +* [1] +* +* +* TODO: +* [1] +* +* +* LICENSE: +* MIT +* +* Copyright (c) 2015. Athan Reines. +* +* +* AUTHOR: +* Athan Reines. kgryte@gmail.com. 2015. +* +*/ + +'use strict'; + +/** +* FUNCTION: loglikelihood( N, rss ) +* Computes the log likelihood. +* +* @param {Number} N - number of observations +* @param {Number} rss - residual sum of squares (sum of squared errors) +* @returns {Number} log likelihood +*/ +function loglikelihood( N, rss ) { + return -N / 2 * ( Math.log( (rss/N) * 2 * Math.PI ) + 1 ); +} // end FUNCTION loglikelihood() + + +// EXPORTS // + +module.exports = loglikelihood; diff --git a/lib/model.js b/lib/model.js index 5aaf820..adb8679 100644 --- a/lib/model.js +++ b/lib/model.js @@ -35,10 +35,20 @@ var isArray = require( 'validate.io-array' ), isFunction = require( 'validate.io-function' ), isNumber = require( 'validate.io-number' ), isBoolean = require( 'validate.io-boolean-primitive' ), - copy = require( 'utils-copy' ), - rss = require( './rss.js' ), + copy = require( 'utils-copy' ); + + +// FUNCTIONS // + +var rss = require( './rss.js' ), mss = require( './mss.js' ), - tss = require( './tss.js' ); + tss = require( './tss.js' ), + aic = require( './aic.js' ), + aicc = require( './aicc.js' ), + bic = require( './bic.js' ), + rsq = require( './rsquared.js' ), + arsq = require( './adjustedrsquared.js' ), + ll = require( './loglikelihood.js' ); // MODEL // @@ -341,10 +351,10 @@ function createModel( x, y, xmu, ymu, slope, yint ) { summary.tss = tss( y, ymu ); // Coefficient of Determination: - summary.rsq = summary.mss / summary.tss; + summary.rsq = rsq( summary.mss, summary.tss ); // Adjusted coefficient of determination: - summary.arsq = summary.rsq - (1-summary.rsq)*(summary.mdf / summary.rdf); + summary.arsq = arsq( summary.mss, summary.tss, summary.mdf, summary.rdf ); // Sample variance: summary.variance = summary.tss / summary.df; @@ -365,7 +375,16 @@ function createModel( x, y, xmu, ymu, slope, yint ) { summary.rstdev = Math.sqrt( summary.rvariance ); // Log likelihood: - summary.loglikelihood = -N / 2 * ( Math.log( (summary.rss/N) * 2 * Math.PI ) + 1 ); + summary.loglikelihood = ll( N, summary.rss ); + + // Akaike information criterion: + summary.aic = aic( summary.loglikelihood, m ); + + // Corrected Akaike information criterion for finite sample sizes: + summary.aicc = aicc( summary.loglikelihood, N, m ); + + // Bayesian information criterion: + summary.bic = bic( summary.loglikelihood, N, m ); // F statistic: summary.fstat = summary.mvariance / summary.rvariance; diff --git a/lib/rsquared.js b/lib/rsquared.js new file mode 100644 index 0000000..0b4a36a --- /dev/null +++ b/lib/rsquared.js @@ -0,0 +1,45 @@ +/** +* +* COEFFICIENT OF DETERMINATION +* +* +* DESCRIPTION: +* - Computes the coefficient of determination (R squared). +* +* NOTES: +* [1] +* +* +* TODO: +* [1] +* +* +* LICENSE: +* MIT +* +* Copyright (c) 2015. Athan Reines. +* +* +* AUTHOR: +* Athan Reines. kgryte@gmail.com. 2015. +* +*/ + +'use strict'; + +/** +* FUNCTION: rsq( mss, tss ) +* Computes the coefficient of determination. +* +* @param {Number} mss - model sum of squares +* @param {Number} tss - total sum of squares +* @returns {Number} coefficient of determination +*/ +function rsq( mss, tss ) { + return mss / tss; +} // end FUNCTION rsq() + + +// EXPORTS // + +module.exports = rsq;