[R] Fitting a sigmoid

Cressoni, Massimo (NIH/NHLBI) [F] cressonim at nhlbi.nih.gov
Sat Mar 3 22:46:56 CET 2007


I am trying to write a function that fits a sigmoid given a X and Y vector guessing the start parameters.
I use nls. What I did (enclosed) seems to work well with many data points but if I want to fit small
vectors like :

pressure <- c(5,15,9,35,45)
gas <- c(1000,2000,3000,4000,5000)

it do not work. The help page says that it do no not work on zero residual data.

Massimo Cressoni

PS 
The functions I wrote are the following :


x25 = function(y_range)
{
 x25r = min(y_range) + (max(y_range) - min(y_range))/4
 x25r
}

x75 = function(y_range)
{
 x75r = min(y_range) + 3*(max(y_range) - min(y_range))/4
 x75r
}Get

GetStartSigmoidValues = function(pointsY)
{
 start_a = max(pointsY) - min(pointsY)
 start_x0 = min(pointsY) + (max(pointsY) - min(pointsY))/2
 start_b = (x75(pointsY) - x25(pointsY))/4
 start_y0 = min(pointsY)

 start_list = list(a=start_a,x0=start_x0,b=start_b,y0=start_y0)
 start_list
}

FitSigmoid = function(pointsX,pointsY)
{
 start_list = GetStartSigmoidValues(pointsY)
 fitting = nls(pointsY ~ y0+a/(1+exp(-(pointsX - x0)/b)),start=start_list)
 fitting
}



More information about the R-help mailing list