-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_profile.R
227 lines (191 loc) · 9.72 KB
/
run_profile.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
#
# NOTE: This code only works for the seroprevalnce version
# of the BASA model. If using the non-sero model, you will
# need to modify lines 54-60. The processing code that
# follows is not fully tested.
#
source(paste0(here::here("functions/"), "/run_basa.r"))
profile.dir <- file.path(here::here(), "profiles")
if(!dir.exists(profile.dir)){
dir.create(profile.dir)
}
# this is the parameter names you woant to modify
# and needs to EXACTLY match the name a parameter
# in the PWS_ASA.par file. For now, it only works
# on single value parameters, not on vectors.
par.name <- "Z_0_8"
profile.values <- seq(0.1, 0.8, 0.025)
setwd(profile.dir)
profile.par.dir <- file.path(profile.dir, par.name)
if(!dir.exists(profile.par.dir)){
dir.create(profile.par.dir)
}
overwrite <- TRUE
for(v in profile.values){
dir.name <- paste0(par.name, "=", v)
profile.value.dir <- file.path(profile.par.dir, dir.name)
if(!dir.exists(profile.value.dir)){
dir.create(profile.value.dir)
}
if(file.exists(file.path(profile.value.dir, "PWS_ASA.rep")) & !overwrite){
print(paste("Skipped", par.name, "=", v))
next;
}
file.list <- c("PWS_ASA.tpl", "PWS_ASA.cor", "PWS_ASA.PIN", "PWS_ASA.par", "agecomp_samp_sizes.txt", "PWS_ASA.dat", "PWS_ASA(covariate).ctl", "PWS_ASA(ESS).ctl", "PWS_ASA(ESS_estimate).ctl", "PWS_ASA(phases).ctl", "PWS_ASA(sim_settings).ctl", "PWS_ASA_disease.dat", "PWS_ASA(par).ctl")
file.copy(
from = file.path(here::here(), "model", file.list),
to = profile.value.dir,
overwrite = TRUE,
copy.mode = TRUE
)
setwd(profile.value.dir)
lines <- readLines("PWS_ASA.tpl")
#idxs <- grepl("_PIN = ", lines)
#PIN.lines <- lines[idxs]
par.grepl <- grepl(paste0(par.name, "_PIN = "), lines)
lines[par.grepl] <- gsub("(?<= = )[\\d\\.]+", v, lines[par.grepl], perl=TRUE)
writeLines(lines, "PWS_ASA.tpl")
##################
## Just need to run in MLE mode to get likelihood values
##################
system("admb -s PWS_ASA")
system("./PWS_ASA -pinwrite")
setwd(profile.dir)
}
# profile.dirs <- file.path(here::here(), "profiles", par.name, paste0(par.name, "=", profile.values))
# likelihoods <- file.path(profile.dirs, "mcmc_out", "llikcomponents.csv")
# base.model.liketable <- read_csv(file.path(here::here(), "model", "mcmc_out", "llikcomponents.csv"), col_names = FALSE) %>%
# `colnames<-`(c("SeAC_Like", "SpAC_Like", "Egg_Like", "ADFGhyd_Like", "PWSSChyd_Like", "MDM_Like", "age0pen_Like", "mortdevspen_Like", "age0covar_prior_Like", "mortcovar_prior_Like", "Z_prior_Like", "ADFGhyd_prior_Like", "PWSSChyd_prior_Like", "m_prior_Like", "juv_Like", "sero_Like", "full_Like")) %>%
# mutate(
# full = full_Like,
# agecomp = -SeAC_Like+-SpAC_Like,
# survey = Egg_Like+ADFGhyd_Like+PWSSChyd_Like+MDM_Like+juv_Like,
# seroprev = sero_Like,
# pen = age0pen_Like+mortdevspen_Like,
# prior = age0covar_prior_Like+mortcovar_prior_Like+Z_prior_Like+ADFGhyd_prior_Like+PWSSChyd_prior_Like+m_prior_Like,
# ) %>%
# select(-c("SeAC_Like", "SpAC_Like", "Egg_Like", "ADFGhyd_Like", "PWSSChyd_Like", "MDM_Like", "age0pen_Like", "mortdevspen_Like", "age0covar_prior_Like", "mortcovar_prior_Like", "Z_prior_Like", "ADFGhyd_prior_Like", "PWSSChyd_prior_Like", "m_prior_Like", "juv_Like", "sero_Like", "full_Like")) %>%
# pivot_longer(everything(), names_to="component", values_to="LLike")
# make.likelihood.tibble <- function(fname, par.value){
# return(
# read_csv(fname, col_names = FALSE) %>%
# `colnames<-`(c("SeAC_Like", "SpAC_Like", "Egg_Like", "ADFGhyd_Like", "PWSSChyd_Like", "MDM_Like", "age0pen_Like", "mortdevspen_Like", "age0covar_prior_Like", "mortcovar_prior_Like", "Z_prior_Like", "ADFGhyd_prior_Like", "PWSSChyd_prior_Like", "m_prior_Like", "juv_Like", "sero_Like", "full_Like")) %>%
# mutate(
# full = full_Like,
# agecomp = -SeAC_Like+-SpAC_Like,
# survey = Egg_Like+ADFGhyd_Like+PWSSChyd_Like+MDM_Like+juv_Like,
# seroprev = sero_Like,
# pen = age0pen_Like+mortdevspen_Like,
# prior = age0covar_prior_Like+mortcovar_prior_Like+Z_prior_Like+ADFGhyd_prior_Like+PWSSChyd_prior_Like+m_prior_Like
# ) %>%
# select(-c("SeAC_Like", "SpAC_Like", "Egg_Like", "ADFGhyd_Like", "PWSSChyd_Like", "MDM_Like", "age0pen_Like", "mortdevspen_Like", "age0covar_prior_Like", "mortcovar_prior_Like", "Z_prior_Like", "ADFGhyd_prior_Like", "PWSSChyd_prior_Like", "m_prior_Like", "juv_Like", "sero_Like", "full_Like")) %>%
# pivot_longer(everything(), names_to="component", values_to="LLike") %>%
# mutate(
# base.LLike = base.model.liketable %>% pull(LLike)
# ) %>%
# mutate(
# LLike.diff = LLike - base.LLike,
# par.val = par.value
# ) %>%
# select(-c(base.LLike))
# )
# }
# likelihood.tibble <- tibble()
# for(i in 1:length(likelihoods)){
# fname <- likelihoods[i]
# val <- profile.values[i]
# likelihood.tibble <- likelihood.tibble %>% bind_rows(make.likelihood.tibble(fname, val))
# }
# lt <- likelihood.tibble %>%
# mutate(
# component = factor(component, levels=c("full", "agecomp", "survey", "seroprev", "prior", "pen"), labels=c("Full", "Age Composition", "Surveys", "Seroprevalence", "Priors", "Penalties"))
# ) %>%
# group_by(component, par.val) %>%
# median_qi(LLike.diff, .width=c(0.50, 0.95)) %>%
# print(n=100)
# ggplot(lt)+
# geom_line(aes(x=par.val, y=LLike.diff, color=component), size=1)+
# geom_point(aes(x=par.val, y=LLike.diff, color=component), size=3)+
# geom_hline(aes(yintercept=-2), linetype="dashed")+
# geom_hline(aes(yintercept=2), linetype="dashed")+
# geom_hline(aes(yintercept=0))+
# #scale_color_manual(values=c("black", "red", "blue", "#009500", "grey60", "grey30")) +
# #scale_y_continuous(limits=c(-5, 5))+
# coord_cartesian(ylim=c(-10, 10))+
# theme_classic()
#load(file.path(here::here(), "profiles", "Z_0_8", "Z_0_8=0.15", "mcmc_out", "NUTS_fit.RDS"))
par.name <- "Z_0_8"
profile.values <- seq(0.1, 0.775, 0.025)
profile.dirs <- file.path(here::here(), "profiles", par.name, paste0(par.name, "=", profile.values))
rep <- readLines(file.path(here::here(), "model", "rep_out", "PWS_ASA.rep"), n=40)
likes <- rep[grepl("[[:<:]][\\d\\.]+", rep, perl=TRUE)]
likes <- as.numeric(likes[2:length(likes)])
names(likes) <- c("Full", "SeAC", "SpAC", "Egg", "ADFGhyd", "PWSSChyd", "mdm", "age0_pen", "mort_pen", "age0_prior", "mort_prior", "Z_prior", "ADFGhyd_prior", "PWSSChyd_prior", "m_prior", "juv", "sero")
base.likes <- as_tibble(data.frame(as.list(likes))) %>%
mutate(
agecomp = SeAC+SpAC,
survey = Egg+ADFGhyd+PWSSChyd+mdm+juv,
seroprev = sero,
pen = age0_pen+mort_pen,
prior = age0_prior+mort_prior+Z_prior+ADFGhyd_prior+PWSSChyd_prior+m_prior
) %>%
select(Full, agecomp, survey, seroprev, pen, prior) %>%
pivot_longer(everything(), names_to="component", values_to="Like")
rep.files <- file.path(profile.dirs, "rep_out", "PWS_ASA.rep")
likelihood.df <- data.frame()
biomass.data <- NA
for(f in rep.files){
rep <- readLines(f)
rep.1 <- rep[1:40]
likes <- rep.1[grepl("[[:<:]][\\d\\.]+", rep.1, perl=TRUE)]
likes <- as.numeric(likes[2:length(likes)])
names(likes) <- c("Full", "SeAC", "SpAC", "Egg", "ADFGhyd", "PWSSChyd", "mdm", "age0_pen", "mort_pen", "age0_prior", "mort_prior", "Z_prior", "ADFGhyd_prior", "PWSSChyd_prior", "m_prior", "juv", "sero")
likelihood.df <- bind_rows(likelihood.df, data.frame(as.list(likes)))
biomass <- as.numeric(strsplit(rep[149], " ")[[1]])
final.biomass <- biomass[length(biomass)]
biomass.data <- c(biomass.data, final.biomass)
}
piner <- as_tibble(likelihood.df) %>%
mutate(
agecomp = SeAC+SpAC,
survey = Egg+ADFGhyd+PWSSChyd+mdm+juv,
seroprev = sero,
pen = age0_pen+mort_pen,
prior = age0_prior+mort_prior+Z_prior+ADFGhyd_prior+PWSSChyd_prior+m_prior
) %>%
select(Full, agecomp, survey, seroprev, pen, prior) %>%
pivot_longer(everything(), names_to="component", values_to="Like") %>%
mutate(
par.val = rep(profile.values, each=6)
) %>%
left_join(base.likes, by="component") %>%
mutate(LIKE = Like.x - Like.y) %>%
select(-c(Like.x, Like.y)) %>%
mutate(
component=factor(component, levels=c("Full", "agecomp", "survey", "seroprev", "prior", "pen"), labels=c("Full", "Age Compositions", "Surveys", "Seroprevalence", "Priors", "Penalties"))
)
ggplot(piner)+
geom_line(aes(x=par.val, y=LIKE, color=component), size=1)+
geom_point(aes(x=par.val, y=LIKE, color=component), size=3)+
geom_hline(aes(yintercept=-2), linetype="dashed")+
geom_hline(aes(yintercept=2), linetype="dashed")+
geom_hline(aes(yintercept=0))+
#scale_color_manual(values=c("black", "red", "blue", "#009500", "grey60", "grey30")) +
#scale_y_continuous(limits=c(-5, 5))+
coord_cartesian(ylim=c(-100, 100))+
labs(x=par.name, y="Change in Likelihood", color="Likelihood \nComponent", title=paste("Likelihood Profile over", par.name))+
theme_classic()+
theme(
axis.title = element_text(size=16),
axis.text = element_text(size=12),
plot.title = element_text(size=18)
)
as_tibble(biomass.data) %>% na.omit() %>%
mutate(par.val = profile.values) %>%
ggplot()+
geom_line(aes(x=par.val, y=value))+
geom_hline(aes(yintercept=20000), linetype="dashed")+
geom_hline(aes(yintercept=40000), linetype="dashed")+
scale_y_continuous(labels = scales::comma)+
coord_cartesian(ylim=c(0, 40000))+
theme_classic()