Learn R Programming

sitar (version 1.1.0)

plot.sitar: 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 sitar
plot(x, opt = "dv", labels, apv = FALSE, xfun = NULL,
  yfun = NULL, subset = NULL, ns = 101, abc = NULL, 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 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 as 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 sign (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.

xfun

optional function to be applied to the x variable prior to plotting. Defaults to NULL, which translates to ifun(x$call.sitar$x) and inverts any transformation applied to x in the original SITAR model call. To plot on the transformed scale set xfun to I.

yfun

optional function to be applied to the y variable prior to plotting. Defaults to NULL, which translates to ifun(x$call.sitar$y) and inverts any transformation applied to y in the original SITAR model call. To plot on the transformed scale set yfun to I.

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 and c 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 or c values are missing, values of zero are assumed.

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).

Further graphical parameters (see par) may also be supplied as arguments, e.g. line type lty, line width lwd, and colour col. For the velocity (y2) plot y2par can be used (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

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) 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. The variable names are '.x', '.y' and (for curves grouped by subject) '.id'. Note that '.x' and '.y' are returned after applying xfun and yfun. Hence if for examplex x = log(age) in the original sitar call then '.x' corresponds by default to age.

Details

For options involving both distance curves (options 'dcDua') and velocity curves (options 'vV') the velocity curve plot (with right axis) can be annotated with par parameters given as a named list called y2par. To suppress the legend that comes with it set legend = NULL.

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, e.g. for use with ggplot2.

See Also

mplot, plotclean, ifun

Examples

Run this code
# NOT RUN {
## fit sitar model
m1 <- sitar(x=age, y=height, id=id, data=heights, df=4)

## draw fitted distance and velocity curves
## with velocity curve in blue
## adding age at peak velocity
plot(m1, y2par=list(col='blue'), apv=TRUE)

## 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 mean curve for a, b, c = -1 SD
lines(m1, opt='d', lwd=2, abc=-sqrt(diag(getVarCov(m1))))

## draw fitted height distance curves coloured by subject, using ggplot
# }
# NOT RUN {
require(ggplot2)
ggplot(plot_D(m1), aes(.x, .y, colour=.id)) +
labs(x='age', y='height') +
geom_line(show.legend=FALSE)
# }

Run the code above in your browser using DataLab