2
我是R的新手,遇到了一些我不明白的問題。我查找過關於R如何工作的文檔或答案,但我找不到任何東西。nls每次迭代多次運行函數?
Test = data.frame(x = c(5, 10, 15, 25), y = c(5, 8, 12, 16))
fun = function(x, a, b) {
cat("a =", a, ", b =", b, "\n")
a*(x**b)
}
nls(y ~ fun(x, a, b), data = Test, start = list(a = 1.613, b = 0.718), trace = TRUE)
輸出如下:
a = 1.613 , b = 0.718
a = 1.613 , b = 0.718
a = 1.613 , b = 0.718
a = 1.613 , b = 0.718
0.7964401 : 1.613 0.718
a = 1.612723 , b = 0.7177708
a = 1.612723 , b = 0.7177708
a = 1.612723 , b = 0.7177708
0.7960992 : 1.6127232 0.7177708
a = 1.612698 , b = 0.7177761
a = 1.612698 , b = 0.7177761
a = 1.612698 , b = 0.7177762
0.7960992 : 1.6126980 0.7177761
Nonlinear regression model
model: y ~ fun(x, a, b)
data: Test
a b
1.6127 0.7178
residual sum-of-squares: 0.7961
Number of iterations to convergence: 2
Achieved convergence tolerance: 1.43e-06
它說,它採取了兩次迭代收斂,但它看起來像它通過函數跑了10次,只有改變的參數值的兩倍。是否有一個原因?我無法找到關於nls如何計算參數猜測的解釋。
我試圖把600個數據點放到一個大的函數中,所以這佔用了很多額外的不需要的時間。
@ alexisdebriere的回答基本上是正確的。如果您能夠明確指定漸變功能(在「?nls」中搜索「漸變」),則可以避免這種情況。 –