Last active
March 15, 2023 14:42
-
-
Save JosepER/2cbac9ad8bf9158c44ace80069de7782 to your computer and use it in GitHub Desktop.
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(rlang) | |
library(dplyr) | |
it14ih <- data.frame(hid = 1:6, | |
hi11 = c(3601, 13190, 0, 11479, 0, 4713), | |
hi13 = c(973, 0, 0, 478, 815, 399), | |
hi12 = c(0, 0, 0, 0, 0, 842), | |
hicapital = c(40, 41, 0, 100, 0, 83), | |
hpopwgt = c(567.1, 5308.3, 666.7, | |
692.2, 254.2, 632.6) | |
) | |
# Lower-level function | |
compute_column_from_formula <- function(data, formula) { | |
# Check if the input is a data.frame | |
if (!is.data.frame(data)) { | |
stop("The first argument should be a data.frame") | |
} | |
# Check if the input is a formula | |
if (!rlang::is_formula(formula)) { | |
stop("The second argument should be a formula") | |
} | |
# Extract the left-hand and right-hand sides of the formula | |
lhs <- as.character(formula[[2]]) | |
rhs <- formula[[3]] | |
# Evaluate the right-hand side expression | |
new_column_values <- eval(rhs, data) | |
# Add the new column to the data.frame with the name from the left-hand side of the formula | |
data[[lhs]] <- new_column_values | |
# Return the updated data.frame | |
return(data) | |
} | |
# Higher-level function | |
compute_columns_from_formulas <- function(data, formulas) { | |
# Check if the input is a data.frame | |
if (!is.data.frame(data)) { | |
stop("The first argument should be a data.frame") | |
} | |
# Check if the input is a list of formulas | |
if (!is.list(formulas) || !all(sapply(formulas, rlang::is_formula))) { | |
stop("The second argument should be a list of formulas") | |
} | |
# Iterate through the list of formulas and add a new column for each formula | |
for (formula in formulas) { | |
data <- compute_column_from_formula(data, formula) | |
} | |
# Return the updated data.frame | |
return(data) | |
} | |
estimates_from_microdata <- function(data, formulas, weights){ | |
df <- compute_columns_from_formulas(data, formulas) | |
# weighted sum of new variables | |
new_vars <- purrr::map_chr(formulas, ~as.character(.x[[2]])) | |
tibble::enframe(purrr::map2_dbl(set_names(new_vars), weights, .f = function(var, weights, data){sum(df[[var]] * df[[weights]])}, data), | |
name = "indicator") | |
} | |
estimates_from_microdata(data = it14ih, | |
formulas = list(D11P ~ hi11 + hi13, | |
`B3GR+D41R+D42R+D45R` ~ hi12 + hicapital), | |
weights = c("hpopwgt", "hpopwgt")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment