11box :: use(
2- dplyr [across , all_of , arrange , bind_cols , bind_rows , distinct , if_else , left_join ],
3- dplyr [mutate , rename , row_number , select , slice , slice_tail , starts_with , summarize_all ],
2+ dplyr [
3+ across ,
4+ all_of ,
5+ arrange ,
6+ bind_cols ,
7+ bind_rows ,
8+ distinct ,
9+ if_else ,
10+ left_join
11+ ],
12+ dplyr [
13+ mutate ,
14+ rename ,
15+ row_number ,
16+ select ,
17+ slice ,
18+ slice_tail ,
19+ starts_with ,
20+ summarize_all
21+ ],
422 purrr [map ],
523 stats [na.omit ],
624 stringr [str_detect ],
@@ -9,9 +27,9 @@ box::use(
927)
1028
1129box :: use(
12- app / logic / model [test_model_flow ],
13- app / logic / scale [get_actual_scales , get_all_scales ],
14- app / logic / transform [get_actual_serie , get_all_transformations ],
30+ app / logic / model [test_model_flow ],
31+ app / logic / scale [get_actual_scales , get_all_scales ],
32+ app / logic / transform [get_actual_serie , get_all_transformations ],
1533)
1634
1735# Get data to work with by using the specified sequence and forecast
@@ -22,7 +40,6 @@ box::use(
2240# "value"
2341# ' @export
2442get_process_data <- function (data , sequence , forecast ) {
25-
2643 if (! any(is.null(sequence ), sequence == " " )) {
2744 new_data <- data [, c(sequence , forecast )]
2845 names(new_data ) <- c(" sequence" , " value" )
@@ -33,8 +50,8 @@ get_process_data <- function(data, sequence, forecast) {
3350 names(new_data ) <- " value"
3451 }
3552
36- new_data <- na.omit( new_data )
37- return (new_data )
53+ # new_data
54+ na.omit (new_data )
3855}
3956
4057# Function to returns min and max value of a column if column exist
@@ -73,15 +90,15 @@ get_all_series <- function(data, transformations, scales) {
7390 se_min_max <- extract_mi_ma(data , " second" )
7491
7592 data <- get_all_scales(data , scales )
76- data <- list (
93+ # data
94+ list (
7795 data = data ,
7896 first_diff = first_diff ,
7997 second_diff = second_diff ,
8098 ex_min_max = ex_min_max ,
8199 fi_min_max = fi_min_max ,
82100 se_min_max = se_min_max
83101 )
84- return (data )
85102}
86103
87104# Obtain all the tests for all the model configuration (modeldata) for
@@ -97,28 +114,30 @@ get_predict <- function(name, data, modeldata) {
97114 predictions <- tibble(tests_results = rep(NA , dim(modeldata )[1 ]))
98115
99116 for (i in seq_len(dim(modeldata )[1 ])) {
100- predictions [i , ] <- test_model_flow(data | > select(all_of(name )), modeldata [i , ])
117+ predictions [i , ] <- test_model_flow(
118+ data | > select(all_of(name )),
119+ modeldata [i , ]
120+ )
101121 }
102122
103123 predictions <- predictions | >
104124 mutate(index = row_number()) | >
105125 select(index , tests_results )
106126 modeldata <- modeldata | >
107127 mutate(index = row_number())
108- predictions <- left_join(modeldata , predictions , by = " index" ) | >
128+ # predictions
129+ left_join(modeldata , predictions , by = " index" ) | >
109130 select(- index ) | >
110131 nest() | >
111132 rename(model_data = data ) | >
112133 mutate(data = name ) | >
113134 select(data , model_data )
114- return (predictions )
115135}
116136
117137# Function to compute squared error
118138# ' @export
119139squared_error <- function (x , y ) {
120- z <- (y - x )^ 2
121- return (z )
140+ (y - x )^ 2
122141}
123142
124143# Function to get RMSE from test results and give proper format for
@@ -153,7 +172,7 @@ get_results <- function(data, original) {
153172
154173 rmse [i , ] <- bind_cols(tests_results , pred_original ) | >
155174 mutate(across(starts_with(" test_" ), \(x ) squared_error(x , value ))) | >
156- select(- value ) | >
175+ select(- value ) | >
157176 summarize_all(mean ) | >
158177 mutate(across(starts_with(" test_" ), \(x ) sqrt(x ))) | >
159178 rowMeans()
@@ -179,16 +198,19 @@ get_results <- function(data, original) {
179198 data [i , " tests_results" ] <- tests_results
180199 }
181200
182- data <- bind_cols(data , rmse ) | >
201+ # data
202+ bind_cols(data , rmse ) | >
183203 mutate(
184204 transformations = str_detect(data , " value" ) | >
185- if_else(" Original" , if_else(str_detect(data , " first" ), " First" , " Second" )),
205+ if_else(
206+ " Original" ,
207+ if_else(str_detect(data , " first" ), " First" , " Second" )
208+ ),
186209 scales = str_detect(data , " z_o" ) | >
187210 if_else(" 0 to 1" , if_else(str_detect(data , " m_p" ), " -1 to 1" , " Exact" )),
188211 .after = data
189212 ) | >
190213 select(- c(data , tests ))
191- return (data )
192214}
193215
194216# Execute the process using the previously defined functions. Receive as
@@ -224,8 +246,13 @@ process <- function(data, sequence, forecast, iterations) {
224246 map(\(x ) get_predict(name = x , new_data , iterations )) | >
225247 bind_rows()
226248
227- predictions <- get_actual_scales(predictions , ex_min_max , fi_min_max , se_min_max )
249+ predictions <- get_actual_scales(
250+ predictions ,
251+ ex_min_max ,
252+ fi_min_max ,
253+ se_min_max
254+ )
228255 predictions <- get_actual_serie(predictions , data , first_diff , second_diff )
229- predictions <- get_results( predictions , data )
230- return (predictions )
256+ # predictions
257+ get_results (predictions , data )
231258}
0 commit comments