-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.R
101 lines (87 loc) · 2.29 KB
/
run.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
rm(list = ls())
library(tidyverse)
library(purrr)
library(dplyr)
library(readr)
source(here::here("R/main.R"))
params <- expand_grid(
freq = c("hourly"),
start_date = c("2022-01-01"),
# end_date = c("2022-07-31"),
param_price_baseline = c(.25, .35, .4),
param_skatt_grunnrente = .37,
param_skatt_selskap = .22,
param_prop_spot_eksponert = c(.5, .7),
param_prop_hushold = .34,
param_prop_inntekt_faktisk = .9,
param_prop_statkraft = .42,
param_prop_kommfylk = .54,
param_prop_private = .04,
na.rm = T
)
result <- pmap_dfr(
params,
function(...) {
rsm.calculate(...) %>% mutate(...)
},
.id = "group"
)
minimize <- function(dat) {
dat %>%
select(
where(
~ sum(!is.na(.x)) > 0
)
) %>%
select(
group,
freq,
date,
starts_with("param_"),
en.staten.spot,
en.staten.normal,
en.staten.ekstra,
en.staten.spot.cum,
en.staten.normal.cum,
en.staten.ekstra.cum,
)
}
national <- result %>% filter(region == "no")
rm(result)
weekly <- national %>%
mutate(week_start = lubridate::floor_date(as.Date(date), "week", week_start = 1)) %>%
group_by(group, week_start, across(starts_with("param_"))) %>%
summarise(
across(c(
en.staten.spot,
en.staten.normal,
en.staten.ekstra,
), ~ sum(.x, na.rm = T)),
.groups = "drop"
) %>%
rename(date = week_start) %>%
group_by(group) %>%
arrange(date) %>%
mutate(
freq = "weekly",
across(c(en.staten.spot, en.staten.normal, en.staten.ekstra), cumsum, .names = "{.col}.cum")
) %>%
ungroup() %>%
minimize()
weekly
latest <- national %>%
filter(date >= Sys.Date() - 1) %>%
minimize()
minimal <- bind_rows(weekly, latest)
dir.create(here::here("outputs"), showWarnings = F)
national %>%
filter(param_price_baseline == .4, param_prop_spot_eksponert == .7) %>%
filter(date == max(date)) %>%
tail(1) %>%
t()
max_date <- format(max(national$date), "%Y-%m-%d-%H%M%S")
max_date
write_csv(national, here::here(glue("outputs/merinntekter-no-{max_date}.csv")))
write_csv(national, here::here(glue("outputs/merinntekter-no-latest.csv")))
write_csv(minimal, here::here(glue("outputs/merinntekter-minimal-latest.csv")))
write_file(minimal %>% jsonlite::toJSON(), here::here(glue("outputs/merinntekter-minimal-latest.json")))