fredrick / gauss

JavaScript statistics, analytics, and data library - Node.js and web browser ready
https://npmjs.org/package/gauss
Other
433 stars 29 forks source link

Treating a collection's element as a vector #19

Closed jetpad closed 10 years ago

jetpad commented 10 years ago

I've just started looking at your gauss project. I have an array of properties that I'm thinking I'd put into a collection. I'd then like to filter that collection. Next, I'd like to treat one of those properties in the filtered collection as a vector and perform a calculation it.

For the collection below, I'd like to filter by 'series' and then calculate the mean on 'latencymsec'.

trials.find({ series: 1 }).vector("latencymsec").mean();

The .vector() function would create a vector of that single element from that filtered collection and then pass it on to .mean(). I searched but it does seem like something like that already exists in gauss to convert a collection into a vector. Or maybe there is an easy javascript way to do it that I'm missing. Does that seem like a good way to implement it?

var trials = new Collection( { "series": 1, "minimumTime": 100, "maximumTime": 2000, "phase": "result", "correction": false, "timeWaited": 1500, "delayPromise": null, "include": true, "problem": "1", "startTime": "2014-02-27T18:18:43.339Z", "answer": "5", "endTime": "2014-02-27T18:18:43.759Z", "latencymsec": 420, "correct": true }, { "series": 1, "minimumTime": 100, "maximumTime": 2000, "phase": "result", "correction": false, "timeWaited": 1500, "delayPromise": null, "include": true, "problem": "8", "startTime": "2014-02-27T18:18:48.064Z", "answer": "5", "endTime": "2014-02-27T18:18:48.805Z", "latencymsec": 741, "correct": true }, .......

fredrick commented 10 years ago

You're on the right path, there is already a way to do this:

trials
  .find({ series: 1 })
  .map(function(t) { return t.latencymsec; })
  .toVector()
  .mean();
jetpad commented 10 years ago

Thanks, I learned something! I think that is really powerful aspect of your package. It's good that you've highlighted it in the readme and tests.

fredrick commented 10 years ago

You're welcome! Thanks for taking a look at Gauss.

jetpad commented 10 years ago

Oh and I discovered that if I create my trials as a vector then I can eliminate that toVector step.

var trials = new Vector( { ... } ); trials .find({ series: 1 }) .map(function(t) { return t.latencymsec; }) .mean();

jetpad commented 10 years ago

I was able to create my trials as a vector to eliminate the toVector step and it is working fine like that when run it as one of the tests (through node), but when I run that same code in the browser (Chrome and Safari) it says the object has no method 'find'. Something about the inheritance of the Collection methods by the Vector objects isn't quite right in the browser. I narrowed it down to the gauss.min.js file. If I include the unminimized collection.js, vector.js and timeseries.js files in my project, the error does not occur. If I replace them with the single gauss.min.js file, the error returns.

var Vector = gauss.Vector; var Collection = gauss.Collection; var things = new Collection( { type: 1, age: 1 }, { type: 2, age: 2 }, { type: 1, age: 3 }, { type: 2, age: 4 }); var things2 = new Vector( { type: 1, age: 1 }, { type: 2, age: 2 }, { type: 1, age: 3 }, { type: 2, age: 4 }); things .find({ type: 2 }) .map(function(thing) { return thing.age; }) .toVector() .sum(); things2 .find({ type: 2 }) // ** find is undefined *** .map(function(thing) { return thing.age; }) .toVector() .sum();

fredrick commented 10 years ago

Are you using the latest gauss.min.js? Also, what version of each respective browser are you using? I cannot reproduce on Chrome Version 33.0.1750.146 m

jetpad commented 10 years ago

Sorry, you are right. I was comparing version 0.2.9 of gauss.min.js to version 0.2.11 of the individual files. I tracked down what was throwing me off. I was using bower to add gauss to my project. "gauss" in bower points to a forked repository https://github.com/samcday/gauss instead of yours and is only up to release 0.2.9. I think https://github.com/bower/bower/issues/120 is where you can submit a request to get them to point it directly to your repository.

fredrick commented 10 years ago

David, no worries. Unfortunately, I have already submitted that request with Bower. In the meantime, I will get in touch with @samcday to push up tags to his fork.

fredrick commented 10 years ago

Tags have been pushed to Sam Day's fork, so Bower should work as expected now. Sam was also nice enough to give me contributor rights to his fork until Bower updates the package.

jetpad commented 10 years ago

Thanks. Just tried updating it via Bower and it fixed the problem I was having.