Search code examples
rcurve-fittingnls

Finding NLS for multiple series


I want to fit a model /find a functional form using 70 series. I have 30 values for each one of them. The plot shows that the trend is not linear so I am trying to use NLS. the data looks like this

Not sure if this is the right approach. I have not implemented this before. Should I be using something else to model? I am getting the following error:

Error in nls(y ~ a * exp(b * x), start = list(a = a_start, b = b_start)) :
singular gradient

x has values 1:30, which are repeated 70 times. Y values vary between 0 and 1. Should I be using 71 vectors (of length 30) or should I gather them all in two columns. At the moment, I have gathered them all in two columns.

The data looks something like this:


a_start<-0.2
b_start<-2*log(2)/a_start

Solution

  • The problem is your formula is not a form that accurately reflects your data. y ~ a * exp(b * x) will never look like your data, so you'll get sharp gradients that don't make any sense.

    Try this:

    #some made up data, since none provided
    x <- runif(1000) * 30
    y <- -0.9 * exp(-0.5 * x) + 1.1 + rnorm(length(x), sd=0.1)
    
    fit <- nls(y ~ a * exp(b * x) + c, start = list(a = -1, b=-.1, c=1))
    
    plot(x,y)
    lines(x[order(x)], predict(fit)[order(x)], col="red", lwd=4)
    

    enter image description here