Search code examples
rnls

Prevent a nls-fit from falling below zero


I'm trying to fit a function in R and therefor I use nls(). Is there a way to prevent the fitted function from falling below zero?

An easy work around would be to rise the parameter b0 in the target function after the fit, but this is actually not what I want because I expect a real fit with the constraint of beeing positive to lead to a better result.

y=c(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10)
d=data.frame(seq(1, 10, 1),y=y)
fitFun <- function(x, add, b0, b1) {b0 + (x+add)^b1}
m=nls(y~fitFun(x,add,intercept,power),d,start=list(intercept=1,power=3.5,add=2),trace=T)

Solution

  • Thanks a lot for the answers. Maybe I didn't give enough information about my problem, but I'm not yet allowed to post pictures and describing everything would have led to a short story.

    @Roland was perfectly right it's not the optimizers task to care about the behaviour of the target function, but as I mentioned I assume the model to be fix.

    @Ben Bolker's suggestion to limit the additive part of the function to positive values led to an unsatifying result.

    What I didn't mention was that m1 to m10 are mean values of a data collection I recorded. I solved my problem by using the variance of the recorded series as weights during the fitting process.

    y=c(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10)
    d=data.frame(seq(1, 10, 1),y=y)
    vars = c(var(lt1$V1),var(lt2$V1),var(lt3$V1),var(lt4$V1),var(lt5$V1),var(lt6$V1),var(lt7$V1),var(lt8$V1),var(lt9$V1),var(lt10$V1))
    weights = rep(max(vars),10)/vars
    fitFun <- function(x, add, b0, b1) {b0 + (x+add)^b1}
    m=nls(y~fitFun(x,add,intercept,power),d,weights=weights,start=list(intercept=1,power=3.5,add=2),trace=T)