# [R] slope estimations of teeth like data

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Tue Jun 15 15:24:43 CEST 2004

```On Tue, 15 Jun 2004 13:11:45 +0200 Petr Pikal wrote:

> Dear all
>
> Suppose I have teeth like data similar like
>
> x <- 1:200
> y <- 0.03*x[1:100]+rnorm(100, mean=.001, sd=.03)
> z <- 3-rep(seq(1,100,10),each=10)*.03+rnorm(100,mean=.001, sd=.03)
> plot(x,c(y,z))
>
> and I want to have a gradient estimations for some values from
> increasing part of data
>
> like
>
> y.agg <- aggregate(diff(c(y,z)),
> list(rep(seq(1,200,10),each=10)[1:199]), mean)
>
> y.agg[1:10,]  ##is OK, I want that
> y.agg[11:20,] ##is not OK, I do not want that
>
> actual data are similar but more irregular and have subsequent gradual
> increases and decreases, more like
>
> set.seed(1)
> yy<-NULL
> for( i in 1:10) yy <- c(yy,c(y,z)[1:floor(runif(1)*200)])
> length(yy)
>  1098
>
> plot(1:1098,yy)

Just like Vito, I am also not really sure what you are looking for. But
some breakpoint-estimating procedure might be of help. Vito already
mentioned segmented(), furthermore there is breakpoints() in
strucchange.

But to estimate all breakpoints in yy (as defined above) is challenging.
breakpoints() requires some time for computations, e.g.

R> library(strucchange)
R> xx <- 1:1098
R> bp <- breakpoints(yy ~ xx, h = 0.05) ## this will take some time
R> plot(xx, yy)
R> lines(xx, fitted(bp), col = 4)

The algorithm in segmented is (often considerably) quicker but requires
start values for the breakpoints.

R> library(segmented)
R> sg <- segmented(lm(yy ~ xx), xx, seq(200, 900, length = 12),
tol = 1e-3) ## decrease tolerance
R> plot(xx, yy)
R> lines(xx, fitted(sg), col = 4)

If this is what you are looking for you can also contact me and Vito, I
guess, to see how the function calls can be improved to suit your needs.

hth,
Z

> Is there anybody who has some experience with such data, mainly how to
> extract only increasing portions or to filter values of "yy" such as
> only aggregated slopes from increasing parts are computed and other
> parts are set to NA. Sometimes actual data have so long parts of
> steady or even slightly increasing values at decreasing part that
> aggregated values are slightly positive although they are actually
> from decreasing portion of data.
>
> Thank you
> Petr Pikal
> petr.pikal at precheza.cz
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help