I have two variables that I'm trying to model the relationship between and extract the residuals. The relationship between the two variables is clearly a non-linear exponential relationship. I've tried a few different approaches with nls, but I keep getting different error messages.
# dataset
df <- structure(list(y = c(464208.56, 334962.43, 361295.68, 426535.68, 258843.93, 272855.46,
166322.72, 244695.28, 227003.03, 190728.4, 156025.45, 72594.24, 56911.4, 175328.95, 161199.76,
152520.77, 190610.57, 60734.34, 31620.9, 74518.86, 45524.49, 2950.58, 2986.38, 15961.77, 12484.05,
6828.41, 2511.72, 1656.12, 5271.4, 7550.66, 3357.71, 3620.43, 3699.85, 3337.56, 4106.55, 3526.66,
2996.79, 1649.89, 4561.64, 1724.25, 3877.2, 4426.69, 8557.61, 6021.61, 6074.17, 4072.77, 4032.95,
5280.16, 7127.22),
x = c(39.23, 38.89, 38.63, 38.44, 38.32, 38.27, 38.3, 38.4, 38.56, 38.79, 39.06, 39.36, 39.68,
40.01, 40.34, 40.68, 41.05, 41.46, 41.93, 42.48, 43.14, 43.92, 44.84, 45.9, 47.1, 48.4, 49.78,
51.2, 52.62, 54.01, 55.31, 56.52, 57.6, 58.54, 59.33, 59.98, 60.46, 60.78, 60.94, 60.92, 60.71,
60.3, 59.69, 58.87, 57.86, 56.67, 55.33, 53.87, 52.33)),
row.names = c(NA, -49L),
class = c("tbl_df", "tbl", "data.frame"),
na.action = structure(c(`1` = 1L, `51` = 51L),
class = "omit"))
# initial model
m <- nls(y ~ a * exp(r * x),
start = list(a = 0.5, r = -0.2),
data = df)
Error in nls(y ~ a * exp(r * x), start = list(a = 0.5, r = -0.2), data = df, : singular gradient
# add term for alg
m <- nls(y ~ a * exp(r * x),
start = list(a = 0.5, r = -0.2),
data = df,
alg = "plinear")
Error in nls(y ~ a * exp(r * x), start = list(a = 0.5, r = -0.2), data = df, :
step factor 0.000488281 reduced below 'minFactor' of 0.000976562


y = a * exp(r * x), the way to think aboutais theyvalue whenx = 0. Looking at your plot, whenxis around 40, the y values are around 300,000, and those values would be exponentially bigger as x approaches 0, soa = 0.5is a pretty terrible starting value, off by many orders of magnitude. In fact, since your data doesn't get anywhere close tox = 0, a shifted exponential model might be a much better fit, maybey ~ a * exp(r * (x - b))with a possible start values ofa = 5e5andb = 38. (Though I still get a singular gradient there)log(y)?lm(log(y) ~ x, data = df)fits just fine, and you can exponentiate the intercept to getaand use thexcoefficient asr(which is-0.1915, very close to ther = -0.2starting value you use). The error structure is a little different, not sure if you care about that... The intercept is 19.1994, soexp(19.1994) = 217868015, which is off from you 0.5 by about10^9times.log(y) ~ poly(x, 2)....