Repository navigation
Expand file tree
/
Copy pathpredict.R
More file actions
82 lines (55 loc) · 2.3 KB
/
Copy pathpredict.R
File metadata and controls
82 lines (55 loc) · 2.3 KB
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
# Load required libraries
library(tidyverse)
library(fable)
library(tsibble)
# Define the predict function
predict_chap <- function(model_fn, historic_data_fn, future_climatedata_fn, predictions_fn) {
# 1. Load the pre-trained model structure
model <- readRDS(model_fn)
print('read model')
# 2. Load the complete historical data
historic_df <- read_csv(historic_data_fn) |>
mutate(target = if_else(is.na(target), 0, target),
time_period = yearmonth(time_period)) |>
as_tsibble(index = time_period, key = location) |>
fill_gaps(target=0L, .full = TRUE)
# 3. Load the future data (covariates only)
future_df <- read_csv(future_climatedata_fn) |>
mutate(time_period = yearmonth(time_period)) |>
as_tsibble(index = time_period, key = location)
print('load_data')
# 4. Refit the model with all historic data and forecast
model <- model |>
refit(historic_df)
y_pred <- model |>
generate(new_data = future_df, bootstrap = FALSE, times = 1000) |>
mutate(.rep = as.integer(.rep)) |>
arrange(.rep) |>
pivot_wider(names_from = .rep, values_from = .sim, names_prefix = "sample_")
print(y_pred)
# saveRDS(model, file = model_fn)
df_out <- future_df |>
left_join(y_pred |>
select(-.model) |>
mutate(across(starts_with("sample_"), ~ if_else(. < 0, 0, .))) |>
rename_with(~ paste0("sample_", as.integer(str_extract(., "\\d+")) - 1), starts_with("sample_")),
by = c('location', 'time_period')
) |>
as_tibble() |>
mutate(time_period = format(as.Date(time_period), "%Y-%m")) |>
select(time_period, location, starts_with("sample_"))
print('made predictions')
df_out |> print()
write.csv(df_out, predictions_fn, row.names = FALSE)
fname <- paste0("df_out_", format(Sys.time(), "%Y%m%d_%H%M%S"), ".csv")
write_csv(y_pred, fname)
}
# --- Command Line Argument Handling (No change here) ---
args <- commandArgs(trailingOnly = TRUE)
if (length(args) == 4) {
model_fn <- args[1]
historic_data_fn <- args[2]
future_climatedata_fn <- args[3]
predictions_fn <- args[4]
predict_chap(model_fn, historic_data_fn, future_climatedata_fn, predictions_fn)
}