Chris A.
Chris A.

Reputation: 435

nls() : "Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates "

I'm trying to use nls(), but I keep getting the error

Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates

and I'm not sure where the problem is.

Code below:

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)

Any help is greatly appreciated.

Upvotes: 0

Views: 2257

Answers (1)

GKi
GKi

Reputation: 39717

You have inter-correlation between the coefficients Alpha and Sigma. A simple solution is to hold one of them constant. Maybe it would be better to reformulate the equation and substitute Alpha or 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)

Upvotes: 1

Related Questions