在R中为复数方程建模 [英] Model complex equation in R
问题描述
我有以下模型:
我在R中编码为:
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.
- 我尝试了
nls
,但是遇到了各种错误(包括那些警告我singular gradient
的错误.
- I tried
nls
but got various errors (including those that warned me about asingular gradient
.
我在在这里看了一下,但是鉴于我的模型没有,我看不到这里的去处; t似乎有效.
I looked here but I don't see where to go form here given that my model doesn;t seem to work.
如何在R中为该函数建模并绘制图形?
我尝试过的事情:
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))
我不知道要选择什么starts
,所以我尝试了一堆随机的选择,这导致了错误.甚至当我看到以y(t)〜t趋势为起点的开始时,我总是会遇到某种错误:
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
更新:
这是一组示例数据:
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))
推荐答案
这出现在关闭队列中,但在我看来,如果对参数名称的一致性检查不善,它的格式合理.这是我的解决方案.我尝试过先将函数的参数更改为x
,然后在不起作用的情况下尝试调整起始值.最终,我决定扩展数据参数:
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
这是否是一个有用的解决方案",将需要进行反变换并将结果相对于原始值绘制,或者可能将变换后的坐标相对于理论绘制.我对A0值小于零并不感到惊讶.从数据可以肯定的是,趋势是上升的,如果s*x
为正,则exp(-s*x)
通常会下降.
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.
这篇关于在R中为复数方程建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!