sim.seg: Simulate data for (generalized) segmented (mixed) models

View source: R/sim.seg.r

sim.segR Documentation

Simulate data for (generalized) segmented (mixed) models

Description

Function to simulate segmented mixed data, namely longitudinal data with individual profiles having piecewise linear relationships with a breakpoint. Heterogeneities (via multinormal random effects) are allowed for each model parameter: intercept, left and right slopes and breakpoint.

Usage

sim.seg(N, ni, fixEff = c(2, -0.2, 0.3, 0.4), S = diag(c(0.05, 0.05, 0.05, 0.07)^2), 
  seg.Z=NULL, psii, fam=c("gauss","pois","bin"), z.psi, x.diff, 
  sd.eps = 0.01, seed, display = FALSE, eta0=FALSE)

Arguments

N

number of clusters (usually subjects).

ni

number of measurements per subject. Three options. If length(ni)=N, it represents the actual number of measurements per each subject; if scalar (and larger than 1), exactly ni measurements per every subject are generated; otherwise the number of measurements for the N ‘subjects’ is computed as sample(min(n):max(n), size=N, replace=TRUE). If ni=1, simple (i.e. without repeated measurements) segmented data are generated.

fixEff

A vector of four fixed effects: intercept, left slope, differences-in-slopes and changepoint. If the subject-specific changepoints are provided (via psii), fixEff should have 3 entries.

S

The covariance matrix of the random effects. The dimension should match the length of fixEff. Ignored if ni=1.

seg.Z

The numeric segmented covariate. Three options: i) a numeric vector; ii) a character to be evaluated to generate the variable; iii) NULL to generate uniform values in (0,1), differently among the subjects. If necessary, such vector is truncated or recycled to match the correct length.

psii

An optional numeric vector (of length N) of subject-specific changepoints.

z.psi

Optional, to specify if the changepoint depends on some covariates. It is a two-components list including the coefficients and the covariate(s) affecting the changepoints. If there are p covariates, these should be organized in a N \times p matrix, otherwise a simple N-dimensional vector. z.psi can also be a single scalar representing the coefficient of the covariate obtained as rep(c(0, 1), length = N). See the example below.

x.diff

Optional. Similar to z.psi, but for the slope difference parameter. If a single value is provided, it represents the coefficient and the covariate is obtained as rep(c(0,1), c(ceiling(N/2), floor(N/2))).

fam

Which family for the response distribution? Only "gauss","pois","bin" allowed;

sd.eps

The standard deviation of the response, if fam="gauss"

seed

The seed for the random generation (used via set.seed(seed)).

display

If TRUE a (rought) plot of the simulated data (with underlying segmented profiles) is produced.

eta0

If TRUE, the true means are also returned in the dataframe.

Details

A list

Value

A list with components

data

The simulated dataframe with variables, such as the response (y), the segmented covariate (x), and the ‘subject’ id (id) if longitudinal data have been generated. The column eta0 represents the true linear predictor (from which the data have been generated). The dataframe also includes the variables x.diff and z.psi, if specified.

fixef

The fixed effects used

psi.i

The (potentially different) individual changepoints.

sd

The standard deviation of the response and of random effects, if longitudinal data have been generated

ni

The number of repeated measurements (per subject).

ranef

the random effects matrix (if longitudinal data have been generated)

perc.psi

the psi percentile: for each subject, the fraction of data having values smaller than the true breakpoint (if longitudinal data have been generated)

Note

If z.psi and/or x.diff have been specified, no (fixed-effects) segmented relationship is drawn (even if display=TRUE) because it is no unique.

Author(s)

Vito M.R. Muggeo vito.muggeo@unipa.it

References

Muggeo, V.M.R. (2026) Fitting segmented mixed models with random changepoints in R. J of Reproducible Statistics 1, art 12. 10.68365/jrepstat.2026.12.

See Also

segmented.lme

Examples

## Not run: 
#simulate gaussian data: (N subjects, each with 12 measurements) 
#   following the segmented relationships  ~ a + b*x + d*(x-psi)_+
# where
# x: the segmented covariate (default is values from U(0,1))
# the intercept:        a=2 (default)
# the left slope:       b=-0.2 (default)
# the slope difference: d=0.3 (default) +.5*x.diff (x.diff built by default, see argument x.diff)
# the breakpoints is:   psi= 0.4 (default) + 0.3*Z[,1] -0.2*Z[,2]

#Each model parameter has its random effects coming from N(0, S) 

N=30
Z=cbind(1:N/N, rnorm(N))
dd=sim.seg(N,ni=12,S=diag(c(.12,.05,.3,.15)^2), 
   x.diff=.5, z.psi=list(c(.3,-.2),Z), sd.e=.003,display=TRUE)
d=dd$data


## End(Not run)


segmented documentation built on Nov. 24, 2026, 5:09 p.m.