# \donttest{
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5), x = rnorm(n)),
outcome = setargs(outcome_count, par = c(1, 0.5, 1), overdispersion = 0.7)
)
trial$estimators(
unadjusted = est_glm(family = "poisson"),
adjusted = est_glm(family = "poisson", covariates = "x")
)
trial$run(n = 200, R = 100)
trial$summary()
# }
## ------------------------------------------------
## Method `Trial$args_model`
## ------------------------------------------------
trial <- Trial$new(
covariates = function(n, p = 0.5) data.frame(a = rbinom(n, 1, p)),
outcome = function(data, ate, mu) rnorm(nrow(data), mu + data$a * ate)
)
# set and update parameters
trial$args_model(.args = list(ate = 2, p = 0.5, mu = 3))
trial$args_model(ate = 5, p = 0.6) # update parameters
trial$args_model(list(ate = 2), p = 0.5) # combine first arg with ...
# retrieve parameters
trial$args_model() # return all set parameters
trial$args_model("ate") # select a single parameter
trial$args_model(c("ate", "mu")) # multiple parameters
# remove parameters
trial$args_model(.reset = "ate") # remove a single parameter
trial$args_model(.reset = TRUE) # remove all parameters
# remove and set/update parameters
trial$args_model(ate = 2, p = 0.5, .reset = TRUE)
trial$args_model(ate = 5, .reset = "p") # removing p and updating ate
## ------------------------------------------------
## Method `Trial$args_summary`
## ------------------------------------------------
trial <- Trial$new(
covariates = function(n, p = 0.5) data.frame(a = rbinom(n, 1, p)),
outcome = function(data, ate, mu) rnorm(nrow(data), mu + data$a * ate)
)
# set and update parameters
trial$args_summary(list(level = 0.05, alternative = "<"))
trial$args_summary(level = 0.25) # update parameters
# retrieve parameters
trial$args_summary() # return all set parameters
trial$args_summary("level") # select a single parameter
trial$args_summary(c("level", "alternative")) # multiple parameters
# remove parameters
trial$args_summary(.reset = "level") # remove a single parameter
trial$args_summary(.reset = TRUE) # remove all parameters
# remove and set/update parameters
trial$args_summary(alternative = "!=", level = 0.05, .reset = TRUE)
# removing alternative and setting level
trial$args_summary(level = 0.05, .reset = "alternative")
## ------------------------------------------------
## Method `Trial$estimators`
## ------------------------------------------------
estimators <- list(marginal = est_glm(), adj = est_glm(covariates = "x"))
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5), x = rnorm(n)),
outcome = \(data, ate = -0.5) rnorm(nrow(data), data$a * ate),
estimators = estimators
)
# get estimators
trial$estimators() |> names() # list all estimators
trial$estimators("marginal") |> names() # select a single estimator
trial$estimators(c("marginal", "adj")) |> names() # select mult. est.
# remove estimators
trial$estimators(.reset = "marginal") # remove a single estimator
trial$estimators(.reset = TRUE) # remove all estimators
# set or update estimators
trial$estimators(estimators)
trial$estimators(adj2 = est_adj(covariates = "x")) # add new estimator
# update adj and remove adj2
trial$estimators(adj = est_glm(covariates = "x"), .reset = "adj2")
# unnamed estimators (adding default name)
estimators <- list(est_glm(), est_glm(covariates = "x"))
trial$estimators(estimators, .reset = TRUE)
trial$estimators(.reset = "est1")
trial$estimators(est_glm()) # replaces removed est1
## ------------------------------------------------
## Method `Trial$simulate`
## ------------------------------------------------
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5)),
outcome = \(data, ate = 0) with(data, rnorm(nrow(data), a * ate))
)
# applying and modifying parameters
n <- 10
trial$simulate(n) # use parameters set during initialization
trial$args_model(ate = -100) # update parameters
trial$simulate(n)
trial$simulate(n, ate = 100) # change ate via optional argument
# rename outcome variable
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5)),
outcome = \(data, ate = 0) {
data.frame(yy = with(data, rnorm(nrow(data), a * ate)))
}
)
trial$simulate(n)
# return multiple outcome variables
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5), y.base = rnorm(n)),
outcome = \(data, ate = 0) {
y <- with(data, rnorm(nrow(data), a * ate))
return(data.frame(y = y, y.chg = data$y.base - y))
}
)
trial$simulate(n)
# use exclusion argument to post-process sampled outcome variable to
# achieve the same as in the above example but without modifying the
# originally defined outcome model
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5), y.base = rnorm(n)),
outcome = \(data, ate = 0) with(data, rnorm(nrow(data), a * ate)),
exclusion = \(data) {
cbind(data, data.frame(y.chg = data$y.base - data$y))
}
)
trial$simulate(n)
# no covariate model
trial <- Trial$new(
outcome = \(data, ate = 0) {
n <- nrow(data)
a <- rbinom(n, 1, 0.5)
return(data.frame(a = a, y = rnorm(n, a * ate)))
}
)
trial$simulate(n)
## ------------------------------------------------
## Method `Trial$run`
## ------------------------------------------------
# \donttest{
# future::plan("multicore")
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5)),
outcome = \(data, ate = 0) with(data, rnorm(nrow(data), a * ate)),
estimators = list(glm = est_glm())
)
trial$args_summary(alternative = "<")
# the returned trial.estimates object contains the estimates for each of
# the R simulated data sets + all necessary information to re-run the
# simulation
res <- trial$run(n = 100, R = 50) # store return object in a new variable
print(trial$estimates) # trial$estimates == res
# the basic usage is to apply the summary method to the generated
# trial.estimates object.
trial$summary()
# combining Trial$run and summary is faster than using
# Trial$estimate_power when modifying only the parameters of the
# decision-making function
sapply(
c(0, 0.25, 0.5),
\(ni) trial$summary(ni.margin = ni)[, "power"]
)
# changing the ate parameter value
trial$run(n = 100, R = 50, ate = -0.2)
# supplying another estimator
trial$run(n = 100, R = 50, estimators = est_glm(robust = FALSE))
# }
## ------------------------------------------------
## Method `Trial$estimate_power`
## ------------------------------------------------
# \donttest{
# toy examples with small number of Monte-Carlo replicates
# future::plan("multicore")
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5)),
outcome = \(data, ate = 0) with(data, rnorm(nrow(data), a * ate)),
estimators = list(glm = est_glm())
)
trial$args_summary(alternative = "<")
# using previously defined estimators and summary.args
trial$estimate_power(n = 100, R = 20)
# supplying parameters to outcome function
trial$estimate_power(n = 100, R = 20, ate = -100)
# modifying arguments of summary function
trial$estimate_power(n = 100, R = 20, ate = -100,
summary.args = list(alternative = ">")
)
# supplying estimators to overrule previously set estimators
trial$estimate_power(n = 100, R = 20,
estimators = list(est_glm(), est_adj()))
# }
## ------------------------------------------------
## Method `Trial$estimate_samplesize`
## ------------------------------------------------
# \donttest{
trial <- Trial$new(
covariates = \(n) data.frame(a = rbinom(n, 1, 0.5)),
outcome = \(data, ate, sd) with(data, rnorm(nrow(data), a * ate, sd)),
estimators = list(marginal = est_glm())
)
trial$args_model(ate = -1, sd = 5)
trial$args_summary(alternative = "<")
# supply model parameter and estimator to call to overwrite previously
# set values
trial$estimate_samplesize(ate = -2, estimator = est_glm())
# reduce number of iterations for bisection step but keep R = 100
# (default value)
# trial$estimate_samplesize(bisection.control = list(niter = 2))
# reduce significance level from 0.05 to 0.025, but keep alternative as
# before
# trial$estimate_samplesize(summary.args = list(level = 0.025))
# }
## ------------------------------------------------
## Method `Trial$summary`
## ------------------------------------------------
outcome <- function(data, p = c(0.5, 0.25)) {
a <- rbinom(nrow(data), 1, 0.5)
data.frame(a = a, y = rbinom(nrow(data), 1, p[1] * (1 - a) + p[2] * a)
)
}
trial <- Trial$new(outcome, estimators = est_glm())
trial$run(n = 100, R = 100)
# two-sided test with 0.05 significance level (alpha = 0.05) (default
# values)
trial$summary(level = 0.05, alternative = "!=")
# on-sided test
trial$summary(level = 0.025, alternative = "<")
# non-inferiority test
trial$summary(level = 0.025, ni.margin = -0.5)
# provide simulation results to summary method via estimates argument
res <- trial$run(n = 100, R = 100, p = c(0.5, 0.5))
trial$summary(estimates = res)
# calculate empirical bias, rmse and coverage for true target parameter
trial$summary(estimates = res, true.value = 0)
## ------------------------------------------------
## Method `Trial$print`
## ------------------------------------------------
trial <- Trial$new(
covariates = function(n) data.frame(a = rbinom(n, 1, 0.5)),
outcome = function(data, sd = 1) rnorm(nrow(data), data$a * -1, sd),
estimators = list(marginal = est_glm()),
info = "Some trial info"
)
trial$args_model(sd = 2)
trial$args_summary(level = 0.025)
print(trial) # only function headers
print(trial, verbose = TRUE) # also print function bodies
Run the code above in your browser using DataLab