datakit
About
A lightweight library/framework for data analysis in JavaScript.
Check out this blog post on the need for more JavaScript data tools.
Usage
npm install datakitjs --save
Documentation & Examples
Reading, Filtering, & Plotting Data
var dk = ; //READ A CSV FILE //file.csv// COL1, COL2// val11, val12// val21, val22 dk; //Output://[{ COL1: val11, COL2: val12 }, { COL1: val21, COL2: val22 }] //GET A COLUMN FROM AN ARRAY OF ROW OBJECTSdk; //Output://[val12, val22] // By default, dk.csv will convert all values to strings. You can convert select// columns to numbers by passing an array of column names to 'dk.numeric'. //file2.csv// COL1, COL2// val11, 1// val21, 2 dk; //Output://[1, 2] //PLOT ARRAY(S) OF DATA var chart = //optional config height: 500 width: 500 xLab: 'x-Axis Label' yLab: 'y-Axis Label'; chart;
Statistical Methods
var dk = ; //MEAN OF AN ARRAYdk; //returns 2 //STANDARD DEVIATION AND VARIANCE OF AN ARRAYdk; //returns 1dk; //returns 1 //COVARIANCE OF TWO ARRAYSdk; //returns -1 //SIMPLE LINEAR REGRESSION var x = 1 2 3;var y = 2 1 3; var model = dk; // model.f is a function that returns the estimated y for an input x (estimated via standard OLS regression)// model.f = function(x) {// return (a + b * x);// }; // model.pts is an array of the estimated y for each element of x// model.pts = [1.5, 2, 2.5]; // model.endPoints is an object with the coordinates of the boundary points// model.endPoints = { x1: 1, x2: 3, y1: 1.5, y2: 2.5 };
Convenience Methods
var dk = ; //GENERATE AN ARRAY WITH A SEQUENCE OF NUMBERS dk; //returns [1, 2, 3, 4, 5] dk; //returns [0, 0.25, 0.5, 0.75, 1] //GENERATE AN ARRAY WITH REPEATED VALUE dk; //returns [1, 1, 1, 1, 1] //CHECK IF NUMBERS ARE CLOSEdk; //returns true dk; //returns false //SUM AN ARRAY OF NUMBERS//uses Kahan summation dk; //returns 6 //PRODUCT OF AN ARRAY OF NUMBERS//implementation from 'Accurate Floating Point Product' - Stef Graillat dk; //returns 6 //MAX AND MIN OF AN ARRAYvar x = 1 2 3;dk; //returns 1dk; //returns 3
Random Numbers
var dk = ; //GET AN ARRAY OF EXPONENTIALLY DISTRIBUTED VALUES dk; //returns [0.3584189321510761, 1.0466439500242446, 0.08887770301056963] //GET AN ARRAY OF NORMALLY DISTRIBUTED VALUES dk; //returns [-1.709768103193772, 0.23530041388459744, 0.4431320382580479] //GET AN ARRAY OF UNIFORMLY DISTRIBUTED VALUES dk; //returns [0.30658303829841316, 0.1601463456172496, 0.8538850131444633]
Testing
Just run npm test
to run the tests.
Contributing
Additional methods for random number generation, data filtration, convenience functions, and common statistical analyses are welcome additions. Just add tests following the structure in spec/test/testSpec.js
.
License
The MIT License (MIT)
Copyright (c) 2015 Nathan Epstein
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.