Homework
# MBA 264 Project
# Problem 21.10
# Forcasting number of passengers ariving at the terminal
# Library
library(forecast)
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff
ride.df = read.csv("/Users/moneill2072/Desktop/MBA 264/bicup2006.csv", header = TRUE)
ride.ts = ts(ride.df$DEMAND,
start= c(0), end= c(24),
frequency = 63)
nValid = 631
nTraining = length(ride.ts)-nValid
train.ts = window(ride.ts, start= c(0), end = c(0,nTraining))
valid.ts = window(ride.ts, start = c(0, nTraining+1), end = c(0,nTraining+nValid))
train.lm.season = tslm(train.ts~season)
train.lm.trend.season = tslm(train.ts ~ trend + I(trend ^2) + season)
train.trend.season.pred = forecast(train.lm.trend.season, h=nValid, level = 0)
accuracy(train.trend.season.pred,valid.ts)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 4.027312e-16 18.15494 13.46820 NaN Inf 0.907931 0.9174838
## Test set 1.287161e+00 20.06739 15.56824 NaN Inf 1.049501 0.8764193
## Theil's U
## Training set NA
## Test set NaN
print(train.trend.season.pred$mean[c(442:631)])
## 442 443 444 445 446 447 448
## 3.001950 2.817019 4.060660 4.875729 8.690798 10.291582 13.749508
## 449 450 451 452 453 454 455
## 12.136006 13.736790 12.837573 13.152642 17.181997 16.854209 16.312136
## 456 457 458 459 460 461 462
## 16.698634 18.727988 19.828772 22.572413 23.530339 24.631123 24.874763
## 463 464 465 466 467 468 469
## 27.761261 31.433473 32.248542 32.777897 37.235824 36.265179 37.223105
## 470 471 472 473 474 475 476
## 38.395317 39.496101 36.454027 38.411953 31.084165 29.256377 28.428590
## 477 478 479 480 481 482 483
## 29.029373 26.844442 26.373797 25.760295 25.218221 26.890434 26.134074
## 484 485 486 487 488 489 490
## 30.234858 34.978498 36.079282 40.751494 44.852277 59.310204 60.839559
## 491 492 493 494 495 496 497
## 65.440342 58.898269 56.427624 47.028407 46.057762 41.158546 36.473615
## 498 499 500 501 502 503 504
## 32.860113 26.746610 22.704537 22.519606 16.334675 11.006887 7.107671
## 505 506 507 508 509 510 511
## 4.145748 3.962842 5.208507 6.025600 9.842694 11.445502 14.905453
## 512 513 514 515 516 517 518
## 13.293975 14.896783 13.999591 14.316685 18.348064 18.022301 17.482252
## 519 520 521 522 523 524 525
## 17.870774 19.902153 21.004961 23.750627 24.710577 25.813385 26.059051
## 526 527 528 529 530 531 532
## 28.947573 32.621809 33.438903 33.970283 38.430233 37.461613 38.421564
## 533 534 535 536 537 538 539
## 39.595800 40.698608 37.658559 39.618510 32.292746 30.466983 29.641220
## 540 541 542 543 544 545 546
## 30.244028 28.061121 27.592501 26.981023 26.440974 28.115210 27.360876
## 547 548 549 550 551 552 553
## 31.463684 36.209349 37.312157 41.986393 46.089201 60.549152 62.080532
## 554 555 556 557 558 559 560
## 66.683340 60.143290 57.674670 48.277478 47.308857 42.411665 37.728759
## 561 562 563 564 565 566 567
## 34.117281 28.005803 23.965754 23.782848 17.599942 12.274178 8.376986
## 568 569 570 571 572 573 574
## 5.417088 5.236206 6.483896 7.303014 11.122132 12.726964 16.188940
## 575 576 577 578 579 580 581
## 14.579486 16.184319 15.289151 15.608270 19.641673 19.317935 18.779910
## 582 583 584 585 586 587 588
## 19.170457 21.203861 22.308693 25.056383 26.018358 27.123190 27.370880
## 589 590 591 592 593 594 595
## 30.261427 33.937688 34.756806 35.290210 39.752185 38.785589 39.747564
## 596 597 598 599 600 601 602
## 40.923826 42.028658 38.990633 40.952609 33.628870 31.805131 30.981392
## 603 604 605 606 607 608 609
## 31.586224 29.405342 28.938746 28.329293 27.791268 29.467529 28.715219
## 610 611 612 613 614 615 616
## 32.820052 37.567741 38.672574 43.348835 47.453667 61.915643 63.449046
## 617 618 619 620 621 622 623
## 68.053879 61.515854 59.049258 49.654091 48.687494 43.792327 39.111445
## 624 625 626 627 628 629 630
## 35.501992 29.392539 25.354514 25.173632 18.992750 13.669011 9.773844
## 631
## 6.815970
#Forecast Line
plot(train.trend.season.pred,ylim = c(0, 160), ylab = "Passengers",
xlab = "Time", bty= "l", xaxt = "n",
xlim= c(0,24), main = "2005 Public Transportation Demand", cex= 2, flty = 2, fcol = "blue")
axis(1, at = seq(0,23),labels = format(as.factor(unique(ride.df$DATE))), las=2)
#Training Line
lines(train.lm.trend.season$fitted.values, lwd= 2, col="blue")
#Validation Line
lines(valid.ts)
abline(v=14, col="red", lwd=3)
abline(v=21, col="red",lwd=3)
text(7,155,"Training", cex = 1.25)
text(17.5,155,"Validation", cex = 1.25)
text(22.5,155,"Future", cex = 1.25)
arrows(0, 145, 13, 145, code = 3, length = 0.1, lwd = 1, angle = 30)
arrows(15, 145, 20, 145, code = 3, length = 0.1, lwd = 1, angle = 30)
arrows(21.5, 145, 23.5, 145, code = 3, length = 0.1, lwd = 1, angle = 30)