Skip to content

Commit

Permalink
Merge branch 'DEV' into DEV_website
Browse files Browse the repository at this point in the history
  • Loading branch information
randrescastaneda committed Feb 20, 2024
2 parents f5d9543 + 56c7c9b commit 5a4bee6
Show file tree
Hide file tree
Showing 99 changed files with 4,354 additions and 375 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports:
cli,
collapse (>= 2.0.9),
data.table,
vctrs (>= 0.6.4),
wbpip (>= 0.1.0.9012)
Depends:
R (>= 2.10)
Expand Down
81 changes: 81 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,89 @@
# Generated by roxygen2: do not edit by hand

S3method(format,pipster_gd)
S3method(format,pipster_md)
S3method(get_gini,default)
S3method(get_gini,pipster_gd)
S3method(get_gini,pipster_md)
S3method(get_mld,default)
S3method(get_mld,pipster_gd)
S3method(get_mld,pipster_md)
S3method(get_polarization,default)
S3method(get_polarization,pipster_gd)
S3method(get_polarization,pipster_md)
S3method(get_pov_gap,default)
S3method(get_pov_gap,pipster_gd)
S3method(get_pov_gap,pipster_md)
S3method(get_pov_headcount,default)
S3method(get_pov_headcount,pipster_gd)
S3method(get_pov_headcount,pipster_md)
S3method(get_pov_severity,default)
S3method(get_pov_severity,pipster_gd)
S3method(get_pov_severity,pipster_md)
S3method(get_quantile,default)
S3method(get_quantile,pipster_gd)
S3method(get_quantile,pipster_md)
S3method(get_quantile_welfare_share,default)
S3method(get_quantile_welfare_share,pipster_gd)
S3method(get_quantile_welfare_share,pipster_md)
S3method(get_watts,default)
S3method(get_watts,pipster_gd)
S3method(get_watts,pipster_md)
S3method(get_welfare_share_at,default)
S3method(get_welfare_share_at,pipster_gd)
S3method(get_welfare_share_at,pipster_md)
S3method(vec_arith,pipster_gd)
S3method(vec_arith,pipster_md)
S3method(vec_arith.numeric,pipster_gd)
S3method(vec_arith.numeric,pipster_md)
S3method(vec_arith.pipster_gd,default)
S3method(vec_arith.pipster_gd,numeric)
S3method(vec_arith.pipster_gd,pipster_gd)
S3method(vec_arith.pipster_md,default)
S3method(vec_arith.pipster_md,numeric)
S3method(vec_arith.pipster_md,pipster_md)
S3method(vec_cast,double.pipster_gd)
S3method(vec_cast,double.pipster_md)
S3method(vec_cast,pipster_gd.double)
S3method(vec_cast,pipster_gd.pipster_gd)
S3method(vec_cast,pipster_md.double)
S3method(vec_cast,pipster_md.pipster_md)
S3method(vec_math,pipster_gd)
S3method(vec_math,pipster_md)
S3method(vec_ptype2,double.pipster_gd)
S3method(vec_ptype2,double.pipster_md)
S3method(vec_ptype2,integer.pipster_gd)
S3method(vec_ptype2,integer.pipster_md)
S3method(vec_ptype2,pipster_gd.double)
S3method(vec_ptype2,pipster_gd.integer)
S3method(vec_ptype2,pipster_gd.pipster_gd)
S3method(vec_ptype2,pipster_md.double)
S3method(vec_ptype2,pipster_md.integer)
S3method(vec_ptype2,pipster_md.pipster_md)
S3method(vec_ptype_abbr,pipster_gd)
S3method(vec_ptype_abbr,pipster_md)
export(as_pip)
export(as_pipster_gd)
export(as_pipster_md)
export(create_pipster_object)
export(get_gini)
export(get_mld)
export(get_polarization)
export(get_pov_gap)
export(get_pov_headcount)
export(get_pov_severity)
export(get_quantile)
export(get_quantile_welfare_share)
export(get_watts)
export(get_welfare_share_at)
export(identify_pip_type)
export(is_pipster_gd)
export(is_pipster_md)
export(pipgd_gini)
export(pipgd_lorenz_curve)
export(pipgd_mld)
export(pipgd_params)
export(pipgd_polarization)
export(pipgd_pov_gap)
export(pipgd_pov_headcount)
export(pipgd_pov_severity)
Expand All @@ -25,4 +103,7 @@ export(pipmd_quantile)
export(pipmd_quantile_welfare_share)
export(pipmd_watts)
export(pipmd_welfare_share_at)
export(pipster_gd)
export(pipster_md)
import(collapse)
import(vctrs)
5 changes: 2 additions & 3 deletions R/check_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ check_pipgd_params <- function(lp) {

## welfare -----------


## welfare and params -----------
if ( all(c("params", "welfare") %in% nlp)) {
if (!is.null(lp$params) &&
(!is.null(lp$welfare) || !is.null(lp$population))) {
(!is.null(lp$welfare) || !is.null(lp$weight))) {
cli::cli_abort("You must specify either {.field params} or
{.field welfare} and {.field population}")
{.field welfare} and {.field weight}")
}
}

Expand Down
103 changes: 103 additions & 0 deletions R/create_pipster_object.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#' Create Pipster Object
#'
#' The first step in using the `pipster` package is to create a pipster object.
#' It creates the appropriate classes for welfare and weights, reformats the
#' data if necessary, and, for grouped data, it estimates and selects the lorenz
#' curves to be used for the estimation of poverty and distributional measures.
#'
#' @param welfare numeric: welfare vector
#' @param weight numeric: weight vector
#' @param imputation_id numeric: vector of ids for multiply imputed data.
#' Default is NULL
#'
#' @return list: pipster object containing welfare and weights,
#' params if grouped data, imputation_id if imputed data
#' @export
#'
#' @examples
#' p <- create_pipster_object(welfare = pip_gd$L,
#' weight = pip_gd$P)
#' p
create_pipster_object <-
function(welfare,
weight = rep(1, length(welfare)),
imputation_id = NULL) {

#_____________________________________________________________________________
# Arguments-------------------------------------------------------------------
if (is.na(welfare) |> any()) {
cli::cli_abort("No elements in welfare vector can be NA")
}
if (!is.numeric(welfare)) {
cli::cli_abort("welfare must be numeric")
}
if (length(weight) > 1 & any(is.na(weight))) {
cli::cli_abort("No elements in weight vector can be NA -
leave argument empty to give equal weighting")
}

#_____________________________________________________________________________
# Class-----------------------------------------------------------------------
tp <- identify_pip_type(welfare = welfare,
weight = weight,
imputation_id = imputation_id)
cl <- substr(tp, start = 1, stop = 2)
#_____________________________________________________________________________
# Convert format--------------------------------------------------------------
weight <- weight[order(welfare)]
welfare <- welfare[order(welfare)]
if (tp %in% c("gd_2", "gd_3", "gd_5")) {
welfare_original <- welfare
weight_original <- weight
}
switch(
tp,
"md" = {
imputation_id <- rep(1, length(welfare))
},
"id" = {
cl <- "md"
},
"gd_1" = invisible(TRUE),
"gd_2" = {
welfare <- fcumsum(welfare)/fsum(welfare)
weight <- fcumsum(weight)/fsum(weight)
},
"gd_3" = {
welfare <- fcumsum(welfare)/fsum(welfare)
},
"gd_5" = {
welfare <- fcumsum(welfare)/fsum(welfare)
weight <- fcumsum(weight)/fsum(weight)
}
)

#_____________________________________________________________________________
# Params----------------------------------------------------------------------
if (cl == "gd") {
params <- pipgd_select_lorenz(welfare = welfare,
weight = weight,
complete = TRUE)
}

#_____________________________________________________________________________
# Return----------------------------------------------------------------------
class_func <- paste0("new_pipster_", cl) |>
parse(text = _)
ret <- list(
welfare = eval(class_func)(welfare),
weight = eval(class_func)(weight)
)
if (cl == "gd") {
ret$params <- params
} else {
ret$imputation_id <- imputation_id
}
class(ret) <- "pipster"

ret

}



Loading

0 comments on commit 5a4bee6

Please sign in to comment.