【问题标题】:Model fitting with nls.lm in R, "Error: unused argument"在 R 中使用 nls.lm 进行模型拟合,“错误:未使用的参数”
【发布时间】:2015-07-11 04:30:15
【问题描述】:

我正在尝试使用 minpack.lm 中的 nls.lm 函数将非线性模型拟合到来自心理物理学实验的某些数据。

我四处搜索,找不到很多关于包的信息,所以基本上复制了 nls.lm 帮助页面上给出的示例格式。不幸的是,我的脚本仍然无法运行,并且 R 抛出了这个错误:

Error in fn(par, ...) : 
unused argument (observed = c(0.1429, 0.2857, 0.375, 0.3846, 0.4667, 0.6154))

脚本似乎认为我要拟合模型的数据无关紧要,这绝对是错误的。

我希望它能够拟合模型并为备用参数 (w) 生成 0.5403 的值。

非常感谢任何帮助。 我正在从 Matlab 转移到 R,如果我的代码看起来很草率,我深表歉意。

这是脚本。

  install.packages("pracma")
  require(pracma)
  install.packages("minpack.lm")
  require(minpack.lm)

  # Residual function, uses parameter w (e.g. .23) to predict accuracy error at a given ratio [e.g. 2:1]
  residFun=function(w,n) .5 * erfc( abs(n[,1]-n[,2])/ ((sqrt(2)*w) * sqrt( (n[,1]^2) + (n[,2]^2) ) ) )

  # example for residFun
  # calculates an error rate of 2.59%
  a=matrix(c(2,1),1,byrow=TRUE)
  residFun(.23,a)

  # Initial guess for parameter to be fitted (w)
  parStart=list(w=0.2)

  # Recorded accuracies in matrix, 1- gives errors to input into residFun
  # i.e. the y-values I want to fit the model
  Acc=1-(matrix(c(0.8571,0.7143,0.6250,0.6154,0.5333,0.3846),ncol=6))

  # Ratios (converted to proportions) used in testing
  # i.e. the points along the x-axis to fit the above data to
  Ratios=matrix(c(0.3,0.7,0.4,0.6,0.42,0.58,0.45,0.55,0.47,0.53,0.49,0.51),nrow=6,byrow=TRUE)

  # non-linear model fitting, attempting to calculate the value of w using the Levenberg-Marquardt nonlinear least-squares algorithm 
  output=nls.lm(par=parStart,fn=residFun,observed=Acc,n=Ratios)

  # Error message shown after running
  # Error in fn(par, ...) : 
  #   unused argument (observed = c(0.1429, 0.2857, 0.375, 0.3846, 0.4667, 0.6154))

【问题讨论】:

    标签: r nls model-fitting


    【解决方案1】:

    错误意味着您向函数传递了一个它不期望的参数。 ?nls.lm 没有参数observed,因此它被传递给传递给fn 的函数,在您的情况下为residFun。但是,residFun 也不期望这个参数,因此会出现错误。你需要像这样重新定义这个函数:

    # Residual function, uses parameter w (e.g. .23) to predict accuracy error at a given ratio [e.g. 2:1]
    residFun=function(par,observed, n) {
      w <- par$w
      r <- observed - (.5 * erfc( abs(n[,1]-n[,2])/ ((sqrt(2)*w) * sqrt( (n[,1]^2) + (n[,2]^2) ) ) ))
      return(r)
    }
    

    它给出以下结果:

    > output = nls.lm(par=parStart,fn=residFun,observed=Acc,n=Ratios)
    > output
    Nonlinear regression via the Levenberg-Marquardt algorithm
    parameter estimates: 0.540285874836135 
    residual sum-of-squares: 0.02166
    reason terminated: Relative error in the sum of squares is at most `ftol'.
    

    为什么会这样:

    你似乎受到了他documentation中的这个例子的启发:

    ## residual function
    residFun <- function(p, observed, xx) observed - getPred(p,xx)
    ## starting values for parameters
    parStart <- list(a=3,b=-.001, c=1)
    ## perform fit
    nls.out <- nls.lm(par=parStart, fn = residFun, observed = simDNoisy,
    xx = x, control = nls.lm.control(nprint=1))
    

    注意observedresidFun 的参数。

    【讨论】:

    • 啊!谢谢!我知道这将是显而易见的。所以这应该有效(至少在下一个错误之前)? residFun=function(w,observed, n) observed= .5 * erfc( abs(n[,1]-n[,2])/ ((sqrt(2)*w) * sqrt( (n[,1]^2) + (n[,2]^2) ) ) )
    • 不,它不会起作用,因为observed 不是你的函数的参数,而是它的输出。我用一个有效的功能更新了我的答案
    • 谢谢,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2017-11-27
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多