[R] Extrapolated regression lines
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Oct 12 16:01:07 CEST 2006
On 10/12/2006 9:41 AM, Johan A. Stenberg wrote:
> Dear list members,
>
> When I create a simple scatterplot with a regression line (se below) the
> line is automatically extrapolated outside the range of data points. Why
> is this and how can I prevent R from extrapolating the regression line?
>
> Thank you in advance,
> Johan
>
> model<-lm(Herb~Para)
> plot(Para,Herb)
> abline(model)
abline draws a line, not a line segment. If you want a segment that
stays within the data, you'll need something like this:
model <- lm(Herb ~ Para)
plot(Para, Herb)
lines(Para, predict(model))
Note that if Para is not sorted, this may look a little strange, and it
will look like a complete mess if you try to fit a polynomial. You can
sort it (and Herb correspondingly) by
o <- order(Para)
Para <- Para[o]
Herb <- Herb[o]
Duncan Murdoch
More information about the R-help
mailing list