Question

I have a similar problem, I'd like to calculate the non-linear regression in R, but I get an error.

This is my code:

f <- function(x1,x2,x3,a,b1,b2,b3) {a * (x1^b1) * (x2^b2) * (x3^b3) }

# generate some data
x1 <- c(9,9,12,12,12,16,9,16)
x2 <- c(0.8,1,0.8,1,1.2,1.2,1.2,1)
x3 <- c(0.14,0.12,0.16,0.14,0.12,0.16,0.16,0.14)
y <- c(304,284,435,489,512,854,517,669)
dat <- data.frame(x1,x2,x3, y)

# fit a nonlinear model
fm <- nls(y ~ f(x1,x2,x3,a,b1,b2,b3), data = dat, start = c(a=0, b1=0,b2=0,b3=0))

# get estimates of a, b
co <- coef(fm)

And I got this error:

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

What should I do?

Thank you!

Was it helpful?

Solution

You need good starting values:

#starting values from linearization
fit0 <- lm(log(y) ~ log(x1) + log(x2) +log(x3), data=dat)

# fit a nonlinear model
fm <- nls(y ~ f(x1,x2,x3,a,b1,b2,b3), data = dat, 
          start = list(a=exp(coefficients(fit0)[1]), 
                    b1=coefficients(fit0)[2],
                    b2=coefficients(fit0)[3],
                    b3=coefficients(fit0)[4]))
summary(fm)
# Parameters:
#     Estimate Std. Error t value Pr(>|t|)    
# a  265.19567  114.37494   2.319 0.081257 .  
# b1   0.97277    0.08186  11.884 0.000287 ***
# b2   0.97243    0.12754   7.624 0.001589 ** 
# b3   0.91938    0.17032   5.398 0.005700 ** 

The usual diagnostics recommended for non-linear models should follow.

Also note, that starting values are supplied to nls as a list.

OTHER TIPS

Or no starting values at all. Then nls will use its "very cheap guess", which works with this data set.

nls(y ~ f(x1,x2,x3,a,b1,b2,b3), data = dat)

Incidentally - at least in 3.1.0 - starting values are "a named list or named numeric vector" so you can use

sVec <- coef(lm(log(y) ~ log(x1)+log(x2)+log(x3), dat))
sVec[1] <- exp(sVec[1])
names(sVec) <- c("a", "b1", "b2", "b3")
nls(y ~ f(x1,x2,x3,a,b1,b2,b3), data = dat, start = sVec)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top