我正在尝试使用nls()
,但我不断收到错误
nlsModel中的错误(公式,mf,start,wts):初始参数估计时的奇异梯度矩阵
而且我不确定问题出在哪里。
代码如下:
TI <- c(0.5, 2, 5, 10, 30)
prices <- cbind(zi, TI)
prices = as.data.frame(prices)
lnz_i <- function(TI, Alpha, Beta, Sigma) -TI*(Alpha*(1 - exp(-Beta*TI)) / (Beta) - (Sigma^2/2)*(1 - exp(-Beta*TI)) / (Beta)^2) - 0.02*(1 - exp(-Beta*TI)) / (Beta)
nls(zi ~ lnz_i(TI, Alpha, Beta, Sigma), start = c(Alpha = 0.02, Beta = 0.3, Sigma = 0.06), data = prices)
任何帮助是极大的赞赏。
您在系数Alpha和Sigma之间存在相互关联。一个简单的解决方案是保持其中一个不变。也许最好重新制定方程式并替换Alpha或Sigma。
set.seed(1)
lnz_i <- function(TI, Alpha, Beta, Sigma) -TI*(Alpha*(1 - exp(-Beta*TI)) / (Beta) - (Sigma^2/2)*(1 - exp(-Beta*TI)) / (Beta)^2) - 0.02*(1 - exp(-Beta*TI)) / (Beta)
TI <- c(0.5, 2, 5, 10, 30)
prices <- data.frame(TI, zi=lnz_i(TI, 0.02, 0.3, 0.06)*runif(length(TI), .9, 1.1))
#Hold Alpha Fixed
Alpha <- 0.02
nls(zi ~ lnz_i(TI, Alpha, Beta, Sigma), start = c(Beta=0.3, Sigma = 0.06), data = prices)
Alpha <- 0.04
nls(zi ~ lnz_i(TI, Alpha, Beta, Sigma), start = c(Beta=0.3, Sigma = 0.06), data = prices)
Alpha <- 0.1
nls(zi ~ lnz_i(TI, Alpha, Beta, Sigma), start = c(Beta=0.3, Sigma = 0.2), data = prices)
#Estimate for Beta is all the time 0.401 and residuals are at 0.003768,
#only Sigma is changing when Alpha is changed
#Hold Sigma Fixed
Sigma <- 0.06
nls(zi ~ lnz_i(TI, Alpha, Beta, Sigma), start = c(Alpha = 0.02, Beta = 0.3), data = prices)
Sigma <- 0.03
nls(zi ~ lnz_i(TI, Alpha, Beta, Sigma), start = c(Alpha = 0.02, Beta = 0.3), data = prices)