knitr::opts_chunk$set(echo = TRUE)

This section demonstrates the use of the functions contained in the demogmx package. This package includes functions to obtain demographic information regarding births, population, migration (immigration and emigration), mortality, and aging rate in Mexico. All these functions are very similar in the way they work, and once you become familiar with using one, using the rest becomes very intuitive.

To start, first we load the package:

# Load libraries
library(demogmx)

To obtain information from the functions, it is important to know the characteristics of the data. The available information is disaggregated by year, state, sex, and age. In some cases, there are other disaggregation variables like the type of migration in the get_migration() function. To obtain information at a state level, we have to introduce their name in English. The list of state names available in the data-sets is:

as.character(unique(df_migration$state))

As we can see, the last element in the list is National, with it we can obtain the aggregated information of all the states in the country.

The list of sex names that are accepted in the functions is the following:

as.character(unique(df_migration$sex))

In the present version of the package, the functions can vary the range of ages that are accepted as parameter inputs. In some cases they accept ages from 0 to 109 years, as is the case with get_population() or, in other cases, from 0 to 89, if we use get_migration().

Most of the functions have the option age_groups that only accepts logical elements as inputs. When age_groups = FALSE the data-sets will only return the data of the ages that are inside the v_ages numeric vector. When age_groups = TRUE, the output data will be grouped by age groups bounded by the elements inside the age vector. Suppose that we have the next numeric vector representing the ages that we are looking into the data:

v_ages <- c(0, 10, 15, 20)

Now let's have a closer look at each of the demogmx functions.

Births

This function gives a dataset with the number and the rate of births in the years, states, and sexes indicated by the user.

In the present version of the package there are two functions to obtain birth information from Mexico. One gives the information with sex disaggregation with data from 1985 to 2020 and the other does not have sex disaggregation but has projections between1970 and 2050.

In this function there is no age_groups option, instead there is the year_groups option that aggregates the information by age in the same way as we explained age_groups before.

General births from 1970 to 2050

This function gets the number of births estimated by the National Council of Population (CONAPO by its acronym in spanish), additionally it provides birth rate information based on the population information that can be accessed with get_population(). This function has birth projections from 1970 to 2050 for each state but does not have sex disaggregation.

Problema: en demog-mx no vienen los scripts de cómo se manipularon las bases de datos originales, quizás sea necesario volverlo a hacer aquí para que pueda ser replicable. En el caso específico de los nacimientos de la CONAPO, hay que procesar la base de datos original para obtener los datos obtenidos en data-raw.

This function contains the next parameters:

Ahead there are some examples of how this function can be used. First we obtain the data without aggregating it by years.

# Get data without grouping by year range
get_births(v_state = c("Guerrero", "Zacatecas", "National"),
           v_year = c(1970, 2000, 2020),
           year_groups = FALSE) 

Now we can see what happens if the output is aggregated.

# Get data grouping by year range
get_births(v_state = c("Michoacan", "Guerrero", "Zacatecas", "National"),
           v_year = c(1970, 2000, 2020),
           year_groups = TRUE)

Note that the year column changes its name based in whether we chose to group the data. If the data was not grouped the column is named year. If it is grouped it will be named year_group.

If any of the functions of this package receive a wrong input in their parameters, they will throw an error message and the list of accepted values, based on the parameter where the error was detected.

# Throw an error
get_births(v_state = 55, # Is not a character and is not a state name.
           v_year = c(1970, 2000, 2020),
           year_groups = FALSE) 

Births by sex

This function gives the number of registered births based on Mexico's National Institute of Statistical and Geographical data (INEGI, by it's acronym in Spanish). As in the previous function, it provides birth rate information. This information disaggregated by sex for each state and has estimations from 1980 to 2020.

This function has a parameter called v_sex that specifies the sexes that will contain the output data-set. This input accepts the character arguments "Female", "Male" and "Total", giving the information of each sex or the aggregation of both, respectively.

Here is an example of how this function can be used, without aggregating by year.

# Get data without grouping by year range
get_births_INEGI(v_state = c( "Guerrero", "National"),
                 v_year = c(2000, 2020),
                 v_sex = c("Female", "Total"),
                 year_groups = FALSE) 

And here we can see how it works if we decide to aggregate the information by years.

# Get data grouping by year range
get_births_INEGI(v_state = c( "Guerrero", "National"),
                 v_year = c(2000, 2020),
                 v_sex = c("Female", "Total"),
                 year_groups = TRUE) 

Population

get_population() is a function that returns the estimations of CONAPO on number of people living in Mexico. This function allows to filter the data based on the year, state, sex and age specified by the user. This function has population projections from 1970 to 2050

get_population() function includes the next parameters:

Let's see an example of how this function is executed without aggregating the data by age.

# Get population data without grouping by age
get_population(v_state = "Mexico City",
               v_year = 2000,
               v_sex = c("Female", "Male"),
               v_age = c(10, 25, 100),
               age_groups = FALSE)

Now, if we use this function aggregating by age we get the next result:

# Get population data grouping by age
get_population(v_state = "Mexico City",
               v_year = 2000,
               v_sex = c("Female", "Male"),
               v_age = c(10, 25, 100),
               age_groups = TRUE)

Migration

This function allows the user to get information about international and interstate migration in Mexico estimated by CONAPO. This data-set can give information disaggregated by year (from 1970 to 2050), state, sex and age (from 0 to 89 years). As most functions in the package, the information retrieved by this function can be aggregated by age.

get_population() has a particular parameter called v_type that is used to define the type of migration that will be present in the data-set output. The available types of migration in this function are Interstate migration, International migration and Total migration.

We can find an examples of how get_population() can be used with different specifications.

# Get migration data without grouping by age and
# with both types of migration
get_migration(v_state = "Mexico City",
              v_year = 2010,
              v_sex = "Total",
              v_age = c(25, 50),
              v_type = c("Interstate", "International"),
              age_groups = FALSE)
# Get migration data grouping by age and
# with both types of migration
get_migration(v_state = "Mexico City",
              v_year = 2010,
              v_sex = "Total",
              v_age = c(25, 50),
              v_type = c("Interstate", "International"),
              age_groups = TRUE)

Note that if v_state = National and v_type = Interstate, the function will return an empty data-set because there is no interstate migration at a National level.

# Get migration data without grouping by age
get_migration(v_state = "National",
              v_year = 2010,
              v_sex = "Total",
              v_age = c(0, 10, 15),
              v_type = "Interstate",
              age_groups = FALSE)

Mortality

This function allows us to get the estimation on number of deaths made by CONAPO. It works identically as get_population(). The user must define the year(s), state(s), sex(es), age(s) to obtain the data. As well, there is the option on whether the information should be grouped by age or not.

Ahead you will find some examples of how this function can be used

# Get mortality data without grouping by age
get_deaths(v_state = c("Guanajuato", "Nuevo Leon"),
           v_year = 2015,
           v_sex = c("Male", "Total"),
           v_age = c(15, 25, 35),
           age_groups = FALSE)
# Get mortality data grouping by age
get_deaths(v_state = c("Guanajuato", "Nuevo Leon"),
           v_year = 2015,
           v_sex = c("Male", "Total"),
           v_age = c(15, 25, 35),
           age_groups = TRUE)

Aging rate

The aging rate is the proportion of the population with age $i$ that were able to live another year, in other words, the aging rate is the division of the aging population between the total population.

$$ aging\ rate = \frac{aging\ population_i}{total\ population_i} $$

The aging population, as we wrote, is the number of people that is able to live another period of time (in this case, another year) in an specific location. Hence, to calculate the aging population in a specific age $i$, we must add the immigrants and subtract the emigrants and deaths to the total population of that age, as it is stated in the next equation.

$$ aging\ population = population_i + immigrants_i - emigrants_i - deaths_i $$

If the age is 0, then it is necessary to substitute the population of age $i$ by the births that occurred, in the specific time and location, to the equation.

As we see, all the elements to get the aging population and the aging rate are already at hand, so this function was created based on the outputs of the rest of functions of demogmx. This function requires that the user specifies the year, state, sex, and the age to filter the output and, as most of the functions, the data can be aggregated by the ages defined by the user in v_age.

Below you will find some examples of how this function can be used as well as its outputs.

# Get mortality data without grouping by age
get_aging_rate(v_state = "National",
               v_year = seq(2000, 2010),
               v_sex = "Total",
               v_age = 47)
# Get aging data grouping by age
get_aging_rate(v_state = "National",
               v_year = seq(2000, 2010),
               v_sex = "Total",
               v_age = 47)

NOTA: creo que especifqué mal la función the get_aging_rate cuando la edad es cero.

Get help

Finally, it is possible to get help of each of the demogmx functions as any other package. You can go to the help tab and search for information about a specific function or you can write ? before the function in the console as it is in the following code chunk.

# ?get_population


PADeCI/demogmx documentation built on Jan. 27, 2024, 6:43 a.m.