-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 18.5 KB
/
.Rhistory
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
forecasts3
#on va avoir une period mean qui va nous donne forcast at eahc pouint h
#if we want to depict foreacts and fitted values along with time series
forecasts3 %>%
# Depicts the original time series and the forecasts
autoplot(manual_decomposition, level = FALSE) +
# Overlays the fitted values
geom_line(data = fitted_vals3, aes(y = .fitted), colour = "blue", linetype = "dashed")
library(fpp3)
## YOUR CODE GOES HERE
scs = scs_pedestrians%>%filter(Date>="2016-07-01"&Date<="2016-10-25")
scs_pedestrians <-
read.csv(file.choose()) %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index=Date)
scs_pedestrians
## YOUR CODE GOES HERE
scs_pedestrians %>% autoplot(Count) + scale_x_date(
breaks = "1 week",
minor_breaks = "5 week"
) + theme(axis.text.x = element_text(angle = 90))
scs_pedestrians$mean_val = mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians
## YOUR CODE GOES HERE
scs_pedestrians <-
scs_pedestrians %>%
mutate(`12-MA` = slider::slide_dbl(Count, mean,.before = 5, .after = 6, .complete =
TRUE),
`trend_class` = slider::slide_dbl(`12-MA`, mean,.before = 1, .after = 0, .complete =
TRUE))
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
# FEEDBACK: IF THE PERIOD IS ONE WEEK AND THE DATA IS DAILY, YOU NEED THE 7-MA
# NOT THE 2X12-MA. THIS CODE WAS COPIED FROM THE NOTES WITHOUT THINKING IF IT APPLIES AT ALL
scs_pedestrians <-
scs_pedestrians %>%
mutate(`12-MA` = slider::slide_dbl(Count, mean,.before = 5, .after = 6, .complete =
TRUE),
`trend_class` = slider::slide_dbl(`12-MA`, mean,.before = 1, .after = 0, .complete =
TRUE))
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
fit = scs_pedestrians %>%
model(
mean = MEAN(Count),
Drift = RW(Count ~ drift()),
SNaive = SNAIVE(Count)
)
fitted_vals <-
fit %>%
augment()
fitted_vals
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
fitted_vals <-
fit %>%
augment()
forecasts <- fit %>% forecast(h = 7)
forecasts
# Depict the forecasts
forecasts %>%
filter(.model == "SNaive")
autoplot(scs_pedestrians, level = FALSE) +
autolayer(fitted_vals, .fitted, colour = "blue", linetype = "dashed")
forecasts %>%
filter(.model == "mean")
autoplot(scs_pedestrians, level = FALSE) +
autolayer(fitted_vals, .fitted, colour = "blue", linetype = "dashed")
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
scs_pedestrians%>%ACF(Count)%>%autoplot()
fit2 = scs_pedestrians %>%
model(
mean = MEAN(Count),
)
fitted_vals2 <-
fit2 %>%
augment()
fitted_vals2%>%ACF(.innov)%>%autoplot()
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
fitted_vals %>%
filter(.model == "mean") %>%
autoplot(Count, colour = "gray") +
geom_line(aes(y=.fitted), colour = "blue", linetype = "dashed")
fit %>%
select(mean) %>%
gg_tsresiduals()
# Compute the mean of the residuals
xx = fitted_vals %>% as_tibble() %>%
filter(.model == "Naive") %>%
summarise(mean = mean(.innov, na.rm = TRUE))
xx <- filter(xx, .model=="mean")
library(fpp3)
library(fpp3)
## YOUR CODE GOES HERE
new_data <- pedestrian %>%
filter(Sensor=="Southern Cross Station" | Sensor=="Bourke Street Mall (North)") %>%
filter(Date_Time>="2016-07-01" & Date_Time<="2016-10-25") %>%
as_tibble() %>%
group_by(Sensor,Date) %>%
summarise(Total_Count=sum(Count))
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
new_data
## YOUR CODE GOES HERE
new_data %>%
ggplot(aes(x=Date, y=Total_Count,fill=Sensor))+
geom_line()+scale_x_date(breaks ="5 weeks", minor_breaks="1 weeks" )
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
new_data %>% group_by(Sensor) %>% summarise(Max=max(Total_Count))
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians <-
read.csv(file.choose()) %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index=Date)
scs_pedestrians
## YOUR CODE GOES HERE
scs_pedestrians %>% autoplot()+
scale_x_date(breaks ="5 weeks", minor_breaks="1 week" )
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians$mean_val = mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians
## YOUR CODE GOES HERE
manual_decompostion <- scs_pedestrians %>%
mutate(
`7-MA`=slider::slide_dbl(Count,mean,
.before=3,.after=3,.complete = TRUE )
) %>%
select(Sensor,Date,Count,mean_val,`7-MA`)
manual_decompostion %>%
autoplot(Count, colour="gray")+
geom_line(aes(y=`7-MA`),colour="red")+
geom_line(aes(y=mean_val ),colour="blue",linetype="dashed")
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
fit_mean <- scs_pedestrians %>%
model(mean= MEAN(Count))
fit_mean
fit_drift <- scs_pedestrians %>%
model(drift=RW(Count~ drift()))
fit_drift
fit_snaive <- scs_pedestrians %>%
model(snaive=SNAIVE(Count))
fit_snaive
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
fit_mean <- scs_pedestrians %>%
model(mean= MEAN(Count))
fit_mean
fit_drift <- scs_pedestrians %>%
model(drift=RW(Count~ drift()))
fit_drift
fit_snaive <- scs_pedestrians %>%
model(snaive=SNAIVE(Count))
fit_snaive
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
## YOUR CODE GOES HERE
#SNAIVE model
fitted_values_snaive <- fit_snaive %>% augment()
head(fitted_values_snaive)
forecast_snaive <- fit_snaive %>% forecast(h=7)
forecast_snaive %>% autoplot(scs_pedestrians,level=FALSE)+
autolayer(fitted_values_snaive,.fitted,colour="blue",linetype="dashed")
#mean model
fitted_values_mean <- fit_mean %>% augment()
head(fitted_values_mean)
forecast_mean <- fit_mean %>% forecast(h=7)
forecast_mean %>% autoplot(scs_pedestrians,level=FALSE)+
autolayer(fitted_values_mean,.fitted,colour="blue",linetype="dashed")
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
library(fpp3)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
data<- pedestrian %>% filter(Sensor %in% c("Southern Cross Station", "Bourke Street Mall (North)")) %>% select(Sensor, Date, Count)
data
data <- data %>% group_by(Sensor) %>% index_by(day= Date) %>% summarise(mean_count = mean(Count))
data=data %>% filter(day>"2016-07-01") %>%filter(day>"2016-10-25")
data
scs_pedestrians <-
read.csv(file.choose()) %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index=Date)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
autoplot(scs_pedestrians, Count) +
scale_x_date(date_breaks = "5 weeks",
minor_breaks = "1 week") +
theme(axis.title.x = element_text(angle=90))
scs_pedestrians$mean_val = mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians <- scs_pedestrians %>% mutate(`trend_class` = slider::slide_dbl
(Count,mean, .before = 3, .after= 3, .complete = TRUE)
)
scs_pedestrians
scs_pedestrians %>% autoplot(Count) +
geom_line(aes(y= `trend_class`), linetype = "dashed") +
geom_line(aes(y= `mean_val`), linetype = "dashed")
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians
fit <- scs_pedestrians %>% model(mean = MEAN(Count))
fit
fit2 <-scs_pedestrians %>% model(Drift = RW(Count ~ drift()))
fit2
fit3<- scs_pedestrians %>% select(Count) %>% model(snaive = SNAIVE(Count))
fit3
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
forecasts1 <- fit3 %>% forecast(h=7)
forecasts1
forecasts2 <- fit %>% forecast(h=7)
forecasts2
autoplot(forecasts1)
autoplot(forecasts2)
library(fpp3)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
q1 <- pedestrian %>% filter(Sensor == "Southern Cross Station" | Sensor == "Bourke Street Mall (North)",
Date >= "2016-07-01" & Date <= "2016-10-25")
q1
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
q1 %>%
autoplot(Count) +
scale_x_datetime(date_breaks = "5 weeks", minor_breaks ="1 week")
# There is not another sensor in the database
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
q1 %>% filter(Sensor == "Southern Cross Station") %>% pull(Count) %>% max()
q1 %>% filter(Sensor== "Bourke Street Mall (North)") %>% pull(Count) %>% max()
q2 = q1 %>% filter(Sensor== "Southern Cross Station", Count == 3474) # date is 2016-08-02
q3 = q1 %>% filter(Sensor== "Bourke Street Mall (North)", Count == 4812) # date is 2016-08-31
scs_pedestrians <-
read.csv(file.choose()) %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index=Date)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians %>%
autoplot() + scale_x_date(date_breaks ="5 weeks", minor_breaks = "1 week")
scs_pedestrians$mean_val = mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians <- scs_pedestrians %>%
mutate(
trend_class = slider::slide_dbl(Count, mean, .before = 3, .after = 3, .complete = TRUE)
)
scs_pedestrians %>%
autoplot(Count, colour = "gray") +
geom_line(aes(y = mean_val), colour = "blue", linetype = "dashed") +
geom_line(aes(y = trend_class), colour = "darkgreen", linetype = "dashed")
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
# all models
fit <-
scs_pedestrians %>%
model(
mean = MEAN(Count),
SNaive = SNAIVE(Count),
Drift = RW(Count ~ drift())
)
fitted_vals <- fit %>% augment()
fitted_vals
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
forecasts <- fit %>% forecast(h=7) # h = 7 since our data goes by days, and our seasonal period is one week
forecasts
fitted_valsSNaive <- fitted_vals %>% filter(.model == "SNaive")
fitted_valsDrift <- fitted_vals %>% filter(.model == "Drift")
forecasts %>%
filter(.model == "SNaive") %>%
# autoplot()
autoplot(scs_pedestrians, colour = "gray") +
autolayer(fitted_vals, .fitted, colour = "blue", linetype = "dashed")
forecasts %>%
filter(.model == "Drift") %>%
# autoplot()
autoplot(scs_pedestrians, colour = "gray") +
autolayer(fitted_vals, .fitted, colour = "blue", linetype = "dashed")
library(fpp3)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
pedestrian_adjust <-
pedestrian %>%
filter(Sensor %in% c("Southern Cross Station", "Bourke Street Mall (North)")) %>%
filter(Date >= "2016-07-01", Date <= "2016-10-25")
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
pedestrian_adjust %>%
#as_tsibble(index = Date, key = c(Sensor)) %>%
autoplot(Count) #+ scale_x_date(breaks = "5 week", minor_breaks = "1 week")
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
BSM <-
pedestrian_adjust %>%
filter(Sensor == "Bourke Street Mall (North)") %>%
arrange(desc(Count)) %>%
pull(Date)
BSM_date <- BSM[1]
SCS <-
pedestrian_adjust %>%
filter(Sensor == "Southern Cross Station") %>%
arrange(desc(Count)) %>%
pull(Date)
SCS_date <- SCS[1]
BSM_date
SCS_date
scs_pedestrians <-
read.csv(file.choose()) %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index=Date)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
library(patchwork) # Used to manage the relative location of ggplots
library(GGally)
scs_pedestrians %>%
autoplot() + scale_x_date(breaks = "5 week", minor_breaks = "1 week")
scs_pedestrians$mean_val = mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians_trend <-
scs_pedestrians %>%
mutate(
`trend_class` = slider::slide_dbl(Count, mean,
.before = 3, .after = 3, .complete = TRUE)
)
scs_pedestrians_trend %>%
autoplot(`trend_class`) +
geom_line(aes(y = mean_val),linetype = "dashed")
scs_pedestrians_trend
scs_pedestrians_trend %>%
autoplot(Count, colour = "gray") +
scale_x_date(
minor_breaks = "1 week",
breaks = "5 weeks"
) +
geom_line(aes(y = trend_class), colour = "#D55E00") +
geom_line(aes(y = mean_val), colour = "red", linetype="dashed") +
# Flip x-labels by 90 degrees
theme(axis.text.x = element_text(angle = 90))
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
fit <-
scs_pedestrians %>%
model(
mean = MEAN(Count),
drift = RW(Count ~ drift()),
snaive = SNAIVE(Count)
)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
forecast <-fit %>% forecast(h=7)
forecast %>%
filter(.model == "snaive") %>%
autoplot(scs_pedestrians)
forecast %>%
filter(.model == "mean") %>%
autoplot(scs_pedestrians)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians %>%
ACF(Count) %>%
autoplot()
fit %>%
select(mean) %>%
gg_tsresiduals()
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
scs_pedestrians %>%
ACF(Count) %>%
autoplot()
fit %>%
select(mean) %>%
gg_tsresiduals()
library(fpp3)
library(ggplot2)
library(dplyr)
library(tsibble)
library(forecast)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
filtered_data <- pedestrian %>%
filter(Sensor %in% c("Southern Cross Station", "Bourke Street Mall (North)") &
Date >= as.Date("2016-07-01") & Date <= as.Date("2016-10-25")) %>%
group_by(Sensor, Date) %>%
summarise(Total_Pedestrians = sum(Count))
print(filtered_data)
library(fpp3)
library(ggplot2)
library(dplyr)
library(tsibble)
library(forecast)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
filtered_data <- pedestrian %>%
filter(Sensor %in% c("Southern Cross Station", "Bourke Street Mall (North)") &
Date >= as.Date("2016-07-01") & Date <= as.Date("2016-10-25")) %>%
group_by(Sensor, Date) %>%
summarise(Total_Pedestrians = sum(Count))
print(filtered_data)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
filtered_data <- pedestrian %>%
filter(Sensor %in% c("Southern Cross Station", "Bourke Street Mall (North)") &
Date >= as.Date("2016-07-01") & Date <= as.Date("2016-10-25")) %>%
group_by(Sensor, Date) %>%
summarise(Total_Pedestrians = sum(Count))
max_pedestrians_per_sensor <- filtered_data %>%
group_by(Sensor) %>%
top_n(1, Total_Pedestrians)
print(max_pedestrians_per_sensor)
filtered_data <- pedestrian %>%
filter(Sensor %in% c("Southern Cross Station", "Bourke Street Mall (North)") &
Date >= as.Date("2016-07-01") & Date <= as.Date("2016-10-25")) %>%
group_by(Sensor, Date) %>%
summarise(Total_Pedestrians = sum(Count))
filtered_data
scs_pedestrians <- read.csv("scs_daily_pedestrian.csv") %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index = Date)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
ggplot(scs_pedestrians, aes(x = Date, y = Count)) +
geom_line() +
scale_x_date(
date_breaks = "5 weeks",
minor_breaks = "1 week")
scs_pedestrians$mean_val = mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
mean_val <- mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians <- scs_pedestrians %>%
mutate('5-MA' = as.numeric(slider::slide_dbl(Count, mean, .before = 2, .after = 3, .complete = TRUE)),
'1-MA' = as.numeric(slider::slide_dbl('5-MA', mean, .before = 1 , .after = 0, .complete = TRUE)))
ggplot(data = scs_pedestrians, aes(x = Date)) +
geom_line(aes(y = Count), colour = "gray", size = 0.5) +
geom_line(aes(y = '1-MA'), colour = "blue")
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
mean_val <- mean(scs_pedestrians$Count, na.rm = TRUE)
scs_pedestrians <- scs_pedestrians %>%
mutate('5-MA' = as.numeric(slider::slide_dbl(Count, mean, .before = 2, .after = 3, .complete = TRUE)),
'1-MA' = as.numeric(slider::slide_dbl('5-MA', mean, .before = 1 , .after = 0, .complete = TRUE)))
ggplot(data = scs_pedestrians, aes(x = Date)) +
geom_line(aes(y = Count), colour = "gray", size = 0.5) +
geom_line(aes(y = '1-MA'), colour = "blue")
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
fit_mean <- scs_pedestrians %>% model(mean = MEAN(Count))
fit_drift <- scs_pedestrians %>% model(Drift = RW(Count ~ drift()))
fit_snaive <- scs_pedestrians %>% model(Naive = NAIVE(Count))
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
forecast_mean <- fit_mean %>% forecast(h=1)
forecast_snaive <- fit_snaive %>% forecast(fit_snaive, h = 1)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
forecast_mean <- fit_mean %>% forecast(h=1)
forecast_snaive <- fit_snaive %>% forecast(fit_snaive, h = 1)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
forecast_mean <- fit_mean %>% forecast(h=1)
#
forecast_snaive <- fit_snaive %>% forecast(h = 1)
autoplot(forecast_mean)
autoplot(forecast_snaive)
## YOUR CODE GOES HERE
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
# FEEDBACK: h=1 means one timestep ahead, meaning one day. To produce
# forecasts of up to 1 week ahead you need h=7
forecast_mean <- fit_mean %>% forecast(h=1)
forecast_snaive <- fit_snaive %>% forecast(h = 1)
autoplot(forecast_mean)
autoplot(forecast_snaive)
library(fpp3)
scs_pedestrians <-
read.csv(file.choose()) %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index=Date)
pedestrian_data <- read.csv(file.choose("scs_daily_pedestrian.csv"))
library(fpp3)
scs_pedestrians <-
read.csv(file.choose()) %>%
mutate(Date = as.Date(Date)) %>%
as_tsibble(index=Date)
pd<-scs_pedestrians
## YOUR CODE GOES HERE
pd%>%autoplot(Count)+
scale_x_yearweek(date_breaks='5 week',minor_breaks='1 week')
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
pd$mean_val = mean(pd$Count, na.rm = TRUE)
pd
## YOUR CODE GOES HERE
trendclass<-pd%>%
mutate("trend_class"=slider::slide_dbl(mean_val, mean,
.before=3, .after=3, .complete=TRUE)
)%>%
select(Sensor,Date,Count,mean_val,`trend_class` )
trendclass
trendclass%>% autoplot(mean_val)+
geom_line()
## DO NOT CREATE ADDITIONAL CODE SNIPPETS, KEEP EVERYTHING IN A SINGLE SNIPPET.
pd