Skip to contents

Takes fitted model output to created predicted values for user-specified histories (pooling for imputed data), before conducting contrast comparisons (pooling for imputed data), correcting for multiple comparisons, and then plotting results.

Usage

compareHistories(
  fit,
  hi_lo_cut,
  dose_level = "h",
  reference = NULL,
  comparison = NULL,
  outcome_level = NULL,
  mc_comp_method = "BH",
  verbose = FALSE,
  save.out = FALSE
)

# S3 method for class 'devMSM_comparisons'
print(x, save.out = FALSE, ...)

# S3 method for class 'devMSM_comparisons'
plot(
  x,
  colors = "Dark2",
  exp_lab = NULL,
  out_lab = NULL,
  save.out = FALSE,
  ...
)

# S3 method for class 'devMSM_comparisons'
summary(object, type = "comps", ...)

Arguments

fit

list of model outputs from fitModel()

hi_lo_cut

list of two numbers indicating quantile values that reflect high and low values, respectively, for continuous exposure

dose_level

(optional) "l" or "h" indicating whether low or high doses should be tallied in tables and plots (default is high "h")

reference

lists of one or more strings of "-"-separated "l" and "h" values indicative of a reference exposure history to which to compare comparison, required if comparison is supplied

comparison

(optional) list of one or more strings of "-"-separated "l" and "h" values indicative of comparison history/histories to compare to reference, required if reference is supplied

outcome_level

levels of a factor outcome within which the user wishes compare histories (default is all levels), history predictions and comparisons will be made within each outcome level.

mc_comp_method

(optional) character abbreviation for multiple comparison correction method for stats::p.adjust, default is Benjamini-Hochburg ("BH")

verbose

(optional) TRUE or FALSE indicator for printing output to console. default is FALSE.

save.out

(optional) Either logical or a character string. If TRUE, it will output the result to a default file name within home_dir set in initMSM(). You can load the data with x <- readRDS(file). To use a non-default file name, specify a character string with the file name. It will save relative to home_dir. There might be naming conflicts where two objects get saved to the same file. In these cases, users should specify a custom name. default is FALSE.

x

devMSM_histories object from compareHistories()

...

ignored

colors

(optional) character specifying Brewer palette or list of colors (n(epochs)+1) for plotting (default is "Dark2" palette)

exp_lab

(optional) character label for exposure variable in plots (default is variable name)

out_lab

(optional) character label for outcome variable in plots (default is variable name)

object

devMSM_histories object from compareHistories()

type

Either "preds" or "comps" corresponding to the results of marginaleffects::avg_predictions() at low and high dosages or marginaleffects::avg_comparisons() respectively

Value

list containing two dataframes: preds with predictions from marginaleffects::avg_predictions() containing average expected outcome for different exposure histories and comps with contrasts from marginaleffects::comparisons() comparing different exposure history

Examples

library(devMSMs)
set.seed(123)
data <- data.frame(
  ID = 1:50,
  A.1 = rnorm(n = 50),
  A.2 = rnorm(n = 50),
  A.3 = rnorm(n = 50),
  B.1 = rnorm(n = 50),
  B.2 = rnorm(n = 50),
  B.3 = rnorm(n = 50),
  C = rnorm(n = 50),
  D.3 = rnorm(n = 50)
)
obj <- initMSM(
  data,
  exposure = c("A.1", "A.2", "A.3"),
  ti_conf = c("C"),
  tv_conf = c("B.1", "B.2", "B.3", "D.3")
)
f <- createFormulas(obj, type = "short")
w <- createWeights(data = data, formulas = f)
fit <- fitModel(
  data = data, weights = w,
  outcome = "D.3", model = "m0"
)
#> Error in all_fits[[1]]: subscript out of bounds

comp <- compareHistories(
  fit = fit,
  hi_lo_cut = c(0.3, 0.6)
)
#> Error: in compareHistories(fit = fit, hi_lo_cut =...:
#>  Argument 'fit' (equal to 'fit') could not be evaluated.
#> PROBLEM: object 'fit' not found.
print(comp)
#> Error: object 'comp' not found
plot(comp)
#> Error: object 'comp' not found
summary(comp, "preds")
#> Error: object 'comp' not found
summary(comp, "comps")
#> Error: object 'comp' not found

comp2 <- compareHistories(
  fit = fit,
  hi_lo_cut = c(0.3, 0.6),
  reference = "l-l-l",
  comparison = c("h-h-h", "h-h-l")
)
#> Error: in compareHistories(fit = fit, hi_lo_cut =...:
#>  Argument 'fit' (equal to 'fit') could not be evaluated.
#> PROBLEM: object 'fit' not found.
print(comp2)
#> Error: object 'comp2' not found
plot(comp2)
#> Error: object 'comp2' not found
summary(comp2, "preds")
#> Error: object 'comp2' not found
summary(comp2, "comps")
#> Error: object 'comp2' not found