寻找曲线以匹配数据 [英] Finding a curve to match data

查看:98
本文介绍了寻找曲线以匹配数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个非线性曲线拟合例程(可能最有可能在R或Python中找到,但我对其他语言持开放态度),该例程将获取x,y数据并为其拟合曲线.

I'm looking for a non-linear curve fitting routine (probably most likely to be found in R or Python, but I'm open to other languages) which would take x,y data and fit a curve to it.

我应该能够将要适合的表达式类型指定为字符串.

I should be able to specify as a string the type of expression I want to fit.

示例:

"A+B*x+C*x*x"
"(A+B*x+C*x*x)/(D*x+E*x*x)"
"sin(A+B*x)*exp(C+D*x)+E+F*x"

我从中得到的至少是常量(A,B,C等)的值,并希望统计出比赛的适用性.

What I would get out of this is at least the values for the constants (A, B, C, etc.) And hopefully stats about the fitness of the match.

有一些商业程序可以做到这一点,但是我希望现在能够在语言库中找到与所需表达相适应的通用内容.我怀疑SciPy的优化工具可以做到这一点,但是我看不到它可以让我定义一个方程式.同样,我似乎无法在R中找到我想要的东西.

There are commercial programs to do this, but I expected to be able to find something as common as fitting to a desired expression in a language library nowadays. I suspect SciPy's optimization stuff might be able to do this, but I can't see that it lets me define an equation. Likewise, I can't seem to find exactly what I want in R.

是我在外面寻找的东西,还是我需要自己动手?如果有它,我不愿意这样做,但是我很难找到它.

Is what I'm looking for out there, or do I need to roll my own? I hate to do it if it's there and I'm just having trouble finding it.

我想比从LAB Fit获得更多的控制过程. LAB Fit用户界面令人恐惧.我还希望能够将范围分成多个部分,并用不同的曲线代表范围的不同部分.最后,结果必须能够(在速度上)通过线性插值击败LUT,否则我不感兴趣.

I want to do this for a bit more control over the process than I get from LAB Fit. The LAB Fit UI is dreadful. I'd also like to be able to break the range into multiple pieces and have different curves represent the different pieces of the range. In the end, the result has to be able to (speed-wise) beat a LUT with linear interpolation or I'm not interested.

在我当前遇到的问题中,我有trig函数或exp(),我需要每秒每秒实时执行352800次(并且仅使用CPU的一小部分).因此,我绘制了曲线并使用数据来驱动曲线拟合器,从而获得了较便宜的近似值.在过去,LUT几乎始终是解决方案,但是如今,跳过内存查找并进行近似有时会更快.

In my current set of problems, I have trig functions or exp() and I need to execute them 352,800 times per second in real time (and use only a fraction of the CPU). So I plot the curve and use the data to drive the curve fitter to get less expensive approximations. In the old days, LUTs were almost always the solution, but nowadays skipping the memory lookups and doing an approximation is sometimes faster.

推荐答案

要在一般意义上回答您的问题(关于R中的参数估计),而无需考虑您指出的方程的细节,我想您正在寻找nls ()或optim()...'nls'是我的首选,因为它为每个估计的参数提供错误估计,当失败时,我使用'optim'.如果您有x,y变量:

To answer your question in a general sense (regarding parameter estimation in R) without considering the specifics of the equations you pointed out, I think you are looking for nls() or optim()... 'nls' is my first choice as it provides error estimates for each estimated parameter and when it fails I use 'optim'. If you have your x,y variables:

out <- tryCatch(nls( y ~ A+B*x+C*x*x, data = data.frame(x,y), 
                start = c(A=0,B=1,C=1) ) ,
                error=function(e) 
                optim( c(A=0,B=1,C=1), function(p,x,y)  
                      sum((y-with(as.list(p),A + B*x + C*x^2))^2), x=x, y=y) )

获得系数,类似

getcoef <- function(x) if(class(x)=="nls") coef(x) else x$par
getcoef(out)

如果要使用'nls'的标准错误,

If you want the standard errors in the case of 'nls',

summary(out)$parameters

帮助文件和r-help邮件列表帖子包含许多讨论,这些讨论涉及每个实现的特定最小化算法(以上每个示例案例中使用的默认算法)以及它们对于手头特定形式方程式的适用性.某些算法可以处理框约束,而另一个名为constrOptim()的函数将处理一组线性约束.该网站还可以帮助:

The help files and r-help mailing list posts contain many discussions regarding specific minimization algorithms implemented by each (the default used in each example case above) and their appropriateness for the specific form of the equation at hand. Certain algorithms can handle box constraints, and another function called constrOptim() will handle a set of linear constraints. This website may also help:

http://cran.r-project.org/web/views/Optimization.html

这篇关于寻找曲线以匹配数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆