2014-02-06 140 views
-2

A R代碼,我試圖符合以下模型:錯誤執行非線性迴歸

> 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 

但你可以看到我關於奇異漸變矩陣的一個錯誤。我能做些什麼來避免這個錯誤?

+3

可否請你說清楚你的問題是什麼? – jbaums

+0

您正在處理迴歸中的奇異矩陣。看看這篇文章:http://stats.stackexchange.com/questions/70899/what-c​​orrelation-makes-a-matrix-singular-and-what-are-implications-of-singularit – Zbynek

+1

請。這不是一個問題,而是一段代碼。 – jbaums

回答

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") 

+0

不客氣。由於你是新手,請參閱:[我應該怎麼做,當有人回答我的問題?](http://stackoverflow.com/help/someone-answers) – jlhoward

+0

我明白了.. @ jlhoward – Chammu

+0

我想做框架在C#中工作,是否有可能在後端使用r .. – Chammu