[R] piecewise regression

Achim Zeileis Achim.Zeileis at uibk.ac.at
Sat Jan 15 16:39:40 CET 2011


On Sat, 15 Jan 2011, vito.muggeo wrote:

> dear all,
> The package segmented allows to estimate piecewise linear relationships (*connected*
> lines, i.e. a gradual change in the slope) with several breakpoints (known or unknown)
> within (generalized) linear models..
> The package also includes some functions for plotting and testing..
>
> Have a look to
> library(help=segmented)
> ?segmented
>
> and references therein (there is a Rnews paper that could be useful..)
>
> vito
>
> PS If I remember correctly strucchange fits "piecewise constant" models, 
> i.e. sudden changes in the intercept.

In the simplest case, yes.

More generally, the breakpoints() function in "strucchange" estimates 
points at which all coefficients of a linear regression model change. This 
may be just an intercept but can also include more effects. Unlike in 
"segmented" there is no restriction that the resulting regression curves 
are continuous.
Z

>
> ====================================
> Vito M.R. Muggeo
> Dip.to Sc Statist e Matem `Vianelli'
> Università di Palermo
> viale delle Scienze, edificio 13
> 90128 Palermo - ITALY
> tel: 091 23895240
> fax: 091 485726/485612
> http://dssm.unipa.it/vmuggeo
> ====================================
>
> On Fri, 14 Jan 2011 14:14:26 -0500, John Sorkin wrote
>> If you know  the knot and want linear segments, lm (or any other "normal"
>> regression software) can perform the analysis. For example if you want to
>> regress y  on x and have a knot a 20 the following code will work:
>>
>> x <- runif(500,0,40)
>> plot(x)
>>
>> for (i in 1:500) {
>> if (x[i] < 20)  y[i] <- (-0.5*x[i])+rnorm(1)
>> if (x[i] >= 20) y[i] <- ( 0.5*x[i])+rnorm(1) -20
>> }
>>
>> plot(x,y)
>>
>> knot <- 20
>> aboveknot <- pmax(x-knot,rep(0,500))# will be 0 for x < 20, x for x >20
>> fit1 <- lm(y~x+aboveknot)
>> summary(fit1)
>>
>> The analysis will give you three parameters
>> (1) the intercept
>> (2) the slope of below 20
>> (3) the difference between the slope below 20 and above 20
>>
>> John
>>
>> John Sorkin M.D., Ph.D.
>> Chief, Biostatistics and Informatics
>> Baltimore VA Medical Center GRECC,
>> University of Maryland School of Medicine Claude D. Pepper OAIC,
>> University of Maryland Clinical Nutrition Research Unit, and
>> Baltimore VA Center Stroke of Excellence
>>
>> University of Maryland School of Medicine
>> Division of Gerontology
>> Baltimore VA Medical Center
>> 10 North Greene Street
>> GRECC (BT/18/GR)
>> Baltimore, MD 21201-1524
>>
>> (Phone) 410-605-7119
>> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>> jsorkin at grecc.umaryland.edu
>>
>>>>> Gene Leynes <gleynes+r at gmail.com> 1/14/2011 11:50 AM >>>
>> oh yes, and the structchange package.
>>
>> After a day of experimentation I couldn't figure out how to get the
>> structchange package to work for my problems.  Although it is probably user
>> error on my part, the package seems to be specific to time series problems.
>> Also, I think it needed regularly spaced x's... but maybe that would be a
>> good thing to look at as part of a two step approach (smoothing first to get
>> regularly spaced data).
>>
>> PS:  Actually, I just noticed that there are some other help topics that I
>> had not read yet which went up last November.
>> http://r-project.markmail.org/thread/npjhlwwt6oh3sby3
>>
>> On Fri, Jan 14, 2011 at 10:25 AM, Spencer Graves <
>> spencer.graves at structuremonitoring.com> wrote:
>>
>>> Hello:
>>>
>>>
>>>      I just found 58 help pages in 32 contributed packages containing the
>>> terms "piecewise" and "regression", as follows:
>>>
>>>
>>> library(sos)
>>> pr <- ???'piecewise regression' # for "piecewise" with "regression"
>>> summary(pr) # 58 matches in 32 packages
>>> pr # view the 58 links sorted by package in a web browser
>>>
>>> p.r <- ???"{piecewise regression}" # for "piecewise" followed immediate by
>>> "regression"
>>> summary(p.r) # 15 matches in 13 packages
>>> p.4 # to view in a web browser
>>>
>>>
>>>      Beyond this, the "structchange" package supports looking for changes
>>> that would be, e.g., linear in one segment then jump to a different linear
>>> regime.  If you want the lines to connect, then you may want splines.
>>>  Probably the most common spline is cubic, but you can also get linear
>>> splines.  One of the most general spline packages is "fda" (functional data
>>> analysis), with multiple companion books by James Ramsay and others.
>>>  However, there are other spline packages.
>>>
>>>
>>>      If this does not answer your question, "PLEASE do read the posting
>>> guide http://www.R-project.org/posting-guide.html and provide commented,
>>> minimal, self-contained, reproducible code", as it says in the signature
>>> block for each email on this list.
>>>
>>>
>>>      Hope this helps.
>>>      Spencer Graves
>>>
>>>
>>>
>>> On 1/14/2011 6:42 AM, Federico Bonofiglio wrote:
>>>
>>>> Hello everybody!!!!
>>>>
>>>> Quick question, if you'd like to throw a little tip:
>>>>  does anyone knows a function that runs piecewise regression models with
>>>> coefficients estimation and inferences ?
>>>>
>>>> Thank you
>>>>
>>>>        [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> R-help at 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.
>>>>
>>>>
>>> ______________________________________________
>>> R-help at 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.
>>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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.
>>
>> Confidentiality Statement:
>> This email message, including any attachments, is for ...{{dropped:12}}
>
> ______________________________________________
> R-help at 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.
>


More information about the R-help mailing list