-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSimLongitudinalGSF.R
More file actions
261 lines (236 loc) · 8.57 KB
/
Copy pathSimLongitudinalGSF.R
File metadata and controls
261 lines (236 loc) · 8.57 KB
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
#' @include SimLongitudinal.R
#' @include generics.R
NULL
#' Simulate Longitudinal Data from a GSF Model
#'
#' @param times (`numeric`)\cr the times to generate observations at.
#' @param sigma (`number`)\cr the variance of the longitudinal values.
#' @param mu_s (`numeric`)\cr the mean shrinkage rates.
#' @param mu_g (`numeric`)\cr the mean growth rates.
#' @param mu_b (`numeric`)\cr the mean baseline values.
#' @param mu_phi (`numeric`)\cr the mean proportion of cells affected by the treatment
#' @param omega_b (`number`)\cr the baseline value standard deviation.
#' @param omega_s (`number`)\cr the shrinkage rate standard deviation.
#' @param omega_g (`number`)\cr the growth rate standard deviation.
#' @param omega_phi (`number`)\cr for the standard deviation of the proportion of cells
#' affected by the treatment `omega_phi`.
#' @param link_dsld (`number`)\cr the link coefficient for the derivative contribution.
#' @param link_ttg (`number`)\cr the link coefficient for the time-to-growth contribution.
#' @param link_identity (`number`)\cr the link coefficient for the SLD Identity contribution.
#' @param link_growth (`number`)\cr the link coefficient for the log-growth parameter contribution.
#' @param link_shrinkage (`number`)\cr the link coefficient for the log-shrinkage parameter contribution.
#' @param scaled_variance (`logical`)\cr whether the variance should be scaled by the expected value
#' (see the "Statistical Specifications" vignette for more details)
#'
#' @slot sigma (`numeric`)\cr See arguments.
#' @slot mu_s (`numeric`)\cr See arguments.
#' @slot mu_g (`numeric`)\cr See arguments.
#' @slot mu_b (`numeric`)\cr See arguments.
#' @slot mu_phi (`numeric`)\cr See arguments.
#' @slot omega_b (`numeric`)\cr See arguments.
#' @slot omega_s (`numeric`)\cr See arguments.
#' @slot omega_g (`numeric`)\cr See arguments.
#' @slot omega_phi (`numeric`)\cr See arguments.
#' @slot link_dsld (`numeric`)\cr See arguments.
#' @slot link_ttg (`numeric`)\cr See arguments.
#' @slot link_identity (`numeric`)\cr See arguments.
#' @slot link_growth (`numeric`)\cr See arguments.
#' @slot link_shrinkage (`numeric`)\cr See arguments.
#' @slot scaled_variance (`numeric`)\cr See arguments.
#' @family SimLongitudinal
#' @name SimLongitudinalGSF-class
#' @exportClass SimLongitudinalGSF
.SimLongitudinalGSF <- setClass(
"SimLongitudinalGSF",
contains = "SimLongitudinal",
slots = c(
sigma = "numeric",
mu_s = "numeric",
mu_g = "numeric",
mu_b = "numeric",
mu_phi = "numeric",
omega_b = "numeric",
omega_s = "numeric",
omega_g = "numeric",
omega_phi = "numeric",
link_dsld = "numeric",
link_ttg = "numeric",
link_identity = "numeric",
link_growth = "numeric",
link_shrinkage = "numeric",
scaled_variance = "logical"
)
)
#' @rdname SimLongitudinalGSF-class
#' @export
SimLongitudinalGSF <- function(
times = c(-100, -50, 0, 50, 100, 150, 250, 350, 450, 550) / 365,
sigma = 0.01,
mu_s = log(c(0.6, 0.4)),
mu_g = log(c(0.25, 0.35)),
mu_b = log(60),
mu_phi = qlogis(c(0.4, 0.6)),
omega_b = 0.2,
omega_s = 0.2,
omega_g = 0.2,
omega_phi = 0.2,
link_dsld = 0,
link_ttg = 0,
link_identity = 0,
link_growth = 0,
link_shrinkage = 0,
scaled_variance = TRUE
) {
if (length(omega_b) == 1) omega_b <- rep(omega_b, length(mu_b))
if (length(omega_s) == 1) omega_s <- rep(omega_s, length(mu_s))
if (length(omega_g) == 1) omega_g <- rep(omega_g, length(mu_g))
if (length(omega_phi) == 1) omega_phi <- rep(omega_phi, length(mu_phi))
.SimLongitudinalGSF(
times = times,
sigma = sigma,
mu_s = mu_s,
mu_g = mu_g,
mu_b = mu_b,
mu_phi = mu_phi,
omega_b = omega_b,
omega_s = omega_s,
omega_g = omega_g,
omega_phi = omega_phi,
link_dsld = link_dsld,
link_ttg = link_ttg,
link_identity = link_identity,
link_growth = link_growth,
link_shrinkage = link_shrinkage,
scaled_variance = scaled_variance
)
}
setValidity(
"SimLongitudinalGSF",
function(object) {
par_lengths <- c(
length(object@mu_s),
length(object@mu_g),
length(object@mu_phi)
)
if (length(unique(par_lengths)) != 1) {
return("The parameters `mu_s`, `mu_g` and `mu_phi` must have the same length.")
}
pairs <- list(
"omega_b" = "mu_b",
"omega_s" = "mu_s",
"omega_g" = "mu_g",
"omega_phi" = "mu_phi"
)
for (i in seq_along(pairs)) {
omega <- slot(object, names(pairs)[[i]])
mu <- slot(object, pairs[[i]])
if (!(length(omega) == length(mu))) {
return(
sprintf("`%s` must be length 1 or the same length as `%s`", omega, mu)
)
}
}
len_1_pars <- c(
"sigma",
"link_dsld", "link_ttg", "link_identity", "link_growth",
"link_shrinkage"
)
for (par in len_1_pars) {
if (length(slot(object, par)) != 1) {
return(sprintf("The `%s` parameter must be a length 1 numeric.", par))
}
}
return(TRUE)
}
)
#' @rdname as_print_string
as_print_string.SimLongitudinalGSF <- function(object, ...) {
return("SimLongitudinalGSF")
}
#' @rdname sampleObservations
#' @export
sampleObservations.SimLongitudinalGSF <- function(object, times_df) {
times_df |>
dplyr::mutate(
mu_sld = gsf_sld(.data$time, .data$psi_b, .data$psi_s, .data$psi_g, .data$psi_phi),
dsld = gsf_dsld(.data$time, .data$psi_b, .data$psi_s, .data$psi_g, .data$psi_phi),
ttg = gsf_ttg(.data$time, .data$psi_b, .data$psi_s, .data$psi_g, .data$psi_phi),
sld_sd = ifelse(object@scaled_variance, .data$mu_sld * object@sigma, object@sigma),
sld = stats::rnorm(dplyr::n(), .data$mu_sld, .data$sld_sd),
log_haz_link =
(object@link_dsld * .data$dsld) +
(object@link_ttg * .data$ttg) +
(object@link_identity * .data$mu_sld) +
(object@link_growth * log(.data$psi_g)) +
(object@link_shrinkage * log(.data$psi_s))
)
}
#' @rdname sampleSubjects
#' @export
sampleSubjects.SimLongitudinalGSF <- function(object, subjects_df) {
assert_that(
is.factor(subjects_df$study),
is.factor(subjects_df$arm),
length(levels(subjects_df$study)) == length(object@mu_b),
length(levels(subjects_df$arm)) == length(object@mu_s),
length(levels(subjects_df$arm)) == length(object@mu_g),
length(levels(subjects_df$arm)) == length(object@mu_phi)
)
res <- subjects_df |>
dplyr::distinct(.data$subject, .data$arm, .data$study) |>
dplyr::mutate(
study_idx = as.numeric(.data$study),
arm_idx = as.numeric(.data$arm),
psi_b = stats::rlnorm(
dplyr::n(),
object@mu_b[.data$study_idx],
object@omega_b[.data$study_idx]
),
psi_s = stats::rlnorm(
dplyr::n(),
object@mu_s[.data$arm_idx],
object@omega_s[.data$arm_idx]
),
psi_g = stats::rlnorm(
dplyr::n(),
object@mu_g[.data$arm_idx],
object@omega_g[.data$arm_idx]
),
psi_phi_logit = stats::rnorm(
dplyr::n(),
object@mu_phi[.data$arm_idx],
object@omega_phi[.data$arm_idx]
),
psi_phi = stats::plogis(.data$psi_phi_logit)
)
res[, c("subject", "arm", "study", "psi_b", "psi_s", "psi_g", "psi_phi")]
}
## sim_lm_gsf ----
#' Generalized Stein-Fojo Functionals
#'
#' @param time (`numeric`)\cr time grid.
#' @param b (`number`)\cr baseline.
#' @param s (`number`)\cr shrinkage.
#' @param g (`number`)\cr growth.
#' @param phi (`number`)\cr shrinkage proportion.
#'
#' @returns The function results.
#'
#' @keywords internal
gsf_sld <- function(time, b, s, g, phi) {
phi <- dplyr::if_else(time >= 0, phi, 0)
b * (phi * exp(-s * time) + (1 - phi) * exp(g * time))
}
#' @rdname gsf_sld
gsf_ttg <- function(time, b, s, g, phi) {
t1 <- (log(s * phi / (g * (1 - phi))) / (g + s))
t1[t1 <= 0] <- 0
return(t1)
}
#' @rdname gsf_sld
gsf_dsld <- function(time, b, s, g, phi) {
phi <- dplyr::if_else(time >= 0, phi, 0)
t1 <- (1 - phi) * g * exp(g * time)
t2 <- phi * s * exp(-s * time)
return(b * (t1 - t2))
}