-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathparam_table.R
197 lines (181 loc) · 6.24 KB
/
param_table.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
#' Creates parameter kable
#'
#' @param ctr Generated controller from e.g. \code{\link{pmx_mlx}} for Monolix.
#' @param return_table If TRUE, returns the same table as in get_data('estimates') otherwise it returns a kable
#' @param fun \code{character} can be "sd" or "var" for shrinkage computation, see \code{\link{pmx_comp_shrink}}
#' @param scientific \code{logical} set to TRUE to get scientific notation of parameter values, or FALSE otherwise
#' @param digits \code{integer} the number of significant digits to use when rounding parameter values
#' @return Returns a kable with the parameter estimates from get_data('estimates')
#' @export
#'
#' @examples
#' #ctr <- theophylline()
#' #my_params <- ctr %>% param_table(fun = "var")
param_table <- function(ctr, fun, return_table=FALSE, scientific=FALSE, digits=2) {
# Argument Checks
if(missing(fun)) fun <- c("var","sd")
stopifnot(
is.character(fun),
fun %in% c("sd", "var") | identical(fun, c("var", "sd")),
is.logical(return_table),
length(return_table) == 1,
is.logical(scientific),
length(scientific) == 1,
is.numeric(digits),
length(digits) == 1,
digits >= 0
)
## Avoid error message in cmd check
SE <- EFFECT <- SHRINK <- RSE <- VALUE <- PARAM <- . <- NULL
pop_pars <- ctr %>% get_data("estimates")
rownames(pop_pars) <- trimws(as.character(pop_pars[["PARAM"]]))
if(return_table) {return(as.data.table(pop_pars))}
config_sys_nm <- ctr[["config"]][["sys"]] == "nm"
config_sys_nlmixr <- ctr[["config"]][["sys"]] == "nlmixr"
# Selecting function to use for shrinkage calculation
fun <- ifelse(
identical(fun, c("var", "sd")),
ifelse(config_sys_nlmixr, "sd", ifelse(config_sys_nm, "sd", "var")),
fun
)
message("`", fun, "`", " was used for shrinkage calculation")
# For nonmem data is used of get_data("omega"), which is the SD
shrinkage <- ctr %>%
pmx_comp_shrink(fun = fun) %>%
dplyr::mutate(SHRINK = paste0(round(SHRINK *
100), "%")) %>%
dplyr::mutate(PARAM = paste0(ifelse(config_sys_nm,
"", ifelse(config_sys_nlmixr, "", "omega_")
), EFFECT), ) %>%
dplyr::select(PARAM, SHRINK)
if (ctr[["config"]][["sys"]] == "nm") {
i <- 1
for(i in i:length(shrinkage[["PARAM"]])) {
str <- shrinkage[["PARAM"]][i]
num <- as.numeric(substring(str, 4, ))
shrinkage[["PARAM"]][i] <- paste0("OMEGA(", num, ",", num, ")")
}
pop_pars <- dplyr::mutate(pop_pars, RSE, ifelse(SE == 1E10, "fixed", round(RSE)))
} else if (ctr[["config"]][["sys"]] == "nlmixr") {
eta_trans <- ctr[["config"]][["eta_trans"]]
shrinkage[, PARAM := eta_trans[PARAM]]
}
blank_row <- data.frame(PARAM=".", VALUE="", RSE ="", SHRINK="")
pop_pars2 <- pop_pars %>%
dplyr::transmute(
PARAM = trimws(PARAM),
VALUE = signif(VALUE, digits) %>%
{
if (scientific) {
(scales::label_scientific(digits = Inf))(.)
} else {
as.character(.)
}
}, RSE = ifelse(is.infinite(RSE), "", ifelse(is.na(RSE),
"fixed", paste0(round(RSE), "%")
)),
) %>%
dplyr::arrange(PARAM) %>%
dplyr::select(PARAM, VALUE, RSE) %>%
dplyr::arrange(PARAM) %>%
dplyr::left_join(shrinkage) %>%
dplyr::mutate(SHRINK = ifelse(is.na(SHRINK),
"", SHRINK
)) %>%
tidyr::replace_na(list(SHRINK = "")) %>%
{
suppressWarnings({
if (ctr[["config"]][["sys"]] %in% c("mlx", "mlx18")) {
dplyr::bind_rows(blank_row, dplyr::filter(
.,
grepl("_pop", PARAM)
), blank_row, dplyr::filter(
.,
grepl("omega_", PARAM)
), blank_row, dplyr::filter(
.,
grepl("beta_", PARAM)
), blank_row, dplyr::filter(
.,
PARAM %in% c("a", "b")
), )
} else if (ctr[["config"]][["sys"]] == "nm") {
dplyr::bind_rows(dplyr::filter(., grepl(
"OBJ",
PARAM
)), blank_row, dplyr::filter(., grepl(
"THETA",
PARAM
)), blank_row, dplyr::filter(., grepl(
"SIGMA",
PARAM
)), blank_row, dplyr::filter(., grepl(
"OMEGA",
PARAM
)), )
} else if (ctr[["config"]][["sys"]] == "nlmixr") {
param_regs <- ctr[["config"]][["param_regs"]]
dplyr::bind_rows(dplyr::filter(., grepl(
"OBJ",
PARAM
)), blank_row, dplyr::filter(., grepl(
param_regs["theta"],
PARAM
)), blank_row, dplyr::filter(., grepl(
param_regs["eta"],
PARAM
)), blank_row, dplyr::filter(., grepl(
param_regs["err"],
PARAM
)))
}
})
}
if (ctr[["config"]][["sys"]] == "mlx") {
pop_pars3 <- data.frame(
PARAM = "Structural parameters",
VALUE = "", RSE = "", SHRINK = ""
) %>%
dplyr::bind_rows(pop_pars2 %>%
dplyr::filter(grepl("pop", PARAM))) %>%
dplyr::bind_rows(data.frame(
PARAM = "Between-subject variability, standard deviations",
VALUE = "", RSE = "", SHRINK = ""
)) %>%
dplyr::bind_rows(pop_pars2 %>%
dplyr::filter(grepl("omega", PARAM))) %>%
dplyr::bind_rows(data.frame(
PARAM = "Covariate effects",
VALUE = "", RSE = "", SHRINK = ""
)) %>%
dplyr::bind_rows(pop_pars2 %>%
dplyr::filter(grepl("beta_", PARAM))) %>%
dplyr::bind_rows(data.frame(
PARAM = "Correlations",
VALUE = "", RSE = "", SHRINK = ""
)) %>%
dplyr::bind_rows(pop_pars2 %>%
dplyr::filter(grepl("corr_", PARAM))) %>%
dplyr::bind_rows(data.frame(
PARAM = "Residual variability",
VALUE = "", RSE = "", SHRINK = ""
)) %>%
dplyr::bind_rows(pop_pars2 %>%
dplyr::filter(PARAM %in% c("a", "b")))
} else {
pop_pars3 <- pop_pars2 %>%
dplyr::bind_rows(data.frame(
PARAM = ".",
VALUE = "", RSE = "", SHRINK = paste(
"comp. with",
fun
)
))
}
pop_pars3 %>%
dplyr::mutate(PARAM = sub("_pop$", "", PARAM)) %>%
kable(
col.names = c("Parameter", "Value", "RSE", "Shrinkage"),
caption = paste0("Parameter estimates (", nrow(pop_pars3) + 1, " lines)")
)
}