-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelselect.R
More file actions
160 lines (129 loc) · 5.54 KB
/
Copy pathmodelselect.R
File metadata and controls
160 lines (129 loc) · 5.54 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# modelselect.R
#
# Model selection via fitting a series of models and scoring them
# for the training data
#### Load data ####
# Load data for the various subsets
load("cm_subsets.RData")
# Drop all globe data for now
rm(list=ls(pattern="globe"))
#### Load estimation functions ####
# Load the count.cf function
# (this is not the same one we have used before -- it is new!)
source("count.cf.R")
#### Define regression models ####
source("formulas.R")
frms <- list(frm, frm1, frm.covar)
#### Define the dataset ####
train <- africa_me.train
test <- africa_me.train.eval
#### GLMs Training Estimations and Prediction ####
# These are going to be applied across the choices of
#
# 1) Training data (globe versus africa_me = 2)
# 2) Regression or dynamic choices (Reg choices here = 3)
# 3) Distribution choices (P, NB, ZIP, ZINB, TW = 5)
N <- 1000
xi <- seq(1.45,1.75,by=0.05)
# Serial versions for debug and easier use
africa_me.train.frm.preds <- count.cf(frm=frm,
train, test,
xi.vec = xi,
N=N)
africa_me.train.frm1.preds <- count.cf(frm=frm1,
train, test,
xi.vec = xi,
N=N)
africa_me.train.frm2.preds <- count.cf(frm=frm.covar,
train, test,
xi.vec = xi,
N=N)
#### GLMM Training Estimation ####
library(glmmTMB)
# Estimate for each density -- can loop or parallel if needed
p.glmm <- glmmTMB(ged_sb ~ ar1(month_factor + 0|country_id),
family = poisson,
data=train,
control=glmmTMBControl(parallel = 4))
nb.glmm <- glmmTMB(ged_sb ~ ar1(month_factor + 0|country_id),
family = nbinom1(),
data=train)
tw.glmm <- glmmTMB(ged_sb ~ ar1(month_factor + 0|country_id),
family = tweedie(),
data=train)
#### GLMM Predictions ####
eval.p.glmm <- predict(p.glmm, newdata=test,
type = "response",
allow.new.levels = TRUE)
eval.nb.glmm <- predict(nb.glmm, newdata=test,
type = "response",
allow.new.levels = TRUE)
eval.tw.glmm <- predict(tw.glmm, newdata=test,
type = "response",
allow.new.levels = TRUE)
#### Sample GLMM Predictions ####
n <- nrow(test)
set.seed(986)
eval.p.glmm.mcmc <- sapply(1:n, function(i) {rpois(N, eval.p.glmm[i])})
eval.nb.glmm.mcmc <- sapply(1:n, function(i) {rnbinom(N, size=sigma(nb.glmm),
mu=eval.nb.glmm[i])})
eval.tw.glmm.mcmc <- t(replicate(N, mgcv::rTweedie(eval.tw.glmm,
p=family_params(tw.glmm))))
#### Stack GLMM results ####
test <- test[,c("month_id", "country_id", "ged_sb")]
colnames(test) <- c("month_id", "country_id", "observed")
f.p <- cbind(test, t(eval.p.glmm.mcmc))
P.stacked <- reshape(f.p,
direction = "long",
varying = list(names(f.p)[4:(N+3)]),
v.names = "predicted",
idvar = c("month_id", "country_id"),
timevar = "sample_id",
times = 1:N)
P.stacked$model <- "Poisson GLMM"
rm(f.p, eval.p.glmm.mcmc, eval.p.glmm)
f.nb <- cbind(test, t(eval.nb.glmm.mcmc))
NB.stacked <- reshape(f.nb,
direction = "long",
varying = list(names(f.nb)[4:(N+3)]),
v.names = "predicted",
idvar = c("month_id", "country_id"),
timevar = "sample_id",
times = 1:N)
NB.stacked$model <- "Neg Binom GLMM"
rm(f.nb, eval.nb.glmm.mcmc, eval.nb.glmm)
f.tw <- cbind(test, t(eval.tw.glmm.mcmc))
TW.stacked <- reshape(f.tw,
direction = "long",
varying = list(names(f.tw)[4:(N+3)]),
v.names = "predicted",
idvar = c("month_id", "country_id"),
timevar = "sample_id",
times = 1:N)
TW.stacked$model <- "Tweedie GLMM"
rm(f.tw, eval.tw.glmm.mcmc, eval.tw.glmm)
#### Save fitted glmm models in case we need them later
save(list=ls(pattern=".glmm"), file = "africa_me.interim-glmm.RData")
rm(list=ls(pattern=".glmm"))
gc()
#### Check all fitted for scoring ####
colnames(africa_me.train.frm.preds$forecasts)
colnames(africa_me.train.frm1.preds$forecasts)
colnames(P.stacked)
colnames(NB.stacked)
colnames(TW.stacked)
#### Stacked and saved ####
africa_me.train.frm.preds$forecasts$model <- paste(africa_me.train.frm.preds$forecasts$model,
"+FE", sep="")
africa_me.train.frm1.preds$forecasts$model <- paste(africa_me.train.frm1.preds$forecasts$model,
"+LAGS", sep="")
africa_me.train.frm2.preds$forecasts$model <- paste(africa_me.train.frm2.preds$forecasts$model,
"+LAGS+COVAR", sep="")
forcs <- rbind(africa_me.train.frm.preds$forecasts,
africa_me.train.frm1.preds$forecasts,
africa_me.train.frm2.preds$forecasts,
P.stacked,
NB.stacked,
TW.stacked)
save.image("africa_me.train.forcs.RData")
q(save="no")