-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_targets.R
587 lines (533 loc) · 16.2 KB
/
_targets.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# General TODO: -----------------------------------------------------------
# add packagename before functions
# figure out what to with same_time_listings
# clean up the indices
# Packages-Setup: ----------------------------------------------
# used during setup of pipeline
req_library <- c(
"targets",
"tarchetypes",
"future",
"future.callr",
"fst",
"renv",
"rlang",
"styler",
"docstring"
)
# used during execution of pipeline
pipeline_library <- c(
"here",
"stringr",
"dplyr",
"tidyr",
"data.table",
"cli",
"glue",
"ggplot2",
"haven", # reading/writing of dta files
"tidyverse", # data manipulation/wrangeling
"magrittr", # two sided pipe
"fst", #
"modelsummary",
"janitor",
"htmlTable",
"rsmatrix",
"kableExtra",
"fixest",
"qs",
"scatterplot3d",
"jsonlite"
)
suppressPackageStartupMessages({
# used during setup of pipeline
library(targets)
library(tarchetypes)
library(future)
library(future.callr)
library(fst)
library(renv)
library(rlang)
library(styler)
library(docstring)
library(jsonlite)
# used during execution of pipeline
library(MetBrewer)
library(here)
library(stringr)
library(dplyr)
library(tidyr)
library(data.table)
library(cli)
library(glue)
library(ggplot2)
library(haven)
library(modelsummary)
library(janitor)
library(kableExtra)
library(htmlTable)
library(fixest)
library(magrittr)
library(rsmatrix)
library(ggplot2)
library(qs)
library(scatterplot3d)
})
# Pipeline Settings ----------------------------------------------------------
#options(warn = 1)
options(scipen=999)
options("modelsummary_format_numeric_latex" = "plain")
# target options
tar_option_set(
resources = tar_resources(
fst = tar_resources_fst(compress = 50)
),
packages = pipeline_library,
garbage_collection = TRUE,
storage = "worker",
retrieval = "worker"
)
# tar_make_future() configuration:
plan(callr)
###########################################################################
# Paths and Globals -----------------------------------------------------------
###########################################################################
# Globals -------------------------------------------------------------------
static_RED_types <- c("WK", "WM", "HK")
RED_version <- "v9"
## similarity settings
categories <- c("wohnflaeche", "etage", "zimmeranzahl")
# time offset for readability
time_offset <- 6
# exact_offset
wohnflaeche_e_o <- 0.05
etage_e_o <- 0
zimmeranzahl_e_o <- 0.5
# plot
base_quarter = "2010-01-01"
# settings export setup
exportJSON <- data.table(
"RED_version" = RED_version,
"RED_types" = static_RED_types,
"categories" = categories,
"etag_e_o" = etage_e_o,
"wohnflaeche_e_o" = wohnflaeche_e_o,
"zimmeranzahl_e_o" = zimmeranzahl_e_o,
"time_offset" = time_offset,
"base_quarter" = base_quarter
)
# Setup Misc --------------------------------------------------------------
# logging setup
logger::log_appender(
logger::appender_file(
paste0(here::here("log"), "/", Sys.Date(), "_log.log")
)
)
# Paths -------------------------------------------------------------------
# main path, path where this file is located
main_path <- here::here()
# code-path
code_path <- here::here("R")
# data-path
data_path <- here::here("data")
markdown_path <- here::here("documentation", "markdown_")
# output paths
static_output_path <- here::here("output")
output_path <- here::here("output")
for (i in static_RED_types) {
if (!dir.exists(file.path(output_path, i))) {
dir.create(file.path(output_path, i), recursive = TRUE)
}
}
# Sourcing ----------------------------------------------------------------
# Read code files
for (sub_dir in c("RED_", "make_", "summary_", "similarity_", "misc","plot_")) {
lapply(
list.files(
file.path(code_path, glue::glue("{sub_dir}")),
pattern = "\\.R$",
full.names = TRUE,
all.files = FALSE,
recursive =TRUE
),
source
)
}
# ###########################################################################
# # tar_eval constants------------------------------------------------------
# ###########################################################################
# # tar_eval variables
federal_state_ids <- 1:16
classification_ids <- glue::glue("classification_blid_{federal_state_ids}")
# # constants used for branching in tar_eval
static_RED_file_names <- glue::glue("{static_RED_types}_file_name")
static_RED_full_data <- glue::glue("{static_RED_types}_full_data")
static_RED_req_data <- glue::glue("{static_RED_types}_req_data")
static_RED_classified <- glue::glue("{static_RED_types}_classified")
static_RED_subset_classified <- glue::glue("{static_RED_types}_subset_classified")
static_RED_classification = glue::glue("{static_RED_types}_classification")
static_prepared_hedonic = glue::glue("{static_RED_types}_prepared_hedonic")
static_hedonic_index = glue::glue("{static_RED_types}_hedonic_index")
static_prepared_repeated = glue::glue("{static_RED_types}_prepared_repeated")
static_repeated_index = glue::glue("{static_RED_types}_repeated_index")
static_hybrid_index = glue::glue("{static_RED_types}_hybrid_index")
static_combined_index = glue::glue("{static_RED_types}_combined_index")
static_split_index = glue::glue("{static_RED_types}_split_index")
static_export = glue::glue("{static_RED_types}_export")
# plots
static_plot_combined = glue::glue("{static_RED_types}_plot_combined")
static_plot_split = glue::glue("{static_RED_types}_plot_split")
# extend some constants to match lengths needed
dynamic_federal_state_ids <- rep(federal_state_ids, length(static_RED_types))
dynamic_RED_req_data = rep(static_RED_req_data, each = length(federal_state_ids))
dynamic_RED_types <- rep(static_RED_types, each = length(federal_state_ids))
dynamic_RED_classification_ids <- glue::glue("{dynamic_RED_types}_classification_blid_{dynamic_federal_state_ids}")
###########################################################################
# RED -----------------------------------------------------------
###########################################################################
RED_targets <- rlang::list2(
# dump settings as json file to make results reproducible
tar_target(
settings_used,
jsonlite::write_json(
exportJSON,
paste0(output_path, "/", "settings_used.json")
),
deployment = "main"
),
tar_eval(
list(
# generates file_name used based on version and type
tar_target(
RED_file_names,
make_RED_file_name(
data_version = RED_version,
data_type = RED_types
),
deployment = "worker",
format = "rds"
),
# read stata file, removes labels and mutate some variables
tar_file_read(
RED_full_data,
RED_file_names,
read_RED(!!.x),
deployment = "worker"
),
# cut down RED data to only columns required for classification
tar_fst_dt(
RED_req_columns,
prepare_RED(
RED_full_data,
var_of_interest = c(
## general info
"blid",
## object info
"wohnflaeche",
"zimmeranzahl",
"etage",
"balkon",
## mutated info
"counting_id",
"latlon_utm",
"amonths",
"emonths",
"price_var"
)
),
deployment = "worker"
)
),
values = list(
RED_types = static_RED_types,
RED_file_names = rlang::syms(static_RED_file_names),
RED_full_data = rlang::syms(static_RED_full_data),
RED_req_columns = rlang::syms(static_RED_req_data)
)
)
)
###########################################################################
# FEDERALSTATE -------------------------------------------------------------
###########################################################################
# create targets for each federal states
federal_state_targets <- rlang::list2(
# this seems slightly slower than prior usage of tar_group_by + pattern(map)
# usage of pattern causes hash names however which makes loading difficult
# classify data
tar_eval(
tar_fst_dt(
RED_classification_ids,
make_classification(
# this way of grouping causes targets problems when tracking changes
# doesnt recognize changes to RED_req_columns and therefore doesnt rerun classification
geo_grouped_data = RED_req_columns[.(RED_federal_state_ids), on = "blid"]
),
deployment = "worker"
),
values = rlang::list2(
RED_federal_state_ids = dynamic_federal_state_ids,
RED_classification_ids = rlang::syms(dynamic_RED_classification_ids),
RED_req_columns = rlang::syms(dynamic_RED_req_data)
)
)
)
# ###########################################################################
# # CLASSIFICATION -------------------------------------------------------------
# ###########################################################################
## combination helpers
# effectively just split the list into halves, since types are guaranteed to be in order
RED_type_count = length(static_RED_types)
WK_indices <- seq(to = length(dynamic_RED_req_data) / RED_type_count)
WM_indices <- length(dynamic_RED_req_data) / RED_type_count + WK_indices
HK_indices = length(dynamic_RED_req_data) / RED_type_count + WM_indices
classification_targets = rlang::list2(
# combine last step of federal state targets together into single output
tar_combine(
WK_classification,
federal_state_targets[[1]][WK_indices],
command = bind_rows(!!!.x),
format = "fst_dt"
),
tar_combine(
WM_classification,
federal_state_targets[[1]][WM_indices],
command = bind_rows(!!!.x),
format = "fst_dt"
),
tar_combine(
HK_classification,
federal_state_targets[[1]][HK_indices],
command = bind_rows(!!!.x),
format = "fst_dt"
),
)
###########################################################################
# PRICE INDICES -----------------------------------------------------------
###########################################################################
indices_targets <- rlang::list2(
tar_eval(
list(
# further process needs the initial version of red with all columns since some are
# used during regression but not during classification
tar_fst_dt(
RED_classified,
remerge_RED(
classification,
RED_full_data
)
),
tar_fst_dt(
RED_subset_classified,
subset_RED(
RED_classified
)
),
# do REDX-esque preperation of the hedonic data
tar_target(
prepared_hedonic,
prepare_hedonic(
RED_subset_classified,
data_type = RED_types
)
),
# hedonic regression + index calc
tar_target(
hedonic_index,
make_hedonic(
prepared_hedonic,
data_type = RED_types
),
format = "rds"
),
tar_fst_dt(
prepared_repeated,
prepare_repeated(
RED_subset_classified,
grouping_var = "gid2019"
)
),
# use remerged RED for now, since i need some variables not in classification
tar_target(
repeated_index,
make_repeated(
prepared_repeated,
grouping_var = "gid2019"
),
format = "rds"
),
tar_target(
hybrid_index,
make_hybrid(
RED_subset_classified,
prepared_repeated,
data_type = RED_types
),
format = "rds"
),
tar_target(
combined_index,
make_combined(
repeated_index,
hybrid_index,
hedonic_index
),
format = "rds"
),
tar_target(
split_index,
make_split(
repeated_index,
hybrid_index,
hedonic_index
),
format = "rds"
)
),
values = rlang::list2(
RED_types = static_RED_types,
RED_subset_classified = rlang::syms(static_RED_subset_classified),
RED_classified = rlang::syms(static_RED_classified),
RED_full_data = rlang::syms(static_RED_full_data),
classification = rlang::syms(static_RED_classification),
prepared_hedonic = rlang::syms(static_prepared_hedonic),
hedonic_index = rlang::syms(static_hedonic_index),
prepared_repeated = rlang::syms(static_prepared_repeated),
repeated_index = rlang::syms(static_repeated_index),
hybrid_index = rlang::syms(static_hybrid_index),
combined_index = rlang::syms(static_combined_index),
split_index = rlang::syms(static_split_index)
)
)
)
###########################################################################
# MARKDOWN --------------------------------------------------------------
###########################################################################
markdown_targets <- rlang::list2(
tar_fst_dt(
example_markdown_data,
make_example_markdown_data(
geo_grouped_data = WK_req_data[.(4), on = "blid"]
),
deployment = "main"
),
tar_render(
example_markdown,
paste0(markdown_path, "/example_markdown.Rmd")
),
deployment = "main"
)
# ###########################################################################
# # PLOT --------------------------------------------------------------
# ###########################################################################
plot_targets = rlang::list2(
tar_eval(
list(
# plot combined
tar_target(
p_combined,
plot_combined(
combined_index,
data_type = RED_types
)
),
# plot split
tar_target(
p_split,
plot_split(
split_index,
data_type = RED_types
)
)
),
values = rlang::list2(
RED_types = static_RED_types,
combined_index = rlang::syms(static_combined_index),
split_index = rlang::syms(static_split_index),
p_split = rlang::syms(static_plot_split),
p_combined = rlang::syms(static_plot_combined)
)
)
)
# ###########################################################################
# # SUMMARY --------------------------------------------------------------
# ###########################################################################
# # arguments to create summary_tables from
# # first of arg1 vector correspondences to first argument of arg2 vector
# cross_tabyl_arguments <- data.table(
# arg1 = c(
# "blid",
# "sim_index",
# "blid",
# "same_time_listing"
# ),
# arg2 = c(
# "sim_index",
# "non_list_reason",
# "non_list_reason",
# "non_list_reason"
# )
# )[
# ,
# target_name := paste0("summary_table", "_", arg1, "_", arg2)
# ]
# # Tables ------------------------------------------------------------------
# table_targets <- rlang::list2(
# # classification
# tar_target(
# summary_skim_numeric,
# datasummary_skim_numerical(
# classification
# )
# ),
# tar_target(
# summary_skim_cat,
# datasummary_skim_categorical(
# classification
# )
# ),
# tar_eval(
# tar_target(
# target_name,
# custom_cross_tabyl(
# classification,
# arg1 = arg1,
# arg2 = arg2
# )
# ),
# values = cross_tabyl_arguments
# ),
# )
# # Figures -----------------------------------------------------------------
# figure_targets <- rlang::list2()
###########################################################################
# EXPORT-TARGETS -----------------------------------------------------------
###########################################################################
export_targets <- rlang::list2(
tar_eval(
tar_target(
export_classification,
export_data(
RED_classified,
data_type = RED_types
)
),
values = rlang::list2(
RED_types = static_RED_types,
RED_classified = rlang::syms(static_RED_classified),
export_classification = rlang::syms(static_export)
)
)
)
###########################################################################
# FINAL_TARGETS -----------------------------------------------------------
###########################################################################
## combine to main pipeline
rlang::list2(
RED_targets,
federal_state_targets,
markdown_targets,
classification_targets,
plot_targets,
export_targets,
indices_targets
)