Skip to contents

This generalizes the built-in sample method to the multimedia class. Given an estimated multimedia object, this function supports sampling along the estimated DAG. It first samples M* | T, X and then Y* | M*, T, X. Each sampling step will call the sample method within the mediation and outcome models that make up the multimedia object on which this is called.

Usage

# S4 method for class 'multimedia'
sample(x, size, pretreatment = NULL, profile = NULL, mediators = NULL, ...)

Arguments

x

An object of class multimedia containing the estimated mediation and outcome models whose mediation and outcome samples we want to obtain.

size

A placeholder argument to agree with the default sample method in R base. We always return the number of samples as set in either the original input x or a new input profile.

pretreatment

By default, we will return mediation and outcome model predictions using the same pretreatment variables as used when initially estimating the models (like setting newdata = NULL in usual predict). To pass in different pretreatment variables, provide a data.frame here whose columns match the pretreatments as the originally trained mediation and outcome models.

profile

An object of class treatment_profile containing the treatment profile to consider in the difference. Defaults to a profile with all the unique treatment configurations observed in the original data, shared across both the mediators and outcomes.

mediators

By default, we will return outcome predictions using the predicted mediators from the mediation model. Modify this argument if you would like to directly control the mediation inputs for the outcome model. Must be a data.frame whose columns are named to match the mediators(object).

...

Additional options to pass to the @sampler method in the estimated mediation model.

Value

An object of class multimedia with mediator and outcome slots sampled according to the description above.

Examples

exper <- demo_spline(tau = c(2, 1)) |>
    mediation_data(starts_with("outcome"), "treatment", "mediator")
fit <- multimedia(exper) |>
    estimate(exper)
samples <- sample(fit)
mediators(samples)
#> # A tibble: 2 × 1
#>   mediator
#>      <dbl>
#> 1     1.47
#> 2     2.04
outcomes(samples)
#> # A tibble: 2 × 2
#>   outcome_1 outcome_2
#>       <dbl>     <dbl>
#> 1    -0.782    -0.207
#> 2     2.47      0.977

# sampling with just different "n" has no effect.
samples <- sample(fit, 100)
#> Warning: The size argument in sample is ignored. Please adjust the treatment profile to
#> adjust the number of samples.

# Instead sample at a new treatment configuration
t1 <- data.frame(treatment = factor(rep(c(0, 1), each = 50)))
profile <- setup_profile(fit, t_mediator = t1, t_outcome = t1)
samples <- sample(fit, profile = profile)
mediators(samples)
#> # A tibble: 100 × 1
#>    mediator
#>       <dbl>
#>  1   -1.59 
#>  2   -2.12 
#>  3   -2.27 
#>  4    0.615
#>  5    1.30 
#>  6   -5.81 
#>  7   -2.71 
#>  8   -0.934
#>  9   -0.345
#> 10   -3.82 
#> # ℹ 90 more rows
outcomes(samples)
#> # A tibble: 100 × 2
#>    outcome_1 outcome_2
#>        <dbl>     <dbl>
#>  1   -0.547    -0.355 
#>  2    0.417    -0.703 
#>  3   -0.246     0.203 
#>  4    0.0750   -0.206 
#>  5   -0.229     0.261 
#>  6   -0.0285   -1.11  
#>  7   -2.27     -0.0886
#>  8    0.337     0.233 
#>  9   -0.991     0.994 
#> 10   -1.07     -2.11  
#> # ℹ 90 more rows