Skip to content
Back to formatted view

Raw Message

Message-ID: <CAGYnQNQMCN7D0gKoX_=F46PTP3Yar+YCj5LrgcyVNhd7_eB2TQ@mail.gmail.com>
Date: 2012-10-02T22:38:55Z
From: Ben Harrison
Subject: Annotate a segmented linear regression plot
In-Reply-To: <073C1694-CFD5-40D5-9019-FA05496ACD49@comcast.net>

On 28 September 2012 16:38, David Winsemius <dwinsemius at comcast.net> wrote:
>
> ?text  # should be fairly clear.

Thank you. I was stupid to ask such a trivial question along with a
not-so-trivial one. The second part of the question was probably more
important: is there a way to obtain the location of segments produced
by the segmented package, so that I can annotate them easily?

I have since asked this question of Vito, the package's author. Here
is his response:

#####
dear Ben,
here a possible solution,

o is the segmented fit

  r<-o$rangeZ[,1]
  est.psi<-o$psi[,2]
  v<-sort(c(r, est.psi))
  xCoord<-rowMeans(cbind(v[-length(v)], v[-1]))

  Z<-o$model[,o$nameUV$Z]

  id<-sapply(xCoord, function(x)which.min(abs(x-Z)))
  yCoord<-broken.line(o)[id]

  plot(o, col=2:4, res=T)
  text(xCoord, yCoord, labels=formatC(slope(o)[[1]][,1], digits=2),
pos=4, cex=1.3)

Play with pos, cex and digits to modify the appearance of the labels.

vito
#####

> Please learn to post in plain text.

I hope this is better.

Ben.