/
bayesiancovsel.R
271 lines (242 loc) · 9.06 KB
/
bayesiancovsel.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
#' Build formula for the brms from the fit
#'
#' @param fit compiled rxode2 nlmir2 model fit
#' @param covarsVec character vector of covariates that need to be added
#' @param inicovarsVec covariates vector to add in initial model, default empty
#' @param eta the eta parameter name to construct formula
#' @return formula for the brms
#' @noRd
.buildbrmsFormula <- function(fit,covarsVec,eta,inicovarsVec=NULL){
if (!inherits(fit, "nlmixr2FitCore")) {
stop("'fit' needs to be a nlmixr2 fit")
} else {
ui <- fit$finalUiEnv
}
checkmate::assert_character(covarsVec)
formula <- brms::bf(stats::as.formula(paste0(eta, " ~ a + b")),
stats::as.formula(paste0("a ~ ", paste(c("0", covarsVec), collapse = " + "))),
stats::as.formula(paste0("b ~ ", paste(c("1", inicovarsVec), collapse = " + "))),
nl = TRUE)
if (!inherits(formula, c("brmsformula" ,"bform"))) {
stop("BRMS Formula construction fails ")
} else {
return(formula)
}
}
#' Global scale for regularized horseshoe prior
#' @param fit compiled rxode2 nlmir2 model fit
#' @param covarsVec character vector of covariates that need to be added
#' @param p0 expected number of covariate terms, default 2
#' @return Global shrinkage parameter
#' @noRd
.calTau0 <- function(fit,covarsVec,p0=2){
if (!inherits(fit, "nlmixr2FitCore")) {
stop("'fit' needs to be a nlmixr2 fit")
}
else {
ui <- fit$finalUiEnv
}
checkmate::assert_character(covarsVec)
D <- length(covarsVec)-1
n <- nrow(nlme::getData(fit))
# Global shrinkage parameter, equation
tau0 <- p0/(D-p0) / sqrt(n)
if (!is.finite(tau0)| tau0 < 0 ){tau0 <- 0.25}
tau0
}
normal <- function(...){}
horseshoe <- function(...){}
#' @import utils
utils::globalVariables("tau0")
#' Build Horseshoe prior
#'
#' @param tauZero Global shrinkage parameter
#' @return Prior for the string
#' @noRd
.horseshoePrior <- function(tau0){
# Check if tau0 is valid
checkmate::assert_double(tau0)
priorString <- c(brms::prior(horseshoe(df = 1, scale_global = tau0,df_global = 1), class ="b",nlpar = "a"),
brms::prior(normal(0, 10), class = "b", nlpar = "b"))
# stan variable for parsing
stanvars <- brms::stanvar(tau0, name='tau0')
list(priorString, stanvars)
}
lasso <- function(...){}
#' Build Lasso prior
#'
#' @param dfr Degrees of freedom of the chi-square prior of the inverse tuning parameter
#' @param scalep Scale of the lasso prior
#' @return Prior for the string
#' @noRd
.lassoPrior <- function(df = 1, scale = 1){
# Check if given parameters are valid
checkmate::assert_double(df)
checkmate::assert_double(scale)
priorString <- c(brms::prior(lasso(df = 1, scale = 1), class ="b",nlpar = "a"),
brms::prior(normal(0, 10), class = "b", nlpar = "b"))
# stan variable for parsing
stanvars <- brms::stanvar(df, name='df')+brms::stanvar(scale, name='scale')
list(priorString, stanvars)
}
#' BRMS model fits for the eta
#'
#' @param fit compiled rxode2 nlmir2 model fit
#' @param covarsVec character vector of covariates that need to be added
#' @param inicovarsVec covariates vector to add in initial model, default empty
#' @param priorVar priorstring and stanvars list for the covariates
#' other parameters passed to brm(): warmup = 1000, iter = 2000, chains = 4, cores = 4,
#' control = list(adapt_delta = 0.99, max_treedepth = 15)
#' @return list of the fitted models
#' @noRd
.fitbrmsModel <- function(fit,covarsVec,inicovarsVec=NULL,priorVar,warmup = 1000, iter = 2000, chains = 4,cores = 2,
control = list(adapt_delta = 0.99, max_treedepth = 15),seed=1015){
#Normalized covariate data
data <- nlme::getData(fit)
covData <- normalizedData(data,covarsVec)
# Extract eta parameters
etaData <- fit$eta
etaVector <- colnames(etaData[grepl('eta', colnames(etaData))])
# Extract Individual column
uidCol <- .idColumn(data)
# Make a combined data set of eta parameters and covariate parameters
combData <- merge(covData,etaData,by=uidCol)
brms_formulas <- list()
# brms formulas for all length of the eta parameters
brms_formulas <- lapply(etaVector, .buildbrmsFormula,fit=fit,covarsVec=covarsVec,inicovarsVec=inicovarsVec)
# Run brms on all eta parameters
brms_models <- list()
brms_models <- suppressWarnings(lapply(brms_formulas,brms::brm,data = combData,family = stats::gaussian(),prior =priorVar[[1]],
stanvars = priorVar[[2]],warmup = warmup, iter = iter, chains = chains,cores = cores,
control = control,seed=seed))
names(brms_models) <- etaVector
brms_models
}
#' Create Summary data frame from the BRMS models
#'
#' @param modelList List of BRMS model fits
#' @return Summary data frame of all covariates
#' @noRd
.brmSummarydf <- function(all_models){
# Check if the model list is named
checkmate::assert_list(all_models,min.len = 1,names = "named")
# Construct data frame of estimates by adding eta and covariate column
dfsList <- a <- lapply(names(all_models),function(x) {
data.frame(eta=x,covariate= gsub("a_|b_","",rownames(summary(all_models[[x]])$fixed)),
summary(all_models[[x]])$fixed,row.names = NULL)})
# Merge Estimates for all eta parameters
summaryDf <- do.call("rbind", dfsList)
summaryDf <- summaryDf[!(summaryDf$covariate=="Intercept"),]
summaryDf
}
tau0 <- NULL
#' Create Horseshoe summary posterior estimates
#'
#' @param fit compiled rxode2 nlmir2 model fit
#' @param covarsVec character vector of covariates that need to be added
#' @param ... other parameters passed to brm(): warmup = 1000, iter = 2000, chains = 4, cores = 4,
#' control = list(adapt_delta = 0.99, max_treedepth = 15)
#' @return Horse shoe Summary data frame of all covariates
#' @export
#' @author Vishal Sarsani, Christian Bartels
#' @examples
#' \dontrun{
#' one.cmt <- function() {
#' ini({
#' tka <- 0.45; label("Ka")
#' tcl <- log(c(0, 2.7, 100)); label("Cl")
#' tv <- 3.45; label("V")
#' eta.ka ~ 0.6
#' eta.cl ~ 0.3
#' eta.v ~ 0.1
#' add.sd <- 0.7
#' })
#' model({
#' ka <- exp(tka + eta.ka)
#' cl <- exp(tcl + eta.cl)
#' v <- exp(tv + eta.v)
#' linCmt() ~ add(add.sd)
#' })
#' }
#'
#' d <- nlmixr2data::theo_sd
#' fit <- nlmixr2(one.cmt, d, est = "saem", control = list(print = 0))
#' covarsVec <- c("WT")
#'
#' # Horseshoe summary posterior estimates:
#'
#' #hsDf <- horseshoeSummardf(fit,covarsVec,cores=2)
#' #brms sometimes may throw a Error in sink(type = “output”)
#' #Issue Should be fixed by uninstalling and re-installing rstan
#' }
horseshoeSummardf <- function(fit,covarsVec,...){
if (!inherits(fit, "nlmixr2FitCore")) {
stop("'fit' needs to be a nlmixr2 fit")
}
checkmate::assert_character(covarsVec)
# Global shrinkage prior estimate
assignInMyNamespace("tau0", .calTau0 (fit,covarsVec,p0=2))
# Get prior String
priorString <- .horseshoePrior(tau0)
# Fit BRMS models
.horseshoeModels <-.fitbrmsModel(fit,covarsVec,priorVar = priorString,inicovarsVec=NULL,
warmup = 1000, iter = 2000, chains = 4,cores = 2,
control = list(adapt_delta = 0.99, max_treedepth = 15),seed=1015)
# Extract Summary of models
horseshoeSummary <- .brmSummarydf(.horseshoeModels)
horseshoeSummary
}
#' Create Lasso summary posterior estimates
#' @param fit compiled rxode2 nlmir2 model fit
#' @param covarsVec character vector of covariates that need to be added
#' @param ... other parameters passed to brm(): warmup = 1000, iter = 2000, chains = 4, cores = 4,
#' control = list(adapt_delta = 0.99, max_treedepth = 15)
#' @return Horse shoe Summary data frame of all covariates
#' @export
#' @author Vishal Sarsani, Christian Bartels
#'
#' @examples
#' \dontrun{
#' one.cmt <- function() {
#' ini({
#' tka <- 0.45; label("Ka")
#' tcl <- log(c(0, 2.7, 100)); label("Cl")
#' tv <- 3.45; label("V")
#' eta.ka ~ 0.6
#' eta.cl ~ 0.3
#' eta.v ~ 0.1
#' add.sd <- 0.7
#' })
#' model({
#' ka <- exp(tka + eta.ka)
#' cl <- exp(tcl + eta.cl)
#' v <- exp(tv + eta.v)
#' linCmt() ~ add(add.sd)
#' })
#' }
#'
#' d <- nlmixr2data::theo_sd
#' fit <- nlmixr2(one.cmt, d, est = "saem", control = list(print = 0))
#' covarsVec <- c("WT")
#'
#' # Horseshoe summary posterior estimates:
#'
#' #lassoDf <- lassoSummardf(fit,covarsVec,cores=2)
#' #brms sometimes may throw a Error in sink(type = “output”)
#' #Issue Should be fixed by uninstalling and re-installing rstan
#' }
lassoSummardf <- function(fit,covarsVec,...){
if (!inherits(fit, "nlmixr2FitCore")) {
stop("'fit' needs to be a nlmixr2 fit")
}
checkmate::assert_character(covarsVec)
# Get prior String
priorString <- .lassoPrior(df=1,scale=1)
# Fit BRMS models
.lassoModels <-.fitbrmsModel(fit,covarsVec,priorVar = priorString,inicovarsVec=NULL,
warmup = 1000, iter = 2000, chains = 4,cores = 2,
control = list(adapt_delta = 0.99, max_treedepth = 15),seed=1015)
# Extract Summary of models
lassoSummary <- .brmSummarydf(.lassoModels)
lassoSummary
}