simulate_smoothbp: Simulate data from the smooth change-point model

View source: R/simulate.R

simulate_smoothbpR Documentation

Simulate data from the smooth change-point model

Description

Generates synthetic data from the model used by smoothbp, including optional between-subject random intercepts. Supports any number of breakpoints K \geq 1. True parameter values are stored as the "true_params" attribute so they can be compared against posterior estimates.

Usage

simulate_smoothbp(
  n_subj = 20L,
  n_obs = 8L,
  b0 = 5,
  b1 = -0.3,
  delta = 1.2,
  omega = 3,
  rho = 4,
  sigma = 0.4,
  sigma_u = 0.5,
  tau_range = c(0, 6),
  seed = NULL
)

Arguments

n_subj

Number of subjects (groups). Set to 1 and sigma_u = 0 for a single-group simulation.

n_obs

Observations per subject. May be a scalar (same for all subjects) or a length-n_subj integer vector for unbalanced designs.

b0

Overall intercept (conditional mean at \tau = \omega_1).

b1

Pre-change-point slope (evaluated relative to \omega_1).

delta

Change in slope at each change-point. A numeric vector of length K; a scalar is treated as K = 1.

omega

Change-point location(s). A numeric vector of length K, in ascending order. A scalar is treated as K = 1.

rho

Sharpness of each transition. A numeric vector of length K (all values must be positive); a scalar is recycled to length K.

sigma

Residual standard deviation.

sigma_u

Between-subject SD for random intercepts. Set to 0 to suppress random effects. Default 0.5.

tau_range

Numeric vector of length 2 giving the range of the time variable. Observations are evenly spaced within this range for each subject. Default c(0, 6).

seed

Integer seed for reproducibility. Sampled randomly if NULL (default).

Details

The data-generating model for K breakpoints is:

y_{ij} = (b_0 + u_j) + b_1 (\tau_{ij} - \omega_1) + \sum_{k=1}^{K} \delta_k \, d_{ijk} \, \text{logistic}(d_{ijk} \, \rho_k) + \varepsilon_{ij}

where d_{ijk} = \tau_{ij} - \omega_k and \text{logistic}(\cdot) is the logistic function \text{logistic}(x) = (1 + e^{-x})^{-1}. The pre-break slope b_1 is centred at the first change-point \omega_1, so b_0 represents the conditional mean at \tau = \omega_1 (consistent with the fitted model).

For a single breakpoint (K = 1), scalar values of omega, rho, and delta are accepted for backward compatibility.

Value

A data.frame with columns:

subject

Subject identifier (factor).

tau

Time variable.

mu

Noise-free conditional mean \mu_{ij}.

y

Observed response.

The attribute "true_params" is a named list containing the data-generating values of b0, b1, delta, omega, rho, sigma, sigma_u, the vector of subject-level deviations u, and the seed used.

Examples

# Single breakpoint (K = 1)
dat1 <- simulate_smoothbp(
  n_subj = 20, n_obs = 8,
  b0 = 5, b1 = -0.3, delta = 1.2,
  omega = 3, rho = 4, sigma = 0.4, sigma_u = 0.5,
  seed = 42
)
head(dat1)
attr(dat1, "true_params")

# Two breakpoints (K = 2)
dat2 <- simulate_smoothbp(
  n_subj = 20, n_obs = 12,
  b0 = 5, b1 = -0.3,
  delta = c(1.2, -0.8),
  omega = c(2, 4),
  rho   = c(4, 4),
  sigma = 0.4, sigma_u = 0.5,
  seed = 42
)
head(dat2)


smoothbp documentation built on June 14, 2026, 9:06 a.m.