[R] Find position of asymptote

Petr Savicky savicky at cs.cas.cz
Fri Apr 20 09:07:05 CEST 2012


On Thu, Apr 19, 2012 at 03:12:34PM -0800, Katrina Bennett wrote:
> Dear R Help,
> 
> Sorry I wasn't more clear before. Here is another crack at this.
> 
> What I am still trying to do is estimate the point on a line when the
> slope changes or asymptotes. I have found some similar postings
> talking about this but no answers.
> 
> https://stat.ethz.ch/pipermail/r-help/2003-January/028847.html for example
> http://stackoverflow.com/questions/8245792/r-marking-slope-changes-in-loess-curve-using-ggplot2
> (best one I see)
> 
> Based on the above, I've modified my code. I have provided below some
> sample data so you can see the entire chain of work that summarizes
> the issue I am trying to resolve.
[...]
> sslogis <- expression(a/((1+exp((m-x)/s))))
> 
> sslogis.deriv1 <- D(sslogis, "x")
> 
> sslogis.deriv2 <- D(D(sslogis, "x"), "x")
> 
> fn.sslogis <- function (x, a, m, s) { a/((1+exp((m-x)/s))) }
> fn.sslogis.deriv1 <- function (x, a, m, s) { a * (exp((m - x)/s) *
> (1/s))/((1 + exp((m - x)/s)))^2 }
> fn.sslogis.deriv2 <- function (x, a, m, s) {
> -(a * (exp((m - x)/s) * (1/s) * (1/s))/((1 + exp((m - x)/s)))^2 -
>   a * (exp((m - x)/s) * (1/s)) * (2 * (exp((m - x)/s) * (1/s) *
>   (1 + exp((m - x)/s)))))/((((1 + exp((m - x)/s)))^2)^2) }
> 
> asym <- a
> 
> slope <- s
> 
> mid.point <- optimize(f=fn.sslogis.deriv1, interval=c(1:153), a=a,
> m=m, s=s)$minimum #this will find the minimum point halfway through
> melt
> 
> init <- optimize(f=fn.sslogis.deriv2, interval=c(1:mid.point), a=a,
> m=m, s=s)$minimum #this is the start of the snowmelt season
> 
> term <- optimize(f=fn.sslogis.deriv2, interval=c(mid.point:153), a=a,
> m=m, s=s, maximum=TRUE)$maximum #this is the end of the snowmelt
> season
> 
> duration <- init-term
> 
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Ok, this part of the code is where I really want to try and calculate
> the points (init and term) where the curve asymptotes (using the first
> derivative). On the below plot, this should be at ~40 and ~110 (these
> represent days in my analysis).

Hi.

I am not sure, what is the definition of asymptotes in this situation.
The code calculates the points, where the second derivative is minimal
(init) and maximal (term).

The definition of an asymptote at 

  http://en.wikipedia.org/wiki/Asymtote

cannot be applied here, since it refers to the behavior of the function
in the limit, where either the argument or the value of the function
tends to infinity. Can you define the notion of asymptote in your
situation?

> plot.deriv1 <- fn.sslogis.deriv1(x.seq, a, m, s)
> plot(plot.deriv1)
> 
> I found in the help pages above that I could do this based only on the
> loess curve. I can take the diff of the loess prediction to calculate
> a new function that has two local minimums.
> 
> loess.2000 <- loess(dat.cl.2000$x[na.dat.2000]~dat.cl.2000$dat[na.dat.2000])
> loess.predict <- predict(loess.2000, newdata=x.seq)
> loess.predict1 <- diff(loess.predict)
> 
> loess.predict2 <- c(NA,loess.predict1)
> plot(x.seq, loess.predict2, main="diff(loess model)")
> 
> However, this still isn't providing me a means to get the points of
> transition along this line.
> 
> Is there a better way to then pick off the change points or find the
> asymptotes of a function in R?

Can you define the transition or change point in terms of derivatives?

Petr Savicky.



More information about the R-help mailing list