Hi, I need to fit a piecewise linear regression. x = c(6.25,6.25,12.50,12.50,18.75,25.00,25.00,25.00,31.25,31.25,37.50,37.50,50.00,50.00,62.50,62.50,75.00,75.00,75.00,100.00,100.00) y = c(0.328,0.395,0.321,0.239,0.282,0.230,0.273,0.347,0.211,0.210,0.259,0.186,0.301,0.270,0.252,0.247,0.277,0.229,0.225,0.168,0.202) there are two change points. so the fitted curve should look like \ \ /\ \/ \ \ \ How do I do this in R ? Thank you, Abhyuday
Abhyuday, There are a number of answers in the archives: http://www.google.com/u/newcastlemaths?q=piecewise+linear+regression&sa=Google+Search Do any of those meet your needs? Sean ----- Original Message ----- From: "Abhyuday Mandal" <amandal at isye.gatech.edu> To: <r-help at stat.math.ethz.ch> Sent: Monday, May 30, 2005 7:38 PM Subject: [R] Piecewise Linear Regression> Hi, > > I need to fit a piecewise linear regression. > > x = > c(6.25,6.25,12.50,12.50,18.75,25.00,25.00,25.00,31.25,31.25,37.50,37.50,50.00,50.00,62.50,62.50,75.00,75.00,75.00,100.00,100.00) > y = > c(0.328,0.395,0.321,0.239,0.282,0.230,0.273,0.347,0.211,0.210,0.259,0.186,0.301,0.270,0.252,0.247,0.277,0.229,0.225,0.168,0.202) > > there are two change points. so the fitted curve should look like > > > > \ > \ /\ > \/ \ > \ > \ > > How do I do this in R ? > > Thank you, > Abhyuday > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
It is conventional to fit piecewise linear models by assuming Gaussian error and using least squares methods, but one can argue that median regression provides a more robust approach to this problem. You might consider the following fit: x = c (6.25,6.25,12.50,12.50,18.75,25.00,25.00,25.00,31.25,31.25,37.50,37.50,5 0.00,50.00,62.50,62.50,75.00,75.00,75.00,100.00,100.00) y = c (0.328,0.395,0.321,0.239,0.282,0.230,0.273,0.347,0.211,0.210,0.259,0.186 ,0.301,0.270,0.252,0.247,0.277,0.229,0.225,0.168,0.202) library(quantreg) plot(x,y) fit <- rqss(y ~ qss(x)) plot(fit) it gives 5 segments not 3, but this can be controlled by the choice of lambda in the qss function, for example, try: fit <- rqss(y ~ qss(x,lambda=3) plot(fit,col="red") which gives a fit like you suggest might be reasonable with only three segments. url: www.econ.uiuc.edu/~roger Roger Koenker email rkoenker at uiuc.edu Department of Economics vox: 217-333-4558 University of Illinois fax: 217-244-6678 Champaign, IL 61820 On May 30, 2005, at 6:38 PM, Abhyuday Mandal wrote:> Hi, > > I need to fit a piecewise linear regression. > > x = c > (6.25,6.25,12.50,12.50,18.75,25.00,25.00,25.00,31.25,31.25,37.50,37.50 > ,50.00,50.00,62.50,62.50,75.00,75.00,75.00,100.00,100.00) > y = c > (0.328,0.395,0.321,0.239,0.282,0.230,0.273,0.347,0.211,0.210,0.259,0.1 > 86,0.301,0.270,0.252,0.247,0.277,0.229,0.225,0.168,0.202) > > there are two change points. so the fitted curve should look like > > > > \ > \ /\ > \/ \ > \ > \ > > How do I do this in R ? > > Thank you, > Abhyuday > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html >
On Mon, 30 May 2005 19:38:58 -0400 (EDT) Abhyuday Mandal wrote:> Hi, > > I need to fit a piecewise linear regression. > > x > c(6.25,6.25,12.50,12.50,18.75,25.00,25.00,25.00,31.25,31.25,37.50,37. > 50,50.00,50.00,62.50,62.50,75.00,75.00,75.00,100.00,100.00) y > c(0.328,0.395,0.321,0.239,0.282,0.230,0.273,0.347,0.211,0.210,0.259,0 > .186,0.301,0.270,0.252,0.247,0.277,0.229,0.225,0.168,0.202) > > there are two change points. so the fitted curve should look like > > > > \ > \ /\ > \/ \ > \ > \ > > How do I do this in R ?There are various ways, as the previous replies already indicated. One approach would be to consider this in a structural change framework, as implemented in strucchange or segmented. With strucchange you can do ## collect data dat <- data.frame(y, x) ## add small noise set.seed(123) dat$x <- dat$x + rnorm(21, sd = 0.001) ## fit breakpoint model bp <- breakpoints(y ~ x, data = dat) ## visualize plot(x, y) lines(x, fitted(bp)) The problem with breakpoints() is that it also fits models on very small subsets, which can lead to singular regressor matrices if there are bindings among the regressors. Hence, I did an ugly hack and added a small error to avoid singularities. segmented can also fit segmented regressions, but always fits broken line segments (i.e. continuous curves) whereas strucchange fits fully segmented models. The latter can also be easily fitted via lm(y ~ fac/x) if fac is a factor coding the different segments. See also ?breakfactor in strucchange. So you can use any segmentation algorithm and then plug the result easily into lm(). Z> Thank you, > Abhyuday > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >