# Our R-package
library(HDCurves)
data(growth)
nmale <- ncol(growth$hgtm)
nfemale <- ncol(growth$hgtf)
ntime <- nrow(growth$hgtf)
dat <- data.frame(age=rep(growth$age, nmale + nfemale),
height = c(growth$hgtm, growth$hgtf),
tmt = c(rep(1, nmale*ntime),
rep(2, nfemale*ntime)),
case = rep(1:(nmale+nfemale), each=ntime))
############
# MCMC Specs
############
ni <- 20000
nb <- 10000
nt <- 10
nout <- (ni - nb)/nt
# B-spline details
ndx <- 40 # number of segments (inner knots)
bdeg <- 3 # Degree of the B-spline
tt <- sort(unique(dat$age))
# \donttest{
# This takes about 5 minutes to run
date();
fit <- HDCurves(y = dat$height, t = dat$age, tpred = tt, trt = dat$tmt,
ids = dat$case, ztype = 0, p = 0.9, A = 1, au = 1, bu = 1,
as = 1, bs = 1, atau = 1, btau = 1/0.5, ndx = ndx, q = bdeg,
balanced = TRUE,draws=ni, burn=nb, thin=nt)
date();
# subject-specific derivative curve means
fpmn <- apply(fit$fprime,c(1,2),mean)
# group-specific derivative curve means
gfpmn <- apply(fit$fgprime,c(1,2),mean)
gfpci <- apply(fit$fgprime,c(1,2),
function(x) quantile(x, c(0.0275, 0.975)))
plot(tt,gfpmn[,1], type="n", ylim=range(c(gfpmn)),
ylab="Height Rate of Change", xlab="age",
xaxp=c(2,18,n=8),cex.axis=1.45, cex.lab=1.45)
lines(tt, gfpmn[,1], type='o', col='blue', pch=20)
lines(tt, gfpci[1,,1], col='blue', pch=20,lty=2)
lines(tt, gfpci[2,,1], col='blue', pch=20,lty=2)
lines(tt, gfpmn[,2], type='o', col='red', pch=20)
lines(tt, gfpci[1,,2], col='red', pch=20,lty=2)
lines(tt, gfpci[2,,2], col='red', pch=20,lty=2)
legend(x="topright", lty=1, pch=20, cex=1.45,
legend=c("Girls","Boys"), col=c("red","blue"))
# }
Run the code above in your browser using DataLab