generated from ncl-icb-analytics/ncl_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrheum.R
173 lines (132 loc) · 4.96 KB
/
rheum.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
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
library(ragg)
library(fpp3)
library(tidyverse)
library(scales)
library(extrafont)
loadfonts(device = "win", quiet = TRUE)
# Load data
rheum <- read_csv("data/rheum.csv")
# Format and pivot
rheum$Date <- yearmonth(as.Date(rheum$Date, format = "%d/%m/%Y"))
rheum <-
rheum %>%
pivot_longer(cols = -Date, names_to = "Trust", values_to = "Referrals")
rheum$Referrals <- as.numeric(rheum$Referrals)
# Assumption 1: RFL between Aug21 - Dec-21
rheum2a <-
rheum %>%
filter(Trust == "RFL", Date >= yearmonth("2021-11-01"), Date <yearmonth("2022-03-01")) %>%
mutate(Referrals = Referrals * 1.8)
# # Not march-22, especially high.
rheum2b <-
rheum %>%
filter(Trust == "Whitt", Date == yearmonth("2022-03-01")) %>%
mutate(Referrals = Referrals * 0.66)
# Update
#rheuma <- rows_update(rheum2a, rheum2b, unmatched = "ignore", by= c("Date", "Trust"))
rheum <- rows_update(rheum, rheum2a, unmatched = "ignore", by= c("Date", "Trust"))
rheum <- rows_update(rheum, rheum2b, unmatched = "ignore", by= c("Date", "Trust"))
# check
rheum %>%
filter(Trust == "RFL", Date >= yearmonth("2021-09-01"), Date <yearmonth("2022-10-01"))
## Assumption 2: replace
## rheum3 <-
# rheum %>%
# filter(Trust == "Whitt", Date >= yearmonth("2021-09-01"), Date <yearmonth("2023-04-01")) %>%
# mutate(Trust = "RFL")
## Update
#rheum <- rows_update(rheum, rheum3, unmatched = "ignore", by= c("Date", "Trust"))
# Make timeseries table
rheum <- as_tsibble(rheum, key = Trust, index = Date)
# check for missing months
has_gaps(rheum)
# Visualise
rheum %>% autoplot(Referrals)
# Check autocorrelation
rheum %>% ACF() %>% autoplot()
rheum %>% ACF(difference(Referrals)) %>% autoplot()
mods_rheum <-
rheum %>%
filter(Date >= yearmonth("2021 Jan")) %>%
#stretch_tsibble(.init = 10) %>%
model(
mean = MEAN(Referrals),
naive = NAIVE(Referrals),
snaive = SNAIVE(Referrals ~ lag("year")),
drift = RW(Referrals ~ drift()),
ets = ETS(Referrals),
ses = ETS(Referrals ~ error("A")+trend("N")+season("N")),
holt_winter_a = ETS(Referrals ~ error("A")+trend("A")+season("A")),
holt_winter_ad = ETS(Referrals ~ error("A")+trend("Ad")+season("A"))
#holt_winter_m = ETS(Referrals ~ error("A")+trend("A")+season("M"))
#arima = ARIMA(Referrals)
)
rheum_forecast <-
mods_rheum %>%
forecast(h="60 months")
# a<- rheum_forecast %>%
# accuracy(rheum) %>%
# select(.model, RMSE:MAPE)
rheum_forecast %>%
filter(Trust == "RFL") %>%
autoplot() +
facet_wrap(~.model, ncol = 3)
rheum_hw <-
rheum_forecast %>%
hilo(95) %>%
filter( .model == "holt_winter_ad") %>%
#filter(((Trust %in% c("RFL", "Whitt") & .model == "holt_winter_ad")) | ((!Trust %in% c("RFL", "Whitt")) & .model == "holt_winter_a")) %>%
mutate(portion = "Forecast")
rheum_hw$lcl <- rheum_hw$`95%`$lower
rheum_hw$ucl <- rheum_hw$`95%`$upper
rheum_hw <-
rheum_hw %>%
select(Trust, Referrals, .model, Date, .mean, lcl, ucl)
rheum_trust <-
ggplot(rheum, aes(x= as.Date(Date), col = Trust))+
geom_line(aes(y=Referrals), linewidth=1)+
geom_line(aes(y=.mean), data=rheum_hw, linewidth=1, alpha=0.6)+
geom_smooth(aes(y=.mean), method="lm", data=rheum_hw, linewidth=1
, se=FALSE, linetype="dashed", alpha=0.6)+
geom_vline(xintercept = as.Date("01/04/2023", format = "%d/%m/%Y"), col = "red"
, linewidth = 1, linetype="dashed")+
#geom_ribbon(data=rheum_hw, aes(ymin = lcl, ymax=ucl, x= Date, fill = Trust)
# , alpha=0.5)+
#scale_y_continuous(breaks = seq(0,1000,200))+
scale_x_date("Date"
, date_breaks = "2 month"
, date_labels = "%b-%y"
# , limits = c(as.Date("01/08/2020", format = "%d/%m/%Y"),
# as.Date("01/03/2028", format = "%d/%m/%Y"))
,expand = c(0,0)
, date_minor_breaks = "2 month"
)+
labs(title = "Rheumatology Referrals - All providers"
, subtitle = "Forecast computed by 'Holt-Winters method', based on Apr-21 - Mar-23,
\nRoyal Free is imputed from Nov-11 - Feb 12, and Whittington in March-22 due to outlier values"
)+
theme_minimal()+
theme(legend.position = "bottom",
axis.text.x = element_text(angle=90, size = 8, colour = "#595959"
, hjust = 1, ),
text = element_text(family="Mulish"),
plot.subtitle = element_text(face="italic", size = 9)
)
rheum_trust
##### Cross-validation #####
mods2 <-
rheum %>%
filter(Date >= yearmonth("2021 Apr")) %>%
stretch_tsibble(.init = 3) %>%
model(
mean = MEAN(Referrals),
naive = NAIVE(Referrals),
snaive = SNAIVE(Referrals ~ lag("year")),
drift = RW(Referrals ~ drift()),
#ets = ETS(Referrals),
#ses = ETS(Referrals ~ error("A")+trend("N")+season("N")),
holt_winter_a = ETS(Referrals ~ error("A")+trend("A")+season("A")),
holt_winter_m = ETS(Referrals ~ error("A")+trend("A")+season("M"))
#arima = ARIMA(Referrals ~ pdq(0,1,1) + PDQ(0,1,1))
)
accuracy(mods2)