2013-12-16 58 views
0
input <- "  
    t y  
1 5.3  
2 7.2  
3 9.6  
4 12.9  
5 17.1  
6 23.2"   
dat<-read.table(textConnection(input),header=TRUE,sep="")  
t<-dat[,1]  
y<-dat[,2] 

y=3.975*(1.341^t)是合適的結果,我該如何使用nls函數來得到它?
也許問題是如何表達公式?如何擬合指數曲線如a * b^t in r?

nls(y~(a*b^t)) 
Error in getInitial.default(func, data, mCall = as.list(match.call(func, : 
no 'getInitial' method found for "function" objects 

回答

2

嘗試

nls(y~(a*b^t),start=c(a=4,b=1)) 

nls需要一個良好的起點,每個參數

+0

通過你的知識/經驗。根據公式,兩者都應該大於零。總是t = 1的值可用嗎?如果是,請將此用於'a'的值。採取第二個,並計算(不適合)B,並使用它。如果他們從錯誤的地方開始,非線性擬合可能會失敗(或只找到最小值)。順便說一下a = 10,b = 10也起作用。 –

+0

考慮使用log(texp)代替t來避免getInitial失敗的簡單策略。 –