## 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 (apv)
plot(m1, y2par = list(col = 'blue'), apv = TRUE)
## bootstrap standard errors for apv and pv
if (FALSE) {
res <- apv_se(m1, nboot = 20, plot = 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))))
## use subset to plot mean curves by group
## 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)
## early group
plot(m2, subset = late == FALSE, col = 4, lwd = 3,
y2par = list(col = 4, lwd = 2), ylim = range(heights$height))
## late group
lines(m2, subset = late == TRUE, col = 2, lwd = 3,
y2par = list(col = 2, lwd = 2))
## add legend
legend('right', paste(c('early', 'late'), 'menarche'),
lty = 1, col = c(4, 2), inset = 0.04)
## alternatively plot both groups together
plot(m2, lwd = 3, col = late, y2par = list(lwd = 3, col = late))
legend('right', paste(c('early', 'late'), 'menarche'),
lwd = 3, col = 1:2, inset = 0.04)
## draw fitted height distance curves coloured by subject, using ggplot
if (FALSE) {
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