问题描述
我想使用 y = a ^(b ^ x)
来拟合以下数据,
I want to use y=a^(b^x)
to fit the data below,
y <- c(1.0385, 1.0195, 1.0176, 1.0100, 1.0090, 1.0079, 1.0068, 1.0099, 1.0038)
x <- c(3,4,5,6,7,8,9,10,11)
data <- data.frame(x,y)
当我使用非线性最小二乘法时,
When I use the non-linear least squares procedure,
f <- function(x,a,b) {a^(b^x)}
(m <- nls(y ~ f(x,a,b), data = data, start = c(a=1, b=0.5)))
它会产生错误:在初始参数估计值处为奇异梯度矩阵。结果大致为a = 1.1466,b = 0.6415,因此初始参数估计应该没有问题,因为我将其定义为a = 1,b = 0.5。
it produces an error: singular gradient matrix at initial parameter estimates. The result is roughly a = 1.1466, b = 0.6415, so there shouldn't be a problem with intial parameter estimates as I have defined them as a=1, b=0.5.
我在其他主题中已经读到,修改曲线很方便。我在考虑类似 log y = log **(b ^ x)
之类的东西,但是我不知道如何处理函数规范。有想法吗?
I have read in other topics that it is convenient to modify the curve. I was thinking about something like log y=log a *(b^x)
, but I don't know how to deal with function specification. Any idea?
推荐答案
我将把我的评论扩展成答案。
I will expand my comment into an answer.
如果我使用以下命令:
y <- c(1.0385, 1.0195, 1.0176, 1.0100, 1.0090, 1.0079, 1.0068, 1.0099, 1.0038)
x <- c(3,4,5,6,7,8,9,10,11)
data <- data.frame(x,y)
f <- function(x,a,b) {a^b^x}
(m <- nls(y ~ f(x,a,b), data = data, start = c(a=0.9, b=0.6)))
或
(m <- nls(y ~ f(x,a,b), data = data, start = c(a=1.2, b=0.4)))
我得到:
Nonlinear regression model
model: y ~ f(x, a, b)
data: data
a b
1.0934 0.7242
residual sum-of-squares: 0.0001006
Number of iterations to convergence: 10
Achieved convergence tolerance: 3.301e-06
如果我使用 1
作为<$的起始值,我总是会报错。 c $ c> a ,也许是因为将 1
加到任何东西都是 1
。
I always obtain an error if I use 1
as a starting value for a
, perhaps because 1
raised to anything is 1
.
至于自动生成起始值,我不熟悉执行此操作的过程。我已经读过的一种方法是模拟曲线并使用起始值生成看起来像您的数据的曲线。
As for automatically generating starting values, I am not familiar with a procedure to do that. One method I have read about is to simulate curves and use starting values that generate a curve that appears to approximate your data.
这里是使用上述参数估计值生成的图使用以下代码。我承认,该行的右下部分可能更适合:
Here is the plot generated using the above parameter estimates using the following code. I admit that maybe the lower right portion of the line could fit a little better:
setwd('c:/users/mmiller21/simple R programs/')
jpeg(filename = "nlr.plot.jpeg")
plot(x,y)
curve(1.0934^(0.7242^x), from=0, to=11, add=TRUE)
dev.off()
这篇关于修改曲线以防止初始参数估计时出现奇异梯度矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!