我正在尝试使用nls()
,但我不断收到错误消息
nlsModel(formula,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)
非常感谢您的帮助。
答案 0 :(得分:1)
您在系数 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)