Skip to contents

fastproj 专注于一件事:把人口预测流程变成“准备 Excel -> 一键输出 Word 报告”。

The goal of fastproj is to simplify demographic projection into a practical pipeline: Excel in, Word out.

1. Install

devtools::install_github("yanyuteng/fastproj")
# Optional (only needed when calling mod_lx()):
# remotes::install_github("timriffe/DemoTools")
library(fastproj)
library(openxlsx)

2. Excel Template Specification

2.1 Baseline population file (infile_pop.xlsx)

Sheet 1 must contain exactly 6 columns in this order:

col name meaning unit
1 age age (single-year) year
2 male male population persons
3 female female population persons
4 mdr male mortality per thousand
5 fdr female mortality per thousand
6 asfr age-specific fertility proportion

Notes:

  • Recommended age range is 0:100 (101 rows).
  • read_pop() will automatically divide mdr and fdr by 1000.

2.2 Scenario parameter file (inparm_scene.xlsx)

One sheet = one scenario (e.g., low/mid/high), each sheet in this column order:

col name meaning
1 year calendar year
2 mex male life expectancy at birth
3 fex female life expectancy at birth
4 tfr total fertility rate
5 srb sex ratio at birth
6 move annual net migration

Notes:

  • If you set inparm_scene_k = 3, the workbook must contain at least 3 sheets.
  • move is used only when fast.proj_pop(move = TRUE).

3. Convert Raw Data To fastproj Format

If your source table uses Chinese column names, convert it first:

# raw_baseline: your original baseline data frame
# raw_scene: your original scenario data frame

baseline <- transform(
  raw_baseline,
  age = as.numeric(年龄),
  male = as.numeric(),
  female = as.numeric(),
  mdr = as.numeric(男死亡率千分比),
  fdr = as.numeric(女死亡率千分比),
  asfr = as.numeric(年龄别生育率)
)[, c("age", "male", "female", "mdr", "fdr", "asfr")]

scene_low <- transform(
  raw_scene,
  year = as.numeric(年份),
  mex = as.numeric(男性预期寿命),
  fex = as.numeric(女性预期寿命),
  tfr = as.numeric(TFR),
  srb = as.numeric(出生性别比),
  move = as.numeric(净迁移)
)[, c("year", "mex", "fex", "tfr", "srb", "move")]

# build more scenarios (edit values as needed)
scene_mid <- scene_low
scene_high <- scene_low

Write Excel templates:

openxlsx::write.xlsx(baseline, "infile_pop.xlsx", overwrite = TRUE)

wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, "low")
openxlsx::addWorksheet(wb, "mid")
openxlsx::addWorksheet(wb, "high")
openxlsx::writeData(wb, "low", scene_low)
openxlsx::writeData(wb, "mid", scene_mid)
openxlsx::writeData(wb, "high", scene_high)
openxlsx::saveWorkbook(wb, "inparm_scene.xlsx", overwrite = TRUE)

4. One-Click Projection (Excel -> Word)

library(fastproj)
library(openxlsx)

# 1) Read user Excel files
infile <- read_pop("infile_pop.xlsx")
inparm_scene <- read_param("inparm_scene.xlsx", inparm_scene_k = 3)

# 2) Build life-table template parameters
inparm_lt <- mod_lx(
  q0_5_m = 0.00318, e0_m = 76.75,
  q0_5_f = 0.00264, e0_f = 82.22
)

# 3) Define output directories
path_excel <- file.path(path.expand("~"), "Desktop", "fastproj_output", "excel")
path_pic <- file.path(path.expand("~"), "Desktop", "fastproj_output", "pic")
path_word <- file.path(path.expand("~"), "Desktop", "fastproj_output", "word")

# 4) Run full pipeline
out <- fast.proj_pop(
  infile = infile,
  inparm_lt = inparm_lt,
  inparm_scene = inparm_scene,
  inparm_scene_k = 3,
  move = TRUE,
  initial_year = 2020,
  area_name = "广东省",
  pop_name = "常住人口",
  # macOS: "PingFang SC", Windows: "Microsoft YaHei"
  font_pic = "PingFang SC",
  font_word = "PingFang SC",
  path_excel = path_excel,
  path_pic = path_pic,
  path_word = path_word
)

# 5) Final Word report path
out$word

5. Minimal Beginner Run

If you want the shortest possible call:

data("infile_pop", package = "fastproj")
data("inparm_scene", package = "fastproj")
inparm_lt <- mod_lx(0.00318, 76.75, 0.00264, 82.22)
fast.proj_pop(infile_pop, inparm_lt, inparm_scene)

fast.proj_pop() will then write outputs to:

  • ~/Desktop/fastproj_output/excel
  • ~/Desktop/fastproj_output/pic
  • ~/Desktop/fastproj_output/word/projection_outcome.docx