generated from ncl-icb-analytics/ncl_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pain.R
174 lines (136 loc) · 4.86 KB
/
pain.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
174
library(ragg)
library(fpp3)
library(tidyverse)
library(scales)
library(extrafont)
loadfonts(device = "win", quiet = TRUE)
# Load data
pain <- read_csv("data/pain.csv")
# Format and pivot
pain$Date <- yearmonth(as.Date(pain$Date, format = "%d/%m/%Y"))
pain <-
pain %>%
pivot_longer(cols = -Date, names_to = "Trust", values_to = "Referrals")
pain$Referrals <- as.numeric(pain$Referrals)
# # Assumption 1: RFL between Aug21 - Dec-21
# pain2a <-
# pain %>%
# filter(Trust == "RFL", Date >= yearmonth("2021-11-01"), Date <yearmonth("2022-03-01")) %>%
# mutate(Referrals = Referrals * 1.8)
#
# # # Not march-22, especially high.
# pain2b <-
# pain %>%
# filter(Trust == "Whitt", Date == yearmonth("2022-03-01")) %>%
# mutate(Referrals = Referrals * 0.66)
#
#
# # Update
# #paina <- rows_update(pain2a, pain2b, unmatched = "ignore", by= c("Date", "Trust"))
# pain <- rows_update(pain, pain2a, unmatched = "ignore", by= c("Date", "Trust"))
# pain <- rows_update(pain, pain2b, unmatched = "ignore", by= c("Date", "Trust"))
#
# # check
# pain %>%
# filter(Trust == "RFL", Date >= yearmonth("2021-09-01"), Date <yearmonth("2022-10-01"))
## Assumption 2: replace
## pain3 <-
# pain %>%
# filter(Trust == "Whitt", Date >= yearmonth("2021-09-01"), Date <yearmonth("2023-04-01")) %>%
# mutate(Trust = "RFL")
## Update
#pain <- rows_update(pain, pain3, unmatched = "ignore", by= c("Date", "Trust"))
# Make timeseries table
pain <- as_tsibble(pain, key = Trust, index = Date)
# check for missing months
has_gaps(pain)
# Visualise
pain %>% autoplot(Referrals)
# Check autocorrelation
pain %>% ACF() %>% autoplot()
pain %>% ACF(difference(Referrals)) %>% autoplot()
mods_pain <-
pain %>%
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)
)
pain_forecast <-
mods_pain %>%
forecast(h="60 months")
# a<- pain_forecast %>%
# accuracy(pain) %>%
# select(.model, RMSE:MAPE)
pain_forecast %>%
filter(Trust == "RFL") %>%
autoplot() +
facet_wrap(~.model, ncol = 3)
pain_hw <-
pain_forecast %>%
hilo(95) %>%
filter( .model == "holt_winter_ad") %>%
#filter((Trust == "NMUH" & .model == "holt_winter_ad") | (Trust != "NMUH" & .model == "holt_winter_a")) %>%
mutate(portion = "Forecast")
pain_hw$lcl <- pain_hw$`95%`$lower
pain_hw$ucl <- pain_hw$`95%`$upper
pain_hw <-
pain_hw %>%
select(Trust, Referrals, .model, Date, .mean, lcl, ucl)
pain_trust <-
ggplot(pain, aes(x= as.Date(Date), col = Trust))+
geom_line(aes(y=Referrals), linewidth=1)+
geom_line(aes(y=.mean), data=pain_hw, linewidth=1, alpha=0.6)+
geom_smooth(aes(y=.mean), method="lm", data=pain_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=pain_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 = "Pain Management Referrals - All providers"
, subtitle = "Forecast computed by 'Holt-Winters method', based on Apr-21 - Mar-23,
\nRNOH is imputed in Mar-21 due to missing data"
)+
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)
)
pain_trust
##### Cross-validation #####
mods2 <-
pain %>%
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)