-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast_London.R
60 lines (43 loc) · 1.87 KB
/
forecast_London.R
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
# Time series ARIMA models with NHS England data
# Created by Dr Pablo N Perez-Guzman
# Imperial College London, 2020
library(tidyverse)
library(forecast)
library(ggplot2)
library(tseries)
### 1. Load the data for analysis
data <- read.csv("timeseries.csv")
london <- data %>% filter(str_detect(region,"London"))
london_past <- london %>% filter(as.numeric(date) < 56)
london_present <- london %>% filter(as.numeric(date) >= 56)
## 2. Create time series object and check stationarity and differencing
# Historic data
london_ats <- ts(as.vector(tapply(london_past$type1_attendances, london_past$date, FUN=sum)),
frequency = 12,start = c(2015,6))
london.diff <- diff(london_ats)
# Stationarity and differencing
par(mfrow=c(1,2))
plot(london_ats)
plot(london.diff)
par(mfrow=c(1,1))
# Augmented Dickey-Fuller test
adf.test(!is.na(london_ats),alternative="stationary",k=0) # Undifferenciated data is stationary
# KPSS unit root test to determine if differencing is required
library(urca)
london_ats %>% ur.kpss() %>% summary() # p non significant; differencing not required
# Current year observed data
london_ats_actual <- ts(as.vector(tapply(london_present$type1_attendances, london_present$date, FUN=sum)),
frequency = 12,start = c(2015,6))
# 3. Build ARIMA model and forecast observed data for 2020
arima_london_ats<-auto.arima(london_ats)
forecast_london_ats <- forecast(arima_london_ats,h=4)
summary(forecast_london_ats)
# Simple plot
plot(forecast_london_ats,ylim = c(100000,480000),main="London",ylab="A&E attendances")
lines(london_ats_actual,lty=2,col="darkblue")
# Advanced plot
autoplot(forecast_london_ats,ylim = c(0,480000),
main="London",ylab="ED attendances") +
autolayer(london_ats_actual, series="Observed") +
autolayer(forecast_london_ats$mean, series="Forecast") +
theme_bw()