# 1. One population of linear growers with a random intercept and a random slope,
# measured yearly for four years (five occasions), with the level-one
# error set directly.
set.seed(113)
d <- simulate_longitudinal_polynomial(
n = 50,
target_times = 0:4,
fixed_coefficients = c(10, 1.5), # intercept 10, slope 1.5 per year
random_variances = c(4, 0.25), # var(intercept) = 4, var(slope) = .25
error_variance = 1
)
head(d)
# 2. Two populations that differ only in their slope (a treatment that changes
# the rate of growth). Pass a list of coefficient vectors, one per population.
set.seed(113)
two <- simulate_longitudinal_polynomial(
n = c(40, 40),
target_times = 0:4,
fixed_coefficients = list(control = c(10, 1.0), treatment = c(10, 1.8)),
random_variances = c(4, 0.25),
error_variance = 1
)
aggregate(y ~ population + occasion, data = two, FUN = mean)
# 3. Pin the level-one error to a target reliability instead of setting it
# directly. The single number is the average reliability across occasions;
# the per-occasion values differ and are returned as an attribute.
set.seed(113)
rel <- simulate_longitudinal_polynomial(
n = 100,
target_times = 0:4,
fixed_coefficients = c(10, 1.5),
random_variances = c(4, 0.25),
reliability = 0.80
)
attr(rel, "error_variance")
round(attr(rel, "reliability_by_occasion"), 3) # not constant across waves
# 4. Assessment-time jitter: the nominal "yearly" schedule, but subjects
# actually arrive a little early or late (SD of about six weeks on a
# one-year scale). The nominal and actual times are kept in separate
# columns so the consequences of analyzing on the nominal time can be
# studied.
set.seed(113)
jit <- simulate_longitudinal_polynomial(
n = 30,
target_times = 0:4,
fixed_coefficients = c(10, 1.5),
random_variances = c(4, 0.25),
error_variance = 1,
timing_sd = 0.12
)
head(jit[, c("id", "occasion", "target_time", "time")])
# 4b. Unit-specific measurement times: each of 12 children is tested
# between 40 and 90 weeks of age, five to nine times, no two on the
# same schedule. The level-one error is a single variance; the
# "schedule" attribute records the design.
set.seed(113)
ages <- simulate_longitudinal_polynomial(
n = 12,
time_range = c(40, 90),
occasions = c(5, 9),
fixed_coefficients = c(10, 0.5),
random_variances = c(4, 0.01),
error_variance = 2
)
attr(ages, "schedule")
table(table(ages$id)) # units per occasion count
# 5. A flat line (order 0): no growth, only a random subject level and
# measurement error. The coefficient vector has length one.
set.seed(113)
flat <- simulate_longitudinal_polynomial(
n = 20,
target_times = 0:4,
fixed_coefficients = 5,
random_variances = 2,
error_variance = 1
)
head(flat)
# 6. Autocorrelated measurement error: the same error variance at each wave,
# but the level-one errors decay as an AR(1) process (errors at adjacent
# occasions correlate 0.5), the kind of dependence a model assuming
# independent errors would miss. The full error covariance is returned.
set.seed(113)
ar <- simulate_longitudinal_polynomial(
n = 40,
target_times = 0:4,
fixed_coefficients = c(10, 1.5),
random_variances = c(4, 0.25),
error_variance = 1,
error_structure = "ar1",
error_correlation = 0.5
)
round(attr(ar, "error_covariance"), 3)
Run the code above in your browser using DataLab