-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels_previous_methods.R
More file actions
140 lines (101 loc) · 4.45 KB
/
models_previous_methods.R
File metadata and controls
140 lines (101 loc) · 4.45 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
##
# example of older code: mortality data, smoothing and forecasting (same type of method was used for fertility in previous years)
# list of older methods for migration forecasts (based on ARDL and bsts models)
##
library(demography)
library(smoothAPC)
#---------------------- V1: functional models, see Hyndman's paper and R-packages ----------------------------------------
# example, mortality data
# STEP 1: mortality smoothing
# use for now an example data from the package
# will add: querying directly our web-data and creating the
# appropriate demogdata object which will be the input for this step
sm_rate_matrices <- list()
i <- 1
for (rate_matrices in list(
fr.mort$rate$female[1:30, 150:160],
fr.mort$rate$male[1:30, 150:160],
fr.mort$rate$total[1:30, 150:160])
)
{
m <- log(rate_matrices)
#plot(m)
#plot3d(m)
sm <- autoSmoothAPC(m)
#plot(sm)
# plot(sm, "period")
# plot(sm, "cohort")
# plot(sm, "surface")
# plot(sm, "residuals")
# plot(sm, "original", main = "Original data")
#
# plot3d(sm)
# plot3d(sm, "surface", color.palette = "special")
# plot3d(sm, "cohort")
# plot3d(sm, "period")
# plot3d(sm, "residuals")
# plot3d(sm, "original", color.palette = rainbow)
sm_rate_matrices[[i]] <- sm$result
i <- i+1
}
# denote the matrices of smoothed rates: --------------
matrix_sm_rate_female <- sm_rate_matrices[[1]]
matrix_sm_rate_male <- sm_rate_matrices[[2]]
matrix_sm_rate_total <- sm_rate_matrices[[3]]
### STEP 2: make a demogdata object for future modeling and forecasting -------------
# make a demogdata object (call it smdemog) from the smoothed components
# plus the corresponding population matrices (total, males, females) by age, time
# define the dimensions age and time:
Dage <- as.numeric(dimnames(matrix_sm_rate_total)[[1]])
Dyear <- as.integer(dimnames(matrix_sm_rate_total)[[2]])
# define the matrices of population total, female, male
matrix_pop_total <- fr.mort$pop$total[1:30, 150:160]
matrix_pop_female <- fr.mort$pop$female[1:30, 150:160]
matrix_pop_male <- fr.mort$pop$male[1:30, 150:160]
# start with some object of the right dimensions
smdemog <- demogdata(data = matrix_pop_total,
pop = matrix_pop_total,
ages = Dage,
years = Dyear,
type = "mortality",
label = "country_name",
name = "total")
# then update the population matrices for females and males ---
smdemog$pop$female <- matrix_pop_female
dimnames(smdemog$pop$female)<- list(Dage, Dyear)
smdemog$pop$male <- matrix_pop_male
dimnames(smdemog$pop$male) <- list(Dage, Dyear)
#smdemog$pop$total # is already in
dimnames(smdemog$pop$total) <- list(Dage, Dyear)
# then update the smoothed (in the previous step) mortality rates ------
# --- attention!: need to do "exp" to recover rates , since had log ----
smdemog$rate$total <- exp(matrix_sm_rate_total)
dimnames(smdemog$rate$total) <- list(Dage, Dyear)
smdemog$rate$female <- exp(matrix_sm_rate_female)
dimnames(smdemog$rate$female) <- list(Dage, Dyear)
smdemog$rate$male <- exp(matrix_sm_rate_male)
dimnames(smdemog$rate$male) <- list(Dage, Dyear)
# make sure using the right names
names(smdemog[["rate"]]) <- c("total","female", "male")
names(smdemog[["pop"]]) <- c("total","female", "male")
### STEP 3: fdm model fitting ------------------------------------
# a) if we wish forecasting coherently (for males and females), use:
mfit <- coherentfdm(smdemog)
# b) if we wish forecasting independently for males and females:
#mfits <- fdm(smdemog)
plot(residuals(mfit$product))
plot(residuals(mfit$ratio$male))
# can also use the other functions from demography package
# and do lifetables, life expectancy etc
# STEP 4: forecasting ----------------------------------
# apply the usual forecasting functions of the demography-package!
mfor <- forecast(mfits, h=50) # other options, etc
plot(mfor)
models(mfor)
# and the usual graphics and results .... (to add)
#-------------------------------------------------------------
#------------------------V2. ARDL models for migration, short term projections -------------------------------------
#..........
#------------------------V3. Bayesian, structural time series models of long term migration projections--------------
#............
#-------------------------------------------------------------