Created
July 8, 2023 03:35
-
-
Save randrescastaneda/9583229d58818cea5f2db642f184cf84 to your computer and use it in GitHub Desktop.
compare NSE with SE in creating variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
ai_nse <- function(dt) { | |
dt[, | |
bottom40 := decile1 + decile2 + decile3 + decile4, | |
][, | |
`:=`( | |
pop_in_poverty = wbpip::get_lh_number_poor( | |
headcount = headcount, | |
pop = pop), | |
average_shortfall = wbpip::get_lh_average_shortfall( | |
headcount = headcount, | |
povgap = poverty_gap, | |
povline = poverty_line), | |
total_shortfall = wbpip::get_lh_total_shortfall( | |
headcount = headcount, | |
pop = pop, | |
povgap = poverty_gap, | |
povline = poverty_line), | |
income_gap_ratio = wbpip::get_lh_income_gap_ratio( | |
headcount = headcount, | |
povgap = poverty_gap), | |
palma_ratio = wbpip::get_lh_palma_ratio( | |
top10 = decile10, | |
bottom40 = bottom40), | |
p90p10_ratio = wbpip::get_lh_9010_ratio(top10 = decile9, | |
bottom10 = decile1) | |
) | |
] | |
} | |
ai_se <- function(dt) { | |
dt$bottom40 <- dt$decile1 + dt$decile2 + dt$decile3 + dt$decile4 | |
dt$pop_in_poverty = wbpip::get_lh_number_poor( | |
headcount = dt$headcount, | |
pop = dt$pop) | |
dt$average_shortfall = wbpip::get_lh_average_shortfall( | |
headcount = dt$headcount, | |
povgap = dt$poverty_gap, | |
povline = dt$poverty_line) | |
dt$total_shortfall = wbpip::get_lh_total_shortfall( | |
headcount = dt$headcount, | |
pop = dt$pop, | |
povgap = dt$poverty_gap, | |
povline = dt$poverty_line) | |
dt$income_gap_ratio = wbpip::get_lh_income_gap_ratio( | |
headcount = dt$headcount, | |
povgap = dt$poverty_gap) | |
dt$palma_ratio = wbpip::get_lh_palma_ratio( | |
top10 = dt$decile10, | |
bottom40 = dt$bottom40) | |
dt$p90p10_ratio = wbpip::get_lh_9010_ratio(top10 = dt$decile9, | |
bottom10 = dt$decile1) | |
} | |
dt <- pipr::get_stats(fill_gaps = TRUE) | |
setDT(dt) | |
bench <- microbenchmark::microbenchmark( | |
times = 50, | |
nse = ai_nse(dt), | |
se = ai_se(dt) | |
) | |
if (requireNamespace("highcharter")) { | |
hc_dt <- highcharter::data_to_boxplot(bench, | |
time, | |
expr, | |
add_outliers = FALSE, | |
name = "Time in milliseconds") | |
highcharter::highchart() |> | |
highcharter::hc_xAxis(type = "category") |> | |
highcharter::hc_chart(inverted=TRUE) |> | |
highcharter::hc_add_series_list(hc_dt) | |
} else { | |
boxplot(bench, outline = FALSE) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment