if (FALSE) {
library(nlme)
data(Cefamandole)
Cefamandole$lTime <-log(Cefamandole$Time)
Cefamandole$lconc <-log(Cefamandole$conc)
o<-lme(lconc ~ lTime, random=~1|Subject, data=Cefamandole)
os<-segmented.lme(o, ~lTime, random=list(Subject=pdDiag(~1+lTime+U+G0)),
control=seg.control(n.boot=0, display=TRUE))
slope(os)
####################################################
# covariate effect on the changepoint and slope diff
#let's assume a new subject-specific covariates..
set.seed(69)
Cefamandole$z <- rep(runif(6), rep(14,6))
Cefamandole$group <- gl(2,42,labels=c('a','b'))
#Here 'group' affects the slopes and 'z' affects the changepoint
o1 <-lme(lconc ~ lTime*group, random=~1|Subject, data=Cefamandole)
os1 <- segmented(o1, ~lTime, x.diff=~group, z.psi=~z,
random=list(Subject=pdDiag(~1+lTime+U+G0)))
slope(os1, by=list(group="a")) #the slope estimates in group="a" (baseline level)
slope(os1, by=list(group="b")) #the slope estimates in group="b"
###################################################
# A somewhat "complicated" example:
# i) strong heterogeneity in the changepoints
# ii) No changepoint for the Subject #7 (added)
d<-Cefamandole
d$x<- d$lTime
d$x[d$Subject==1]<- d$lTime[d$Subject==1]+3
d$x[d$Subject==5]<- d$lTime[d$Subject==5]+5
d$x[d$Subject==3]<- d$lTime[d$Subject==3]-5
d<-rbind(d, d[71:76,])
d$Subject <- factor(d$Subject, levels=c(levels(d$Subject),"7"))
d$Subject[85:90] <- rep("7",6)
o<-lme(lconc ~ x, random=~1|Subject, data=d)
os2<-segmented.lme(o, ~x, random=list(Subject=pdDiag(~1+x+U+G0)),
control=seg.control(n.boot=5, display=TRUE))
#plots with common x- and y- scales (to note heterogeneity in the changepoints)
plot(os2, n.plot = c(3,3))
os2$psi.i
attr(os2$psi.i, "is.break") #it is FALSE for Subject #7
#plots with subject-specific scales
plot(os2, n.plot = c(3,3), xscale=-1, yscale = -1)
}
Run the code above in your browser using DataLab