-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlisting-5.12.js
More file actions
34 lines (32 loc) · 1.36 KB
/
listing-5.12.js
File metadata and controls
34 lines (32 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"use strict";
const dataForge = require('data-forge');
const formulajs = require('formulajs');
dataForge.readFile("./data/monthly_crashes-cut-down.csv")
.parseCSV()
.then(dataFrame => {
dataFrame = dataFrame
.parseFloats(["Month#", "Year", "Crashes", "Fatalities", "Hospitalized"])
.setIndex("Month#");
const fatalitiesSeries = dataFrame.getSeries("Fatalities");
const fatalitiesSeriesWithForecast = fatalitiesSeries.rollingWindow(6) // Returns a series of windows.
.select(window => { // Transform each window into a forecast indexed by month#.
const fatalitiesValues = window.toArray();
const monthNoValues = window.getIndex().toArray();
const nextMonthNo = monthNoValues[monthNoValues.length-1] + 1;
return [
nextMonthNo,
formulajs.FORECAST(nextMonthNo, fatalitiesValues, monthNoValues)
];
})
.withIndex(pair => pair[0])
.select(pair => pair[1]);
const dataFrameWithForecast = dataFrame.withSeries({
Trend: fatalitiesSeriesWithForecast
});
return dataFrameWithForecast
.asCSV()
.writeFile("./output/trend_output.csv");
})
.catch(err => {
console.error(err && err.stack || err);
});