Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
evaaepelde committed Sep 18, 2024
1 parent ca254f5 commit 196b2f3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 48 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export(rename_values)
export(standardize)
export(weighted.median)
export(weighted.quantile)
importFrom(dplyr,"%>%")
3 changes: 3 additions & 0 deletions R/main.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ex_shocks <- function(){
#' distributional impacts, if save TRUE. By default "DI_impacts".
#' @param fig if TRUE (by default) create and save the figures of the distributional
#' impacts calculated by the function. If FALSE do not create neither save.
#' @importFrom dplyr %>%
#' @return a list containing the generated datasets summarising the basic or/and the
#' intersectional distributional impacts per selected variable or set of variables.
#' @export
Expand Down Expand Up @@ -176,6 +177,7 @@ ex_var_intersec <- function(){
#' @param index energy poverty index or indices to be calculated. Possible
#' options: 10%, 2M, LIHC, HEP, HEP_LI. If "all" (by default) calculates
#' all the indices for the selected year/s.
#' @importFrom dplyr %>%
#' @return a dataframe with the selected energy poverty indices
#' @export
calc_ep <- function(year, index = "all"){
Expand Down Expand Up @@ -256,6 +258,7 @@ calc_ep <- function(year, index = "all"){
#' @param index transport poverty index or indices to be calculated. Possible
#' options: 10%, 2M, LIHC, VTU. If "all" (by default) calculates all the indices
#' for the selected year/s.
#' @importFrom dplyr %>%
#' @return a dataframe with the selected transport poverty indices
#' @export
calc_tp <- function(year, index = "all"){
Expand Down
14 changes: 14 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ options(dplyr.summarise.inform = FALSE)
#' values detailed in the mapping included in the package.
#' @param data dataset to be standardized.
#' @param current_var column name to be standardized.
#' @importFrom dplyr %>%
#' @return a dataset with labels renamed based in the mapping included in the package.
#' @export
rename_values = function(data, current_var) {
Expand All @@ -35,6 +36,7 @@ rename_values = function(data, current_var) {
#'
#' Function to standarize data names.
#' @param data dataset to be standardized.
#' @importFrom dplyr %>%
#' @return a dataset with the variables and labels renamed based in the mapping included in the package.
#' @export
standardize <- function(data) {
Expand Down Expand Up @@ -138,6 +140,7 @@ weighted.median <- function(x, w, na.rm=TRUE, type=2, collapse=TRUE) {
#'
#' Function to identify energy poor households from 2016
#' @param data dataset with the data from the HBS.
#' @importFrom dplyr %>%
#' @return a dataset with HBS data where energy poor households are identified.
#' @export
id_ep1 <- function(data){
Expand Down Expand Up @@ -178,6 +181,7 @@ id_ep1 <- function(data){
#'
#' Function to identify energy poor households before 2016 (included)
#' @param data dataset with the data from the HBS.
#' @importFrom dplyr %>%
#' @return a dataset with HBS data where energy poor households are identified.
#' @export
id_ep2 <- function(data){
Expand Down Expand Up @@ -218,6 +222,7 @@ id_ep2 <- function(data){
#' Function to identify transport poor households before 2015 (included)
#' @param data dataset with the data from the HBS.
#' @param year year of the HBS data.
#' @importFrom dplyr %>%
#' @return a dataset with HBS data where transport poor households are identified.
#' @export
id_tp <- function(data, year){
Expand Down Expand Up @@ -286,6 +291,7 @@ id_tp <- function(data, year){
#' @param year year of the HBS you want to load.
#' @param path Local path to the folder where the HBS's are stored. Not included in the package.
#' @param path_outputs path to save the results (RData)
#' @importFrom dplyr %>%
#' @return a list with the 3 files of the HBS.
#' @export
load_rawhbs <- function(year, path, path_outputs) {
Expand Down Expand Up @@ -462,6 +468,7 @@ load_rawhbs <- function(year, path, path_outputs) {
#' (HBS) according to the aggregation (coicop_year) specified in the package.
#' @param data dataset with the data from the HBS.
#' @param year year of the HBS to be modified according to the aggregation specified in the package.
#' @importFrom dplyr %>%
#' @return a dataset with HBS data where COICOP categories are aggregated according to the classification specified in the package.
#' @export
add_coicop <- function(data, year) {
Expand Down Expand Up @@ -508,6 +515,7 @@ add_coicop <- function(data, year) {
#' @param year year of the HBS you want to elevate to national accounting.
#' @param country country of the HBS you want to elevate. By default "ES"
#' (for the moment it only works for Spain, so DO NOT TOUCH).
#' @importFrom dplyr %>%
#' @return a dataset with the HBS data where expenses are elevated to national accounting.
#' @export
elevate_hbs <- function(data, year, country = "ES") {
Expand Down Expand Up @@ -661,6 +669,7 @@ elevate_hbs <- function(data, year, country = "ES") {
#' the package, if you are not going to aggregate the COICOP variables you have to
#' replace the column labels by the COICOP variables that appear in your dataset.
#' @param year base year for the simulation. It must be the same as the year of the HBS.
#' @importFrom dplyr %>%
#' @return a dataset with the HBS data and the new expenses for COICOP categories
#' after the application of the price shock.
#' @export
Expand Down Expand Up @@ -778,6 +787,7 @@ adjust_wh_is <- function(data, var_w, var_h) {
#' Function to order the labels of the socioeconomic and demographic variables
#' @param data dataset in which we want to order the labels of the socioeconomic and demographic variables
#' @param g variable for which we want to sort the labels
#' @importFrom dplyr %>%
#' @return a dataset in which the labels are ordered for the selected socioeconomic or demographic variable
#' @export
order_var <- function(data, g){
Expand Down Expand Up @@ -813,6 +823,7 @@ order_var <- function(data, g){
#' impacts for each of the variables specified in the package. If not, you can
#' indicate a variable or a vector of variables to crate the graph.If you want to
#' see the variables for which the function is available run `available_var_impact()`.
#' @importFrom dplyr %>%
#' @return a graph per selected variable/s summarizing distributional impacts.
#' @export
basic_graph <- function(data, var = categories$categories){
Expand Down Expand Up @@ -874,6 +885,7 @@ basic_graph <- function(data, var = categories$categories){
#' @param fig generates and saves a graph that summarises the distributional impacts.
#' By default it is TRUE, for the graph/s not to be generated and saved indicate FALSE.
#' @param shocks_scenario_names vector of the names of the considered scenario shocks
#' @importFrom dplyr %>%
#' @return a list containing the generated datasets (.RData) summarising the
#' distributional impacts per selected variable.
#' @export
Expand Down Expand Up @@ -935,6 +947,7 @@ impact <- function(data, var = categories$categories, save = T, file_name = "D_i
#' variables for the calculation, it must follow the same format as the output of
#' `available_var_intersec()`, i.e. a table whose columns have category_a and
#' category_b as their titles.
#' @importFrom dplyr %>%
#' @return a graph per selected set of variables summarizing the distributional impacts.
#' @export
intersectional_graph <- function(data, pairs = is_categories){
Expand Down Expand Up @@ -1024,6 +1037,7 @@ intersectional_graph <- function(data, pairs = is_categories){
#' @param fig generates and saves a graph that summarises the intersectional distributional
#' impacts. By default it is TRUE, for the graph/s not to be generated and saved indicate FALSE.
#' @param shocks_scenario_names vector of the names of the considered scenario shocks
#' @importFrom dplyr %>%
#' @return a list containing the generated datasets (.RData) summarising the intersectional
#' distributional impacts per selected set of variables.
#' @export
Expand Down
86 changes: 56 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
---
editor_options:
markdown:
wrap: 72
---

# MEDUSA - Modelling Equity and DistribUtional impacts for Socioeconomic Analysis

[![docs](https://github.com/bc3LC/medusa/actions/workflows/docs.yaml/badge.svg)](https://github.com/bc3LC/medusa/actions/workflows/docs.yaml)
Expand All @@ -7,15 +13,15 @@
[![build](https://github.com/bc3LC/medusa/actions/workflows/build.yaml/badge.svg)](https://github.com/bc3LC/medusa/actions/workflows/build.yaml)
[![draft-pdf](https://github.com/bc3LC/medusa/actions/workflows/draft-pdf.yml/badge.svg)](https://github.com/bc3LC/medusa/actions/workflows/draft-pdf.yml)

## <a name="contents"></a>Contents
## <a name="contents"></a>Contents {#contents}

<!-- ------------------------>

<!-- ------------------------>

- [Introduction](#introduction)

- [The package](#pkg)
- [The package](#pkg)

- [The microsimulation model](#ms-model)

Expand All @@ -27,7 +33,7 @@

<!-- ------------------------>

## <a name="introduction"></a>Introduction
## <a name="introduction"></a>Introduction {#introduction}

<!-- ------------------------>

Expand All @@ -37,16 +43,29 @@

### <a name="pkg"></a>The package

MEDUSA is an R package that allows the development of distributional analyses in isolation or in connection with other models (soft links). The extensive database in which the microsimulation model is based allows for highly disaggregated results, taking into account numerous socioeconomic and demographic characteristics of households, such as income level, place of residence, type of family or the feminization degree of the household. Additionally, the package combines these with the calculation of energy and transport poverty indices.
MEDUSA is an R package that allows the development of distributional
analyses in isolation or in connection with other models (soft links).
The extensive database in which the microsimulation model is based
allows for highly disaggregated results, taking into account numerous
socioeconomic and demographic characteristics of households, such as
income level, place of residence, type of family or the feminization
degree of the household. Additionally, the package combines these with
the calculation of energy and transport poverty indices.

:exclamation::exclamation: **Note**: At the moment, `medusa` package works for Spain, but in the short term the idea is to extend it to all EU countries. Moreover, the package could be extended to all countries that are able to provide the raw data of the model.
:exclamation::exclamation: **Note**: At the moment, `medusa` package
works for Spain, but in the short term the idea is to extend it to all
EU countries. Moreover, the package could be extended to all countries
that are able to provide the raw data of the model.

### <a name="ms-model"></a>The microsimulation model

MEDUSA allows to develop distributional analyses based in an overnight effect microsimulation model. The model is built up with the microdata from the Household Budget Survey. For more details on the model in which `medusa` is based click [here](https://bc3lc.github.io/medusa/articles/TheModel.html).

MEDUSA allows to develop distributional analyses based in an overnight
effect microsimulation model. The model is built up with the microdata
from the Household Budget Survey. For more details on the model in which
`medusa` is based click
[here](https://bc3lc.github.io/medusa/articles/TheModel.html).

## <a name="installation-guide"></a>Installation Guide
## <a name="installation-guide"></a>Installation Guide {#installation-guide}

<!-- ------------------------>

Expand All @@ -60,17 +79,21 @@ To use `medusa` package first you need to follow this steps:

- To download R click [here](https://www.r-project.org/)

- To download Rstudio click [here](https://www.rstudio.com/)
- To download Rstudio click [here](https://www.rstudio.com/)

2. Load `medusa` package. For this, open Rstudio and load the library:

``` r
install.packages('remotes')
options(timeout=400) # to prevent errors if the connection is slow
remotes::install_github("bc3LC/medusa")
library(medusa)
```
<br>
Now the package is fully loaded and you can start using its functions.

:exclamation::exclamation: This may take a few minutes because the package contains a lot of data.

<br>
Now the package is fully loaded and you can start using its functions.
<br>

## <a name="get-started"></a>Getting Started
Expand All @@ -81,21 +104,24 @@ Now the package is fully loaded and you can start using its functions.

[Back to Contents](#contents)

The `medusa` package contains 3 main blocks of functions:

1. Module 1. Functions to calculate distributional impacts
- Main function: `calc_di`, `ex_shocks`, `available_var_impact` , `available_var_intersec` and `ex_var_intersec`.
- Auxiliary functions: `load_rawhbs`, `rename_values`, `standardize`, `add_coicop`, `elevate_hbs`, `price_shock`, `impact`, `impact_intersectional`, `basic_graph`, `intersectional_graph`, `order_var`, `adjust_wh` and `adjust_wh_is`.


2. Module 2. Functions to calculate energy poverty indices
- Main function: `calc_ep`
- Auxiliary functions: `id_ep1`, `id_ep2`, `weighted.median` and `weighted.quantile`.


3. Module 3. Functions to calculate transport poverty indices
- Main function: `calc_tp`
- Auxiliary functions: `id_tp`, `weighted.median` and `weighted.quantile`.


In addition, the package includes some default input files (.Rda), that are read by the different functions.
The `medusa` package contains 3 main blocks of functions:

1. Module 1. Functions to calculate distributional impacts
- Main function: `calc_di`, `ex_shocks`, `available_var_impact` ,
`available_var_intersec` and `ex_var_intersec`.
- Auxiliary functions: `load_rawhbs`, `rename_values`,
`standardize`, `add_coicop`, `elevate_hbs`, `price_shock`,
`impact`, `impact_intersectional`, `basic_graph`,
`intersectional_graph`, `order_var`, `adjust_wh` and
`adjust_wh_is`.
2. Module 2. Functions to calculate energy poverty indices
- Main function: `calc_ep`
- Auxiliary functions: `id_ep1`, `id_ep2`, `weighted.median` and
`weighted.quantile`.
3. Module 3. Functions to calculate transport poverty indices
- Main function: `calc_tp`
- Auxiliary functions: `id_tp`, `weighted.median` and
`weighted.quantile`.

In addition, the package includes some default input files (.Rda), that
are read by the different functions.
4 changes: 3 additions & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ articles:
Tutorials: Tutorials.html
TutorialsEnergyPoverty: TutorialsEnergyPoverty.html
TutorialsTransportPoverty: TutorialsTransportPoverty.html
last_built: 2024-09-13T08:27Z
last_built: 2024-09-18T14:47Z
2 changes: 1 addition & 1 deletion man/calc_ep.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/calc_tp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 196b2f3

Please sign in to comment.