Skip to content

Instantly share code, notes, and snippets.

View randrescastaneda's full-sized avatar
😊
I may be slow to respond.

R.Andrés Castañeda randrescastaneda

😊
I may be slow to respond.
View GitHub Profile
@randrescastaneda
randrescastaneda / use_with.R
Created February 21, 2024 14:49
How to use with
foo <- function(x) {
d <- "r"
s <- with(x , {
r <- if (exists("a", inherits = FALSE)) {
a + b
} else {
0
}
dd <- if (exists("d", inherits = FALSE)) {
1
@randrescastaneda
randrescastaneda / alt_coverage.R
Created January 19, 2024 16:39
Check coverage of file when `test_coverage_active_file()` does not find Rds files in tests
alt_coverage <- function(file_name = devtools:::find_active_file() |>
fs::path_file(),
report = TRUE) {
rf <- fs::path("R", file_name)
tf <- "tests/testthat" |>
fs::path(paste0("test-", file_name))
dd <- covr::file_coverage(source_files = rf,
test_files = tf)
@randrescastaneda
randrescastaneda / CHN_update.R
Created August 14, 2023 20:05
Estimate poverty and mean in CHN using new files
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# libraries ---------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library(data.table)
library(collapse)
library(ggplot2)
options(pipload.verbose = FALSE)
@randrescastaneda
randrescastaneda / nse_vs_se.R
Created July 8, 2023 03:35
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),
@randrescastaneda
randrescastaneda / IND_2016_deciles_lineup_replicate.R
Created July 6, 2023 22:52
Replicate PIP deciles using imputes data with weights adjusted
library(data.table)
library(tidyverse)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# load data ---------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
path <- fs::path("p:/03.pip/estimates/lineup/IND_2016_survey_year.fst")
sdt <- fst::read_fst(path = path)
@randrescastaneda
randrescastaneda / lineup_testing.R
Last active June 28, 2023 15:05
Test lineup dataset for group data and imputed data. Check with current PIP API
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# load packages ---------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# library(dtplyr) # worth talking about this.
library(data.table)
library(tidyverse)
# If you don't have installed some of the packages that I use below you, it is
# likely that Rstudio is going to let you know that you need to install them.
@randrescastaneda
randrescastaneda / parse_endpoints.R
Created June 7, 2023 15:38
Parse PIP API endpoints documentation to text file
library(data.table)
url <- "https://raw.githubusercontent.com/PIP-Technical-Team/pipapi/master/inst/plumber/v1/endpoints.R"
lns <- readLines(con = url) |>
{\(.) .[grep("\\@(get|param)", .)]}()
@randrescastaneda
randrescastaneda / replace_text_in_file.R
Created November 22, 2022 22:16
replace the same text pattern in different files
rfile <- "R/pip_indicators.R"
rfiles <- fs::dir_ls("R", type = "file", regexp = "[rR]$")
srch <- 'msrdir <- fs::path\\(maindir, "_aux", branch, measure\\)'
rplc <-
"if (branch == \"main\") {
branch <- \"\"
}
msrdir <- fs::path(maindir, \"_aux\", branch, measure)"
@randrescastaneda
randrescastaneda / extra_vars.R
Last active August 20, 2022 15:02
Find Variables available in one data.frame not available in another.
#' Find variable that are not available in one on another data.frame
#'
#' @param x data.frame 1
#' @param y data.frame 2
#'
#' @return
#' @export
#'
#' @examples
extra_vars <- function(x, y) {
@randrescastaneda
randrescastaneda / compare_pip_versions.R
Created May 4, 2022 20:43
Compare versions of PIP
# ____________________________________________________________________________
# country level ####
sr0 <- pipr::get_stats("SYR", fill_gaps = TRUE, server = "dev")
sr1 <-
pipr::get_stats("SYR",
fill_gaps = TRUE,
server = "dev",
version = "20220428_2011_02_02_PROD")