-
Notifications
You must be signed in to change notification settings - Fork 1
/
3_fit_models.R
121 lines (102 loc) · 3.57 KB
/
3_fit_models.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
level_sets <- c(m1 = 1, m3 = 3, m5 = 5, m10 = 10)
tresol <- 1
sresol <- 30
source("prepare_fit.R")
th.ini <- c(4, 4, 10, 10, -2)
print(th.ini)
nm <- length(models)
results <- vector("list", nm)
names(results) <- paste0("u", models)
names(results)
rfls <- paste0(
rdir,
"fg_u", models,
"_n", ndata,
"_ns", sspde$n.spde,
"_nt", tmesh$n,
".rds")
rfls
for(i.u in 1:4) {
cat("Results file:", rfls[i.u], "\n")
### file to save the result
if(file.exists(rfls[i.u])) {
t0 <- Sys.time()
cat("Loading the previously saved result ... ")
rtmp <- readRDS(rfls[i.u])
print(Sys.time() - t0)
} else {
t0 <- Sys.time()
cat("Defining model",
models[i.u], "... ")
stmodel <- stModel.define(
smesh, tmesh, models[i.u],
control.priors = list(
prs = c(S0, 0.05),
prt = c(ifelse(models[i.u]=='121', R0t*2, R0t), 0.05),
psigma = c(S0, 0.05)),
constr = TRUE)
cat(stmodel$f$cgeneric$data$matrices$xx[1:2], " ")
print(Sys.time() - t0)
cat("Fitting model", models[i.u], "\n")
rtmp <- bru(
comps,
lhood,
options = list(
control.mode = list(
theta = th.ini, restart = TRUE),
verbose = TRUE,
inla.call = "remote",
control.compute = ctrc)
)
cat(i.u, "")
cat(c(th = unname(rtmp$mode$theta)), "\n")
print(Sys.time() - t0)
rtmp$cpu <- c(rtmp$cpu, nfn = max(rtmp$misc$nfunc))
cat(i.u, "cpu, n:", rtmp$cpu, "\n")
t0 <- Sys.time()
cat(i.u, " computing GCPO ... ")
rtmp$.args$verbose <- FALSE
if(i.u==1) {
rtmp$gcvlist <- lapply(level_sets, function(m)
inla.group.cv(
result = rtmp,
num.level.sets = m,
size.max = 50,
strategy = "posterior")
)
cat("Define the reference automatic groups for \n",
i.u, "", "model", models[i.u], "\n")
reference.groups <- lapply(rtmp$gcvlist, function(rgcv) {
return(lapply(rgcv$groups, function(x) x$idx))
})
} else {
if(!any(ls() == "reference.groups")) {
cat("Collect the reference automatic groups from \n",
"model", models[1], "\n")
refgcv <- readRDS(rfls[1])$gcvlist
reference.groups <- lapply(refgcv, function(gcv) {
return(lapply(gcv$groups, function(x) x$idx))
})
rm(refgcv)
}
rtmp$gcvlist <- lapply(1:length(level_sets), function(im)
inla.group.cv(
result = rtmp,
num.level.sets = level_sets[im],
groups = reference.groups[[im]],
strategy = "posterior")
)
names(rtmp$gcvlist) <- names(level_sets)
}
print(Sys.time() - t0)
cat("Saving results ... ")
t0 <- Sys.time()
saveRDS(rtmp[irsel], rfls[i.u])
print(Sys.time() - t0)
rm(stmodel)
} ## end else fit
results[[i.u]] <- rtmp
cat("cpu.nfn:", rtmp$cpu, "\n")
rm(rtmp)
gc(reset = TRUE)
} ## end for stmodels