Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初始参数估计的R-奇异梯度矩阵_R_Nls - Fatal编程技术网

初始参数估计的R-奇异梯度矩阵

初始参数估计的R-奇异梯度矩阵,r,nls,R,Nls,我试图将调和方程拟合到我的数据中,但当我应用nls函数时,R给出了以下错误: nlsModel(公式、mf、start、wts)中的错误:初始参数估计时的奇异梯度矩阵 我看到的所有关于这个错误的帖子都是指数函数,其中线性化被用来修正这个错误,但是在这个例子中,我不能用这种方法来解决它。我试着使用其他的起点,但仍然不起作用 代码: y <- c(20.91676, 20.65219, 20.39272, 20.58692, 21.64712, 23.30965, 23.35657, 24.2

我试图将调和方程拟合到我的数据中,但当我应用
nls
函数时,R给出了以下错误:

nlsModel(公式、mf、start、wts)中的错误:初始参数估计时的奇异梯度矩阵

我看到的所有关于这个错误的帖子都是指数函数,其中线性化被用来修正这个错误,但是在这个例子中,我不能用这种方法来解决它。我试着使用其他的起点,但仍然不起作用

代码:

y <- c(20.91676, 20.65219, 20.39272, 20.58692, 21.64712, 23.30965, 23.35657, 24.22724, 24.83439, 24.34865, 23.13173, 21.96117)
t <- c(1, 2, 3, 4 , 5 , 6, 7, 8, 9, 10, 11, 12)


# Fitting function

fit <- function(x, a, b, c) {a+b*sin(2*pi*x)+c*cos(2*pi*x)}

res <- nls(y ~ fit(t, a, b, c), data=data.frame(t,y), start = list(a=1,b=0, c=1))

y有几个问题:

  • cos(2*pi*t)
    是问题中给出的
    t
    的所有1的向量,因此,如果已经存在截距,则无法识别模型

  • 该模型在参数上是线性的,因此可以使用
    lm
    而不是
    nls
    ,并且不需要起始值

  • 即使我们解决了大的第二个系数所看到的那些问题,该模型也不能很好地工作。改进模型

  • 给予:

    Call:
    lm(formula = y ~ sin(2 * pi * t))
    
    Coefficients:
        (Intercept)  sin(2 * pi * t)  
          2.195e+01       -2.262e+14  
    
    Nonlinear regression model
      model: y ~ cbind(1, cos(a * t + b))
       data: parent.frame()
          a       b   .lin1   .lin2 
     0.5226  4.8814 22.4454 -2.1530 
     residual sum-of-squares: 0.7947
    
    Number of iterations to convergence: 9 
    Achieved convergence tolerance: 8.865e-06
    
    相反,请使用plinear算法尝试此模型,该算法不需要线性输入的参数的起始值。这实现了模型
    .lin1+.lin2*cos(a*t+b)
    ,其中.lin1和.lin2参数是线性输入的隐式参数,不需要起始值

    fm <- nls(y ~ cbind(1, cos(a * t + b)), start = list(a = 1, b = 1), alg = "plinear")
    
    plot(y ~ t)
    lines(fitted(fm) ~ t, col = "red")
    
    fm
    

    您能否提供一个产生此错误的示例数据?例如,可能只有前几行数据,或者一些虚假数据。当然!我编辑这篇文章是为了给出一个数字例子。感谢您的帮助和出色的解释!我对所有线性和非线性方程使用了
    nls
    函数,因为到目前为止它对我来说工作正常,但在这种情况下,我将实时向量转换为简单的数字,并没有意识到荒谬的错误。
    Nonlinear regression model
      model: y ~ cbind(1, cos(a * t + b))
       data: parent.frame()
          a       b   .lin1   .lin2 
     0.5226  4.8814 22.4454 -2.1530 
     residual sum-of-squares: 0.7947
    
    Number of iterations to convergence: 9 
    Achieved convergence tolerance: 8.865e-06