Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super slow #40

Open
johanneskoch94 opened this issue Oct 14, 2024 · 0 comments
Open

Super slow #40

johanneskoch94 opened this issue Oct 14, 2024 · 0 comments

Comments

@johanneskoch94
Copy link
Member

This should be replaced with a pre-filled out source, so that it doesn't have to be done for every conversion.

GDPuc/R/adapt_source.R

Lines 94 to 150 in f107ebc

if ("with_USA" %in% replace_NAs) {
USA_def_growth <- source %>%
dplyr::filter(.data$iso3c == "USA") %>%
dplyr::select("year", "gd" = "GDP deflator") %>%
dplyr::mutate("gd" = .data$gd / dplyr::lag(.data$gd))
source_adapted <- source_adapted %>%
dplyr::filter(.data$iso3c %in% unique(gdp$iso3c)) %>%
# Fill in the MER and PPPs with the growth rates from the USA (= 1)
dplyr::group_by(.data$iso3c) %>%
tidyr::fill(c("MER (LCU per US$)",
"PPP conversion factor, GDP (LCU per international $)"),
.direction = "downup") %>%
dplyr::ungroup() %>%
# For the deflator, we need to multiply the bordering values with the actual USA growth
dplyr::left_join(USA_def_growth, by = dplyr::join_by("year")) %>%
# Forward
dplyr::mutate(
`GDP deflator` = purrr::accumulate(
dplyr::row_number(),
~ dplyr::coalesce(.data$`GDP deflator`[.y], .x * .data$gd[.y]),
.init = NA
)[-1],
.by = c("iso3c")
) %>%
# Backward
dplyr::arrange(-.data$year) %>%
dplyr::mutate(
`GDP deflator` = purrr::accumulate(
dplyr::row_number(),
~ dplyr::coalesce(.data$`GDP deflator`[.y], .x / .data$gd[.y]),
.init = NA
)[-1],
.by = c("iso3c")
) %>%
dplyr::arrange(.data$iso3c, .data$year) %>%
dplyr::select(-"gd")
# If there is no data whatsoever for the country, use US values
ec <- dplyr::group_by(source_adapted, .data$iso3c) %>%
dplyr::filter(all(is.na(.data$`GDP deflator`))) %>%
dplyr::pull("iso3c") %>%
unique()
source_ec <- source %>%
dplyr::filter(.data$iso3c == "USA") %>%
tidyr::complete(iso3c = ec,
tidyr::nesting(!!!(rlang::syms(colnames(source)[-1])))) %>%
dplyr::filter(.data$iso3c %in% ec)
source_adapted <- source_adapted %>%
dplyr::filter(!.data$iso3c %in% ec) %>%
dplyr::bind_rows(source_ec)
}
source_adapted
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant