Create a class of endpoint to specify its name, type, readout time (optional) and assign a random number generator.
Public methods in this R6 class are used in developing this package. Thus, I have to export the whole R6 class which exposures all public methods. However, none of the public methods is useful to end users except for the one below.
$print()
Methods
Method new()
initialize an endpoint.
Arguments
name
character vector. Name(s) of endpoint(s)
type
character vector. Type(s) of endpoint(s). It supports
"tte"
for time-to-event endpoints, and"non-tte"
for all other types of endpoints (e.g., continous, binary, categorical, or repeated measurement.TrialSimulator
will do some verification if an endpoint is of type"tte"
. However, no special manipulation is done for non-tte endpoints.readout
a named numeric vector with name to be non-tte endpoint(s).
readout
must be specified for every non-tte endpoint. For example,c(endpoint1 = 6, endpoint2 = 3)
, which means that it takes 6 and 3 unit time to get readout ofendpoint1
andendpoint2
of a patient since being randomized. Error message would be prompted ifreadout
is not named or readout is not specified for some non-tte endpoint. If all endpoints are tte,readout
should beNULL
as default.generator
a random number generation (RNG) function. It supports all built-in random number generators in
stats
, e.g.,stats::rnorm
,stats::rexp
, etc. that withn
as the argument for number of observations and returns a vector. A custom RNG function is also supported.generator
could be any functions as long as (1) its first argument isn
; and (2) it returns a vector of lengthn
(univariate endpoint) or a data frame ofn
rows (multiple endpoints), i.e., custom RNG can return data of more than one endpoint. This is useful when users need to simulate correlated endpoints or longitudinal data. The column names of returned data frame should match toname
exactly, although order of columns does not matter. If an endpoint is of type"tte"
, the customgenerator
should also return a column as its event indicator. For example, if"pfs"
is"tte"
, then customgenerator
should return at least two columns"pfs"
and"pfs_event"
. Usuallypfs_event
can be all 1s if no censoring. Some RNG functions, e.g.,TrialSimulator::PiecewiseConstantExponentialRNG()
andTrialSimulator::CorrelatedPfsAndOs4()
, simulate TTE endpoint data with censoring simultaneously, thus 0 exists in the columns of event indicators. Users can implement censorship in their own RNG. Censoring can also be specified later when defining a trial object through argumentdropout
. See?trial
. Note that if covariates, e.g., biomarker, subgroup, are needed in generating and analyzing trial data, they can and should be defined as endpoints inendpoint()
as well....
optional arguments for
generator
.
Method test_generator()
test random number generator of the endpoints. It returns an example
dataset of an endpoint object. Note that users of TrialSimulator
does not need to call this function to generate trial data; instead,
the package will call this function at milestone automatically.
Users may see example in vignette where this function is called.
However, it is for illustration purpose only. In practice, this function
may be used for debugging if users suspect some issues in custom
generator, otherwise, this function should never been called in formal
simulation.
Method update_generator()
update endpoint generator
Arguments
generator
a random number generation (RNG) function. See
generator
ofendpoint()
....
optional arguments for
generator
.
Method print()
print an endpoint object
Arguments
categorical_vars
a character vector of endpoints. This can be used to force variables with limited distinct values as categorical variables in summary report. For example, a numeric endpoint may take integer values 0, 1, 2. Instead of computing mean and standard derivation in the summary report, put this endpoint in
categorical_vars
can force it be a categorical variable and a barplot is generated in summary report instead.
Examples
rng <- function(n){
data.frame(x = sample(1:3, n, replace = TRUE),
y = sample(1:3, n, replace = TRUE)
)
}
ep <- endpoint(name = c('x', 'y'),
type = c('non-tte', 'non-tte'),
readout = c(x = 0, y = 0),
generator = rng)
## x and y as continuous endpoints, thus mean and sd are reported
ep
## force y to be categorical to create barplot of it
print(ep, categorical_vars = 'y')
Examples
# Instead of using Endpoints$new(), please use endpoint(), a user-friendly
# wrapper to define endpoints. See examples in ?endpoint.
## ------------------------------------------------
## Method `Endpoints$print`
## ------------------------------------------------
rng <- function(n){
data.frame(x = sample(1:3, n, replace = TRUE),
y = sample(1:3, n, replace = TRUE)
)
}
ep <- endpoint(name = c('x', 'y'),
type = c('non-tte', 'non-tte'),
readout = c(x = 0, y = 0),
generator = rng)
## x and y as continuous endpoints, thus mean and sd are reported
ep
#> Summary generated.
## force y to be categorical to create barplot of it
print(ep, categorical_vars = 'y')
#> Summary generated.