[R] Segmented regression
Power, Brendan D (Toowoomba)
Brendan.Power at dpi.qld.gov.au
Fri Dec 7 03:35:12 CET 2007
Thanks for your reply and apologies for not being clearer.
I'd like to fit a three-segmented relationship to each level but have only 3 unique breakpoints. The result would be 9 slopes, one of which would be zero.
I achieved this by finding the 3 breakpoint with:
init.bp <- c(297.4,639.6,680.6)
lm.1 <- lm(y~tt+group,data=df)
seg.1 <- segmented(lm.1, seg.Z=~tt, psi=list(tt=init.bp))
The starting values of init.bp came from a grid search and are that which minimised residuals.
I then used these breakpoints to get the 9 coefficients (which I omitted from original email for brevity):
df.KW <- df[df$group=="KW",]
lm.KW <- lm(y~tt,data=df.KW)
seg.KW <- segmented(lm.KW, seg.Z=~tt, psi=list(tt=seg.1$psi[,"Est."]),control = list(it.max = 0))
And similarly for the other 2 levels. This was done just for plotting purposes, my main interest is in the identification of the breakpoints.
Btw I'd appreciate a copy of your rnews article.
Thanks for you help,
From: vito muggeo [mailto:vmuggeo at dssm.unipa.it]
Sent: Thursday, 6 December 2007 7:55 PM
To: Power, Brendan D (Toowoomba)
Cc: r-help at r-project.org
Subject: Re: [R] Segmented regression
I am not sure to understand your code..
It seems to me that your are interested in fitting a one-breakpoint segmented relationship in each level of your grouping variable
If this is the case, the correct code is below.
In order to fit a segmented relationship in each group you have to define the relevant variable before fitting, and to constrain the last slope to be zero you have to consider the `minus' variable..(I discuss these points in the submitted Rnews article..If you are interested, let me know off list).
If my code does not fix your problem, please let me know,
##--define the group-specific segmented variable:
##-fit the unconstrained model
#have a look to results:
#constrain the last slope in group KW
Power, Brendan D (Toowoomba) ha scritto:
> Hello all,
> I have 3 time series (tt) that I've fitted segmented regression models
> to, with 3 breakpoints that are common to all, using code below
> (requires segmented package). However I wish to specifiy a zero
> coefficient, a priori, for the last segment of the KW series (green)
> only. Is this possible to do with segmented? If not, could someone point
> in a direction?
> The final goal is to compare breakpoint sets for differences from those
> derived from other data.
> Thanks in advance,
> init.bp <- c(297.4,639.6,680.6)
> lm.1 <- lm(y~tt+group,data=df)
> seg.1 <- segmented(lm.1, seg.Z=~tt, psi=list(tt=init.bp))
> platform i386-pc-mingw32
> arch i386
> os mingw32
> system i386, mingw32
> major 2
> minor 6.0
> year 2007
> month 10
> day 03
> svn rev 43063
> language R
> version.string R version 2.6.0 (2007-10-03)
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Vito M.R. Muggeo
Dip.to Sc Statist e Matem `Vianelli'
Università di Palermo
viale delle Scienze, edificio 13
90128 Palermo - ITALY
tel: 091 6626240
fax: 091 485726/485612
The information contained in the above e-mail message or messages
(which includes any attachments) is confidential and may be legally
privileged. It is intended only for the use of the person or entity
to which it is addressed. If you are not the addressee any form of
disclosure, copying, modification, distribution or any action taken
or omitted in reliance on the information is unauthorised. Opinions
contained in the message(s) do not necessarily reflect the opinions
of the Queensland Government and its authorities. If you received
this communication in error, please notify the sender immediately and
delete it from your computer system network.
More information about the R-help