-
Notifications
You must be signed in to change notification settings - Fork 1
/
NextYearPop.R
69 lines (56 loc) · 2.52 KB
/
NextYearPop.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
##########
#From March 2017 Google+ post (https://plus.google.com/+AppliedDemographyToolbox/posts/TPFmUnch3tQ)
#Eddie Hunsinger, December 2018
#http://www.demog.berkeley.edu/~eddieh/
##########
#Demographers are sometimes asked what-to-expect for population next year, and total population (rather than births or deaths,
#or general migration or aging trends, etc) is often the most difficult part to provide helpful information on,
#so: To view a distribution of possibilities for next year's total population based on best guesses you have for high/lows
#on components of change (easy to forget how Very easy that is to do/see in R, say w uniform distributions, and no correlation between components):
#####
#####
# Number of iterations
iter<-10000
# Starting population
Pop2016<-739828
# Components of change for the period (year)
# Uniform distribution guesses for components (iter, low bound, highbound)
Deaths<-runif(iter,4000,5000)
Births<-runif(iter,9000,14000)
InMigration<-runif(iter,30000,50000)
OutMigration<-runif(iter,40000,70000)
# Sampling
Pop2017<-array(0,iter)
for(i in 1:iter){Pop2017[i]<-(Pop2016 - Deaths[i]
+ Births[i] - OutMigration[i] + InMigration[i])}
# Output
quantile(Pop2017-Pop2016,c(.005,.05,.1,.25,.5,.75,.90,.95,.995))
quantile(Pop2017,c(.005,.05,.1,.25,.5,.75,.90,.95,.995))
hist(Pop2017-Pop2016,50)
#####
#####
Related interface posted at: http://shiny.demog.berkeley.edu/eddieh/NextYearPop/
Also for above (repost of https://plus.google.com/+AppliedDemographyToolbox/posts/Zu4yQphpF8c): ~fun to combine and see: if unsure of the high/low bounds for components, can very easily set those as distributions as well (I just use uniform here, but could use anything, incl sample (sample()) of experts, etc):
#####
#####
# Number of iterations
iter<-10000
# Starting population
Pop2016<-739828
# Components of change for the period (year)
# Uniform distribution guesses for components (iter, low bound, highbound)
Deaths<-runif(iter,4000,5000)
Births<-runif(iter,runif(iter,9000,10000),runif(iter,13000,14000))
InMigration<-runif(iter,runif(iter,25000,30000),runif(iter,45000,55000))
OutMigration<-runif(iter,40000,runif(iter,60000,80000))
# Sampling
Pop2017<-array(0,iter)
for(i in 1:iter){Pop2017[i]<-(Pop2016 - Deaths[i]
+ Births[i] - OutMigration[i] + InMigration[i])}
# Output
quantile(Pop2017-Pop2016,c(.005,.05,.1,.25,.5,.75,.90,.95,.995))
quantile(Pop2017,c(.005,.05,.1,.25,.5,.75,.90,.95,.995))
hist(Pop2017-Pop2016,50)
hist(Pop2017,50)
#####
#####