用于实现非线性回归的r代码中的错误

时间:2014-02-06 06:56:22

标签: r regression

我试图使用以下模型:

> x<-c(8100,12900,13800,14250,14700,20700,23100,25200,27300,28560,29760,30060,39060,39660,42060,
42660,57720,57840,58200,59400,59700,60900,62100,65400,85200,88200,88800,98400,106800,114900)
> y<-c(1:30)
> df<-data.frame(x,y)
> fit <- nls(y ~ a*(1-exp(-x/b))^c, data=df, start=c(a=1,b=1,c=1),algorithm="plinear")
Error in qr.solve(QR.B, cc) : singular matrix 'a' in solve

> fit <- nls(y ~ a*(1-exp(-x/b))^c, data=df, start=c(a=1,b=1,c=1),algorithm="port")
Error in nlsModel(formula, mf, start, wts, upper) : 
  singular gradient matrix at initial parameter estimates

但是你可以看到我得到一个关于奇异梯度矩阵的错误。我该怎么做才能避免这个错误?

1 个答案:

答案 0 :(得分:2)

问题在于您对b的估计偏离了。使用x ~O(10000),使用b=1e4作为起点。

x<-c(8100,12900,13800,14250,14700,20700,23100,25200,27300,28560,29760,30060,39060,39660,4206042660,57720,57840,58200,59400,59700,60900,62100,65400,85200,88200,88800,98400,106800,114900)
y<-c(1:30)
df<-data.frame(x,y)

fit <- nls(y ~ a*(1-exp(-x/b))^c, data=df, start=c(a=1,b=1e4,c=1),algorithm="port")
summary(fit)
# Formula: y ~ a * (1 - exp(-x/b))^c
#
# Parameters:
#    Estimate Std. Error t value Pr(>|t|)    
# a 3.357e+01  1.850e+00  18.146  < 2e-16 ***
# b 4.295e+04  6.139e+03   6.995 1.61e-07 ***
# c 1.725e+00  1.887e-01   9.145 9.33e-10 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 1.119 on 27 degrees of freedom
#
# Algorithm "port", convergence message: relative convergence (4)


plot(x,y, col="red")
lines(x,predict(fit), col="blue")