R中的自动曲线拟合 [英] Automatic curve fitting in R

查看:125
本文介绍了R中的自动曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用许多简单模型自动拟合曲线的软件包?

我是说简单模型:

Is there any package that automatically fits a curve using many simple models?
By simple models I mean:


  • ax + b

  • ax ^ 2 + bx + c

  • a * log(x)+ b

  • a * x ^ n + b

  • ax /(1 + bx)

  • ax ^ n /(1 + bx ^ n)

  • ...

  • ax+b
  • ax^2+bx+c
  • a*log(x) + b
  • a*x^n+b
  • ax/(1+bx)
  • ax^n/(1+bx^n)
  • ...

最好是具有两个矢量参数的函数X和Y,并返回带有其SSE的拟合简单模型的列表。

The best would be to have a function that takes two vector parameters X and Y and returns a list of fitted simple models with their SSE.

推荐答案

尝试一下。 rhs 是右侧的字符矢量, x y 是数据。它为每个参数构造公式 fo ,然后提取参数并将每个参数的起始值设置为1。最后,它运行 nls 并返回已排序的SSE,以便结果是通过右侧命名的SSE的向量。如果 verbose = TRUE (默认情况下),则还会显示每个拟合的输出。

Try this. rhs is a character vector of right sides and x and y are the data. It constructs the formula fo for each and then extracts the parameters and sets each to 1 for the starting value. Finally it runs nls and returns the SSEs sorted so that the result is a vector of SSE's named via the right hand sides. If verbose=TRUE (which it is by default) then it also displays the output from each fit.

sse <- function(rhs, x, y) sort(sapply(rhs, function(rhs, x, y, verbose = TRUE) {
    fo <- as.formula(paste("y", rhs, sep = "~"))
    nms <- setdiff(all.vars(fo), c("x", "y"))
    start <- as.list(setNames(rep(1, length(nms)), nms))
    fm <- nls(fo, data.frame(x, y), start = start)
    if (verbose) { print(fm); cat("---\n") }
    deviance(fm)
}, x = x, y = y))

## test

set.seed(123)
x <- 1:10
y <- rnorm(10, x)

# modify to suit
rhs <- c("a*x+b", "a*x*x+b*x+c")

sse(rhs, x, y)

这篇关于R中的自动曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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