Revisión | 3a489fb6339d1748bd331dc3133de7ed31bfa55f (tree) |
---|---|
Tiempo | 2022-11-24 00:53:33 |
Autor | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
A rewriting of the arima code using the map function instead of loops. The final step will be the parallelized version of map.
@@ -0,0 +1,151 @@ | ||
1 | +rm(list=ls()) | |
2 | + | |
3 | +library(tidyverse) | |
4 | +library(janitor) | |
5 | +library(tidymodels) | |
6 | +library(modeltime) | |
7 | +library(lubridate) | |
8 | +library(forecast) # for `auto.arima` | |
9 | +library(timetk) # for `tk_ts` | |
10 | +library(tictoc) | |
11 | +library(openxlsx) | |
12 | +## library(workflowsets) | |
13 | +library(furrr) | |
14 | + | |
15 | +tidymodels_prefer() | |
16 | + | |
17 | +plan(multisession, workers = 4) | |
18 | + | |
19 | +source("/home/lorenzo/myprojects-hg/R-codes/stat_lib.R") | |
20 | + | |
21 | + | |
22 | +choose_log <- 1 | |
23 | + | |
24 | + | |
25 | +present_month <- month(today()) | |
26 | + | |
27 | +price_series <- readRDS("price.RDS") |> | |
28 | + arrange(geo, time_period) |> | |
29 | + rename("value"="obs_value", | |
30 | + "date"="time_period") |> | |
31 | + filter(geo %in% iso_map_eu27$iso2) | |
32 | + | |
33 | + | |
34 | +ms_list <- price_series |> | |
35 | + pull(geo) |> | |
36 | + su() | |
37 | + | |
38 | +ms_list <- c("DE", "AT", "FI") | |
39 | + | |
40 | + | |
41 | +tic() | |
42 | + | |
43 | +out.total <- c() | |
44 | + | |
45 | + | |
46 | +set.seed(1234) | |
47 | + | |
48 | + | |
49 | +ff <- function( ms){ | |
50 | + | |
51 | + res <- price_series |> | |
52 | + filter(geo==ms)|> | |
53 | + select(-geo) | |
54 | + | |
55 | + if (choose_log==1){ | |
56 | + res <- res|> | |
57 | + mutate(value=log(value)) | |
58 | + | |
59 | + } | |
60 | + | |
61 | +} | |
62 | + | |
63 | + | |
64 | +ms_data <- map(ms_list, ff ) | |
65 | + | |
66 | + | |
67 | +splits_parallel <- map( ms_data, | |
68 | + function(x) time_series_split(x, assess = "3 months", cumulative = TRUE) | |
69 | +) | |
70 | + | |
71 | + | |
72 | + | |
73 | +arima_parallel <- function(splits){ arima_reg() |> | |
74 | + set_engine("auto_arima") |> | |
75 | + fit(value ~ date, training(splits)) | |
76 | +} | |
77 | + | |
78 | +model_fit_arima_parallel <- map(splits_parallel, function(x) arima_parallel(x)) | |
79 | + | |
80 | + | |
81 | +model_table_parallel <-map(model_fit_arima_parallel, modeltime_table) | |
82 | + | |
83 | +## calibration_table_parallel <- map2( model_table, | |
84 | + | |
85 | +## modeltime_calibrate(testing(splits))) | |
86 | + | |
87 | +testing_splits <- map(splits_parallel,testing ) | |
88 | + | |
89 | +calibration_table_parallel <- map2( model_table_parallel, | |
90 | +testing_splits, modeltime_calibrate | |
91 | +) | |
92 | + | |
93 | +refit_parallel <-map2( calibration_table_parallel, ms_data, | |
94 | + modeltime_refit | |
95 | + ) | |
96 | + | |
97 | + | |
98 | +new_forecast_parallel <- map2(refit_parallel, ms_data, | |
99 | + function(x,y) modeltime_forecast(x,h = "3 months", actual_data = y) | |
100 | +) | |
101 | + | |
102 | + | |
103 | +ff2 <- function(x,y){ | |
104 | + res <- x |> filter(.key=="prediction") |> | |
105 | + select(.index, .value) |> | |
106 | + mutate(geo=y) | |
107 | + | |
108 | +return(res) | |
109 | + | |
110 | +} | |
111 | + | |
112 | +out.total <- map2_df(new_forecast_parallel, ms_list, ff2) | |
113 | + | |
114 | + | |
115 | + | |
116 | + | |
117 | +## out.total <- out.forecast_parallel |> | |
118 | +## filter(.key=="prediction") |> | |
119 | +## select(.index, .value) |> | |
120 | +## mutate(geo=sel_ms) | |
121 | + | |
122 | + | |
123 | + | |
124 | + | |
125 | + if (choose_log==1){ | |
126 | + | |
127 | + out.total <- out.total |> | |
128 | + mutate(.value=exp(.value)) | |
129 | + } | |
130 | + | |
131 | +out.total <- out.total |> | |
132 | + mutate(month_nowcast=month(.index)) |> | |
133 | + filter(month_nowcast==present_month) |> | |
134 | + select(geo, month_nowcast, .value) |> | |
135 | + rename("value"=".value") | |
136 | + | |
137 | +fn <- paste("nowcast_price_month_", present_month, ".xlsx", sep="" ) | |
138 | + | |
139 | +save_excel(out.total, fn) | |
140 | + | |
141 | +###################################################################### | |
142 | + | |
143 | + | |
144 | + | |
145 | + | |
146 | + | |
147 | + | |
148 | + | |
149 | + | |
150 | + | |
151 | +print("So far so good") |