Model complex equation in R

二次信任 提交于 2020-01-04 14:09:17

问题


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 a singular 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))

回答1:


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:

LTI.func <- function(x,C,Ao,s,wd,ph) {C + Ao * exp(-s*x) * cos(wd * x + ph)}
 mod <- nls(Y ~ LTI.func(x=t , C, Ao, s, wd, ph), data=data.frame(scale(dat)), 
                     start=list(C = 0,Ao = -1,s = 1,wd = 1,ph = 0))
 mod
#-------------
Nonlinear regression model
  model: Y ~ LTI.func(x = t, C, Ao, s, wd, ph)
   data: data.frame(scale(dat))
        C        Ao         s        wd        ph 
 0.288729 -0.986426  0.517128  2.002040  2.756004 
 residual sum-of-squares: 1.53608

Number of iterations to convergence: 18 
Achieved convergence tolerance: 0.0000038776

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 if s*x were positive.



来源:https://stackoverflow.com/questions/50182884/model-complex-equation-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!