timriffe / DemoTools

Tools for the evaluation, adjustment, and standardization of demographic data
https://timriffe.github.io/DemoTools
Other
61 stars 31 forks source link

Adds automatic downloading for basepop #148

Closed cimentadaj closed 4 years ago

cimentadaj commented 4 years ago

This one is a long one. Here is the summary:

Everything is passing locally

cimentadaj commented 4 years ago

Some examples taken from the documentation of basepop_*:

################ BPE for females (five year age groups) ############################

library(DemoTools)
#> Loading required package: Rcpp
# Grab population counts for females
refYear <- 1986
country <- "Brazil"
pop_female_single <- fertestr::FetchPopWpp2019(country, refYear, ages = 0:100, sex = "female")
pop_female_counts <- single2abridged(setNames(pop_female_single$pop, pop_female_single$ages))

# Automatically downloads the nLx and ASFR data
bpe_female <- basepop_five(
  country = country,
  refYear = refYear,
  Females_five = pop_female_counts
)
#> Assuming the two prior dates for the nLx matrix to be: 1978, 1986.5
#> Assuming the two prior dates for the Asfr matrix to be: 1978, 1985.5
#> Downloading nLx data for Brazil, year 1978, gender female
#> Downloading nLx data for Brazil, year 1986.5, gender female
#> Setting radix to value of lx: 1. Can be overwritten with the `radix` argument
#> Downloading Asfr data for Brazil, year 1978
#> Downloading Asfr data for Brazil, year 1985.5

# The counts for the first three age groups have been adjusted:
bpe_female[1:3]
#>        0        1        5 
#> 1686.869 6754.924 8338.116
pop_female_counts[1:3]
#>        0        1        5 
#> 1805.874 7137.340 8352.195

################ BPE for males (five year age groups) ###############################

pop_male_single <- fertestr::FetchPopWpp2019(country, refYear, ages = 0:100, sex = "male")
pop_male_counts <- single2abridged(setNames(pop_male_single$pop, pop_male_single$ages))

# Automatically downloads the nLx and ASFR data
bpe_male <- basepop_five(
  country = country,
  refYear = refYear,
  Females_five = pop_female_counts,
  Males_five = pop_male_counts,
  female = FALSE
)
#> Assuming the two prior dates for the nLx matrix to be: 1978, 1986.5
#> Assuming the two prior dates for the Asfr matrix to be: 1978, 1985.5
#> Downloading nLx data for Brazil, year 1978, gender female
#> Downloading nLx data for Brazil, year 1986.5, gender female
#> Setting radix to value of lx: 1. Can be overwritten with the `radix` argument
#> Downloading nLx data for Brazil, year 1978, gender male
#> Downloading nLx data for Brazil, year 1986.5, gender male
#> Downloading Asfr data for Brazil, year 1978
#> Downloading Asfr data for Brazil, year 1985.5

bpe_male[1:3]
#>        0        1        5 
#> 1752.986 6944.060 8514.521
pop_male_counts[1:3]
#>        0        1        5 
#> 1868.725 7345.414 8535.430

################ BPE for females (single ages) ###############################

pop_female_single <- setNames(pop_female_single$pop, pop_female_single$ages)

# Automatically downloads the nLx and ASFR data
bpe_female <- basepop_single(
  country = country,
  refYear = refYear,
  Females_single = pop_female_single
)
#> Assuming the two prior dates for the nLx matrix to be: 1978, 1986.5
#> Assuming the two prior dates for the Asfr matrix to be: 1978, 1985.5
#> Downloading nLx data for Brazil, year 1978, gender female
#> Downloading nLx data for Brazil, year 1986.5, gender female
#> Setting radix to value of lx: 1. Can be overwritten with the `radix` argument
#> Downloading Asfr data for Brazil, year 1978
#> Downloading Asfr data for Brazil, year 1985.5

# The counts for the first 10 age groups have been adjusted:
bpe_female[1:10]
#>        0        1        2        3        4        5        6        7 
#> 1686.869 1707.511 1698.904 1684.202 1664.307 1730.054 1700.976 1669.284 
#>        8        9 
#> 1635.931 1601.871
pop_female_single[1:10]
#>        0        1        2        3        4        5        6        7 
#> 1805.874 1804.178 1795.084 1779.549 1758.528 1732.976 1703.849 1672.102 
#>        8        9 
#> 1638.693 1604.576

################ BPE for males (single ages) ############################

pop_male_single <- setNames(pop_male_single$pop, pop_male_single$ages)

# Automatically downloads the nLx and ASFR data
bpe_male <- basepop_single(
  country = country,
  refYear = refYear,
  Females_single = pop_female_single,
  Males_single = pop_male_single,
  female = FALSE
)
#> Assuming the two prior dates for the nLx matrix to be: 1978, 1986.5
#> Assuming the two prior dates for the Asfr matrix to be: 1978, 1985.5
#> Downloading nLx data for Brazil, year 1978, gender female
#> Downloading nLx data for Brazil, year 1986.5, gender female
#> Setting radix to value of lx: 1. Can be overwritten with the `radix` argument
#> Downloading nLx data for Brazil, year 1978, gender male
#> Downloading nLx data for Brazil, year 1986.5, gender male
#> Downloading Asfr data for Brazil, year 1978
#> Downloading Asfr data for Brazil, year 1985.5

bpe_female[1:10]
#>        0        1        2        3        4        5        6        7 
#> 1686.869 1707.511 1698.904 1684.202 1664.307 1730.054 1700.976 1669.284 
#>        8        9 
#> 1635.931 1601.871
pop_female_single[1:10]
#>        0        1        2        3        4        5        6        7 
#> 1805.874 1804.178 1795.084 1779.549 1758.528 1732.976 1703.849 1672.102 
#>        8        9 
#> 1638.693 1604.576

################ BPA for females (five year age groups) #####################

 smoothed_females <- smooth_age_5(Value = pop_female_counts,
                                  Age = as.numeric(names(pop_female_counts)),
                                  method = "Arriaga",
                                  OAG = TRUE,
                                  young.tail = "Original")

# Automatically downloads the nLx and ASFR data
bpa_female <- basepop_five(
  country = country,
  refYear = refYear,
  Females_five = pop_female_counts,
  SmoothedFemales = smoothed_females
)
#> Assuming the two prior dates for the nLx matrix to be: 1978, 1986.5
#> Assuming the two prior dates for the Asfr matrix to be: 1978, 1985.5
#> Downloading nLx data for Brazil, year 1978, gender female
#> Downloading nLx data for Brazil, year 1986.5, gender female
#> Setting radix to value of lx: 1. Can be overwritten with the `radix` argument
#> Downloading Asfr data for Brazil, year 1978
#> Downloading Asfr data for Brazil, year 1985.5

# The counts for the first three age groups have been adjusted:
bpa_female[1:3]
#>        0        1        5 
#> 1683.804 6754.117 8354.375
pop_female_counts[1:3]
#>        0        1        5 
#> 1805.874 7137.340 8352.195

################ BPA for males (five year age groups) #####################

# Automatically downloads the nLx and ASFR data
bpa_male <- basepop_five(
  country = country,
  refYear = refYear,
  Females_five = pop_female_counts,
  Males_five = pop_male_counts,
  SmoothedFemales = smoothed_females,
  female = FALSE
)
#> Assuming the two prior dates for the nLx matrix to be: 1978, 1986.5
#> Assuming the two prior dates for the Asfr matrix to be: 1978, 1985.5
#> Downloading nLx data for Brazil, year 1978, gender female
#> Downloading nLx data for Brazil, year 1986.5, gender female
#> Setting radix to value of lx: 1. Can be overwritten with the `radix` argument
#> Downloading nLx data for Brazil, year 1978, gender male
#> Downloading nLx data for Brazil, year 1986.5, gender male
#> Downloading Asfr data for Brazil, year 1978
#> Downloading Asfr data for Brazil, year 1985.5

# The counts for the first three age groups have been adjusted:
bpa_male[1:3]
#>        0        1        5 
#> 1749.800 6943.255 8531.124
pop_male_counts[1:3]
#>        0        1        5 
#> 1868.725 7345.414 8535.430
timriffe commented 4 years ago

Thanks man, merged! It was showing build errors on the different OSs, but will see if it was a fluke