| sim.seg | R Documentation |
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.
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)
N |
number of clusters (usually subjects). |
ni |
number of measurements per subject. Three options. If |
fixEff |
A vector of four fixed effects: intercept, left slope, differences-in-slopes and changepoint. If the subject-specific changepoints are provided (via |
S |
The covariance matrix of the random effects. The dimension should match the length of |
seg.Z |
The numeric segmented covariate. Three options: i) a numeric vector; ii) a character to be evaluated to generate the variable; iii) |
psii |
An optional numeric vector (of length |
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 |
x.diff |
Optional. Similar to |
fam |
Which family for the response distribution? Only "gauss","pois","bin" allowed; |
sd.eps |
The standard deviation of the response, if |
seed |
The |
display |
If |
eta0 |
If |
A list
A list with components
data |
The simulated dataframe with variables, such as the response ( |
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) |
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.
Vito M.R. Muggeo vito.muggeo@unipa.it
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.
segmented.lme
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.