[R] piecewise linear regression
vito muggeo
vmuggeo at dssm.unipa.it
Mon Mar 9 17:28:56 CET 2009
Sorry for my delay..
If you do not know the breakpoint, I would suggest to estimate it..
Have a look to the segmented package. The relevant code here is
attach(d)
m0<-lm(percent~ year , weights=1/se)
library(segmented)
mseg<-segmented(m0,seg.Z=~year,psi=1995)
points(year, fitted(mseg))
Hope this helps you,
regards,
vito
David Winsemius ha scritto:
> It actually looked reasonably economical but the output certainly is
> ugly. I see a variety of approaches in the r-help archives. This thread
> discusses two other approaches, degree-one splines from Berry and hard
> coded-coefficients from Lumley:
>
> http://finzi.psych.upenn.edu/R/Rhelp08/archive/118046.html
>
> The Lumley solution has the advantage which he articulates that the
> slopes are more directly interpretabel and in this case you can see that
> yourversion's year slope agrees with Lumley's suggested parametrization:
>
> > m=lm(percent~ year + pmax(year,1996) + pmin(year, 1996), weights=1/se,
> + subset=year>=1988, da=d);
> > m
>
> Call:
> lm(formula = percent ~ year + pmax(year, 1996) + pmin(year, 1996),
> data = d, subset = year >= 1988, weights = 1/se)
>
> Coefficients:
> (Intercept) year pmax(year, 1996) pmin(year, 1996)
> 1161.3126 -0.2177 -0.3494 NA
>
> More compact output to boot.
>
--
====================================
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
http://dssm.unipa.it/vmuggeo
More information about the R-help
mailing list