Skip to content

Instantly share code, notes, and snippets.

@randrescastaneda
Created July 8, 2023 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrescastaneda/9583229d58818cea5f2db642f184cf84 to your computer and use it in GitHub Desktop.
Save randrescastaneda/9583229d58818cea5f2db642f184cf84 to your computer and use it in GitHub Desktop.
compare NSE with SE in creating variables
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