---
title: "Introduction to Mexican Regional Gross Domestic Product"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to Mexican Regional Gross Domestic Product}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(mrgdp)
```

Autors: 

Dr. Marco Antonio Pérez-Méndez
Dra. Hortensia Moreno Macias
Mtra. Jocabed Sánchez Martínez

In this user guide you can see how to use Mexican Regional Gross Domestic Product package (mrgdp). We built several functions to download economic census, filter municipal data and join several years.

## General Description

_mrgdp_ package allows to download, select, clean and built variables from mexican economic census for several years (2004, 2009, 2014 and 2019) and all federal entities. We can filter the census data table by municipal data and build a data.frame for all federal entities and several years. With this data frame we can test a regional convergence hypotesis with econometric models. 

## Objectives

The objectives resesearch are: 1) identifie correct data to built municipal gross domestic product proxy variable; 2) select any federal entitie to built, for several years, the municipal gross domestic product for 2004, 2009, 2014 and 2019; 3) create a data frame with municipal gross domestic product to run an econometric model to test regional convergence hypotesis. 

## Theoretical bases

## programm list

This version of "Mexican Regional Gross Domestic Product" (mrgdp) package include  4 basic functions: 
1.- Economic_units()
2.- pbt_munR()
3.- economic_census()
4.- PBTM_04-19()

## Flow chart

```{r eval=FALSE, include=FALSE}
library(DiagrammeR)

DiagrammeR::grViz("digraph {

# initiate graph
graph [layout = dot, rankdir = LR, label = 'mrgdp package process\n\n',labelloc = t]

# global node settings
node [shape = rectangle, style = filled, fillcolor = Linen]

# label nodes
data1 [label = 'INEGI \n Economic \n Census \n  URL', shape = folder, fillcolor = Beige]
mrgdp [label =  'mrgdp \n package \n functions']
fa [label= 'function \n arguments' , shape= parallelogram]
economic_census [label = 'economic_census() ']
economic_units [label = 'economic_units() ']
pbtm [label = 'PBTM_04_19() ']
results [label= 'Results', shape = diamond]
results2 [label= 'Results2', shape = diamond]
results3 [label= 'Results3', shape = diamond]
results4 [label= 'Results4', shape = diamond]
ecd [label='Economic \n census \n downloaded', shape=cylinder]
pbtmun [label= 'pbt_mun()' ]
pbtmun2 [label = 'Municpal \n gross \n domestic \n product', shape=cylinder]
ecu [label='Economic \n units \n downloaded', shape=cylinder]
pbtmun4 [label = 'Municpal \n gross \n domestic \n product \n panel', shape=cylinder]


# edge definitions with the node IDs
{data1}  -> fa -> mrgdp -> {economic_census economic_units pbtm}
economic_census -> results
results -> {good bad}
bad -> {'wrong arguments'} -> fa
good -> ecd -> pbtmun
pbtmun -> results2 -> {good2 bad2}
good2 -> pbtmun2
bad2 -> {'wrong arguments'} -> fa
economic_units -> results3 ->{good3, bad3}
good3 -> ecu
bad3 -> {'wrong arguments'} -> fa
pbtm -> results4 -> {good4 bad4}
good4 -> pbtmun4
bad4 -> {'wrong arguments'} -> fa
}")
```
The internal process of any function is:

```{r eval=FALSE, include=FALSE}
DiagrammeR::grViz("digraph {

# initiate graph
graph [layout = twopi, rankdir = RL, label = 'Results_i internal process',labelloc = t]

# global node settings
node [shape = box, style = filled, fillcolor = Linen]

# label nodes
functions [label='Select mrgdp function', shape=parallelogram]
arguments [label='function arguments', shape=parallelogram]
conect [label = 'INEGI Economic \n Census URL conection']
download [label= 'Download \n Economic Census in \n temporal memory']
filter [label= 'Filter data \n  by \n federal entitie \n code']
build [label='build a dataframe \n of filtered data', shape=cylinder]
clean [label='clean temporal memory']

# edge definitions with the node IDs
functions -> arguments -> conect -> download ->filter -> build -> clean
}")
```


## Operation demostration

The arguments for functions are: 

* year (2004, 2009, 2014 or 2019)
* state:

| state   | entitie             |
----------|---------------------|
| "ags"   | Aguascalientes      |
| "bc"    | Baja California     |
| "bcs"   | Baja California Sur |
|"camp"   | Campeche            |
| "coah"  | Coahuila            |
| "col"   | Colima              |
| "chis"  | Chiapas             |
| "chih"  | Chihuahua           |
| "cdmx"  | Ciudad de México    |
| "dgo"   | Durango             |
| "gto"   | Guanajuato          |
| "gro"   | Guerrero            |
| "hgo"   | Hidalgo             |
| "jal"   | Jalisco             |
| "mex"   | México              |
|"mich"   | Michoacán           |
| "mor"   | Morelos             |
| "nay"   | Nayarit             |
| "nl"    | Nuevo León          |
| "pue"   |  Puebla             |
| "qro"   | Queretaro           |
| "qroo"  | Quintana Roo        |
| "slp"   | San Luis Potosí     |
| "sin"   | Sinaloa             |
| "son"   | Sonora              |
| "tab"   | Tabasco             |
| "tamps" | Tamaulipas          |
| "tlax"  | Tlaxcala            |
| "ver"   | Veracruz            |
| "yuc"   | Yucatán             |
| "zac"   | Zacatecas           |
| "oax"   | Oaxaca              |

select economic_census() function and type '2004' and "ags". The function downloads the economic census for federal entitie and year selected. With names() function we can see the names of all variables on economic census.

```{r}
#data <- economic_census(2004, "ags")
#names(data)
```

  [1] "entidad"    "municipio"  "codigo"     "id_estrato"
  [5] "ue"         "a111a"      "a121a"      "a131a"     
  [9] "a211a"      "a221a"      "a511a"      "a700a"     
 [13] "a800a"      "h000a"      "h000b"      "h000c"     
 [17] "h000d"      "h001a"      "h001b"      "h001c"     
 [21] "h001d"      "h010a"      "h010b"      "h010c"     
 [25] "h010d"      "h020a"      "h020b"      "h020c"     
 [29] "h020d"      "h101a"      "h101b"      "h101c"     
 [33] "h101d"      "h203a"      "h203b"      "h203c"     
 [37] "h203d"      "i000a"      "i000b"      "i000c"     
 [41] "i000d"      "i100a"      "i100b"      "i100c"     
 [45] "i100d"      "i200a"      "i200b"      "i200c"     
 [49] "i200d"      "j000a"      "j010a"      "j203a"     
 [53] "j300a"      "j400a"      "j500a"      "j600a"     
 [57] "k000a"      "k010a"      "k020a"      "k030a"     
 [61] "k042a"      "k412a"      "k050a"      "k060a"     
 [65] "k070a"      "k090a"      "k096a"      "k311a"     
 [69] "k610a"      "k620a"      "k810a"      "k820a"     
 [73] "k910a"      "k950a"      "k976a"      "m000a"     
 [77] "m010a"      "m020a"      "m030a"      "m050a"     
 [81] "m090a"      "m091a"      "m700a"      "o010a"     
 [85] "o020a"      "p000a"      "p000b"      "p000c"     
 [89] "p030a"      "p030b"      "p030c"      "p100a"     
 [93] "p100b"      "q000a"      "q000b"      "q000c"     
 [97] "q000d"      "q010a"      "q020a"      "q030a"     
[101] "q400a"      "q900a"      "a112a"      "a113a"     
[105] "a114a"      "a115a"      "a116a"      "a117a"     
[109] "a118a"      "a119a"      "a141a"      "a143a"     
[113] "a144a"      "a145a"      "a146a"      "a147a"     
[117] "a148a"      "a149a"      "a151a"      "a152a"     
[121] "a153a"      "a154a"      "a155a"      "a156a"     
[125] "a157a"      "a158a"      "a159a"      "a171a"     
[129] "a173a"      "a174a"      "a175a"      "a176a"     
[133] "a177a"      "a179a"      "a181a"      "a182a"     
[137] "a192a"      "a193a"      "a194a"      "a195a"     
[141] "a197a"      "a201a"      "a202a"      "a204a"     
[145] "a206a"      "a208a"      "a209a"      "a212a"     
[149] "a213a"      "a214a"      "a215a"      "a216a"     
[153] "a217a"      "a218a"      "a219a"      "a222a"     
[157] "a223a"      "a224a"      "a225a"      "a226a"     
[161] "a227a"      "a228a"      "a229a"      "a231a"     
[165] "a235a"      "a236a"      "a237a"      "a238a"     
[169] "a242a"      "a243a"      "a244a"      "a245a"     
[173] "a247a"      "a248a"      "a252a"      "a253a"     
[177] "a256a"      "a433a"      "a434a"      "a436a"     
[181] "a529a"      "a534a"      "a747a"      "a748a"     
[185] "a752a"      "a764a"     
Show in New Window
Description:df [6 × 3]
 
 
entidad
<chr>
municipio
<dbl>
a111a
<dbl>
9013	01	6	433.305	
9793	01	7	725.663	
10663	01	8	24.947	
11009	01	9	671.078	
11379	01	10	224.185	
11677	01	11	3980.801	
6 rows
Show in New Window
Description:df [6 × 6]
 
 
Entidad
<chr>
Muicipio
<dbl>
pbt04
<dbl>
pbt09
<dbl>
pbt14
<dbl>
6	01	6	433.305	757.077	619.236	
7	01	7	725.663	1611.430	1957.359	
8	01	8	24.947	55.888	71.758	
9	01	9	671.078	1477.156	1725.812	
10	01	10	224.185	56.259	118.744	
11	01	11	3980.801	7271.449	11322.543	
6 rows | 1-6 of 6 columns



Once economic census is downloaded, we can filter values by municipality. This action is reached by pbt_mun() function, this function selects the correct variable that represents a proxy variable of municipal gross domestic product. With tail() function we can see the last six rows of data frame.

```{r}
#ags04 <- pbt_mun(data)
#tail(ags04)
```

 
 
entidad
<chr>
municipio
<dbl>
a111a
<dbl>
9013	01	6	433.305	
9793	01	7	725.663	
10663	01	8	24.947	
11009	01	9	671.078	
11379	01	10	224.185	
11677	01	11	3980.801	
6 rows
Show in New Window
Description:df [6 × 6]
 
 
Entidad
<chr>
Muicipio
<dbl>
pbt04
<dbl>
pbt09
<dbl>
pbt14
<dbl>
6	01	6	433.305	757.077	619.236	
7	01	7	725.663	1611.430	1957.359	
8	01	8	24.947	55.888	71.758	
9	01	9	671.078	1477.156	1725.812	
10	01	10	224.185	56.259	118.744	
11	01	11	3980.801	7271.449	11322.543	
6 rows | 1-6 of 6 columns
 
 
 as we can see, tail function shows the last six observations in data set. The last one in column "municipio" indicates the number of municipalities into the entity. In this case, Aguascalientes has eleven municipalities. 
 
If we are interested in build a data set with many years for this entity, we can run the PIBM_04_19() function: 

```{r}
#ags_04_19 <- PIBM_04_19(data, "ags")
#tail(ags_04_19)
```
 
 
Entidad
<chr>
Muicipio
<dbl>
pbt04
<dbl>
pbt09
<dbl>
pbt14
<dbl>
6	01	6	433.305	757.077	619.236	
7	01	7	725.663	1611.430	1957.359	
8	01	8	24.947	55.888	71.758	
9	01	9	671.078	1477.156	1725.812	
10	01	10	224.185	56.259	118.744	
11	01	11	3980.801	7271.449	11322.543	
6 rows | 1-6 of 6 columns




