I have the following model:
which I've coded in R as:
function(t,C,Ao,s,wd,ph) C + Ao * exp(-s*t) * cos(wd*t + ph)
I want to use this equation to form a predictive model.
However, I can't figure out how to either successfully run or plot this equation.
- I tried
nls
but got various errors (including those that warned me about asingular gradient
.
I looked here but I don't see where to go form here given that my model doesn;t seem to work.
How can I model and graph this function in R?
What I tried:
LTI.func <- function(t,C,Ao,s,wd,ph) C + Ao * exp(-s*t) * cos(wd * t + ph)
mod <- nls(Y ~ LTI.func(t = I(scale(t)), C, Ao, s, wd, ph),
data = dat,
start = list(C = 1, Ao = 1, s = 1, w = 1, ph = 1))
I had no idea what starts
to select, so I tried a bunch of random ones, which resulted in errors. Even when I picked starts guided by the y(t) ~ t trend I could see, I always got some kind of error:
Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates Error in nls(Y ~ LTI.func(I(scale(t)), C, Ao, s, wd, ph), data = dat : singular gradient
Update:
Here is an example set of data:
dat <- data.frame(t = c(72, 25, 10, 88, 67, 63, 34, 41, 75, 13, 59, 8, 30, 52, 21),
Y = c(108.7, 157.5, 17.7, 175, 246.8, 233.5, 208.6, 246.5, 126.5,
45.5, 214.1, 4.9, 184, 239.2, 113.3))
This showed up in the close queue but seemed to me to be reasonably well-formed, if poorly checked for consistency of parameter names. Here's my shot at a solution. I've tried first changing the argument to the function to
x
and when that didn't work tried tweaking the starting values. Eventually, I decided to scale the data argument:Whether that would be a useful "solution" will require back-transforming and plotting the results against the original values or perhaps plotting the transformed coordinates against the theoretical. I wasn't surprised that the A0 value was less than zero. It certainly looked from the data that the trend was upward and
exp(-s*x)
would generally be downward ifs*x
were positive.