Hello, I would like to use modFit and modCost from the package FME to find the optimal initial time t0 of a process. For simplicity, the process is either "off" (value 0) or "on" (value h). So I have a data vector with some zeros followed by some h's, e.g.
> c(0,0,0,2,2,2,2,2,2,2) [1] 0 0 0 2 2 2 2 2 2 2 (hence h=2 here). I want to find the best guess for the initial time t0 when the process starts, in the example the process starts at t0 = 3 (in fact, t0 is any number between 3 and 4. All we know is that the process must have started before t=4). And I want to use modFit and modCost for this (in this example it's of course trivial, it suffices to find the first non-zero entry, but it become more challenging when e.g. the data is noisy). However, it seems that modFit can not do this, at least not in a straight forward manner. Here is my minimal example: ########### library(FME) #function makeframe creates a vector with 10 entries, the first t0 entries are 0, the remaining entries are h. Output is a #data.frame with "time" (1,2,...,10) and "M" (0,...,0,h,...,h) makeframe <- function(Par){ with(as.list(c(Par)),{ t <- seq(1,10,by=1) vec <- h*as.numeric(t>t0) return(data.frame(time=t,M=vec)) }) } #Let the "measurement" be (0,0,0,2,2,2,2,2,2,2) fakeobs <- makeframe(c(t0=3,h=2)) #cost function provided by modCost cost <- function(Par){ out <- makeframe(Par) cost <- modCost(model=out,obs=fakeobs) return(cost) } #Fit for best t0 and h value, initial guess t0=5.5, h=1.2 initial <- c(t0=5.5,h=1.2) Fit <- modFit(f=cost,p=initial) coef(Fit) #this should be t0=3 (or and value t0 in the interval [3,4)) and h=2. #h=2 is fitted correctly, but t0 remains at the initial guess. #plot results for illustration plot(fakeobs) lines(makeframe(initial),lty=2) lines(makeframe(coef(Fit))) legend("bottomright",c("data","initial","fitted"),lty=c(NA, 2,1),pch=c(1,NA,NA)) ########### > coef(Fit) t0 h 5.5 2.0 As you can see, the height h is fitted correctly, but the initial time t0 remains at the initial guess. I assume this is because the cost function does not change for any t0 in the interval (5,6), as makeframe(c(t0=5.5,h=...)) is the same as makeframe(c(t0=5,h=...)). Hence the numerical solver is at a local minimum for any value of t0 in (5,6), when h=2. So the x-values can not be fitted because they are discrete; the y-values can be fitted because they are continuous. Is there a common "trick" used in such a situation? How can I find the best t0 (more precisely: a best t0)? Thank you and kind regards! ______________________________________________ R-help@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.