Using R to fit a Sigmoidal Curve

FunnyFunkyBuggy picture FunnyFunkyBuggy · Oct 9, 2015 · Viewed 23.8k times · Source

I have read a post ( Sigmoidal Curve Fit in R ). It was labeled duplicated, but I can't see anything related with the posts. And the answer given for the posts was not enough.

I read a webpage

Similar to the others, he uses this format to fit the line:

fitmodel <- nls(y~a/(1 + exp(-b * (x-c))), start=list(a=1,b=.5,c=25))

The problem is that, a,b,c were given in most of the cases and I have no clues which set of a,b,c should I use for my set of data. Could someone give me some advice on how to obtain the parameters?

Here is my set of numbers :

x <- c(3.9637878,3.486667,3.0095444,2.5324231,2.0553019,1.5781806,1.1010594,0.6242821)
y <- c(6491.314,6190.092,2664.021,2686.414,724.707,791.243,1809.586,541.243)

Answer

Roland picture Roland · Oct 9, 2015

Luckily R offers a selfstarting model for the logistic model. It uses a slight reparametrization, but is really the same model as yours: Asym/(1+exp((xmid-input)/scal))

A selfstarting model can estimate good starting values for you, so you don't have to specify them.

plot(y ~ x)
fit <- nls(y ~ SSlogis(x, Asym, xmid, scal), data = data.frame(x, y))

summary(fit)
#Formula: y ~ SSlogis(x, Asym, xmid, scal)
#
#Parameters:
#      Estimate Std. Error t value Pr(>|t|)
#Asym 1.473e+04  2.309e+04   0.638    0.551
#xmid 4.094e+00  2.739e+00   1.495    0.195
#scal 9.487e-01  5.851e-01   1.622    0.166
#
#Residual standard error: 941.9 on 5 degrees of freedom
#
#Number of iterations to convergence: 0 
#Achieved convergence tolerance: 4.928e-06

lines(seq(0.5, 4, length.out = 100), 
      predict(fit, newdata = data.frame(x = seq(0.5, 4, length.out = 100))))

resulting plot

Of course your data doesn't really support the model. The estimated mid point is just at the right limit of your data range and thus the parameter estimates (in particular for the asymptote) are very uncertain.