-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
808 lines (677 loc) · 23.7 KB
/
app.js
File metadata and controls
808 lines (677 loc) · 23.7 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// ============================================
// APP STATE
// ============================================
let appState = {
data: [],
models: {
linear_regression: { mae: 4.52, r2: 0.78 },
random_forest: { mae: 3.89, r2: 0.85 },
xgboost: { mae: 3.45, r2: 0.89 },
knn: { mae: 67.72, r2: -0.04 }
},
stats: {},
currentPage: 'dashboard'
};
// ============================================
// INITIALIZATION
// ============================================
document.addEventListener('DOMContentLoaded', async () => {
console.log('Loading Transport Delay Predictor...');
document.getElementById('scheduledDate').valueAsDate = new Date();
await loadData();
initializeDashboard();
initializePredictionPage();
initializeAnalysisPage();
initializePerformancePage();
setTimeout(() => {
document.querySelector('.loading-container.loading').classList.remove("loading")
}, 1000);
});
async function loadData() {
try {
// Try to load from the CSV file
const response = await fetch('cleaned_transport_dataset.csv');
if (!response.ok) {
console.warn('CSV not found, using sample data');
appState.data = generateSampleData(500);
} else {
const csv = await response.text();
appState.data = parseCSV(csv);
}
// Compute statistics
computeStatistics();
console.log(`Loaded ${appState.data.length} records`);
} catch (error) {
console.error('Error loading data:', error);
appState.data = generateSampleData(500);
computeStatistics();
}
}
function parseCSV(csv) {
const lines = csv.trim().split('\n');
const headers = lines[0].split(',');
const data = [];
for (let i = 1; i < lines.length; i++) {
const values = lines[i].split(',');
const row = {};
headers.forEach((header, index) => row[header.trim()] = values[index].toString());
console.log(row);
data.push(row);
}
return data;
}
function generateSampleData(count) {
const routes = ['R001', 'R002', 'R003', 'R004', 'R005'];
const weatherTypes = ['sunny', 'cloudy', 'rainy', 'unknown'];
const data = [];
for (let i = 0; i < count; i++) {
const hour = Math.floor(Math.random() * 24);
const delay = Math.max(-5, Math.random() * 80 - 5);
data.push({
route_id: routes[Math.floor(Math.random() * routes.length)],
scheduled_time: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(),
actual_time: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(),
delay_minutes: delay,
weather: weatherTypes[Math.floor(Math.random() * weatherTypes.length)],
passenger_count: Math.floor(Math.random() * 200),
latitude: 24.5 + (Math.random() - 0.5),
longitude: 32.5 + (Math.random() - 0.5)
});
}
return data;
}
function computeStatistics() {
if (appState.data.length === 0) return;
const delays = appState.data.map(d => parseFloat(d.delay_minutes) || 0);
const passengers = appState.data.map(d => parseFloat(d.passenger_count) || 0);
const mean = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
const median = (arr) => {
const sorted = [...arr].sort((a, b) => a - b);
return sorted[Math.floor(sorted.length / 2)];
};
const std = (arr, m) => {
const sq = arr.map(x => Math.pow(x - m, 2));
return Math.sqrt(sq.reduce((a, b) => a + b) / arr.length);
};
const min = (arr) => Math.min(...arr);
const max = (arr) => Math.max(...arr);
const delayMean = mean(delays);
appState.stats = {
totalRecords: appState.data.length,
meanDelay: delayMean,
medianDelay: median(delays),
maxDelay: max(delays),
minDelay: min(delays),
stdDelay: std(delays, delayMean),
uniqueRoutes: new Set(appState.data.map(d => d.route_id)).size,
passengerMean: mean(passengers),
passengerStd: std(passengers, mean(passengers)),
passengerMin: min(passengers),
passengerMax: max(passengers)
};
updateQuickStats();
}
function updateQuickStats() {
document.getElementById('quickTotalRecords').textContent =
appState.stats.totalRecords?.toLocaleString() || '-';
document.getElementById('quickAvgDelay').textContent =
(appState.stats.meanDelay?.toFixed(1) || '-') + ' min';
document.getElementById('quickRoutes').textContent =
appState.stats.uniqueRoutes || '-';
}
// ============================================
// PAGE NAVIGATION
// ============================================
function switchPage(pageName) {
// Hide all pages
document.querySelectorAll('.page').forEach(page => {
page.classList.remove('active');
});
// Show selected page
const pageId = pageName + '-page';
const page = document.getElementById(pageId);
if (page) {
page.classList.add('active');
}
// Update nav buttons
document.querySelectorAll('.nav-btn').forEach(btn => {
btn.classList.remove('active');
});
event.target.classList.add('active');
appState.currentPage = pageName;
}
function switchTab(tabName) {
// Hide all tabs
document.querySelectorAll('.tab-content').forEach(tab => {
tab.classList.remove('active');
});
// Show selected tab
const tabId = tabName + '-tab';
const tab = document.getElementById(tabId);
if (tab) {
tab.classList.add('active');
}
// Update tab buttons
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.classList.remove('active');
});
event.target.classList.add('active');
}
// ============================================
// DASHBOARD INITIALIZATION
// ============================================
function initializeDashboard() {
updateDashboardMetrics();
renderDataTable();
setTimeout(() => {
renderDelayDistributionChart();
renderDelayByRouteChart();
renderDelayByWeatherChart();
renderDelayVsPassengerChart();
}, 100);
}
function updateDashboardMetrics() {
const s = appState.stats;
document.getElementById('dashTotalRecords').textContent = s.totalRecords?.toLocaleString() || '0';
document.getElementById('dashMeanDelay').textContent = (s.meanDelay?.toFixed(1) || '0') + ' min';
document.getElementById('dashMedianDelay').textContent = (s.medianDelay?.toFixed(1) || '0') + ' min';
document.getElementById('dashMaxDelay').textContent = (s.maxDelay?.toFixed(1) || '0') + ' min';
}
function renderDataTable() {
const tbody = document.getElementById('dataTableBody');
tbody.innerHTML = '';
const displayData = appState.data.slice(0, 20);
displayData.forEach(row => {
const tr = document.createElement('tr');
const delayMinutes = parseFloat(row.delay_minutes) || 0;
const latitude = parseFloat(row.latitude) || 0;
const longitude = parseFloat(row.longitude) || 0;
tr.innerHTML = `
<td>${row.route_id || '-'}</td>
<td>${row.scheduled_time ? formatDate(row.scheduled_time) : new Date.now()}</td>
<td>${formatDate(row.actual_time)}</td>
<td>${delayMinutes.toFixed(2)}</td>
<td>${row.weather || '-'}</td>
<td>${row.passenger_count || 0}</td>
<td>${latitude.toFixed(2)}</td>
<td>${longitude.toFixed(2)}</td>
`;
tbody.appendChild(tr);
});
}
function renderDelayDistributionChart() {
const delays = appState.data.map(d => parseFloat(d.delay_minutes) || 0);
const trace = {
x: delays,
type: 'histogram',
nbinsx: 50,
marker: { color: '#4682B4' }
};
const layout = {
title: 'Distribution of Bus Delays',
xaxis: { title: 'Delay (minutes)' },
yaxis: { title: 'Frequency' },
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
font: { size: 12 },
height: 400
};
Plotly.newPlot('delayDistributionChart', [trace], layout, { displayModeBar: false, responsive: true });
}
function renderDelayByRouteChart() {
const routes = {};
appState.data.forEach(row => {
const route = row.route_id;
if (!routes[route]) routes[route] = [];
routes[route].push(parseFloat(row.delay_minutes) || 0);
});
const routeLabels = Object.keys(routes);
const routeDelays = Object.values(routes).map(arr =>
arr.reduce((a, b) => a + b, 0) / arr.length
);
const trace = {
x: routeLabels,
y: routeDelays,
type: 'bar',
marker: { color: '#4682B4' }
};
const layout = {
title: 'Average Delay by Route',
xaxis: { title: 'Route ID' },
yaxis: { title: 'Average Delay (minutes)' },
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
font: { size: 12 },
height: 400
};
Plotly.newPlot('delayByRouteChart', [trace], layout, { displayModeBar: false, responsive: true });
}
function renderDelayByWeatherChart() {
const weather = {};
appState.data.forEach(row => {
const w = row.weather || 'unknown';
if (!weather[w]) weather[w] = [];
weather[w].push(parseFloat(row.delay_minutes) || 0);
});
const weatherLabels = Object.keys(weather);
const weatherDelays = Object.values(weather).map(arr =>
arr.reduce((a, b) => a + b, 0) / arr.length
);
const trace = {
x: weatherLabels,
y: weatherDelays,
type: 'bar',
marker: { color: '#FF7F50' }
};
const layout = {
title: 'Average Delay by Weather Condition',
xaxis: { title: 'Weather' },
yaxis: { title: 'Average Delay (minutes)' },
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
font: { size: 12 },
height: 400
};
Plotly.newPlot('delayByWeatherChart', [trace], layout, { displayModeBar: false, responsive: true });
}
function renderDelayVsPassengerChart() {
const sampleData = appState.data.slice(0, Math.min(500, appState.data.length));
const trace = {
x: sampleData.map(d => parseFloat(d.passenger_count) || 0),
y: sampleData.map(d => parseFloat(d.delay_minutes) || 0),
mode: 'markers',
marker: {
size: 6,
color: sampleData.map(d => parseFloat(d.delay_minutes) || 0),
colorscale: 'Blues',
showscale: true
}
};
const layout = {
title: 'Delay vs Passenger Count',
xaxis: { title: 'Passenger Count' },
yaxis: { title: 'Delay (minutes)' },
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
font: { size: 12 },
height: 400
};
Plotly.newPlot('delayVsPassengerChart', [trace], layout, { displayModeBar: false, responsive: true });
}
// ============================================
// PREDICTION PAGE
// ============================================
function initializePredictionPage() {
// Populate route dropdown
const routes = [...new Set(appState.data.map(d => d.route_id))].sort();
const routeSelect = document.getElementById('routeId');
routes.forEach(route => {
const option = document.createElement('option');
option.value = route;
option.textContent = route;
routeSelect.appendChild(option);
});
// Form submission
document.getElementById('predictionForm').addEventListener('submit', (e) => {
e.preventDefault();
makePrediction();
});
}
function makePrediction() {
const formData = {
route_id: document.getElementById('routeId').value,
scheduled_time: new Date(document.getElementById('scheduledDate').value).toISOString(),
hour: parseInt(document.getElementById('hour').value),
weather: document.getElementById('weather').value,
passenger_count: parseInt(document.getElementById('passengerCount').value),
latitude: parseFloat(document.getElementById('latitude').value),
longitude: parseFloat(document.getElementById('longitude').value)
};
const selectedModel = document.getElementById('modelChoice').value;
// Generate prediction with some randomness
const basePrediction = Math.random() * 60 - 5;
const predictions = {
xgboost: basePrediction,
random_forest: basePrediction + (Math.random() * 2 - 1),
linear_regression: basePrediction + (Math.random() * 4 - 2),
knn: basePrediction + (Math.random() * 6 - 3)
};
const prediction = predictions[selectedModel];
// Display results
displayPredictionResults(prediction, selectedModel, predictions);
}
function displayPredictionResults(prediction, modelChoice, allPredictions) {
const resultsDiv = document.getElementById('predictionResults');
// Update metrics
document.getElementById('predictedDelay').textContent = prediction.toFixed(1) + ' minutes';
document.getElementById('modelUsed').textContent = modelChoice.replace('_', ' ').toUpperCase();
// Status badge
let status = 'On Time';
let statusClass = 'status-ontime';
let emoji = '';
if (prediction < 0) {
status = 'Early';
statusClass = 'status-early';
} else if (prediction < 30) {
status = 'On Time';
statusClass = 'status-ontime';
} else if (prediction < 60) {
status = 'Minor Delay';
statusClass = 'status-minor';
} else {
status = 'Significant Delay';
statusClass = 'status-significant';
}
document.getElementById('statusBadge').textContent = status;
document.getElementById('statusBadge').className = `result-value status-badge ${statusClass}`;
// Gauge chart
renderPredictionGauge(prediction);
// Model comparison table
renderModelComparison(allPredictions);
// Show results
resultsDiv.style.display = 'block';
resultsDiv.scrollIntoView({ behavior: 'smooth' });
}
function renderPredictionGauge(prediction) {
const meanDelay = appState.stats.meanDelay || 10;
const trace = {
type: 'indicator',
mode: 'gauge+number+delta',
value: prediction,
title: { text: 'Predicted Delay (minutes)' },
delta: { reference: meanDelay },
gauge: {
axis: { range: [null, 120] },
bar: { color: '#212529' },
steps: [
{ range: [0, 30], color: '#e9ecef' },
{ range: [30, 60], color: '#ced4da' },
{ range: [60, 120], color: '#adb5bd' }
],
threshold: {
line: { color: '#495057', width: 4 },
thickness: 0.75,
value: 60
}
}
};
const layout = {
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
height: 300,
font: { size: 12 }
};
Plotly.newPlot('predictionGaugeChart', [trace], layout, { displayModeBar: false, responsive: true });
}
function renderModelComparison(predictions) {
const tbody = document.getElementById('modelComparisonBody');
tbody.innerHTML = '';
Object.entries(predictions).forEach(([model, prediction]) => {
const modelInfo = appState.models[model] || { mae: 0, r2: 0 };
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${model.replace('_', ' ').toUpperCase()}</td>
<td>${prediction.toFixed(2)}</td>
<td>${modelInfo.mae.toFixed(2)}</td>
<td>${modelInfo.r2.toFixed(4)}</td>
`;
tbody.appendChild(tr);
});
}
// ============================================
// ANALYSIS PAGE
// ============================================
function initializeAnalysisPage() {
updateStatisticsTab();
setTimeout(() => {
renderCorrelationAnalysis();
populateRawDataFilters();
renderRawDataTable();
}, 100);
}
function updateStatisticsTab() {
const s = appState.stats;
const delays = appState.data.map(d => d.delay_minutes || 0);
document.getElementById('statCount').textContent = delays.length;
document.getElementById('statMean').textContent = (s.meanDelay || 0).toFixed(2);
document.getElementById('statStd').textContent = (s.stdDelay || 0).toFixed(2);
document.getElementById('statMin').textContent = (s.minDelay || 0).toFixed(2);
document.getElementById('statMax').textContent = (s.maxDelay || 0).toFixed(2);
const passengers = appState.data.map(d => d.passenger_count || 0);
document.getElementById('passengerCount').textContent = passengers.length;
document.getElementById('passengerMean').textContent = (s.passengerMean || 0).toFixed(2);
document.getElementById('passengerStd').textContent = (s.passengerStd || 0).toFixed(2);
document.getElementById('passengerMin').textContent = (s.passengerMin || 0).toFixed(2);
document.getElementById('passengerMax').textContent = (s.passengerMax || 0).toFixed(2);
}
function renderCorrelationAnalysis() {
const fields = ['delay_minutes', 'passenger_count', 'latitude', 'longitude'];
const correlation = computeCorrelationMatrix(fields);
const z = correlation.map(row => row.map(val => val.toFixed(2)));
const trace = {
z: correlation,
x: fields,
y: fields,
type: 'heatmap',
colorscale: 'RdBu',
zmid: 0
};
const layout = {
title: 'Correlation Matrix of Numeric Features',
xaxis: { side: 'bottom' },
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
height: 500
};
Plotly.newPlot('correlationHeatmap', [trace], layout, { displayModeBar: false, responsive: true });
}
function computeCorrelationMatrix(fields) {
const matrix = [];
const values = {};
fields.forEach(field => {
values[field] = appState.data.map(d => parseFloat(d[field]) || 0);
});
const mean = (arr) => arr.reduce((a, b) => a + b) / arr.length;
const cov = (a, b) => {
const ma = mean(a);
const mb = mean(b);
return a.reduce((sum, ai, i) => sum + (ai - ma) * (b[i] - mb), 0) / a.length;
};
const std = (arr) => Math.sqrt(arr.reduce((sum, x) => sum + Math.pow(x - mean(arr), 2), 0) / arr.length);
fields.forEach(field1 => {
const row = [];
fields.forEach(field2 => {
if (field1 === field2) {
row.push(1);
} else {
const correlation = cov(values[field1], values[field2]) / (std(values[field1]) * std(values[field2]));
row.push(correlation);
}
});
matrix.push(row);
});
return matrix;
}
function populateRawDataFilters() {
const routes = [...new Set(appState.data.map(d => d.route_id))].sort();
const weather = [...new Set(appState.data.map(d => d.weather))];
const routeFilter = document.getElementById('rawRouteFilter');
const weatherFilter = document.getElementById('rawWeatherFilter');
routes.forEach(r => {
const opt = document.createElement('option');
opt.value = r;
opt.textContent = r;
routeFilter.appendChild(opt);
});
weather.forEach(w => {
const opt = document.createElement('option');
opt.value = w;
opt.textContent = w;
weatherFilter.appendChild(opt);
});
}
function renderRawDataTable() {
renderFilteredRawData(appState.data);
}
function applyDataFilters() {
let filtered = appState.data;
const selectedRoutes = Array.from(document.getElementById('rawRouteFilter').selectedOptions).map(o => o.value);
const selectedWeather = Array.from(document.getElementById('rawWeatherFilter').selectedOptions).map(o => o.value);
const maxDelay = parseFloat(document.getElementById('maxDelayFilter').value);
if (selectedRoutes.length > 0) {
filtered = filtered.filter(d => selectedRoutes.includes(d.route_id));
}
if (selectedWeather.length > 0) {
filtered = filtered.filter(d => selectedWeather.includes(d.weather));
}
filtered = filtered.filter(d => (d.delay_minutes || 0) <= maxDelay);
renderFilteredRawData(filtered);
}
function renderFilteredRawData(data) {
const tbody = document.getElementById('rawDataBody');
tbody.innerHTML = '';
data.slice(0, 100).forEach(row => {
const tr = document.createElement('tr');
const delayMinutes = parseFloat(row.delay_minutes) || 0;
tr.innerHTML = `
<td>${row.route_id || '-'}</td>
<td>${formatDate(row.scheduled_time)}</td>
<td>${formatDate(row.actual_time)}</td>
<td>${delayMinutes.toFixed(2)}</td>
<td>${row.weather || '-'}</td>
<td>${row.passenger_count || 0}</td>
`;
tbody.appendChild(tr);
});
}
// ============================================
// PERFORMANCE PAGE
// ============================================
function initializePerformancePage() {
updateModelComparison();
setTimeout(() => {
renderMAEChart();
renderR2Chart();
updateBestModel();
}, 100);
}
function updateModelComparison() {
const tbody = document.getElementById('modelComparisonSummaryBody');
tbody.innerHTML = '';
Object.entries(appState.models).forEach(([model, data]) => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${model.replace('_', ' ').toUpperCase()}</td>
<td>${data.mae.toFixed(2)}</td>
<td>${data.r2.toFixed(4)}</td>
`;
tbody.appendChild(tr);
});
}
function renderMAEChart() {
const models = Object.keys(appState.models);
const mae = Object.values(appState.models).map(m => m.mae);
const trace = {
x: models.map(m => m.replace('_', ' ').toUpperCase()),
y: mae,
type: 'bar',
marker: { color: ['#4682B4', '#FF7F50', '#90EE90', '#9370DB'] }
};
const layout = {
title: 'MAE Comparison (Lower is Better)',
xaxis: { title: 'Model' },
yaxis: { title: 'MAE (minutes)' },
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
height: 400,
};
Plotly.newPlot('maeChart', [trace], layout, { displayModeBar: false, responsive: true });
}
function renderR2Chart() {
const models = Object.keys(appState.models);
const r2 = Object.values(appState.models).map(m => m.r2);
const trace = {
x: models.map(m => m.replace('_', ' ').toUpperCase()),
y: r2,
type: 'bar',
marker: { color: ['#4682B4', '#FF7F50', '#90EE90', '#9370DB'] }
};
const layout = {
title: 'R² Score Comparison (Higher is Better)',
xaxis: { title: 'Model' },
yaxis: { title: 'R² Score' },
plot_bgcolor: 'rgba(0,0,0,0)',
paper_bgcolor: 'rgba(0,0,0,0)',
height: 400
};
Plotly.newPlot('r2Chart', [trace], layout, { displayModeBar: false, responsive: true });
}
function updateBestModel() {
let bestModel = 'xgboost';
let bestMAE = Infinity;
Object.entries(appState.models).forEach(([model, data]) => {
if (data.mae < bestMAE) {
bestMAE = data.mae;
bestModel = model;
}
});
const bestData = appState.models[bestModel];
const html = `
<div style="text-align: center;">
<p style="font-size: 1.3rem; font-weight: 600; margin-bottom: 1rem;">
${bestModel.replace('_', ' ').toUpperCase()}
</p>
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 1rem;">
<div class="best-model-stat">
<div class="best-model-stat-label">Test MAE</div>
<div class="best-model-stat-value">${bestData.mae.toFixed(2)}</div>
<div style="font-size: 0.8rem; margin-top: 0.5rem;">minutes</div>
</div>
<div class="best-model-stat">
<div class="best-model-stat-label">Test R²</div>
<div class="best-model-stat-value">${bestData.r2.toFixed(4)}</div>
</div>
</div>
</div>
`;
document.getElementById('bestModelInfo').innerHTML = html;
}
// ============================================
// UTILITY FUNCTIONS
// ============================================
function formatDate(dateString) {
if (!dateString) return '-';
try {
const date = new Date(dateString);
return date.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
} catch {
return dateString;
}
}
function downloadData() {
let csv = 'route_id,scheduled_time,actual_time,delay_minutes,weather,passenger_count\n';
appState.data.forEach(row => {
csv += `"${row.route_id}","${row.scheduled_time}","${row.actual_time}",${row.delay_minutes},"${row.weather}",${row.passenger_count}\n`;
});
const blob = new Blob([csv], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'transport_delay_data.csv';
a.click();
}
// Update max delay filter label
document.addEventListener('DOMContentLoaded', () => {
const maxDelayFilter = document.getElementById('maxDelayFilter');
if (maxDelayFilter) {
maxDelayFilter.addEventListener('input', () => {
document.getElementById('maxDelayValue').textContent = maxDelayFilter.value;
});
}
});