Creates a data generating mechanism (DGM) for non-survival outcomes using generalised linear models.
Usage
generate_glm_dgm(
data,
factor_vars,
continuous_vars = NULL,
outcome_var,
treatment_var,
outcome_type = c("binary", "continuous", "count"),
effect_measure = NULL,
offset_var = NULL,
subgroup_vars = NULL,
subgroup_cuts = NULL,
model = c("alt", "null"),
k_treat = 1,
k_inter = 0,
adverse_outcome = FALSE,
n_super = 5000L,
seed = 8316951L,
verbose = FALSE,
k_random_noise = 0L,
noise_seed = 20260807L
)Arguments
- data
A data frame containing the source dataset.
- factor_vars
Character vector of factor/categorical variable names to include as covariates. These are the candidate confounders for ForestSearch (e.g.,
paste0("z", 1:12)).- continuous_vars
Character vector of continuous variable names to include as prognostic covariates in the baseline GLM. These improve the model fit but do not participate in subgroup definition. Consistent with
generate_aft_dgm_flex, which accepts bothcontinuous_varsandfactor_vars. DefaultNULL(factor-only model, backward compatible).- outcome_var
Character string naming the outcome variable.
- treatment_var
Character string naming the treatment variable (must be 0/1 coded).
- outcome_type
Character. One of
"binary","continuous", or"count".- effect_measure
Character. Effect measure for the GLM. Default is
NULL, which selects"OR"for binary,"MD"for continuous,"IRR"for count outcomes.- offset_var
Character or
NULL. Name of the exposure / follow-up time column for count outcomes with an offset. Required wheneffect_measureis"IRR"or"IRD". The offset enters the Poisson GLM asoffset(log(offset_var)). DefaultNULL.- subgroup_vars
Character vector of variable names defining the subgroup. Default
NULL(no subgroup).- subgroup_cuts
Named list of cutpoint specifications for subgroup variables. Uses the same flexible format as
generate_aft_dgm_flex: fixed values, quantiles, or functions.- model
Character.
"alt"for alternative hypothesis with treatment–subgroup interaction,"null"for uniform treatment effect. Default"alt".- k_treat
Numeric. Scaling factor for the fitted treatment coefficient on the linear predictor scale.
k_treat = 1(default) preserves the fitted treatment effect as-is.k_treat > 1amplifies the treatment main effect, useful when the source dataset has a weak treatment effect and a stronger ITT is desired for simulation.k_treat < 1attenuates the effect. Analogous to thek_treatparameter ingenerate_aft_dgm_flex. Note: the null DGM (model = "null") represents a homogeneous treatment effect (no HTE), not a zero treatment effect.k_treatscales the magnitude of that homogeneous effect.- k_inter
Numeric. Direct additive shift on the linear predictor for Q members under treatment. The interpretation depends on
outcome_type:- binary
Shift on the log-odds scale. Positive increases P(Y=1) for Q under treatment.
- continuous
Shift on the mean scale (identity link). Positive increases \(E(Y)\) for Q under treatment.
- count
Shift on the log-rate scale. Positive increases \(E(Y)\) for Q under treatment (multiplicative: rate multiplied by
exp(k_inter)).
Default
0(no interaction, equivalent tomodel = "null").- adverse_outcome
Logical. If
TRUEandoutcome_type = "binary", the interaction shiftk_interis negated internally so that a positivek_interconsistently amplifies the treatment contrast on the beneficial (1 - Y) scale. The flag is stored in the returned object for downstream use bycalibrate_glm_interactionandrun_simulation_analysis. DefaultFALSE.- n_super
Integer. Size of the super-population. Default
5000L.- seed
Integer. Random seed for super-population sampling. Default
8316951L.- verbose
Logical. Print diagnostic information. Default
FALSE.- k_random_noise
Integer. Number of standard-normal noise columns (
noise1,noise2, ...) drawn once onto the super-population at construction. Default0L(no noise columns; output identical to previous versions).- noise_seed
Integer. Seed for the population-level noise draw, used only when
k_random_noise > 0. Default20260807L, the committed population-noise seed (commit2a4787bc).
Value
An object of class c("glm_dgm", "list") with:
df_superSuper-population data frame with potential outcome columns:
p0,p1(binary probabilities), ormu0,mu1(conditional means for continuous / count outcomes), andflag_harm.df_sourceThe observed analysis frame in observed order (every source patient exactly once), carrying the same potential-outcome columns as
df_supercomputed by the identical prediction calls, plusflag_harmand the discretised covariates. This is the frozen panel consumed bysimulate_from_glm_dgm(baseline = "fixed").hazard_ratiosNamed list with
harm_subgroup,no_harm_subgroup, andoverall– effect estimates on the scale determined byeffect_measure. Field name retained for compatibility withget_dgm_hr()and reporting functions.outcome_typeCharacter:
"binary","continuous", or"count".effect_measureCharacter: the effect measure used.
model_paramsList with fitted coefficients, family,
k_inter, residual SD (continuous), and offset variable name (count).subgroup_infoList with subgroup definition, size, and proportion.
model_typeCharacter:
"alt"or"null".noise_namesCharacter vector of noise column names (
character(0)whenk_random_noise = 0).noise_scheme"population"when noise was drawn, otherwise"none".noise_seedThe seed used for the noise draw, or
NA_integer_when none was drawn.
Details
Fits a GLM to the source data, creates a super-population with
individual-level potential outcomes, and stores true subgroup effects.
The returned object can be passed to simulate_from_glm_dgm
for simulation studies, and is compatible with
run_simulation_analysis (which dispatches on class).
Noise columns requested via k_random_noise are inert
N(0,1) population attributes intended as candidate confounders; the
builder never puts them in the outcome model. They are drawn once onto
df_super after sampling, so simulated trials inherit them by
row resampling.
Examples
if (FALSE) { # \dontrun{
library(speff2trial)
actg <- subset(ACTG175, arms %in% c(1, 3))
actg$treat <- 1L - as.integer(actg$arms == 1)
actg$y <- as.integer(actg$cd420 > actg$cd40)
actg$z1 <- as.factor(ifelse(actg$age > 34, 1L, 0L))
actg$z2 <- as.factor(ifelse(actg$preanti <= 745, 1L, 0L))
dgm <- generate_glm_dgm(
data = actg,
factor_vars = c("z1", "z2"),
outcome_var = "y",
treatment_var = "treat",
outcome_type = "binary",
subgroup_vars = c("z1", "z2"),
subgroup_cuts = list(z1 = 1L, z2 = 1L),
model = "alt",
k_inter = 1.0, # additive log-odds shift for Q under treatment
verbose = TRUE
)
} # }