-
Notifications
You must be signed in to change notification settings - Fork 0
/
neonatalSimStudyWeighted.R
357 lines (293 loc) · 11.1 KB
/
neonatalSimStudyWeighted.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# library(haven)
library(survey)
# -- a few helpful functions -- #
expit<-function(x){
exp(x)/(1+exp(x))
}
logit<-function(x){
log(x/(1-x))
}
# function to extend dataset to binary form
extendData <- function(clustDatRow, v001, divideWeight=TRUE, useNumChildrenDied=TRUE){
# add extra columns for ageMonth, ageGrpD, v001, v002
if(useNumChildrenDied) {
clustDatRow$n = clustDatRow$numChildren
clustDatRow$y = clustDatRow$died
}
n = clustDatRow$n
# tmp = data.frame(clustDatRow[c(1, 6:16)])
tmp = data.frame(clustDatRow[c(1, c(4, 6:ncol(clustDatRow)))])
tmp$v001 = v001
ageMonth = rep(0, n)
ageGrpD = rep("[0,1)", n)
v001 = rep(v001, n)
# there is only one child and one mother per household.
# All 25 households are sampled
v002 = 1:n
y = c(rep(0,n-clustDatRow$y), rep(1, clustDatRow$y))
if(clustDatRow["urban"][1,1]){
urbanRural = rep("urban", n)
} else {
urbanRural = rep("rural", n)
}
# admin1 = rep(clustDatRow$admin1, n)
res = merge(data.frame(y, ageMonth, ageGrpD, v001, v002, urbanRural), tmp, by="v001")
# the below line was commented out since each cluster only has one type of admin and urban level.
# The equivalent line has been added into the parent function
# res$regionRural <- with(res, interaction(admin1, urbanRural), drop=TRUE)
if(divideWeight)
res$samplingWeight = res$samplingWeight / n
return(res)
}
extendDataDat <- function(clustDatRow, v001, divideWeight=TRUE){
# add extra columns for ageMonth, ageGrpD, v001, v002
n = clustDatRow$n
# the only things we need are admin1 and sampling weight, but we must get rid of
# urban, y, and the number of women since those will be recalculated
# tmp = data.frame(clustDatRow[c(1, 6:16)])
# tmp = data.frame(clustDatRow[c(1, c(4, 6:ncol(clustDatRow)))])
tmp = data.frame(clustDatRow[c(1, c(4, 6:(ncol(clustDatRow) - 2)))])
tmp$v001 = v001
ageMonth = rep(0, n)
# ageGrpD = rep("[0,1)", n)
v001 = rep(v001, n)
# there is only one child and one mother per household.
# All 25 households are sampled
v002 = 1:n
y = c(rep(0,n-clustDatRow$y), rep(1, clustDatRow$y))
if(clustDatRow["urban"][1,1]){
urbanRural = rep("urban", n)
} else {
urbanRural = rep("rural", n)
}
# admin1 = rep(clustDatRow$admin1, n)
# res = merge(data.frame(y, ageMonth, ageGrpD, v001, v002, urbanRural), tmp, by="v001")
res = merge(data.frame(y, ageMonth, v001, v002, urbanRural), tmp, by="v001")
# the below line was commented out since each cluster only has one type of admin and urban level.
# The equivalent line has been added into the parent function
# res$regionRural <- with(res, interaction(admin1, urbanRural), drop=TRUE)
if(divideWeight)
res$samplingWeight = res$samplingWeight / n
return(res)
}
# - a function that reads in a glm or svyglm - #
# - object and returns the estimate and SE - #
# - specifics in the supplementary materials - #
## This function takes care of the delta method
## to calculate the variance of u5m as a function
## of the age specific hazards, \beta_a .
get.est<-function(glm.ob){
beta<-summary(glm.ob)$coef[,1]
est <-expit(beta)
var.est <- vcov(glm.ob)[1,1]
# compute 80% CI intervals
lower <- logit(est)+qnorm(c(0.9))*sqrt(var.est)
upper <- logit(est)+qnorm(c(0.1))*sqrt(var.est)
return(c(est,lower, upper,logit(est),var.est))
}
# -- a function to subset the design based on a region and time period -- #
# -- and then run the svyglm function and return the get.est() results -- #
## First line in function allows you to subset your data and ALSO the specified
## svydesign object into area (usually v024 variable in DHS)
## and time (per5 is a variable we construct for the 5-year periods in the Stata step)
## Second line fits the survey-weighted glm
region.time.HT<-function(dataobj, svydesign, area){
tmp<-subset(svydesign, (admin1==area))
tt2 <- tryCatch(glmob<-svyglm(y.x~1,
design=tmp,family=quasibinomial, maxit=50),
error=function(e) e, warning=function(w) w)
if(is(tt2, "warning")){
if(grepl("agegroups", tt2)){
res <- get.est(glmob)
res = c(res, 2)
} else {
res = c(rep(NA, 5), 3)
}
return(res)
}
if(is(tt2,"error")){
res = c(rep(NA, 5), 1)
return(res)
} else {
res <- get.est(glmob)
res = c(res, 0)
return(res)
}
}
region.time.HTDat<-function(dataobj, svydesign, area, nationalEstimate){
if(!nationalEstimate) {
tmp<-subset(svydesign, (admin1==area))
tt2 <- tryCatch(glmob<-svyglm(y~1,
design=tmp,family=quasibinomial, maxit=50),
error=function(e) e, warning=function(w) w)
} else {
thisUrban = area == 1
tmp<-subset(svydesign, (urban==thisUrban))
tt2 <- tryCatch(glmob<-svyglm(y~1,
design=tmp,family=quasibinomial, maxit=50),
error=function(e) e, warning=function(w) w)
}
if(is(tt2, "warning")){
if(grepl("agegroups", tt2)){
res <- get.est(glmob)
res = c(res, 2)
} else {
res = c(rep(NA, 5), 3)
}
return(res)
}
if(is(tt2,"error")){
res = c(rep(NA, 5), 1)
return(res)
} else {
res <- get.est(glmob)
res = c(res, 0)
return(res)
}
}
defineSurvey <- function(dat_obj, stratVar, useSamplingWeights=TRUE){
options(survey.lonely.psu="adjust")
# --- setting up a place to store results --- #
regions <- sort(unique(dat_obj$admin1))
regions_num <- 1:length(regions)
results<-data.frame(admin1=rep(regions,each=1))
results$var.est<-results$logit.est<-results$upper<-results$lower<-results$est<-NA
results$converge <- NA
if(useSamplingWeights){
dat_obj$wt <- dat_obj$samplingWeight
} else {
dat_obj$wt <- NULL
}
if(is.null(stratVar)){
# --- setting up the design object --- #
## NOTE: -the v001 denote
## one stage cluster design (v001 is cluster)
## -This call below specifies our survey design
## nest = T argument nests clusters within strata
my.svydesign <- svydesign(id= ~v001,
strata =NULL,
weights=NULL, data=dat_obj)
} else {
## not in all surveys does v022 contain the correct sampling strata
## Thus, the correct vector has to be provided externally
dat_obj$strat <- stratVar
# --- setting up the design object --- #
## NOTE: -the v001 denote
## one stage cluster design (v001 is cluster)
## -This call below specifies our survey design
## nest = T argument nests clusters within strata
my.svydesign <- svydesign(id= ~v001,
strata=~strat, nest=T,
weights=~wt, data=dat_obj)
}
for(i in 1:nrow(results)){
results[i, 2:7] <- region.time.HT(dataobj=dat_obj, svydesign=my.svydesign,
area=results$admin1[i])
}
return(results)
}
defineSurveyDat <- function(dat_obj, stratVar, useSamplingWeights=TRUE, nationalEstimate=FALSE,
getContrast=nationalEstimate){
options(survey.lonely.psu="adjust")
# --- setting up a place to store results --- #
regions <- sort(unique(dat_obj$admin1))
regions_num <- 1:length(regions)
if(!nationalEstimate) {
results<-data.frame(admin1=rep(regions,each=1))
results$var.est<-results$logit.est<-results$upper<-results$lower<-results$est<-NA
results$converge <- NA
}
else {
results<-data.frame(urban=c(TRUE, FALSE))
results$var.est<-results$logit.est<-results$upper<-results$lower<-results$est<-NA
results$converge <- NA
}
if(useSamplingWeights){
dat_obj$wt <- dat_obj$samplingWeight
} else {
dat_obj$wt <- NULL
}
if(is.null(stratVar)){
# --- setting up the design object --- #
## NOTE: -the v001 denote
## one stage cluster design (v001 is cluster)
## -This call below specifies our survey design
## nest = T argument nests clusters within strata
my.svydesign <- svydesign(id= ~v001,
strata =NULL,
weights=NULL, data=dat_obj)
} else {
## not in all surveys does v022 contain the correct sampling strata
## Thus, the correct vector has to be provided externally
dat_obj$strat <- stratVar
# --- setting up the design object --- #
## NOTE: -the v001 denote
## one stage cluster design (v001 is cluster)
## -This call below specifies our survey design
## nest = T argument nests clusters within strata
my.svydesign <- svydesign(id= ~v001,
strata=~strat, nest=T,
weights=~wt, data=dat_obj)
}
for(i in 1:nrow(results)){
if(!nationalEstimate) {
results[i, 2:7] <- region.time.HTDat(dataobj=dat_obj, svydesign=my.svydesign,
area=results$admin1[i], nationalEstimate=nationalEstimate)
}
else {
results[i, 2:7] <- region.time.HTDat(dataobj=dat_obj, svydesign=my.svydesign,
area=i, nationalEstimate=nationalEstimate)
}
}
if(getContrast) {
# out = svyby(~y, by = ~urban, design = svydesign, svymean)
glmob<-svyglm(y~urban,
design=my.svydesign,family=quasibinomial, maxit=50)
# get contrast mean and variance
est = glmob$coefficients[2]
urbanVar = vcov(glmob)[2,2]
# get confidence interval
lower = est + qnorm(0.025, sd=sqrt(urbanVar))
upper = est + qnorm(0.975, sd=sqrt(urbanVar))
contrastStats = list(est=est, sd=sqrt(urbanVar), lower95=lower, upper95=upper)
return(list(results=results, contrastStats=contrastStats))
} else {
return(results)
}
}
# Set dat_obj$admin1 to be something else for different kinds of aggregations
run_naive <- function(dat_obj){
regions <- sort(unique(dat_obj$admin1))
regions_num <- 1:length(regions)
results<-data.frame(admin1=rep(regions,each=1))
results$var.est<-results$logit.est<-results$upper<-results$lower<-results$est<-NA
results$converge <- NA
for(i in 1:nrow(results)){
my.glm <- glm(y.x~1, family=binomial,
data=dat_obj,
subset = admin1 == results$admin1[i] )
# newdat = dat_obj[dat_obj$admin1==results$admin1[i], ]
# my.glm2 <- glm(y.x~1, family=binomial,
# data=newdat)
results[i, 2:7] <- c(get.est(my.glm),0)
}
return(results)
}
# running the analysis for the actual mortality dataset is slightly different
run_naiveDat <- function(dat_obj){
regions <- sort(unique(dat_obj$admin1))
regions_num <- 1:length(regions)
results<-data.frame(admin1=rep(regions,each=1))
results$var.est<-results$logit.est<-results$upper<-results$lower<-results$est<-NA
results$converge <- NA
for(i in 1:nrow(results)){
my.glm <- glm(y~1, family=binomial,
data=dat_obj,
subset = admin1 == results$admin1[i] )
# newdat = dat_obj[dat_obj$admin1==results$admin1[i], ]
# my.glm2 <- glm(y.x~1, family=binomial,
# data=newdat)
results[i, 2:7] <- c(get.est(my.glm),0)
}
return(results)
}