The example given at R seems show that EST/STDERR for each slope is not a good tell (slope1 abs-t-value <1.0).
The visual tells a segmented line there.
My quest is how to tell whether 2-step/segment is better than 1-step (or 3-step is better than 2-step).
> slope(o)
$x
Est. St.Err. t value CI(95%).l CI(95%).u
slope1 -0.058993 0.062105 -0.94991 -0.18230 0.06431700
slope2 1.414600 0.046151 30.65100 1.32290 1.50620000
slope3 -0.142800 0.071994 -1.98350 -0.28575 0.00014734
# http://127.0.0.1:16212/library/segmented/html/segmented.html
set.seed(12)
xx<-1:100
zz<-runif(100)
yy<-2+1.5*pmax(xx-35,0)-1.5*pmax(xx-70,0)+15*pmax(zz-.5,0)+rnorm(100,0,2)
dati<-data.frame(x=xx,y=yy,z=zz)
out.lm<-lm(y~x,data=dati)
#the simplest example: the starting model includes just 1 covariate
#.. and 1 breakpoint has to be estimated for that
o<-segmented(out.lm) #1 breakpoint for x
#the single segmented variable is not in the starting model and 1 breakpoint for that:
#... you need to specify the variable via seg.Z, but no starting value for psi
o<-segmented(out.lm, seg.Z=~z)
#note the leftmost slope is constrained to be zero (since out.lm does not include z)
#2 segmented variables, 1 breakpoint each (again no need to specify npsi or psi)
o<-segmented(out.lm,seg.Z=~z+x)
#1 segmented variable, 2 breakpoints: you have to specify starting values (vector) for psi:
o<-segmented(out.lm,seg.Z=~x,psi=c(30,60), control=seg.control(display=FALSE))
#or by specifying just the *number* of breakpoints
#o<-segmented(out.lm,seg.Z=~x, npsi=2, control=seg.control(display=FALSE))
slope(o) #the slopes of the segmented relationship
dat2 = data.frame(x = xx, y = broken.line(o)$fit)
ggplot(dati, aes(x = x, y = y)) +
geom_point() +
geom_line(data = dat2, color = 'blue')
... View more