[R] Regression line limited by the range of values

Andreas Svensson andreas.svensson at bio.ntnu.no
Wed May 24 21:53:23 CEST 2006


Thankyou very much Marc for that nifty little script.

When I use it on my real dataset though, the lines are fat in the middle 
and thinner towards the ends. I guess it's because "lines" draw one 
fitted line for each x, and if you have hundreds of x, this turns into a 
line that is thicker that it should be (due to rounding errors perhaps).

I got the tip to use "segments", and draw one line from min(x),min(y) to 
max(x),max(y) but with real data with a bunch of "na.rm" and 
"na.action"s this becomes very long and bulky.

For a regression with two data swarms and two trend lines it becomes 
this long mess:

<>plot(TCgonad[Period=="1"], 
ABelly[Period=="1"],xlim=c(0,20),ylim=c(130,160), col=”blue”)
points(TCgonad[Period=="2"], ABelly[Period=="2"],col=”red”)

segments(
min(TCgonad[Period=="1"],na.rm=T), 
min(fitted(lm(ABelly[Period=="1"]~TCgonad[Period=="1"],
na.action=na.exclude)),na.rm=T),
max(TCgonad[Period=="1"],na.rm=T), 
max(fitted(lm(ABelly[Period=="1"]~TCgonad[Period=="1"],
na.action=na.exclude)),na.rm=T),col=”blue”)

<>segments(
min(TCgonad[Period=="2"],na.rm=T), 
min(fitted(lm(ABelly[Period=="2"]~TCgonad[Period=="2"],
na.action=na.exclude)),na.rm=T),
max(TCgonad[Period=="2"],na.rm=T), 
max(fitted(lm(ABelly[Period=="2"]~TCgonad[Period=="2"],
na.action=na.exclude)),na.rm=T),col=”red”)


I just think it's strange that abline has as a nonadjustable default to 
extrapolate the line to outside the data - a mortal sin in my field.

Cheers
Andreas



More information about the R-help mailing list