<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>regression | Vasco Cadavez</title>
    <link>http://www.ipb.pt/~vcadavez/websiteVC/tags/regression/</link>
      <atom:link href="http://www.ipb.pt/~vcadavez/websiteVC/tags/regression/index.xml" rel="self" type="application/rss+xml" />
    <description>regression</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>regression</title>
      <link>http://www.ipb.pt/~vcadavez/websiteVC/tags/regression/</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>
    
    <item>
      <title>Fitting a first-order or loglinear growth model using R</title>
      <link>http://www.ipb.pt/~vcadavez/websiteVC/tutorial/rcode/2019-04-25-firstordermodel/</link>
      <pubDate>Thu, 25 Apr 2019 21:13:14 -0500</pubDate>
      <guid>http://www.ipb.pt/~vcadavez/websiteVC/tutorial/rcode/2019-04-25-firstordermodel/</guid>
      <description>


&lt;p&gt;The data set “&lt;strong&gt;FirstOrder.csv&lt;/strong&gt;” contains observations of microbial concentrations (&lt;strong&gt;log N&lt;/strong&gt;) measured at different times (&lt;strong&gt;t&lt;/strong&gt;) at a given environmental condition. Lets fit a first-order growth kinetics model &lt;span class=&#34;math inline&#34;&gt;\(log N = log N_0 + k \times t\)&lt;/span&gt; to the experimental data.&lt;/p&gt;
&lt;p&gt;Let’s import the “&lt;strong&gt;FirstOrder.csv&lt;/strong&gt;” dataset, and observe the first five lines.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- read.csv(&amp;quot;FirstOrder.csv&amp;quot;, sep=&amp;quot;;&amp;quot;, header=TRUE)
dat
##    Time        N
## 1     0   37.298
## 2     1   56.149
## 3     2   81.580
## 4     3  108.758
## 5     4  147.276
## 6     5  197.160
## 7     6  273.014
## 8     7  368.254
## 9     8  490.822
## 10    9  673.304
## 11   10  909.398
## 12   11 1204.429
## 13   12 1640.532
## 14   13 2218.299
## 15   14 2983.926
## 16   15 4033.458&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Plot &lt;strong&gt;N&lt;/strong&gt; against &lt;strong&gt;Time&lt;/strong&gt;, and observe that the relationship between the two variables is non-linear.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(dat$Time, dat$N, xlab=&amp;quot;Time (hours)&amp;quot;, ylab=&amp;quot;N (cells/g)&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-25-FirstOrderModel_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Take the logarithm of the variable N and add the new variable logN in the data set.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat$logN &amp;lt;- log10(dat$N)
dat[1:5,]
##   Time       N     logN
## 1    0  37.298 1.571686
## 2    1  56.149 1.749342
## 3    2  81.580 1.911584
## 4    3 108.758 2.036461
## 5    4 147.276 2.168132&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now plot &lt;strong&gt;logN&lt;/strong&gt; against &lt;strong&gt;Time&lt;/strong&gt;, and observe that they have now a linear relationship.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(dat$Time, dat$logN, xlab=&amp;quot;Time (hours)&amp;quot;, ylab=&amp;quot;log N (cels/g)&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-25-FirstOrderModel_files/figure-html/plot1-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The first-order kinetics model is defined as &lt;code&gt;Nt = N0*exp(k*t)&lt;/code&gt;, then the log-linear model becomes &lt;code&gt;ln(Nt) = ln(N0) + k*t&lt;/code&gt;. Fit a linear model using the &lt;strong&gt;lm()&lt;/strong&gt; (linear models) function.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;lm1 &amp;lt;- lm(logN ~ Time, data=dat)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The results can be summarised using the &lt;strong&gt;summary()&lt;/strong&gt; fucntion.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(lm1)
## 
## Call:
## lm(formula = logN ~ Time, data = dat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.052876 -0.006170  0.004664  0.011795  0.021331 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept) 1.6245612  0.0084968   191.2   &amp;lt;2e-16 ***
## Time        0.1328460  0.0009652   137.6   &amp;lt;2e-16 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 0.0178 on 14 degrees of freedom
## Multiple R-squared:  0.9993, Adjusted R-squared:  0.9992 
## F-statistic: 1.894e+04 on 1 and 14 DF,  p-value: &amp;lt; 2.2e-16&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can add fitted values to the original data set.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat$fitted &amp;lt;- fitted(lm1)
dat[1:5,]
##   Time       N     logN   fitted
## 1    0  37.298 1.571686 1.624561
## 2    1  56.149 1.749342 1.757407
## 3    2  81.580 1.911584 1.890253
## 4    3 108.758 2.036461 2.023099
## 5    4 147.276 2.168132 2.155945&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Plot the observations against time and superimpose a fitted line. First, extract the model’s parameters and use the model to draw a fitted line.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;coefs &amp;lt;- coef(lm1)
coefs
## (Intercept)        Time 
##    1.624561    0.132846
logN0 &amp;lt;- coefs[1]
logN0
## (Intercept) 
##    1.624561
k &amp;lt;- coefs[2]
k
##     Time 
## 0.132846&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Plot observed versus fitted values.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(dat$logN ~ dat$Time, data=dat,
     xlab=&amp;quot;Time (hours)&amp;quot;, ylab=&amp;quot;log N (cells/g)&amp;quot;)
time.levels &amp;lt;- seq(0, 20, by=1)
lines(time.levels, logN0 + k*time.levels, lty = 2, col = &amp;quot;blue&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-25-FirstOrderModel_files/figure-html/unnamed-chunk-8-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
