Forcing nls to fit a curve passing through a specified point

前端 未结 2 1391
悲哀的现实
悲哀的现实 2020-12-21 13:55

I\'m trying to fit a Boltzmann sigmoid 1/(1+exp((x-p1)/p2)) to this small experimental dataset:

xdata <- c(-60,-50,-40,-30,-20,-10,-0,10)
yda         


        
2条回答
  •  鱼传尺愫
    2020-12-21 14:13

    Building on @Cleb's answer, here's a way to pick a specified point the function must pass through and solve the resulting equation for one of the parameters:

    dd <- data.frame(x=c(-60,-50,-40,-30,-20,-10,-0,10),
                     y=c(0.04, 0.09, 0.38, 0.63, 0.79, 1, 0.83, 0.56))
    

    Initial fit (using plogis() rather than 1/(1+exp(-...)) for convenience):

    fit <- nls(y ~ plogis(-(x-p1)/p2),
               data=dd,
               start=list(p1=mean(dd$x),p2=-5))
    

    Now plug in (x3,y3) and solve for p2:

    y3 = 1/(1+exp((x-p1)/p2))
    logit(x) = qlogis(-x) = log(x/(1-x))
    e.g. plogis(2)=0.88 -> qlogis(0.88)=2
    qlogis(y3) = -(x-p1)/p2
    p2 = -(x3-p1)/qlogis(y3)
    

    Set up a function and plug it in for p2:

    p2 <- function(p1,x,y) {
        -(x-p1)/qlogis(y)
    }
    fit2 <- nls(y ~ plogis(-(x-p1)/p2(p1,dd$x[3],dd$y[3])),
        data=dd,
        start=list(p1=mean(dd$x)))
    

    Plot the results:

    plot(y~x,data=dd,ylim=c(0,1.1))
    xr <- data.frame(x = seq(min(dd$x),max(dd$x),len=200))
    lines(xr$x,predict(fit,newdata=xr))
    lines(xr$x,predict(fit2,newdata=xr),col=2)
    

提交回复
热议问题