plot.sitar: Plot SITAR model

View source: R/plot.sitar.R

plot.sitarR Documentation

Plot SITAR model

Description

plot and lines methods for objects of class sitar, providing various flavours of plot of the fitted growth curves. Also helper functions to return the data for plotting, e.g. with ggplot2.

Usage

## S3 method for class 'sitar'
plot(
  x,
  opt = "dv",
  labels = NULL,
  apv = FALSE,
  xfun = identity,
  yfun = identity,
  subset = NULL,
  ns = 101,
  abc = NULL,
  trim = 0,
  add = FALSE,
  nlme = FALSE,
  returndata = FALSE,
  ...,
  xlab = NULL,
  ylab = NULL,
  vlab = NULL,
  xlim = c(NA, NA),
  ylim = c(NA, NA),
  vlim = c(NA, NA),
  legend = list(x = "topleft", inset = 0.04, bty = "o")
)

## S3 method for class 'sitar'
lines(x, ...)

plot_d(x, ...)

plot_v(x, ...)

plot_D(x, ...)

plot_V(x, ...)

plot_u(x, ...)

plot_a(x, ...)

plot_c(x, ...)

Arguments

x

object of class sitar.

opt

character string containing a subset of letters corresponding to the options: 'd' for fitted Distance curve, 'v' for fitted Velocity curve, 'c' for fitted Crosssectional distance curve, 'D' for individual fitted Distance curves, 'V' for individual fitted Velocity curves, 'u' for Unadjusted individual growth curves, and 'a' for Adjusted individual growth curves. Options 'dvcDV' give spline curves, while 'ua' give data curves made up of line segments. If both distance and velocity curves are specified, the axis for the velocity curve appears on the right side of the plot (y2), and a legend identifying the distance and velocity curves is provided.

labels

optional character vector containing plot labels for x, y and y velocity from the original SITAR model. The three elements can alternatively be provided via parameters xlab, ylab and vlab. The latter take precedence. Default labels are the names of x and y, and "y velocity", suitably adjusted to reflect any back-transformation via xfun and yfun.

apv

optional logical specifying whether or not to calculate the age at peak velocity from the velocity curve. If TRUE, age at peak velocity is calculated as the age when the second derivative of the fitted curve changes from positive to negative (after applying xfun and/or yfun). Age at peak velocity is marked in the plot with a vertical dotted line, and its value, along with peak velocity, is printed and returned. NB their standard errors can be obtained using the bootstrap with the function apv_se. With plot options 'D' or 'V' values of apv for individual subjects or groups are also returned invisibly.

xfun

optional function to be applied to the x variable prior to plotting (default identity, see Details).

yfun

optional function to be applied to the y variable prior to plotting (default identity, see Details).

subset

optional logical vector of length x defining a subset of data rows to be plotted, for x and data in the original sitar call.

ns

scalar defining the number of points for spline curves (default 101).

abc

vector of named values of random effects a, b, c and d used to define an individual growth curve, e.g. abc = c(a = 1, c = -0.1). Alternatively a single character string defining an id level whose random effect values are used. If abc is set, level is ignored. If abc is NULL (default), or if a, b, c or d values are missing, values of zero are assumed.

trim

number (default 0) of long line segments to be excluded from plot with option 'u' or 'a'. See Details.

add

optional logical defining if the plot is pre-existing (TRUE) or new (FALSE). TRUE is equivalent to using lines.

nlme

optional logical which set TRUE plots the model as an nlme object, using plot.nlme arguments.

returndata

logical defining whether to plot the data (default FALSE) or just return the data for plotting (TRUE). See Value.

...

Further graphical parameters (see par) may also be supplied as arguments, e.g. line type lty, line width lwd, and colour col. For plots involving both distance curves (y1) and velocity curves (y2) the par arguments can be combined (see Details).

xlab

optional label for x axis

ylab

optional label for y axis

vlab

optional label for v axis (velocity)

xlim

optional x axis limits

ylim

optional y axis limits

vlim

optional v axis limits

legend

optional list of arguments for legend with distance-velocity plots, default list(x = 'topleft', inset = 0.04, bty = 'o')

Details

For plots involving both distance curves (options 'dcDua') and velocity curves (options 'vV') the two sets of curves use the y1 and y2 axes respectively and can be annotated differently by combining their par parameters. For example col = c(2, 4) sets the distance curve(s) to red and the velocity curve(s) to blue. (Previously this was done using the named list y2par.) To suppress the associated legend set legend = NULL.

The transformations xfun and yfun are applied to the x and y variables after back-transforming any transformations in the original SITAR call. So for example if y = log(height) in the SITAR call, then yfun is applied to height. Thus the default yfun = identity has the effect of back-transforming the SITAR call transformation - this is achieved by setting yfun = yfun(ifun(x$call.sitar$y)). For no transformation set yfun = NULL. The same applies to xfun.

For models that include categorical fixed effects (e.g. a.formula = ~sex + region) the options 'dv' provide mean curves for each distinct group. Any continuous (as opposed to grouped) fixed effect variables are set to their mean values in the plots, to ensure that the mean curves are smooth. The resulting plots can be formatted with par in the usual way, indexed either by the individual grouping variables (e.g. sex or region in the example) or the subject factor id which indexes all the distinct plots.

The helper functions plot_d, plot_v, plot_D, plot_V, plot_u, plot_a and plot_c correspond to the seven plot options defined by their last letter, and return the data for plotting as a tibble, e.g. for use with ggplot2. Setting returndata = TRUE works similarly but handles multiple options, returning a list of tibbles corresponding to each specified option.

The trim option allows unsightly long line segments to be omitted from plots with options 'a' or 'u'. It ranks the line segments on the basis of the age gap (dx) and the distance of the midpoint of the line from the mean curve (dy) using the formula abs(dx)/mad(dx) + abs(dy)/mad(dy) and omits those with the largest values.

Value

If returndata is FALSE returns invisibly a list of (up to) three objects:

usr

value of par('usr') for the main plot.

.

usr2

the value of par('usr') for the velocity (y2) plot.

apv

if argument apv is TRUE a named list giving the age at peak velocity (apv) and peak velocity (pv) from the fitted velocity curve, either overall or (with options D or V, invisibly) for all subjects.

If returndata is TRUE (which it is with the helper functions) returns invisibly either a tibble or named list of tibbles, containing the data to be plotted. The helper functions each return a tibble where the first three variables are named according to x, y and id from the original sitar call, plus variable '.groups' and the relevant categorical variables for grouped curves. Note that x and y are returned after applying xfun and yfun. Hence if for example x = log(age) in the SITAR call then x as returned corresponds by default to age.

Author(s)

Tim Cole tim.cole@ucl.ac.uk

See Also

mplot, plotclean, ifun, apv_se

Examples


## fit sitar model
m1 <- sitar(x = age, y = height, id = id, data = heights, df = 4)

## draw fitted distance and velocity curves
## with distance in red and velocity in blue
## marking age at peak velocity (apv)
plot(m1, col = c('red', 'blue'), apv = TRUE)

## bootstrap standard errors for apv and pv
## Not run: 
res <- apv_se(m1, nboot = 20, plot = TRUE)

## End(Not run)
## draw individually coloured growth curves adjusted for random effects
## using same x-axis limits as for previous plot
plot(m1, opt = 'a', col = id, xlim = xaxsd())

## add mean curve in red
lines(m1, opt = 'd', col = 'red', lwd = 2)

## add curve for an individual with random effects a, b and c = -1 SD
lines(m1, opt = 'd', lwd = 2, abc = -sqrt(diag(getVarCov(m1))))

## compare curves for early versus late menarche
heights <- within(sitar::heights, {
  men <- abs(men)
  late <- factor(men > median(men))
})
## fit model where size and timing differ by early vs late menarche
m2 <- sitar(log(age), height, id, heights, 5,
  a.formula = ~late, b.formula = ~late)
## plot distance and velocity curves for the two groups
plot(m2, opt = 'dv', lwd = 2, col = late)
legend('bottom', paste(c('early', 'late'), 'menarche'),
  lwd = 2, col = 1:2, inset = 0.04)
## alternatively plot early and late groups separately
## early
lines(m2, opt = 'dv', subset = late == FALSE, col = 'white')
## late
lines(m2, opt = 'dv', subset = late == TRUE, col = 'white')

## draw fitted height distance curves coloured by subject, using ggplot
## Not run: 
require(ggplot2)
ggplot(plot_D(m1), aes(age, height, colour = .id)) +
geom_line(show.legend = FALSE)

## End(Not run)

statist7/sitar documentation built on April 13, 2025, 12:57 a.m.