nls.lm:二进制运算符错误的非数字参数,但我所有的参数都是数字

nls.lm : Non-numeric argument to binary operator error but all my args are numeric

我正在尝试 运行 使用 minpack.lm:nls.lm 进行非线性最小二乘回归。要最小化的方程是 e = x - p1*t1 -p2*t2 - svdep ,其中 t1 和 t2 发生变化,以便 e 的平方和最小化。

gnfun <- function(x, p1, p2, t1, t2, svdep){
  x - p1*t1 - p2*t2 - svdep 
}
start <- list(t1=-0.1095389, t2=0.02329868)
gn2 <- nls.lm(par=start, fn=gnfun, x=X1, p1=p1.1, p2=p1.2,      
svdep=Epsvd1)
Error in p1 * t1 : non-numeric argument to binary operator

X1
    X1           X2           X3           X4           X5           X6 
7.725156e-08 7.342344e-08 7.334688e-08 7.572031e-08 7.350000e-08 7.441875e-08 
    X7           X8           X9          X10 
7.388281e-08 7.105000e-08 7.357656e-08 7.028438e-08 

Epsvd1
[1] 3.210028e-05 3.238753e-05 3.160692e-05 3.270296e-05 3.625271e-05 3.167958e-05
[7] 3.667674e-05 3.317648e-05 3.574715e-05 3.335333e-05

p1.1
[1] 0.001156993 0.001159083 0.001158931 0.001162099 0.001160497 0.001158225
[7] 0.001157901 0.001157770 0.001161935 0.001163280

p1.2
[1] 0.005751636 0.005710543 0.005711749 0.005697742 0.005720252 0.005765593
[7] 0.005759443 0.005778480 0.005759381 0.005712900

class(p1.1)
[1] "numeric"

class(p1.2)
[1] "numeric"

> class(X1)
[1] "numeric"

> class(Epsvd1)
[1] "numeric"

我不明白为什么我得到 p1*t1 的错误 'non-numeric argument to binary operator',即使 p1 和 t1 都是数字。

我将不胜感激任何关于为什么我收到此错误消息的建议,以及我需要做什么才能正确地将其发送到 运行。

您需要记住,您正在传递一个列表,并且需要使用 [[:

从该列表中提取参数
 gnfun <- function(x, p1, p2, par=par, svdep){
                x - p1*par[[1]] - p2*par[[2]] - svdep 
          }
 start <- list(t1=-0.1095389, t2=0.02329868)
 gn2 <- nls.lm(par=start, fn=gnfun, x=X1, p1=p1.1, p2=p1.2,      
               svdep=Epsvd1)
 summary(gn2)

Parameters:
    Estimate Std. Error t value Pr(>|t|)
t1  0.003322   0.092779   0.036    0.972
t2 -0.006510   0.018755  -0.347    0.737

Residual standard error: 2.019e-06 on 8 degrees of freedom
Number of iterations to termination: 2 
Reason for termination: Relative error in the sum of squares is at most `ftol'.