robjhyndman / demography

demography package for R
https://pkg.robjhyndman.com/demography
73 stars 25 forks source link

read.demogdata or demogdata for pop for non-hmd files #44

Closed Maks2615 closed 3 years ago

Maks2615 commented 3 years ago

Good day, Professor, I find it impossible to create demogdata object (as in example on your blog) if data is just population and not from hmd site (but has same structure).

robjhyndman commented 3 years ago

Please post a reproducible example on http://community.rstudio.com

Maks2615 commented 3 years ago

I try to paraphrase question, may be reproducible example won't be needed.

  1. For non-hmd countries we should use external files.
  2. External files could be loaded as demogdata object by 2 commands - demogdata and read.demogdata.
  3. Both commands have "type" argument. Either for “mortality”, “fertility” or “migration”, but not for "population".
  4. I need type "population", how can I load demogdata for population?
robjhyndman commented 3 years ago

read.demogdata() requires a rates file, a population file, and the type argument determines what rates are included. Use help(read.demogdata) to see an example.

Maks2615 commented 3 years ago

"read.demogdata" works very well with rates, no problems with rates. but how to substitute with it "hmd.pop"?

anyway we need pure population data to use in "pop.sim".

robjhyndman commented 3 years ago

Here's an example.

library(demography)
library(tidyverse)
# Read Australian population data (HMD file)
pop <- read.table("Population.txt", skip=2, header=TRUE, na.strings=".")
# Set up population matrix
pop <- matrix(pop$Female, nrow=111, ncol=99)
rownames(pop) = 0:110
colnames(pop) = 1921:2019
# Create demogdata object
auspop <- structure(list(
    pop=list(Female=pop), 
    ages=0:110, 
    years=1921:2019, 
    type='population',
    lambda=1,
    label="Australia"
  ), class='demogdata')

plot(auspop)
Maks2615 commented 3 years ago

Thank you very much, very appreciate your help