<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Gompertz | Vasco Cadavez</title>
    <link>http://www.ipb.pt/~vcadavez/websiteVC/tags/gompertz/</link>
      <atom:link href="http://www.ipb.pt/~vcadavez/websiteVC/tags/gompertz/index.xml" rel="self" type="application/rss+xml" />
    <description>Gompertz</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>© 2021</copyright><lastBuildDate>Sun, 28 Apr 2019 21:13:14 -0500</lastBuildDate>
    <image>
      <url>img/map[gravatar:%!s(bool=false) shape:circle]</url>
      <title>Gompertz</title>
      <link>http://www.ipb.pt/~vcadavez/websiteVC/tags/gompertz/</link>
    </image>
    
    <item>
      <title>Fitting a reparameterised Gompertz growth model using R</title>
      <link>http://www.ipb.pt/~vcadavez/websiteVC/tutorial/rcode/2019-04-28-gompertzmodel/</link>
      <pubDate>Sun, 28 Apr 2019 21:13:14 -0500</pubDate>
      <guid>http://www.ipb.pt/~vcadavez/websiteVC/tutorial/rcode/2019-04-28-gompertzmodel/</guid>
      <description>


&lt;p&gt;Using the data set “&lt;strong&gt;gomp1.csv&lt;/strong&gt;”, find the parameters of the reparameterised Gompertz model.&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{equation}
y= y_0 + (y_{max} -y_0)*exp(-exp(k*(lag-x)/(y_{max}-y_0) + 1) )
\end{equation}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Import the data set.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- read.csv(&amp;quot;gomp1.csv&amp;quot;, sep=&amp;quot;;&amp;quot;, header=T)
plot(dat$Time, dat$logN)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;http://www.ipb.pt/~vcadavez/websiteVC/~vcadavez/websiteVC/tutorial/Rcode/2019-04-28-GompertzModel_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;str(dat)
## &amp;#39;data.frame&amp;#39;:    13 obs. of  2 variables:
##  $ Time: int  0 2 4 6 8 10 12 14 16 18 ...
##  $ logN: num  -0.105 0.108 -0.111 0.734 2.453 ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next step is define the &lt;strong&gt;Gompertz&lt;/strong&gt; function.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Gompertz function
Gompertz &amp;lt;- function(x, y0, ymax, k, lag){
      result &amp;lt;- y0 + (ymax -y0)*exp(-exp(k*(lag-x)/(ymax-y0) + 1) )
      return(result)
      }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Check data by plot.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot( logN ~ Time, data=dat, xlim=c(-1, 25), ylim=c(-1, 10))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;http://www.ipb.pt/~vcadavez/websiteVC/~vcadavez/websiteVC/tutorial/Rcode/2019-04-28-GompertzModel_files/figure-html/unnamed-chunk-3-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Fit the model to data using the &lt;strong&gt;nls()&lt;/strong&gt; funtion and extract the models parameters.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;Gomp1 &amp;lt;-  nls(logN ~ Gompertz(Time, y0, ymax, k, lag),
               data = dat,
               start = list(y0=0.15, ymax=7, k=0.5, lag=3))
# ectract model parameters
coefs &amp;lt;- coef(Gomp1)
coefs
##          y0        ymax           k         lag 
## -0.02517334  9.60030027  2.78548012  5.88823263
y0   &amp;lt;- coefs[1]
y0
##          y0 
## -0.02517334
ymax &amp;lt;- coefs[2]
ymax
##   ymax 
## 9.6003
k   &amp;lt;- coefs[3]
k
##       k 
## 2.78548
lag &amp;lt;- coefs[4]
lag
##      lag 
## 5.888233&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, plot the observed values against the fitted values.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;time.levels &amp;lt;- seq(0, 24, by=0.1)
pred &amp;lt;- predict(Gomp1, list(x=time.levels) )
plot(logN ~ Time, data=dat, xlim=c(0,24), ylim=c(0, 10),
     main = &amp;quot;Fitted Gompertz model&amp;quot;,
     sub = &amp;quot;Blue: fit; green: known&amp;quot;)
lines(time.levels, Gompertz(time.levels,
                            y0 = y0, ymax = ymax, k = k, lag=lag), 
      lty = 1, col = &amp;quot;red&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;http://www.ipb.pt/~vcadavez/websiteVC/~vcadavez/websiteVC/tutorial/Rcode/2019-04-28-GompertzModel_files/figure-html/unnamed-chunk-5-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Add the confidence interval to the line offitted values.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(nlme)
library(twNlme)
Gomp11 &amp;lt;- gnls(logN ~ y0 + (ymax -y0)*exp(-exp(k*(lag-Time)/(ymax-y0) + 1) ),
              data = dat,
              start = list(y0=-0.025, ymax=9.5, k=2.7, lag=5.8))
Gomp11
## Generalized nonlinear least squares fit
##   Model: logN ~ y0 + (ymax - y0) * exp(-exp(k * (lag - Time)/(ymax - y0) +      1)) 
##   Data: dat 
##   Log-likelihood: -2.123991
## 
## Coefficients:
##          y0        ymax           k         lag 
## -0.02511771  9.60008391  2.78571200  5.88858202 
## 
## Degrees of freedom: 13 total; 9 residual
## Residual standard error: 0.3424286

# automatic derivation with accounting for residual variance model
nlmeFit &amp;lt;- attachVarPrep(Gomp11, fVarResidual=varResidPower)

newData &amp;lt;- data.frame( Time= seq(0,24,by=0.1))

resNlme &amp;lt;- varPredictNlmeGnls(Gomp11, newData)

# plotting prediction and standard errors
plot( logN ~ Time, dat, ylab=&amp;quot;log CFU&amp;quot;,
      xlim=c(0,24), ylim=c(-1, 10))
par(new=T)
plot( resNlme[,&amp;quot;fit&amp;quot;] ~ Time, newData, ylab=&amp;quot;&amp;quot;,
      type=&amp;quot;l&amp;quot;, xlim=c(0,24), ylim=c(-1, 10), lty=&amp;quot;solid&amp;quot;)
lines( resNlme[,&amp;quot;fit&amp;quot;]+resNlme[,&amp;quot;sdInd&amp;quot;] ~ Time, newData, 
       col=&amp;quot;orange&amp;quot;, lty=&amp;quot;dashed&amp;quot;  )
lines( resNlme[,&amp;quot;fit&amp;quot;]-resNlme[,&amp;quot;sdInd&amp;quot;] ~ Time, newData, 
       col=&amp;quot;orange&amp;quot;, lty=&amp;quot;dashed&amp;quot; )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;http://www.ipb.pt/~vcadavez/websiteVC/~vcadavez/websiteVC/tutorial/Rcode/2019-04-28-GompertzModel_files/figure-html/unnamed-chunk-6-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
