s-news
[Top] [All Lists]

[S] RE: segmented regression

To: "'S-plus post'" <s-news@wubios.wustl.edu>
Subject: [S] RE: segmented regression
From: "Park Kyong H Mr. SBCCOM" <kyong.park@sbccom.apgea.army.mil>
Date: Fri, 29 Oct 1999 14:10:17 -0400
Sender: owner-s-news@wubios.wustl.edu
Nick Ellis sent me a nice function for segmented regression. In case I
provided the function below. Thanks again Nick!

Kyong Park     U.S. Army

x <- seq(-1, 1, len = 21)
y <- ifelse(x < 0, 0 * x + 0, 1 * x + 0) + 0.1 * rnorm(21)
dat <- data.frame(x = x, y = y)
parameters(dat) <- list(a = 0, b = 0, cc = 0, d = 1)
broken.line <- function(x, a, b, cc, d)
{
        ifelse(x < (a - cc)/(d - b), a + b * x, cc + d * x)
}
nls.dat <- nls(y ~ broken.line(x, a, b, cc, d), dat)
line.break <- function(a, b, cc, d)
{
        (a - cc)/(d - b)
}
k <- do.call("line.break", structure(as.list(co <- coef(nls.dat)), names =
names(co)))
plot(data.matrix(dat))
lines(kx <- sort(c(k, dat$x)), do.call("broken.line", c(list(x = kx),
structure(as.list(co <- coef(nls.dat)), names = names(co)))))

Happy exploring.

Nick


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>