transform_to_posttest: Helper to transform the simulated longitudinal 'data.frame'

View source: R/simulate.R

transform_to_posttestR Documentation

Helper to transform the simulated longitudinal data.frame

Description

This is en example of a data transformation applied during simulation. It takes the longitudinal data and transforms it into a pretest-posttest model in wide format. Useful if you want to compare the longitudinal LMM with e.g. AN(C)OVA models.

Usage

transform_to_posttest(data)

Arguments

data

a data.frame created using simulate_data

Value

a data.frame with y now only includes the posttest values. Also includes three new columns:

  • pre subject-level pretest scores.

  • pre_cluster cluster-level pretest scores.

  • pre_subject_c subject-level pretest scores center around the cluster-level pretest.

See Also

simulate.plcp, study_parameters

Examples


# Compare longitudinal 3-level model to 2-level model
# fit to just the posttest data
#
# Both models are fit to the same dataset during simulation.
p <- study_parameters(n1 = 11,
                      n2 = 20,
                      n3 = 3,
                      icc_pre_subject = 0.5,
                      icc_pre_cluster = 0.1,
                      icc_slope = 0.05,
                      var_ratio = 0.03)

# simulation formulas
# analyze as a posttest only 2-level model
f_pt <- sim_formula("y ~ treatment + (1 | cluster)",
                 test = "treatment",
                 data_transform = transform_to_posttest)

# analyze as 3-level longitudinal
f_lt <- sim_formula("y ~ time*treatment +
                         (1 + time | subject) +
                         (1 + time | cluster)")

f <- sim_formula_compare("posttest" = f_pt,
                         "longitudinal" = f_lt)
## Not run: 
res <- simulate(p,
                formula = f,
                nsim = 2000,
                cores = parallel::detectCores(),
                satterthwaite = TRUE)
summary(res)

## End(Not run)


rpsychologist/powerlmm documentation built on May 11, 2023, 12:24 a.m.