[R] problem in plot after calculating the derivative

David Winsemius dwinsemius at comcast.net
Tue Mar 8 13:48:36 CET 2011


On Mar 8, 2011, at 12:39 AM, Hong Qin wrote:

> Here is a test code to calculate the derivative of a curve in sliding
> windows. I generated a linear example, and it should plot the  
> derivative in
> a straight line. But, it does not?!

Try adding ylim=c(0.1). You will get your straight line. I consider  
the current behavior (of not labeling the y axis and producing a  
jagged line when given a series of values whose successive differences  
are below machine tolerances to be a bug. the output from print would  
lead you do think they were equal, but if you use diff(slope you find  
that they are slightly variable.)

>
> --------------
> x = seq(0,10,by=0.1)
> y = x * 0.5 + 0.1
>
> window=15;
> half = floor( window/2)
>
> x2 = half: (length(x)-half) ;
> slope = half: (length(x)-half) ;
>
> count = 1;
> for( i in half: (length(x)-half) ) {
>   pos = (i-half):(i+half)
>   m = lm( y[pos] ~ x[pos])
>   x2[count] = x[i]
>   slope[count] = m$coeff[2];
>   count = count + 1;
> }
>
> plot( slope ~ x2, type='l' ) #does not give a straight line, why?
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list