Search code examples
rnonlinear-functions

Fit saturation growth-rate model in R


I have a response variable and an independent variable that visually fit to a saturation growth-rate model. How can I fit such model in R? Thank you!


Solution

  • give the nls function a try, but next time please provide some example data. I use the data from this excellent tutorial of a colleague (https://bscheng.com/2014/05/07/modeling-logistic-growth-data-in-r/):

    library("car"); library("ggplot2")
    #Here's the data
    mass<-c(6.25,10,20,23,26,27.6,29.8,31.6,37.2,41.2,48.7,54,54,63,66,72,72.2,
            76,75) #Wilson's mass in pounds
    days.since.birth<-c(31,62,93,99,107,113,121,127,148,161,180,214,221,307,
                        452,482,923, 955,1308) #days since Wilson's birth
    data<-data.frame(mass,days.since.birth) #create the data frame
    plot(mass~days.since.birth, data=data) #always look at your data first!
    
    wilson<-nls(mass~phi1/(1+exp(-(phi2+phi3*days.since.birth))),
                start=list(phi1=100,phi2=-1.096,phi3=.002),data=data,trace=TRUE)
    
    #set parameters
    phi1<-coef(wilson)[1]
    phi2<-coef(wilson)[2]
    phi3<-coef(wilson)[3]
    x<-c(min(data$days.since.birth):max(data$days.since.birth)) #construct a range of x values bounded by the data
    y<-phi1/(1+exp(-(phi2+phi3*x))) #predicted mass
    predict<-data.frame(x,y) #create the prediction data frame#And add a nice plot (I cheated and added the awesome inset jpg in another program)
    ggplot(data=data,aes(x=days.since.birth,y=mass))+
      geom_point(color='blue',size=5)+theme_bw()+
      labs(x='Days Since Birth',y='Mass (lbs)')+
      scale_x_continuous(breaks=c(0,250,500,750, 1000,1250))+
      scale_y_continuous(breaks=c(0,10,20,30,40,50,60,70,80))+
      theme(axis.text=element_text(size=18),axis.title=element_text(size=24))+
      geom_line(data=predict,aes(x=x,y=y), size=1)
    

    enter image description here