-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSimLongitudinalRandomSlope.R
More file actions
110 lines (100 loc) · 3.5 KB
/
Copy pathSimLongitudinalRandomSlope.R
File metadata and controls
110 lines (100 loc) · 3.5 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
#' @include SimLongitudinal.R
#' @include generics.R
NULL
#' Simulate Longitudinal Data from a Random Slope Model
#'
#' @param times (`numeric`)\cr the times to generate observations at.
#' @param intercept (`number`)\cr the mean baseline value for each study.
#' @param slope_mu (`numeric`)\cr the population slope for each treatment arm.
#' @param slope_sigma (`number`)\cr the random slope standard deviation.
#' @param sigma (`number`)\cr the variance of the longitudinal values.
#' @param link_dsld (`number`)\cr the link coefficient for the DSLD contribution.
#' @param link_identity (`number`)\cr the link coefficient for the identity contribution.
#'
#' @slot intercept (`numeric`)\cr See arguments.
#' @slot slope_mu (`numeric`)\cr See arguments.
#' @slot slope_sigma (`numeric`)\cr See arguments.
#' @slot sigma (`numeric`)\cr See arguments.
#' @slot link_dsld (`numeric`)\cr See arguments.
#' @slot link_identity (`numeric`)\cr See arguments.
#'
#' @family SimLongitudinal
#' @name SimLongitudinalRandomSlope-class
#' @exportClass SimLongitudinalRandomSlope
.SimLongitudinalRandomSlope <- setClass(
"SimLongitudinalRandomSlope",
contains = "SimLongitudinal",
slots = c(
intercept = "numeric",
slope_mu = "numeric",
slope_sigma = "numeric",
sigma = "numeric",
link_dsld = "numeric",
link_identity = "numeric"
)
)
#' @rdname SimLongitudinalRandomSlope-class
#' @export
SimLongitudinalRandomSlope <- function(
times = c(-100, -50, 0, 50, 100, 150, 250, 350, 450, 550),
intercept = 50,
slope_mu = c(0.01, 0.03),
slope_sigma = 0.5,
sigma = 2,
link_dsld = 0,
link_identity = 0
) {
.SimLongitudinalRandomSlope(
times = times,
intercept = intercept,
slope_mu = slope_mu,
slope_sigma = slope_sigma,
sigma = sigma,
link_dsld = link_dsld,
link_identity = link_identity
)
}
#' @rdname as_print_string
as_print_string.SimLongitudinalRandomSlope <- function(object) {
return("SimLongitudinalRandomSlope")
}
#' @rdname sampleObservations
#' @export
sampleObservations.SimLongitudinalRandomSlope <- function(object, times_df) {
times_df |>
dplyr::mutate(
err = stats::rnorm(dplyr::n(), 0, object@sigma),
sld_mu = .data$intercept + .data$slope_ind * .data$time,
sld = .data$sld_mu + .data$err,
log_haz_link =
object@link_dsld * .data$slope_ind +
object@link_identity * .data$sld_mu
)
}
#' @rdname sampleSubjects
#' @export
sampleSubjects.SimLongitudinalRandomSlope <- function(object, subjects_df) {
assert_that(
is.factor(subjects_df[["study"]]),
is.factor(subjects_df[["arm"]])
)
assert_that(
length(object@slope_mu) == nlevels(subjects_df[["arm"]]),
msg = "`length(slope_mu)` should be equal to the number of arms"
)
assert_that(
length(object@intercept) == nlevels(subjects_df[["study"]]),
msg = "`length(intercept)` should be equal to the number of studies"
)
assert_that(
nrow(subjects_df) == length(unique(subjects_df[["subject"]])),
msg = "The number of rows in `subjects_df` should be equal to the number of unique subjects"
)
subjects_df |>
dplyr::mutate(intercept = object@intercept[as.numeric(.data$study)]) |>
dplyr::mutate(slope_ind = stats::rnorm(
n = dplyr::n(),
mean = object@slope_mu[as.numeric(.data$arm)],
sd = object@slope_sigma
))
}