I am trying to do an exponential regression in R but I keep getting this error
Error in nls(y ~ a * exp(b * x), data = DF, start = list(a = -10, b = -10)):singular gradient
The data and code I'm using are:
x <-c(0.00, 6.40, 8.61, 15.20, 28.10, 42.60, 66.70, 73.00, 73.00, 85.00, 88.00, 88.00, 88.00, 88.00, 88.00, 88.00, 94.00, 94.00, 94.00, 94.00, 94.00, 94.00, 94.00, 94.00, 94.00, 94.00, 94.00, 102.00, 102.00, 102.00, 102.00, 102.00, 160.00, 160.00, 169.00, 320.00, 320.00, 320.00, 432.00, 432.00)
y <- c(6.52, 1.95, 1.51, 1.94, 3.04, 1.81, 2.07, 0.88, 1.59, 1.18, 0.47, 0.69, 0.90, 1.27, 0.94, 1.84, 0.71, 1.30, 0.50, 1.09, 0.69, 4.07, 0.68, 0.91, 0.64, 0.97, 0.99, 1.34, 0.82, 0.34, 0.39, 1.14, 0.90, 0.36, 0.86, 0.59, 0.36, 1.14, 1.09, 1.81)
DF <- data.frame(x,y)
m <- nls(y ~ a*exp(b*x), data = DF, start=list(a=-10, b=-10))
It's probably an easy fix but I have been stuck with this for days, thank you very much, any help highly appreciated!
First look at the data. Many times you need to supply starting values that are somewhat plausible.
plot(y~x)
It seemed clear that a
would be positive and b
would be negative. Furthermore the long "time-scale" ( I think in terms of survival analysis.) would require a fairly small b
:
m <- nls(y ~ a*exp(b*x), data = DF, start=list(a=-1, b=-.1) )
> m
Nonlinear regression model
model: y ~ a * exp(b * x)
data: DF
a b
3.5092 -0.0128
residual sum-of-squares: 33.85
Number of iterations to convergence: 18
Achieved convergence tolerance: 5.09e-06
Add a curve to the plot with:
curve( 3.5*exp(-0.0128*x),add=TRUE, col="blue")
I cannot place an image in a comment, and so place it here. When I fit your data with an offset, "a * exp(b * x) + offset", as suggested to me by a scatterplot of the data, I get what appears to be a better fit with R-squared = 0.584 and RMSE = 0.714 from fitted parameters a = 5.3702154953394219E+00, b = -2.7909919440915620E-01, and offset = 1.1363689273642967E+00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.