Skip to content
Prev 247356 / 398503 Next

piecewise regression

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
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:

            
______________________________________________
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 th...{{dropped:6}}