---
title: "MPI_demo"
author: "Kittiya Kukiattikun"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{MPI_demo}
  %\VignetteEngine{knitr::knitr}
  %\VignetteEncoding{UTF-8}
  
---

## Simple Simulation

```{r warning=F, message=F}
library(MPI)
library(kableExtra) 
```

### Loading Data

`MPI::examplePovertydf` is a simulation poverty data frame contains 16 indicators column which 1 means deprived and 0 means not deprived, and simulated forth-level administrative division of France. 

```{r}
data(examplePovertydf)
```
```{r example, results = 'asis', echo = FALSE}
kable(head(examplePovertydf, n = 3), 
      "html", 
      col.names = gsub("[.]", " ", names(examplePovertydf))) %>% 
  kable_styling()
```

### Calculation

For calculating MPI using `AF_Seq` for sequential run or `AF_Par` for parallel run.

Input will be 

* `df` A poverty data frame
* `g` A column name that will be used to divide data into groups. When the value is NULL, the entire data is not separated into groups.
* `w` An indicator weight vectors
* `k` A poverty cut-off. If an aggregate value of indicators of a specific person is above or equal the value of k, then this person is considered to be a poor.(default as 1)
```{r}
out_seq <- AF_Seq(examplePovertydf, g = "Region", k = 3)
```

Output will be *list of lists* separated into group, and each list contains

* `groupname`  A Grouped value from column input `g`
```{r, echo=FALSE}
out_seq[[1]]$groupname
```

* `total`  Number of population in each group
```{r, echo=FALSE}
out_seq[[1]]$total
```

* `poors`  Number of deprived people in each group
```{r, echo=FALSE}
out_seq[[1]]$poors
```

* `H` Head count ratio, the proportion of the population that is multidimensionally deprived calculated by divide the number of poor people with the total number of people. 
```{r, echo=FALSE}
out_seq[[1]]$H
```
* `A` Average deprivation share among poor people, by aggregating the proportion of total deprivations each person and dividing by the total number of poor people.
```{r, echo=FALSE}
out_seq[[1]]$A
```
* `M0` Multidimensional Poverty Index, calculated by H times A. 
```{r, echo=FALSE}
out_seq[[1]]$M0
```
* `DimentionalContribution`
  + `indnames` The poverty indicators
  + `diCont` Dimensional contributions denotes the magnitude of each indicator impacts on MPI.
  + `UncensoredHCount` Uncensored head count of indicator denotes the population that are deprived in that indicator.
  + `UncensoredHRatio` Uncensored head count ratio of indicator denotes the proportion of the population deprived in that indicator.
  + `CensoredHCount` Censored head count of indicator denotes the population that are multidimensionally poor and deprived in that indicator at the same time.
  + `CensoredHRatio` Censored head count ratio of indicator denotes the proportion that is multidimensionally poor and deprived in that indicator at the same time.
```{r outt1, results = 'asis', echo=FALSE}
kable(out_seq[[1]]$DimentionalContribution, 
            "html",
            col.names = gsub("[.]", " ", names(out_seq[[1]]$DimentionalContribution))) %>%
  kable_styling()
```
* `pov_df` poverty data frame 
  + `Cvector` is a vector of total values of deprived indicators adjusted by weight of indicators. Each element in `Cvector` represents a total value of each individual.
  + `IsPoverty` is a binary variable with only 1 and 0, with 1 indicating that person does not meet the threshold(poor person) and 0 indicating the opposite.
  + `Intensity` The intensity of a deprived indication among impoverished people is computed by dividing the number of deprived indicators by the total number of indicators.
```{r outt2, results = 'asis', echo=FALSE}
kable(out_seq[[1]]$pov_df, 
            "html",
            col.names = gsub("[.]", " ", names(out_seq[[1]]$pov_df))) %>%
  kable_styling()
```
