[R] Trendline for a subset of data

0 views
Skip to first unread message

Steve Murray

unread,
Oct 9, 2009, 5:50:59 AM10/9/09
to r-h...@r-project.org

Dear all,

I am using abline(lm ...) to insert a linear trendline through a portion of my data (e.g. dataset[,36:45]). However, I am finding that whilst the trendline is correctly displayed and representative of the data portion I've chosen, the line continues to run beyond this data segment and continues until it intersects the vertical axes at each side of the plot.

How do I display the line so that it only runs between point 36 and 45 (as shown in the example above) as doesn't continue to display a line throughout the rest of the plot space?

Many thanks,

Steve

_________________________________________________________________
View your other email accounts from your Hotmail inbox. Add them now.
http://clk.atdmt.com/UKM/go/167688463/direct/01/
______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

David Winsemius

unread,
Oct 9, 2009, 9:27:43 AM10/9/09
to Steve Murray, r-h...@r-project.org

On Oct 9, 2009, at 5:50 AM, Steve Murray wrote:

>
> Dear all,
>
> I am using abline(lm ...) to insert a linear trendline through a
> portion of my data (e.g. dataset[,36:45]). However, I am finding
> that whilst the trendline is correctly displayed and representative
> of the data portion I've chosen, the line continues to run beyond
> this data segment and continues until it intersects the vertical
> axes at each side of the plot.
>
> How do I display the line so that it only runs between point 36 and
> 45 (as shown in the example above) as doesn't continue to display a
> line throughout the rest of the plot space?
>

?segments

> Many thanks,
>
> Steve
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

Mark Difford

unread,
Oct 9, 2009, 10:17:48 AM10/9/09
to r-h...@r-project.org

Hi Steve,

>> However, I am finding that ... the trendline ... continues to run beyond


>> this data segment
>> and continues until it intersects the vertical axes at each side of the
>> plot.

Your "best" option is probably Prof. Fox's reg.line function in package car.

##
library(car)
?reg.line
reg.line

Regards, Mark.

--
View this message in context: http://www.nabble.com/Trendline-for-a-subset-of-data-tp25818425p25821972.html
Sent from the R help mailing list archive at Nabble.com.

Steve Murray

unread,
Oct 9, 2009, 10:45:37 AM10/9/09
to dwins...@comcast.net, r-h...@r-project.org

<CB247343-29EC-495A...@comcast.net>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0


Thanks for flagging up the 'segments' command. However=2C I'm having troubl=
e getting it to work - this is probably due to me misunderstanding the docu=
mentation for this command.

The plot section of my script appears as follows:

test3 <- 36:45
plot(data_means=2C type=3D"b"=2C pch=3D4=2C ylab=3D""=2C xlab=3D""=2C xaxt=
=3D"n"=2C yaxt=3D"n"=2C col=3D3=2C ylim=3Dc(250=2C380))
abline(lm(data_means[36:45] ~ test3)=2C lty=3D2)


As you can see=2C I'm only plotting points 36 to 45 from the object 'data_m=
eans'. This intentionally results in much white space along the x-axis unti=
l point 36 is reached. However=2C when inserting the trendline=2C this runs=
along the entire length of the x-axis instead of just through points 36 to=
45. Ideally=2C a line which slightly overshoots the data subset would look=
the best=2C but one constrained to the extents of the 36:45 would also do =
the job just fine.

So=2C my question is=2C how do I use 'segments' (or otherwise) to create a =
linear trendline which only extends through points 36:45? (and overshoots a=
t either end very slightly=2C if possible=2C rather than running along the =
entire length of the x-axis).

Many thanks again=2C

Steve

----------------------------------------
> CC: r-h...@r-project.org
> From: dwins...@comcast.net
> To: smurr...@hotmail.com
> Subject: Re: [R] Trendline for a subset of data
> Date: Fri=2C 9 Oct 2009 09:27:43 -0400
>
>
> On Oct 9=2C 2009=2C at 5:50 AM=2C Steve Murray wrote:
>
>>
>> Dear all=2C


>>
>> I am using abline(lm ...) to insert a linear trendline through a

>> portion of my data (e.g. dataset[=2C36:45]). However=2C I am finding


>> that whilst the trendline is correctly displayed and representative

>> of the data portion I've chosen=2C the line continues to run beyond


>> this data segment and continues until it intersects the vertical
>> axes at each side of the plot.
>>
>> How do I display the line so that it only runs between point 36 and
>> 45 (as shown in the example above) as doesn't continue to display a
>> line throughout the rest of the plot space?
>>
>
> ?segments
>

>> Many thanks=2C
>>
>> Steve
>>
>
> David Winsemius=2C MD
> Heritage Laboratories
> West Hartford=2C CT
>
=0A=
_________________________________________________________________=0A=
View your other email accounts from your Hotmail inbox. Add them now.=0A=

David Winsemius

unread,
Oct 9, 2009, 1:15:46 PM10/9/09
to Steve Murray, r-h...@r-project.org
You are emitting weird hex-stuff from your mail client. And from what
I can tell that is not a reproducible example even if hex-ation
problem gets fixed. There is a worked example in segments.

On Oct 9, 2009, at 10:45 AM, Steve Murray wrote:

>
> <CB247343-29EC-495A...@comcast.net>
> Content-Type: text/plain; charset="iso-8859-1"
> Content-Transfer-Encoding: quoted-printable
> MIME-Version: 1.0
>
>
> Thanks for flagging up the 'segments' command. However=2C I'm having
> troubl=
> e getting it to work - this is probably due to me misunderstanding
> the docu=
> mentation for this command.
>
> The plot section of my script appears as follows:
>
> test3 <- 36:45
> plot(data_means=2C type=3D"b"=2C pch=3D4=2C ylab=3D""=2C
> xlab=3D""=2C xaxt=
> =3D"n"=2C yaxt=3D"n"=2C col=3D3=2C ylim=3Dc(250=2C380))
> abline(lm(data_means[36:45] ~ test3)=2C lty=3D2)
>
>
> As you can see=2C

I cannot "see" anything. If you are under the impression that there
should have been an attachment, then (re-?)read the Posting Guide on
that topic.


> I'm only plotting points 36 to 45 from the object 'data_m=
> eans'. This intentionally results in much white space along the x-
> axis unti=
> l point 36 is reached. However=2C when inserting the trendline=2C
> this runs=
> along the entire length of the x-axis instead of just through points
> 36 to=
> 45. Ideally=2C a line which slightly overshoots the data subset
> would look=
> the best=2C but one constrained to the extents of the 36:45 would
> also do =
> the job just fine.
>
> So=2C my question is=2C how do I use 'segments' (or otherwise) to
> create a =
> linear trendline which only extends through points 36:45? (and
> overshoots a=
> t either end very slightly=2C if possible=2C rather than running
> along the =
> entire length of the x-axis).

The basic strategy would be to calculate the x and y coordinates of
the two points at the end of the desired segment and then "say"
segments( x[1], y[1], x[2], y[2] )

>
> Many thanks again=2C
>
> Steve
>
> ----------------------------------------
>> CC: r-h...@r-project.org
>> From: dwins...@comcast.net
>> To: smurr...@hotmail.com
>> Subject: Re: [R] Trendline for a subset of data
>> Date: Fri=2C 9 Oct 2009 09:27:43 -0400
>>
>>
>> On Oct 9=2C 2009=2C at 5:50 AM=2C Steve Murray wrote:
>>
>>>
>>> Dear all=2C
>>>
>>> I am using abline(lm ...) to insert a linear trendline through a
>>> portion of my data (e.g. dataset[=2C36:45]). However=2C I am finding
>>> that whilst the trendline is correctly displayed and representative
>>> of the data portion I've chosen=2C the line continues to run beyond
>>> this data segment and continues until it intersects the vertical
>>> axes at each side of the plot.
>>>
>>> How do I display the line so that it only runs between point 36 and
>>> 45 (as shown in the example above) as doesn't continue to display a
>>> line throughout the rest of the plot space?
>>>
>>
>> ?segments


>>


> =0A=
> _________________________________________________________________=0A=
> View your other email accounts from your Hotmail inbox. Add them
> now.=0A=

> http://clk.atdmt.com/UKM/go/167688463/direct/01/=

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________

Steve Murray

unread,
Oct 9, 2009, 1:30:51 PM10/9/09
to mark_d...@yahoo.co.uk, r-h...@r-project.org

<B2A99330-1D1D-49CC...@comcast.net>

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0


Thanks Mark=2C the reg.line trick seemed to work really well.

David - hopefully the hex-text will have gone now - if not=2C please accept=
my apologies as=2C this is=2C as far as I know=2C the first time this has =
happened. If it hasn't gone=2C then I'm afraid I'm a little clueless as to =
how to remove it!

Thanks again both of you=2C

Steve
=0A=
_________________________________________________________________=0A=
Save time by using Hotmail to access your other email accounts.=0A=
http://clk.atdmt.com/UKM/go/167688463/direct/01/=

Greg Snow

unread,
Oct 12, 2009, 2:57:35 PM10/12/09
to Steve Murray, r-h...@r-project.org
Others have show how to use the segments function, but this can also be done using the original abline function along with the clip function. Here is an example:

plot( iris$Petal.Width, iris$Petal.Length,
col=c('red','green','blue')[iris$Species])

tmp <- levels(iris$Species)
tmp2 <- par('usr')

for(i in 1:3) {
fit <- lm( Petal.Length ~ Petal.Width, data=iris, subset= Species==tmp[i] )
tmpx <- with(iris, Petal.Width[ Species==tmp[i] ] )
clip( min(tmpx), max(tmpx), tmp2[3], tmp2[4] )
abline( fit, col=c('red','green','blue')[i] )
}


Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg...@imail.org
801.408.8111

Reply all
Reply to author
Forward
0 new messages