-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_frequentist_multilevel_preliminary.R
240 lines (185 loc) · 7.72 KB
/
models_frequentist_multilevel_preliminary.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
### using frequentist estimates of multilevel models for individual/aggregated events (births, deaths, migration)
### results are useful because fast but also because may be used for initalization, to speed up convergence of Bayesian methods
###
######### M1. Models for counts and rates #######################
## simplest models. One may easily improve by testing models with random slopes, interactions, etc.
## given: data frames dbirths, ddeaths, dmigr
library(lme4)
library(gamm4)
library(splines)
#---------- births
# baseline and testing icc
mbf0 <- glmer(nr ~ offset(log(nrExposed))
+ (1|svf),
family=poisson(), data= dbirths)
mbf <- gamm4::gamm4(nr ~ t2(ar) + t2(aldur) + citiz + offset(log(nrExposed)),
family=poisson, random = ~ (1|svf), data= dbirths)
mbf1 <- glmer(nr ~ ns(ar) + ns(aldur) + citiz + offset(log(nrExposed))
+ (1|svf),
family=poisson(), data= dbirths)
mbf2 <- glmer(nr ~ ns(ar,aldur) + citiz + offset(log(nrExposed))
+ (1|svf),
family=poisson(), data= dbirths)
mbS1 <- gamm4::gamm4(nnr ~ s(ar) + s(aldur) + citiz + offset(log(nnrExposed)),
family=poisson, data= dbirthsSums)
#--------- deaths
mdf <- gamm4::gamm4(nr ~ s(ar, aldur) + kyn + offset(log(nrExposed)),
family=poisson, data= ddeaths)
#---------- migration
mmf <- gamm4::gamm4(nr ~ t2(aldur) +t2(ar) + kyn + gerd + citiz + offset(log(nrExposed)) ,
family=poisson
#, random = ~ (1|svf)
, data= dmigr)
mmf1 <- glmer(nr ~ ns(aldur) + ns(ar)
+ kyn + gerd + citiz
+ (1|svf)
+ offset(log(nrExposed)) ,
family=poisson() , data= dmigr)
#--------- check model performance ------------------------
m <- mmf1 # if it is glmer type
### or
# m <- mmf$mer # if it is gamm4
summary(m)
ranef(m)
performance::icc(m)
reEx <- merTools::REsim(m)
#head(reEx)
merTools::plotREsim(reEx, labs=TRUE, oddsRatio=TRUE)
# merTools::plotREsim(reEx, labs=TRUE)
feEx <- merTools::FEsim(m)
#head(feEx)
merTools::plotFEsim(feEx)
library(arm)
sim_fit <- arm::sim( m, 10000)
bayestestR::hdi(sim_fit)
bayestestR::eti(sim_fit)
xf <- bayestestR::ci(sim_fit, ci = c(.5, .8, .95), effects = "fixed")
library("see")
plot(xf)
# if appropriate:
# plot(ggeffects::ggpredict(m, terms = c(".....", ....)))
######### M2. Models for microdata #############################
## simplest models also here. One may easily improve by testing models with random slopes, interactions, etc.
## given a data frame called micro: all microdata, with y, yd, y_immExt, y_em_ext, y_m_int:
## binary valued responses concerning
## births, deaths, immigration, emigration, internal migration
## other variables are:
## svf-municipality, Menntun-education, fjs-size of family, svffjolgun-size change of municipality, kyn-gender,
## aldur-age, aldurF-age as factor, fjolskyldustaerd-size of family, rfang-citizenship, fland-country of birth
#------------ births
# baseline, testing icc for birth data (y=0,1)
m0_freq <- lme4::glmer(y ~ 1
+ (1|svf)
##+ (1|region)
, family=binomial(logit)
, data=micro)
# more complex
m1_freq <- lme4::glmer(y ~ 1 + Menntun + fjs + fjolskyldustaerd + svffjolgun
+ I(ar-min(ar))
+ (1|aldurF)
, family=binomial()
, data=subset(micro, ar<2021))
# random slopes also
m3_freq <- lme4::glmer(y ~ 1 + Menntun + fjs + fjolskyldustaerd + svffjolgun
+ I(ar-min(ar))
+ (1+ I(ar-min(ar))|aldurF)
, family=binomial()
, data=subset(micro, ar<2021))
#------------ deaths
## check if municipality matters --------------
m0d_freq <- lme4::glmer(y_d ~ 1
+(1|svf)
, family=binomial()
, data=subset(micro, ar<2021))
summary(m0d_freq)
performance::icc(m0d_freq) ## very small grouping by svf
## more complex
m1d_freq <- lme4::glmer(y_d ~ 1
+ kyn
+ fjolskyldustaerd
## + Menntun
+ fland
+ I(ar-min(ar))
+ (1|aldurF)
, family=binomial()
, data=subset(micro, ar<2021))
#------------migration---------------------------
#----- emigration as a separate component
## check if different by municipality
m0eE_freq <- lme4::glmer(y_em_ext ~ 1
+(1|svf)
, family=binomial()
, data=subset(micro, ar<2021))
## more complex -----------------------
m1eE_freq <- lme4::glmer(y_em_ext ~ 1
+ kyn
# + Menntun
+ fjs + fjolskyldustaerd
# + rfang
+ fland
# + svffjolgun
## + FlutnFyrr ## to test if matters
+ I(ar-min(ar))
+ (1|aldurF)
, family=binomial()
, data=subset(micro, ar<2021))
#----- immigration as a separate component
## check if different by municipality
m0ImmExt_freq <- lme4::glmer(y_imm_ext ~ 1
+(1|svf)
, family=binomial()
, data=subset(micro, ar<2021))
## more complex
#### could check whether slopes of various predictors depend on RE
#### could also check whether various interactions matter
m1ImmExt_freq <- lme4::glmer(y_imm_ext ~ 1 + kyn
+ Menntun + fjs + fjolskyldustaerd
+ rfang
+ svffjolgun
+ ns(aldur) + ns(I(arImmExt-min(arImmExt)))
+ (1|svf)
, family=binomial()
, data=subset(micro, ar<2021))
#------- internal migration
## baseline
m0k_mInt_freq <- lme4::glmer(y_m_int ~ 1
#+ kyn
+(1|svf)
, family=binomial()
, data=subset(micro, ar<2021))
## more complex proposals
m1mInt_freq <- lme4::glmer(y_m_int ~ 1 + kyn
+ Menntun + fjs + fjolskyldustaerd
+ rfang
##+ fland
+ FlutnSidar
+ svffjolgun
+ I(ar-min(ar))
+ (1|aldurF)
+ (1|svf)
#### and could check whether slopes of various depend on it
#### could also check whether various interactions matter
, family=binomial()
, data=subset(micro, ar<2021))
#-------------------------------------------------
############## models' evaluation #######################
## for any of the models above do:
# m <-.... ## the model of interest
summary(m)
# random effects
reEx <- merTools::REsim(m)
merTools::plotREsim(reEx, labs=TRUE, oddsRatio=TRUE)
## or:
## merTools::plotREsim(reEx, labs=TRUE)
# fixed effects
feEx <- merTools::FEsim(m)
merTools::plotFEsim(feEx)
# simulating credible intervals
library(arm)
sim_fit <- arm::sim( m, 10000)
bayestestR::hdi(sim_fit)
bayestestR::eti(sim_fit)
xf <- bayestestR::ci(sim_fit, ci = c(.5, .8, .95), effects = "fixed")
library("see")
plot(xf)
##-----------------------------