-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelResults.R
327 lines (276 loc) · 12.1 KB
/
modelResults.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
# this script combines the results for any given model when fit to the data sets
# this function fits a given model to the (100) data sets given. Note that fitModelFun must return
# the following variables (all of them being vectors):
## preds
## sigmas
## lower
## upper
## interceptSummary
## rangeSummary
## sdSummary
## varSummary
# otherVariableNames: if the user wants any other variables to be summarized they should pass the
# variable names in the vector `otherVariableNames'
# otherArgs: a list of other arguments to the input fitModelFun aside from the standard. Examples
# include "betas" for the GPpreds function
fitModelToDataSets = function(fitModelFun, dataSets, randomSeeds=NULL, otherVariableNames=NULL, otherArgs=NULL,
maxDataSets=NULL, parClust=cl, includeIntercept=TRUE, seed=123) {
# remove data sets past maxDataSets
if(!is.null(maxDataSets)) {
dataSets$xTrain = dataSets$xTrain[,1:maxDataSets]
dataSets$yTrain = dataSets$yTrain[,1:maxDataSets]
dataSets$zTrain = dataSets$zTrain[,1:maxDataSets]
dataSets$xTest = dataSets$xTest[,1:maxDataSets]
dataSets$yTest = dataSets$yTest[,1:maxDataSets]
dataSets$zTest = dataSets$zTest[,1:maxDataSets]
}
nsim = ncol(dataSets$xTrain)
# set random number seed for generating random number seeds...
if(!is.null(seed))
set.seed(seed)
# generate random seeds for each data set
if(is.null(randomSeeds))
randomSeeds = sample(1:2000000, ncol(dataSets$xTrain), replace=FALSE)
else if(length(randomSeeds) != length(dataSets)) {
warning(paste0("length(randomSeeds) (", length(randomSeeds), ") is not equal to length(dataSets), (", ncol(dataSets$xTrain), "). Regenerating randomSeeds"))
randomSeeds = sample(1:2000000, ncol(dataSets$xTrain), replace=FALSE)
}
# this function combines the results from the model fit to each data set, and can be called in parallel
combineResults = function(...) {
print("Combining results...")
# not sure why the [[1]] is necessary in the non-parallel case
if(!doParallel)
results = list(...)[[1]]
else
results = list(...)
# predictive distribution summary statistics
preds = do.call("cbind", lapply(results, function(x) {c(x$preds)}))
sigmas = do.call("cbind", lapply(results, function(x) {c(x$sigmas)}))
lower = do.call("cbind", lapply(results, function(x) {c(x$lower)}))
upper = do.call("cbind", lapply(results, function(x) {c(x$upper)}))
# parameter estimate summary statistics
interceptSummary = do.call("rbind", lapply(results, function(x) {c(x$interceptSummary)}))
interceptSummary = colMeans(matrix(unlist(interceptSummary), ncol=5))
rangeSummary = do.call("rbind", lapply(results, function(x) {c(x$rangeSummary)}))
if(!is.null(rangeSummary))
rangeSummary = colMeans(matrix(unlist(rangeSummary), ncol=5))
sdSummary = do.call("rbind", lapply(results, function(x) {c(x$sdSummary)}))
if(!is.null(sdSummary))
sdSummary = colMeans(matrix(unlist(sdSummary), ncol=5))
varSummary = do.call("rbind", lapply(results, function(x) {c(x$varSummary)}))
if(!is.null(varSummary))
varSummary = colMeans(matrix(unlist(varSummary), ncol=5))
# if(includeClustEffect) {
# nuggetVarSummary = do.call("rbind", lapply(results, function(x) {x$nuggetVarSummary}))
# nuggetVarSummary = colMeans(nuggetVarSummary)
# nuggetSDSummary = do.call("rbind", lapply(results, function(x) {x$nuggetSDSummary}))
# nuggetSDSummary = colMeans(nuggetSDSummary)
# } else {
# nuggetVarSummary = NULL
# nuggetSDSummary = NULL
# }
# summarize compute time
computeTime = sapply(results, function(x) {c(x$computeTime)})
computeSummary = data.frame(matrix(c(mean(computeTime), sd(computeTime), quantile(probs=c(0.1, 0.5, 0.9), computeTime)), nrow=1))
names(computeSummary) = names(interceptSummary)
# summarize other variables if necessary, such as latticeKrig layer weights and other hyperparameters
otherVariableSummaries = list()
if(!is.null(otherVariableNames)) {
for(i in 1:length(otherVariableNames)) {
thisVariableName = otherVariableNames[i]
thisVariableSummary = do.call("rbind", lapply(results, function(x) {c(x[[thisVariableName]])}))
thisVariableSummary = colMeans(matrix(unlist(thisVariableSummary), ncol=5))
otherVariableSummaries[[thisVariableName]] = thisVariableSummary
}
}
list(preds=preds, sigmas=sigmas, lower=lower, upper=upper,
interceptSummary=interceptSummary, rangeSummary=rangeSummary,
varSummary=varSummary, sdSummary=sdSummary, computeSummary=computeSummary,
otherVariableSummaries=otherVariableSummaries)
}
# this function is a wrapper for the input model fitting function that could, if the user
# desires, be run in parallel
fitModelWrapper = function(i, doSink=FALSE) {
thisSeed = randomSeeds[i]
set.seed(thisSeed)
# get this dataset and associated
xTrain = dataSets$xTrain[,i]
yTrain = dataSets$yTrain[,i]
zTrain = dataSets$zTrain[,i]
xTest = dataSets$xTest[,i]
yTest = dataSets$yTest[,i]
# zTest = dataSets$zTest[,i]
if(includeIntercept) {
xObs = matrix(rep(1, length(zTrain)), ncol=1)
xPred = matrix(rep(1, length(xTest)), ncol=1)
}
else {
xObs = NULL
xPred = NULL
}
standardArgList = list(obsCoords=cbind(xTrain, yTrain), obsValues=zTrain, xObs=xObs,
predCoords=cbind(xTest, yTest), xPred=xPred, significanceCI=.8)
fullArgList = c(standardArgList, otherArgs)
computeTime = system.time(out <- do.call("fitModelFun", fullArgList))[3]
c(out, list(computeTime=computeTime))
}
# compute Bias & MSE & mean(Var) & 80\% coverage for each simulation
if(is.null(parClust)) {
## sequential version
surveyResults = list()
for(i in 1:nsim) {
surveyResults = c(surveyResults, list(fitModelWrapper(i, FALSE)))
}
results = combineResults(surveyResults)
} else {
# parallel version
results = foreach(i = 1:nsim, .combine=combineResults, .verbose=TRUE, .multicombine=TRUE, .export=ls()) %dopar% {
fitModelWrapper(i, FALSE)
}
}
list(results=results, randomSeeds=randomSeeds)
}
# this function fits a given model to the (100) data sets given. Note that fitModelFun must return
# the following variables (all of them being vectors):
## preds
## sigmas
## lower
## upper
## interceptSummary
## rangeSummary
## sdSummary
## varSummary
# otherVariableNames: if the user wants any other variables to be summarized they should pass the
# variable names in the vector `otherVariableNames'
# otherArgs: a list of other arguments to the input fitModelFun aside from the standard. Examples
# include "betas" for the GPpreds function
fitModelToKenyaData = function(fitModelFun, dat, randomSeeds=NULL, otherVariableNames=NULL, otherArgs=NULL,
parClust=cl, urbanEffect=TRUE, clusterEffect=TRUE, seed=123) {
includeIntercept=TRUE
# set random number seed for generating random number seeds...
if(!is.null(seed))
set.seed(seed)
list(results=results, randomSeeds=randomSeeds)
}
# given model output, aggregates predictions to the requested levels
# NOTE: for validation, all "obs____" variables must be modified to include the left out cluster
# predictions in the correct order,
aggregateModelResultsKenya = function(results, clusterLevel=TRUE, pixelLevel=TRUE, countyLevel=TRUE,
regionLevel=TRUE, targetPop=c("women", "children")) {
targetPop = match.arg(targetPop)
# get information on all the observations
if(targetPop == "children") {
out = load("../U5MR/kenyaData.RData")
dat = mort
}
else {
out = load("../U5MR/kenyaDataEd.RData")
dat = ed
}
# obsValues = dat$y/dat$n
# obsCoords = cbind(dat$east, dat$north)
# obsNs = dat$n
# xObs = matrix(rep(1, length(obsValues)), ncol=1)
obsUrban = dat$urban
# Get population density grid, adjusted for the target population
kmres = results$kmres
if(kmres == 5) {
if(targetPop == "children") {
load("../U5MR/popGridAdjusted.RData")
}
else {
load("../U5MR/popGridAdjustedWomen.RData")
}
}
else {
popGrid = makeInterpPopGrid(kmres, adjustPopSurface=TRUE, targetPop)
}
predPts = cbind(popGrid$east, popGrid$north)
predsUrban = popGrid$urban
predsCounty = popGrid$admin1
predsRegion = countyToRegion(predsCounty)
# Cluster level predictions (no aggregation required)
if(clusterLevel) {
clusterPredictions = data.frame(areaName=1:length(results$obsPreds),
urban=results$obsUrban,
preds=results$obsPreds,
SDs=results$obsSDs,
Q10=results$obsLower,
Q50=results$obsMedian,
Q90=results$obsUpper)
} else {
clusterPredictions = NULL
}
# Pixel level predictions (no aggregation required)
if(pixelLevel) {
pixelPredictions = data.frame(areaName=1:length(results$preds),
urban=predsUrban,
preds=results$preds,
SDs=results$sigmas,
Q10=results$lower,
Q50=results$median,
Q90=results$upper)
} else {
pixelPredictions = NULL
}
# From here onwards, we will need to aggregate predictions over the
# population density grid. Use the following function to get numerical
# integration matrix for a given level of areal aggregation
getIntegrationMatrix = function(areaNames) {
densities = popGrid$popOrig
uniqueNames = sort(unique(areaNames))
integrationMatrix = sapply(1:length(uniqueNames), function(i) {
areaI = areaNames == uniqueNames[i]
theseDensities = densities
theseDensities[!areaI] = 0
theseDensities * (1/sum(theseDensities))
})
integrationMatrix
}
# Use the following function to perform the
# aggregations
getIntegratedPredictions = function(areaNames, urbanProportions) {
# get numerical integration matrix
A = getIntegrationMatrix(areaNames)
# aggregate the prediction matrix
newPredMat = t(A) %*% results$predMat
# calculate relevant summary statistics
data.frame(areaName=sort(unique(areaNames)),
urban=urbanProportions,
preds=rowMeans(newPredMat),
SDs=apply(newPredMat, 1, sd),
Q10=apply(newPredMat, 1, quantile, probs=.1),
Q50=apply(newPredMat, 1, quantile, probs=.5),
Q90=apply(newPredMat, 1, quantile, probs=.9))
}
# County level predictions
if(countyLevel) {
load("../U5MR/poppc.RData")
urbanProportions = poppc$popUrb / poppc$popTotal
sortI = sort(poppc$County, index.return=TRUE)$ix
urbanProportions = urbanProportions[sortI]
countyPredictions = getIntegratedPredictions(predsCounty, urbanProportions)
} else {
countyPredictions = NULL
}
# Region level predictions
if(regionLevel) {
load("../U5MR/poppr.RData")
urbanProportions = poppr$popUrb / poppr$popTotal
sortI = sort(as.character(poppr$Region), index.return=TRUE)$ix
urbanProportions = urbanProportions[sortI]
regionPredictions = getIntegratedPredictions(predsRegion, urbanProportions)
} else {
regionPredictions = NULL
}
# combine fixed effects and hyperparameter summary tables into one single table
names(results$fixedEffectSummary) = colnames(results$parameterSummaryTable)
parameterSummary = rbind(results$fixedEffectSummary,
results$parameterSummaryTable)
# return results
list(predictions = list(clusterPredictions=clusterPredictions,
pixelPredictions=pixelPredictions,
countyPredictions=countyPredictions,
regionPredictions=regionPredictions),
parameterSummary = parameterSummary)
}