Population Projections using R - Including dynamic graphical visualisations
A textbook on population projections with R.
Last updated
A textbook on population projections with R.
Last updated
Farid FLICI
Last Revision: 2020-04-30
Farid FLICI (2020). Populations Projections Using R - Including Dynamic graphical Visualisations. A textbook published with Gitbook, Available at: [https://farid-flici.gitbook.io/pop-proj-dz/], Version of 2020-04-30.
In this textbook, we are going to illustrate how to perform populations projections using the Cohort-Component Method using simple R functions and without using population projections specific Packages such as popdemo
. We use the algerian population data for our case study. Then, we are going to show how to carry-out practical plots of population pyramid.
The most practical way to make population projections consists of using the Cohort-Compnent Method. This methods consists of treating the baseline population (population at time ) to be composed of different cohorts born in different years. So, their actual ages go from age to the maximum surviving age, that we note . Then, numbers within each cohort are projected using prospective life tables
, and each year, new-borns are added in age . When migration and emigration data are available, population numbers are adjusted accordingly. That's where Component comes from; population dynamics is driven from the expected evolution of three components: Mortality, Fertility and Immigration.
If we set to be the population aged in the begenning of the year , and to be the Age-Specific Mortality Rate (ASMR) corresponding to and , the year-to-year evolution of the the population within each cohort can be driven using the equation:
The new-borns during the year are added to the population of the year as the population aged . To estimate the number of new-borns during the year , noted , the mid-year population of females at the procreation ages, i.e., years, needs to be multiplied by the Age-Specific Fertility Rates (ASFRs), . The mid-year population can be approximated by the average population between the begenning of the years and . We can write:
Then, the number of new-borns are to be split out into and . If we set "" to be the number of boys corresponding to girl among new-borns, we can split into and , with designs males and females, using the equation:
$$a$$can be estimated based on the historical recorded values.
All datasets need to be extended on the period going from the reference yars, 2015 in our case, to the horizon of the projection, 2070 in our case.
We associated the datasets to this textbook as embded files which can be downloaded from the links provided below.
Baseline population: We use the population of 2015, by detailed ages, as a baseline population.
This population pyramid, for males and females, can also be downloaded fom :[http://www.cread.dz/Actuarial_Demography/Donnees_Site/Detailed_Ages_Proj.xlsx]
The projected Age-Specific mortality Rates (ASMRs) from 0 to 120 years for the period from 2015 to 2017 were driven from the coherent mortality foracst of Flici (2016a) following the methodology of Hyndman et al. (2013).
The dataset of ASMRs of males and females can also be downloaded from [ http://www.cread.dz/Actuarial_Demography/Donnees_Site/Projected_ASMRs.xlsx].
The projected Age-Specific Fertility Rates (ASFRs) for females ages 15-49 years along the period from 2015-2070 are estimated by Flici (2016b) following the methodology of Lee (1993), and can be downloaded from [http://www.cread.dz/Actuarial_Demography/Donnees_Site/Projected_ASFRs.xlsx]
The immigration sold is supposed to be null.
The number of males corresponding to 1 female among newborns is equal to 1.045 according to historical data.
1.3.1. Baseline population pyramid
For the needs of this work, we use the population pyramid of Algeria in the begening of 2015 for males and females for the ages 0-99 years old.
We upload the data file:
We separate males and females into different datasheets, namely PM15 and PF15 as accronyms of "Population of males 2015" and "Population of females 2015".
1.3.2. Mortality forecast
We need to upload the mortality surface for males and females.
Male mortality
Female Mortality
1.3.3. Fertility Forecast
Upload the data file, and we name it as (FR) as "Fertility".
First, we create an ampty matrix to receive the projection results (PopM for males and PopF for females). This matrix should be of a dimension (n=121 * t = 56
).
Then,
Then, we copy the PM15 into the first row in PopM (PF15 into PopF[,1])
$$B_{t-1}=\sum_{s=15}^{49} PFM_{s,t-1}*f_{s,t-1}$$
We estimate the Mid-year population (PFM as an average of the populations at the begening and the end of the previous year). It makes:
$$B_{t-1}=\sum_{s=15}^{49} \frac{PF_{s,t-1}+PF_{s,t}}{2}*f_{s,t-1}$$
First, we need to call ggplot
A first example
by adding + coord_flip()
by adding scale_y_continuous(breaks = seq(-600000, 600000, 200000), labels = paste(as.character(c(seq(6, 0, -2),seq(2,6,2))), "x105"), limits=c(-650000,650000))
It makes
by adding
xlab("Age") + ylab("Population number")
It makes :
by adding
ggtitle(paste("Population Pyramid of Algeria:", year1)
It makes :
by adding
annotate('text', x = 95, y = 250000, label = 'Females', size = 5, colour="red") + annotate('text', x = 95, y = - 250000, label = 'Males', size = 5, colour="blue")
It makes :
by adding theme(plot.title = element_text(hjust = 0.5,size=12,face="bold"), panel.border = element_rect(colour = "black",fill=NA,size=1), axis.text = element_text(size=12,face="bold"), panel.background = element_rect(fill="white")))
It makes :
In order to change the background color from gray to white we just need to put:
panel.background = element_rect(fill=NA) into +theme()
It makes :
Required packages:
The code to run:
After having executed the R-code above, a message about the progression of the GIF creation is posted on the screen:
Once you get this message, the gif file can be found in the same directory as the R-project location. To open it, you should use any internet browser or to put it on power point full screen.
In order to visualize the dynamic pyramid, you can insert the GIF plot into a power point file and to make it on full screen view or you can just open the GIF using any internet browser.
Converter Error Message
Sometimes, when trying to run the code to create the GIF, an error message appears and it concerns the converter ImageMagick
. This last is not a part of R, and i is an independent graphical tool which allow the creation of a GIF correctely. What do you need to do in such a case, is to install ImageMagic
version 7 or higher because the old versions work with converter.exe
which is not adapted to the newest versions of R-studio. The compatible converter is magick
. This application can be downloaded from: [www.imagemagick.org/script/download.php].
Then, you need to run the following code in R to update the location of the converter being installed:
ani.options(convert = 'C:/PROGRA
~1/ImageMagick-7.0.7-Q8/convert.exe)
Before to run again the saveGIF()
.
Download historical and the projected series of life expectancy at birth for males and females in Algeria from : [http://www.cread.dz/Actuarial_Demography/Donnees_Site/Total_Fertility_Rate.xlsx]
plot the observed and the projected Total Fertility Rate
Now, make it dynami
Download historical and the projected series of life expectancy at birth for males and females in Algeria from : [http://www.cread.dz/Actuarial_Demography/Donnees_ Site/Life_exp_coherent_forecast.xlsx]
Do it, plot it
Make it movING
Flici, F. (2016a). Coherent mortality forecasting for the Algerian population. Presented at Samos Conference in Actuarial Sciences and Finance, Samos, Greece (May).
Flici, F. (2016b). Projection des taux de fécondité de la population algérienne á l'horizon 2050. MPRA Paper No. 99077, posted 12 Mar 2020. [https://mpra.ub.uni-muenchen.de/99077/1/MPRA_paper_99077.pdf]
Flici, F. (2017). Longevity and pension plan sustainability in Algerie: Taking the re- tirees mortality experience into account. Doctoral dissertation, Higher National School of Statistics and Applied Economics (ENSSEA), Kolea, Algeria.
Hyndman, R. J., Booth, H., & Yasmeen, F. (2013). Coherent mortality forecasting: the product-ratio method with functional time series models. Demography, 50 (1), 261-283.
Lee, R. D. (1993). Modeling and forecasting the time series of US fertility: Age distribution, range, and ultimate level. International Journal of Forecasting, 9(2), 187-202.
Then, and are introduced as populations, of males and females, at age 0 in the begenning of the year .
The implementation of the Cohort-Component Method requires to make available 4 types of data: a Baseline population, projected mortality surfaces (for males and females), a projected fertility surface, and the part of males for female among new-borns.
The projection of population number by age is deduced by a year to year approach by applying the survival probabilities. If we not to be the probablity to die (Age Specific Mortality Rates) between the ages and during the year , the population at age during the year noted is deduced from : $$P_{x+1,t+1}=P_{x,t}*(1-q_{x,t})$$.
The population at age in the begenning of the year is estimated by the total of newborns during the previous years, i.e., year , (noted ) on the basis of combining the population (Mid-year population rather than that of the begening of the year) of females at fertility age ( years : noted with the age specific fertility rates ASFRs (noted ). We can write :
In order to separate males and females among the newborns, we introduce (define) which represents the number of males aong newborn corresponding to female.
The part of males among a cohort of newborns is equal to $$B*\frac{a}{1+a}$$ while the number of females is equal to $$B*\frac{1}{1+a}$$.
The newborns during the year are introduced as the population of age in the begenning of year .