Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Probability density function with javascript d3 and dc

I'm trying to create a probability density function of a dataset using Javascript, (d3 and dc libraries) (similar to density function of R) but I have not found how can I do it.

Can it be done?

Thanks

PD: With jqplot is this: http://services.mbi.ucla.edu/jqplot/examples/kcp_pdf.html

like image 508
fhuertas Avatar asked Nov 22 '25 00:11

fhuertas


1 Answers

Finally, I have to be able to do with the KDE algorithm (implemented in science.js library)

How ever the library dc need that the data is adapted. When the data is reduced in a object with all appearances and its frequency, the object must be transformed to an array with a entry for each appearance. The groups is needed because dc need a group to paint.

My solutions has been:

// Creating the group
var group = dimension.group().reduceSum();
......
// Process the object. 
groups = group.all()
var newValues = []
for (var i = 0; i < groups.length; i++) {
    for (var j = 0; j < groups[i].value;j++){
        if (groups[i].key == "null" ||
            groups[i].key == null ||
            (groups[i].key - start < 0) ||
            (groups[i].key - end > 0)) {
        } else {
            newValues.push(parseFloat(groups[i].key))
        }
    }
}
.....
// Creating the new data that represent a density function. 
var kde = science.stats.kde().sample(newValues);
// I have replaced the bandwidth method (Multivariate Density Estimation)
// because this method works better than previous (Density Estimation) 
// min and max are calculated previously 
kde.bandwidth(science.stats.bandwidth.nrd0);
var frequency = Math.abs(parseFloat(max) - parseFloat(min)) / 512
var newData = kde(d3.range(min,max,frequency));

.....
// The array that is inside of the group is a reference (obviously but it is important)
// Deleting the contains of the array
groups.splice(0,groups.length) 
// add the new data
for (var key in newData) {
    groups.push({key:newData[key][0],value:newData[key][2]
})
like image 66
fhuertas Avatar answered Nov 23 '25 15:11

fhuertas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!