游戏有点晚了,但这可能对其他人有用。
我建议阅读一下什么是回归以及它们是如何计算的。手动做一些...
https://www.khanacademy.org/math/statistics-probability/describing-relationships-quantitative-data
简短的回答是创建一个包含 x 和 y 值的数组:
for(var i=data.length-1; i>=0 ; i--){
data[i] = [i,data[i]];
}
我看到原始数据的一个大问题是它忽略了“x”。在这种情况下,“x”可能是日期,但是通过将数据存储在数组中,我们假设日期是连续的。考虑到您正在使用的数据,这似乎不太可能:我预计周末和节假日的数据会出现缺口。
基于这个假设,这里有一个例子来说明如何实现你所说的。这在很多类型的数据中都很有用:
debugger;
// get some stock data: daily close prices
var data = [{Date:"2016-09-23",Close:85.81},{Date:"2016-09-22",Close:86.35},...];
// Before we can use this data, we need to format
// it for display and processing. Basically, just
// make sure that everything is in the right order
// and such...
var metrics = {
mindate : moment(data[0].Date),
maxdate : moment(data[0].Date),
};
data.forEach(function(rec){
// cast the date to something more friendly
rec.Date = moment(rec.Date);
// get the min and max
if(metrics.mindate.isAfter (rec.Date)){ metrics.mindate = moment(rec.Date); }
if(metrics.maxdate.isBefore(rec.Date)){ metrics.maxdate = rec.Date; }
});
metrics.days = metrics.maxdate.diff(metrics.mindate,'days');
// One thing you will notice about stock data
// is that the markets are not open on holidays
// this means that we do not have data about
// what the price was during those days, also
// the distance between our datapoints Friday and
// Monday is not 1 day. Some work is necessary to
// make it clear that there is a gap
data.forEach(function(rec){
rec.day = rec.Date.diff(metrics.mindate,'days');
});
// At this point the data is probably ready
// for the regression library, we just need
// to format it correclty
var d = [];
data.forEach(function(rec){
var x = rec.day;
var y = rec.Close;
d.push([x,y]);
});
var result = regression('polynomial', d, 4);
// Now that the regressino has been calculated, we
// can make use of it. First let's determine what
// data was missing from our original dataset
// (basically teh weekends and holidays)
for(var i=data.length-2; i>=0;i--){
for(var day=data[i+1].day-1;day>data[i].day;day--){
data.push({
day:day,
Date:moment(metrics.mindate).add(day,'days'),
Close:null,
Est:null
});
}
}
// While we are at, let's project a couple days into
// the future
var lastday = metrics.days;
for(var day=metrics.days+30; day>=metrics.days; day--){
data.push({
day:day,
Date:moment(metrics.mindate).add(day,'days'),
Close:null,
Est:null
});
}
// for convenience sake, copy it back into our
// original dataset
data.forEach(function(rec){
rec.Est = 0;
for(var i=result.equation.length-1; i>=0; i--){
rec.Est += result.equation[i] * Math.pow(rec.day,i);
}
});
// better sort it at this point
data.sort(function(a,b){
if(a.day < b.day) return -1;
if(a.day > b.day) return 1;
return 0;
});
// Now that processing is complete, we can use
// the data in some manner that is meaningful
// in this case, I display the data with the
// gaps filled in, as well as a projection
data.forEach(function(rec){
$('table#prices').append(
'<tr><td>{{day}}</td><td>{{Date}}</td><td>{{Close}}</td><td>{{Est}}</td></tr>'
.replace(/{{day}}/g,rec.day) .replace(/{{Date}}/g,rec.Date.format('YYYY-MM-DD'))
.replace(/{{Close}}/g,rec.Close || '')
.replace(/{{Est}}/g,rec.Est || '')
);
});
$('pre#est').html(
metrics.mindate.format('YYYY-MM-DD')
+ ' -> ' + metrics.maxdate.format('YYYY-MM-DD')
+ ' (' + metrics.days + ' days)'
+ '\n' + result.string
);
要处理的东西很多,所以这里有一个小提琴:
https://jsfiddle.net/v95evuv8/6/
这取决于regression.js 和moment.js(用于日期操作)
cmets 包含有关 doint this 实用性的问题。在聚合结果集时,我在 CouchDB 中做了类似的事情(减少到回归参数并监控偏离预期的回归),并且还经常在基于 Web 的报告的图表中显示回归线。