<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/linear-models-anova-glms-and-mixed-effects-models-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-video-tutorial.blogspot.com/2017/06/linear-models-anova-glms-and-mixed.html"> R tutorial for Spatial Statistics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div></div>
<p>As part of my new role as Lecturer in Agri-data analysis at Harper Adams University, I found myself applying a lot of techniques based on linear modelling. Another thing I noticed is that there is a lot of confusion among researchers in regards to what technique should be used in each instance and how to interpret the model. For this reason I started reading material from books and on-line to try and create a sort of reference tutorial that researchers can use. This post is the result of my work so far.</p>
<p>Please feel free to comment, provide feedback and constructive criticism!!</p>
<p>
<h2></h2>
<h2 name="theoreticalbackground">Theoretical Background – Linear Model and ANOVA</h2>
<h3></h3>
<h2></h2>
<h3>Linear Model</h3>
<div>
<div><span lang="EN-GB">The classic linear model forms the basis for ANOVA (with categorical treatments) and ANCOVA (which deals with continuous explanatory variables). Its basic equation is the following:</span></div>
<div><span lang="EN-GB"><br></span></div>
<div>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/--lzwHrvVjz8/WVNw_gZyFCI/AAAAAAAAXOI/tOFJniofKjcxUDNpkLq0VPIN8eRpHuI7wCLcBGAs/s1600/Eq1.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i2.wp.com/4.bp.blogspot.com/--lzwHrvVjz8/WVNw_gZyFCI/AAAAAAAAXOI/tOFJniofKjcxUDNpkLq0VPIN8eRpHuI7wCLcBGAs/s640/Eq1.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>where β_0 is the intercept (i.e. the value of the line at zero), β_1 is the slope for the variable x, which indicates the changes in y as a function of changes in x. For example if the slope is +0.5, we can say that for each unit increment in x, y increases of 0.5. Please note that the slope can also be negative.</p></div>
<div><span lang="EN-GB"></span></div>
<div>
<div></div>
<p>This equation can be expanded to accommodate more that one explanatory variable x:</p></div>
<div>
<div><a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-3O87mf2Ll8I/WVNxGhN49LI/AAAAAAAAXOM/hF-y9d8ZVOoPBZwoCMNz-8TINaFznzU8gCLcBGAs/s1600/Eq2.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i0.wp.com/1.bp.blogspot.com/-3O87mf2Ll8I/WVNxGhN49LI/AAAAAAAAXOM/hF-y9d8ZVOoPBZwoCMNz-8TINaFznzU8gCLcBGAs/s640/Eq2.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>In this case the interpretation is a bit more complex because for example the coefficient β_2 provides the slope for the explanatory variable x_2. This means that for a unit variation of x_2 the target variable y changes by the value of β_2, if the other explanatory variables are kept constant. </p></div>
<div></div>
<div>In case our model includes interactions, the linear equation would be changed as follows:</div>
<div>
<div><a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-Ia-ueZcaeAA/WVNxMM3shQI/AAAAAAAAXOQ/-LchwJOpdRk57ex22wVdT6DAt5uMawzIgCLcBGAs/s1600/Eq3.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i0.wp.com/1.bp.blogspot.com/-Ia-ueZcaeAA/WVNxMM3shQI/AAAAAAAAXOQ/-LchwJOpdRk57ex22wVdT6DAt5uMawzIgCLcBGAs/s640/Eq3.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>notice the interaction term between x_1  and x_2. In this case the interpretation becomes extremely difficult just by looking at the model. </p></div>
<div></div>
<div>In fact, if we rewrite the equation focusing for example on x_1:</div>
<div>
<div><a rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-QzXfiJ3ewx4/WVNxQ9sYTTI/AAAAAAAAXOU/FP7JsaCPdvsyr63sjLVSl2Ul3vbL1EkaACLcBGAs/s1600/Eq4.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i1.wp.com/1.bp.blogspot.com/-QzXfiJ3ewx4/WVNxQ9sYTTI/AAAAAAAAXOU/FP7JsaCPdvsyr63sjLVSl2Ul3vbL1EkaACLcBGAs/s640/Eq4.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>we can see that its slope become affected by the value of x_2 (Yan &amp; Su, 2009), for this reason the only way we can actually determine how x_1 changes Y, when the other terms are kept constant, is to use the equation with new values of x_1. </p></div>
<div></div>
<div>This linear model can be applied to continuous target variables, in this case we would talk about an ANCOVA for exploratory analysis, or a linear regression if the objective was to create a predictive model.</div>
</div>
<div></div>
<div></div>
<h3>ANOVA</h3>
<div>
<div>The Analysis of variance is based on the linear model presented above, the only difference is that its reference point is the mean of the dataset. When we described the equations above we said that to interpret the results of the linear model we would look at the slope term; this indicates the rate of changes in Y if we change one variable and keep the rest constant. The ANOVA calculates the effects of each treatment based on the grand mean, which is the mean of the variable of interest. </div>
<div></div>
<div>In mathematical terms ANOVA solves the following equation (Williams, 2004):</div>
<div>
<div><a rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-B1Ot9OOV4cE/WVNxeF7sm3I/AAAAAAAAXOY/7-YyDpopKi8dib1byTBbIaszbQvpKeE4QCLcBGAs/s1600/Eq5.png?ssl=1" target="_blank"><img border="0" data-original-height="84" src="https://i0.wp.com/3.bp.blogspot.com/-B1Ot9OOV4cE/WVNxeF7sm3I/AAAAAAAAXOY/7-YyDpopKi8dib1byTBbIaszbQvpKeE4QCLcBGAs/s640/Eq5.png?resize=450%2C84&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
</div>
<div>where y is the effect on group j of treatment τ_1, while μ is the grand mean (i.e. the mean of the whole dataset). From this equation is clear that the effects calculated by the ANOVA are not referred to unit changes in the explanatory variables, but are all related to changes on the grand mean. </div>
</div>
<div></div>
<div></div>
<h2>Examples of ANOVA and ANCOVA in R</h2>
<div>
<div>For this example we are going to use one of the datasets available in the package <span>agridat</span>available in CRAN:</div>
<div></div>
</div>
<pre><code> install.packages(&quot;agridat&quot;)  <br></code></pre>
<p>We also need to include other packages for the examples below. If some of these are not installed in your system please use again the function install.packages (replacing the name within quotation marks according to your needs) to install them.</p>
<pre><code> library(agridat)  <br> library(ggplot2)  <br> library(plotrix)  <br> library(moments)  <br> library(car)  <br> library(fitdistrplus)  <br> library(nlme)  <br> library(multcomp)  <br> library(epade)  <br> library(lme4)  <br></code></pre>
<p>Now we can load the dataset lasrosas.corn, which has more that 3400 observations of corn yield in a field in Argentina, plus several explanatory variables both factorial (or categorical) and continuous.</p>
<pre><code> > dat = lasrosas.corn  <br> > str(dat)  <br> 'data.frame':  3443 obs. of 9 variables:  <br>  $ year : int 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 ...  <br>  $ lat : num -33.1 -33.1 -33.1 -33.1 -33.1 ...  <br>  $ long : num -63.8 -63.8 -63.8 -63.8 -63.8 ...  <br>  $ yield: num 72.1 73.8 77.2 76.3 75.5 ...  <br>  $ nitro: num 132 132 132 132 132 ...  <br>  $ topo : Factor w/ 4 levels &quot;E&quot;,&quot;HT&quot;,&quot;LO&quot;,..: 4 4 4 4 4 4 4 4 4 4 ...  <br>  $ bv  : num 163 170 168 177 171 ...  <br>  $ rep : Factor w/ 3 levels &quot;R1&quot;,&quot;R2&quot;,&quot;R3&quot;: 1 1 1 1 1 1 1 1 1 1 ...  <br>  $ nf  : Factor w/ 6 levels &quot;N0&quot;,&quot;N1&quot;,&quot;N2&quot;,..: 6 6 6 6 6 6 6 6 6 6 ...  <br></code></pre>
<p>Important for the purpose of this tutorial is the target variable yield, which is what we are trying to model, and the explanatory variables: topo (topographic factor), bv (brightness value, which is a proxy for low organic matter content) and nf (factorial nitrogen levels). In addition we have rep, which is the blocking factor. </p>
<h3>Checking Assumptions</h3>
<h2></h2>
<div>
<div>Since we are planning to use an ANOVA we first need to check that our data fits with its assumptions. ANOVA is based on three assumptions:</div>
<div></div>
<ul>
<li><span>Data independence</span></li>
<li>Normality</li>
<li>Equality of variances between groups</li>
<li>Balance design (i.e. all groups have the same number of samples)</li>
</ul>
<div>Let’s see how we can test for them in R. Clearly we are talking about environmental data so the assumption of independence is not met, because data are autocorrelated with distance. Theoretically speaking, for spatial data ANOVA cannot be employed and more robust methods should be employed (e.g. REML); however, over the years it has been widely used for analysis of environmental data and it is accepted by the community. That does not mean that it is the correct method though, and later on in this tutorial we will see the function to perform linear modelling with REML.</div>
<div>The third assumption is the one that is most easy to assess using the function <span>tapply</span>:</div>
<div></div>
</div>
<pre><code> > tapply(dat$yield, INDEX=dat$nf, FUN=var)  <br>    N0    N1    N2    N3    N4    N5   <br> 438.5448 368.8136 372.8698 369.6582 366.5705 405.5653  <br>   <br></code></pre>
<p>In this case we used tapply to calculate the variance of yield for each subgroup (i.e. level of nitrogen). There is some variation between groups but in my opinion it is not substantial. Now we can shift our focus on normality. There are tests to check for normality, but again the ANOVA is flexible (particularly where our dataset is big) and can still produce correct results even when its assumptions are violated up to a certain degree. For this reason, it is good practice to check normality with descriptive analysis alone, without any statistical test. For example, we could start by plotting the histogram of yield: </p>
<pre><code> hist(dat$yield, main=&quot;Histogram of Yield&quot;, xlab=&quot;Yield (quintals/ha)&quot;)  <br></code></pre>
<p>
<div><a rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-AfaVEHwEbyA/WU_F6TuWT5I/AAAAAAAAXLs/KZGdHt_2bMc3Jer4OhjEf3jo1LwD8KyNwCPcBGAYYCw/s1600/Fig1.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i0.wp.com/3.bp.blogspot.com/-AfaVEHwEbyA/WU_F6TuWT5I/AAAAAAAAXLs/KZGdHt_2bMc3Jer4OhjEf3jo1LwD8KyNwCPcBGAYYCw/s400/Fig1.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>By looking at this image it seems that our data are more or less normally distributed. Another plot we could create is the QQplot (<a rel="nofollow" href="http://www.itl.nist.gov/div898/handbook/eda/section3/qqplot.htm" target="_blank">http://www.itl.nist.gov/div898/handbook/eda/section3/qqplot.htm</a>):</div>
<div></div>
<pre><code> qqnorm(dat$yield, main=&quot;QQplot of Yield&quot;)  <br> qqline(dat$yield)  <br>   <br></code></pre>
<p>
<div><a rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/-7b0SRszfW-s/WU_Gk_ygarI/AAAAAAAAXL0/kgXT_YHNbrwo2u6BPnAzhWsdwIZa29zSQCPcBGAYYCw/s1600/Fig2.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i0.wp.com/3.bp.blogspot.com/-7b0SRszfW-s/WU_Gk_ygarI/AAAAAAAAXL0/kgXT_YHNbrwo2u6BPnAzhWsdwIZa29zSQCPcBGAYYCw/s400/Fig2.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div>For normally distributed data the points should all be on the line. This is clearly not the case but again the deviation is not substantial. The final element we can calculate is the skewness of the distribution, with the function <span>skewness</span>in the package <span>moments</span>:</div>
<div></div>
<p>
<pre><code> > skewness(dat$yield)  <br> [1] 0.3875977  <br>   <br></code></pre>
<p>
<div>According to Webster and Oliver (2007) is the skewness is below 0.5, we can consider the deviation from normality not big enough to transform the data. Moreover, according to Witte and Witte (2009) if we have more than 10 samples per group we should not worry too much about violating the assumption of normality or equality of variances.</div>
<div></div>
<div>To see how many samples we have for each level of nitrogen we can use once again the function <span>tapply</span>:</div>
<div></div>
<pre><code> > tapply(dat$yield, INDEX=dat$nf, FUN=length)  <br>  N0 N1 N2 N3 N4 N5   <br> 573 577 571 575 572 575  <br>   <br></code></pre>
<p>
<div>As you can see we have definitely more than 10 samples per group, but our design is not balanced (i.e. some groups have more samples). This implies that the normal ANOVA cannot be used, this is because the standard way of calculating the sum of squares is not appropriate for unbalanced designs (look here for more info: <a rel="nofollow" href="http://goanna.cs.rmit.edu.au/~fscholer/anova.php" target="_blank">http://goanna.cs.rmit.edu.au/~fscholer/anova.php</a>). </div>
<div></div>
<p>
<div>In summary, even though from the descriptive analysis it appears that our data are close to being normal and have equal variance, our design is unbalanced, therefore the normal way of doing ANOVA cannot be used. In other words we cannot function <span>aov</span>for this dataset. However, since this is a tutorial we are still going to start by applying the normal ANOVA with <span>aov</span>.</div>
<div></div>
<h3>ANOVA with <span>aov</span></h3>
<h2></h2>
<div>
<div>The first thing we need to do is think about the hypothesis we would like to test. For example, we could be interested in looking at nitrogen levels and their impact on yield. Let’s start with some plotting to better understand our data:</div>
<div></div>
</div>
<pre><code> means.nf = tapply(dat$yield, INDEX=dat$nf, FUN=mean)  <br> StdErr.nf = tapply(dat$yield, INDEX=dat$nf, FUN= std.error)  <br>   <br> BP = barplot(means.nf, ylim=c(0,max(means.nf)+10))  <br> segments(BP, means.nf - (2*StdErr.nf), BP,  <br>      means.nf + (2*StdErr.nf), lwd = 1.5)  <br>   <br> arrows(BP, means.nf - (2*StdErr.nf), BP,  <br>      means.nf + (2*StdErr.nf), lwd = 1.5, angle = 90,  <br>     code = 3, length = 0.05)  <br>   <br></code></pre>
<p>
<div>This code first uses the function <span>tapply</span> to compute mean and standard error of the mean for yield in each nitrogen group. Then it plots the means as bars and creates error bars using the standard error (please remember that with a normal distribution ± twice the standard error provides a 95% confidence interval around the mean value). The result is the following image:</div>
<div><a rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-5GPnPntd3ks/WU_IdvkjxvI/AAAAAAAAXMA/55bUL82FXoclEgGINrTJ9nkAzz611Q-9wCPcBGAYYCw/s1600/Fig3.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i0.wp.com/1.bp.blogspot.com/-5GPnPntd3ks/WU_IdvkjxvI/AAAAAAAAXMA/55bUL82FXoclEgGINrTJ9nkAzz611Q-9wCPcBGAYYCw/s400/Fig3.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div>By plotting our data we can start figuring out what is the interaction between nitrogen levels and yield. In particular, there is an increase in yield with higher level of nitrogen. However, some of the error bars are overlapping, and this may suggest that their values are not significantly different. For example, by looking at this plot N0 and N1 have error bars very close to overlap, but probably not overlapping, so it may be that N1 provides a significant different from N0. The rest are all probably significantly different from N0. For the rest their interval overlap most of the times, so their differences would probably not be significant.</div>
<div></div>
<div>We could formulate the hypothesis that nitrogen significantly affects yield and that the mean of each subgroup are significantly different. Now we just need to test this hypothesis with a one-way ANOVA:</div>
<div></div>
<pre><code> mod1 = aov(yield ~ nf, data=dat)   <br></code></pre>
<p>
<div>The code above uses the function <span>aov</span> to perform an ANOVA; we can specify to perform a one-way ANOVA simply by including only one factorial term after the tilde (~) sign. We can plot the ANOVA table with the function <span>summary</span>:</div>
<div></div>
<pre><code> > summary(mod1)  <br>        Df Sum Sq Mean Sq F value  Pr(>F)    <br> nf       5  23987  4797  12.4 6.08e-12 ***  <br> Residuals  3437 1330110   387             <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1   <br>   <br></code></pre>
<p>
<div>It is clear from this output that nitrogen significantly affects yield, so we tested our first hypothesis. To test the significance for individual levels of nitrogen we can use the Tukey’s test:</div>
<div></div>
<div></div>
<pre><code> > TukeyHSD(mod1, conf.level=0.95)  <br>  Tukey multiple comparisons of means  <br>   95% family-wise confidence level  <br>   <br> Fit: aov(formula = yield ~ nf, data = dat)  <br>   <br> $nf  <br>       diff    lwr    upr   p adj  <br> N1-N0 3.6434635 0.3353282 6.951599 0.0210713  <br> N2-N0 4.6774357 1.3606516 7.994220 0.0008383  <br> N3-N0 5.3629638 2.0519632 8.673964 0.0000588  <br> N4-N0 7.5901274 4.2747959 10.905459 0.0000000  <br> N5-N0 7.8588595 4.5478589 11.169860 0.0000000  <br> N2-N1 1.0339723 -2.2770686 4.345013 0.9489077  <br> N3-N1 1.7195004 -1.5857469 5.024748 0.6750283  <br> N4-N1 3.9466640 0.6370782 7.256250 0.0089057  <br> N5-N1 4.2153960 0.9101487 7.520643 0.0038074  <br> N3-N2 0.6855281 -2.6283756 3.999432 0.9917341  <br> N4-N2 2.9126917 -0.4055391 6.230923 0.1234409  <br> N5-N2 3.1814238 -0.1324799 6.495327 0.0683500  <br> N4-N3 2.2271636 -1.0852863 5.539614 0.3916824  <br> N5-N3 2.4958957 -0.8122196 5.804011 0.2613027  <br> N5-N4 0.2687320 -3.0437179 3.581182 0.9999099   <br>   <br></code></pre>
<p>
<div>There are significant differences between the control and the rest of the levels of nitrogen, plus other differences between N4 and N5 compared to N1, but nothing else. If you look back at the bar chart we produced before, and look carefully at the overlaps between error bars, you will see that for example N1, N2, and N3 have overlapping error bars, thus they are not significantly different. On the contrary, N1 has no overlaps with either N4 and N5 , which is what we demonstrated in the ANOVA.</div>
<div></div>
<div>The function <span>model.tables</span> provides a quick way to print the table of effects and the table of means:</div>
<div></div>
<pre><code> > model.tables(mod1, type=&quot;effects&quot;)  <br> Tables of effects  <br>   <br>  nf   <br>      N0   N1   N2    N3   N4   N5  <br>    -4.855 -1.212 -0.178  0.5075  2.735  3.003  <br> rep 573.000 577.000 571.000 575.0000 572.000 575.000  <br>   <br></code></pre>
<p>
<div>These values are all referred to the gran mean, which we can simply calculate with the function <span>mean(dat$yield)</span> and it is equal to 69.83. This means that the mean for N0 would be 69.83-4.855 = 64.97. we can verify that with another call to the function <span>model.tables</span>, this time with the option <span>type=”means”</span>:</div>
<div></div>
<div></div>
<pre><code> > model.tables(mod1, type=&quot;means&quot;)  <br> Tables of means  <br> Grand mean  <br>        <br> 69.82831   <br>   <br>  nf   <br>     N0   N1   N2   N3   N4   N5  <br>    64.97 68.62 69.65 70.34 72.56 72.83  <br> rep 573.00 577.00 571.00 575.00 572.00 575.00  <br>   <br></code></pre>
<h3>Linear Model with 1 factor</h3>
<h2></h2>
<div>
<div>The same results can be obtain by fitting a linear model with the function lm, only their interpretation would be different. The assumption for fitting a linear models are again independence (which is always violated with environmental data), and normality.</div>
<div></div>
<div>Let’s look at the code:</div>
<div></div>
</div>
<pre><code> mod2 = lm(yield ~ nf, data=dat)   <br></code></pre>
<p>
<div>This line fits the same model but with the standard linear equation. This become clearer by looking at the summary table:</div>
<div></div>
<div></div>
<pre><code> > summary(mod2)  <br>   <br> Call:  <br> lm(formula = yield ~ nf, data = dat)  <br>   <br> Residuals:  <br>   Min   1Q Median   3Q   Max   <br> -52.313 -15.344 -3.126 13.563 45.337   <br>   <br> Coefficients:  <br>       Estimate Std. Error t value Pr(>|t|)    <br> (Intercept) 64.9729   0.8218 79.060 &lt; 2e-16 ***  <br> nfN1     3.6435   1.1602  3.140  0.0017 **   <br> nfN2     4.6774   1.1632  4.021 5.92e-05 ***  <br> nfN3     5.3630   1.1612  4.618 4.01e-06 ***  <br> nfN4     7.5901   1.1627  6.528 7.65e-11 ***  <br> nfN5     7.8589   1.1612  6.768 1.53e-11 ***  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> Residual standard error: 19.67 on 3437 degrees of freedom  <br> Multiple R-squared: 0.01771,  Adjusted R-squared: 0.01629   <br> F-statistic: 12.4 on 5 and 3437 DF, p-value: 6.075e-12   <br>   <br></code></pre>
<p>
<div>There are several information in this table that we should clarify. First of all it already provides with some descriptive measures for the residuals, from which we can see that their distribution is relatively normal (first and last quartiles have similar but opposite values and the same is true for minimum and maximum). Then we have the table of the coefficients, with the intercept and all the slopes. As you can see the level N0 is not shown in the list; this is called the reference level, which means that all the other are referenced back to it. In other words, the value of the intercept is the mean of nitrogen level 0 (in fact is the same we calculated above 64.97). To calculate the means for the other groups we need to sum the value of the reference level with the slopes. For example N1 is 64.97 + 3.64 = 68.61 (the same calculated from the ANOVA). The p-value and the significance are again in relation to the reference level, meaning for example that N1 is significantly different from N0 (reference level) and the p-value is 0.0017. This is similar to the Tukey’s test we performed above, but it is only valid in relation to N0. We need to change the reference level, and fit another model, to get the same information for other nitrogen levels:</div>
<div></div>
<div></div>
<pre><code> dat$nf = relevel(dat$nf, ref=&quot;N1&quot;)  <br>   <br> mod3 = lm(yield ~ nf, data=dat)  <br> summary(mod3)   <br>   <br></code></pre>
<p>
<div>Now the reference level is N1, so all the results will tell us the effects of nitrogen in relation to N1.</div>
<div></div>
<div></div>
<pre><code> > summary(mod3)  <br>   <br> Call:  <br> lm(formula = yield ~ nf, data = dat)  <br>   <br> Residuals:  <br>   Min   1Q Median   3Q   Max   <br> -52.313 -15.344 -3.126 13.563 45.337   <br>   <br> Coefficients:  <br>       Estimate Std. Error t value Pr(>|t|)    <br> (Intercept)  68.616   0.819 83.784 &lt; 2e-16 ***  <br> nfN0     -3.643   1.160 -3.140 0.001702 **   <br> nfN2      1.034   1.161  0.890 0.373308    <br> nfN3      1.720   1.159  1.483 0.138073    <br> nfN4      3.947   1.161  3.400 0.000681 ***  <br> nfN5      4.215   1.159  3.636 0.000280 ***  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> Residual standard error: 19.67 on 3437 degrees of freedom  <br> Multiple R-squared: 0.01771,  Adjusted R-squared: 0.01629   <br> F-statistic: 12.4 on 5 and 3437 DF, p-value: 6.075e-12  <br>   <br></code></pre>
<p>
<div>For example, we can see that N0 has a lower value compared to N1, and that only N0, N4 and N5 are significantly different from N1, which is what we saw from the bar chart and what we found from the Tukey’s test.</div>
<div></div>
<div></div>
<div>Interpreting the output of the function <span>aov</span> is much easier compare to <span>lm</span>. However, in many cases we can only use the function <span>lm</span> (for example in an ANCOVA where alongside categorical we have continuous explanatory variables) so it is important that we learn how to interpret its summary table.</div>
<div></div>
<div></div>
<div>We can obtain the ANOVA table with the function <span>anova</span>:</div>
<div></div>
<pre><code> > anova(mod2)  <br> Analysis of Variance Table  <br>   <br> Response: yield  <br>       Df Sum Sq Mean Sq F value  Pr(>F)    <br> nf      5  23987 4797.4 12.396 6.075e-12 ***  <br> Residuals 3437 1330110  387.0             <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br></code></pre>
<p>
<div>This uses the type I sum of squares (more info at: http://www.utstat.utoronto.ca/reid/sta442f/2009/typeSS.pdf), which is the standard way and it is not indicated for unbalanced designs. The function <span>Anova</span> in the package <span>car</span> has the option to select which type of sum of squares to calculate and we can specify <span>type=c(“III”)</span>to correct for the unbalanced design:</div>
<div></div>
<div></div>
<pre><code> > Anova(mod2, type=c(&quot;III&quot;))  <br> Anova Table (Type III tests)  <br>   <br> Response: yield  <br>        Sum Sq  Df F value  Pr(>F)    <br> (Intercept) 2418907  1 6250.447 &lt; 2.2e-16 ***  <br> nf      23987  5  12.396 6.075e-12 ***  <br> Residuals  1330110 3437              <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br></code></pre>
<p>
<div>In this example the two results are the same, probably the large sample size helps in this respect. However, for smaller samples this distinction may become important. For this reason, if your design is unbalanced please remember not to use the function <span>aov</span>, but always <span>lm</span>and <span>Anova</span> with the option for type III sum of squares.</div>
<div></div>
<h3>Two-way ANOVA</h3>
<h2></h2>
<div>
<div>So far we have looked on the effect of nitrogen on yield. However, in the dataset we also have a factorial variable named topo, which stands for topographic factor and has 4 levels: W = West slope, HT = Hilltop, E = East slope, LO = Low East. We already formulated an hypothesis about nitrogen, so now we need to formulate an hypothesis about topo as well. Once again we can do that by using the function <span>tapply</span> and a simple bar charts with error bars. Look at the code below:</div>
<div></div>
<div></div>
</div>
<pre><code> means.topo = tapply(dat$yield, INDEX=dat$topo, FUN=mean)  <br> StdErr.topo = tapply(dat$yield, INDEX=dat$topo, FUN= std.error)  <br>   <br> BP = barplot(means.topo, ylim=c(0,max(means.topo)+10))  <br> segments(BP, means.topo - (2*StdErr.topo), BP,  <br>      means.topo + (2*StdErr.topo), lwd = 1.5)  <br>   <br> arrows(BP, means.topo - (2*StdErr.topo), BP,  <br>      means.topo + (2*StdErr.topo), lwd = 1.5, angle = 90,  <br>     code = 3, length = 0.05)  <br>   <br></code></pre>
<p>
<div>Here we are using the same exact approach we used before to formulate an hypothesis about nitrogen. We first calculate mean and standard error of yield for each level of topo, and then plot a bar chart with error bars.</div>
<div></div>
<div>The result is the plot below:</div>
<div><a rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-PV4wiY0A7qo/WU_chC3dViI/AAAAAAAAXMY/J77IFVjtXhYzjPtJrGzsvg84l4RHaF-5ACPcBGAYYCw/s1600/Fig4.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i0.wp.com/1.bp.blogspot.com/-PV4wiY0A7qo/WU_chC3dViI/AAAAAAAAXMY/J77IFVjtXhYzjPtJrGzsvg84l4RHaF-5ACPcBGAYYCw/s400/Fig4.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div>From this plot it is clear that the topographic factor has an effect on yield. In particular, hilltop areas have low yield while the low east corner of the field has high yield. From the error bars we can say with a good level of confidence that probably all the differences will be significant, at least up to an alpha of 95% (significant level, meaning a p-value of 0.05).</div>
<div></div>
<div></div>
<div>We can test this hypothesis with a two way ANOVA, by simply adding the term topo to the equation:</div>
<div></div>
<div></div>
<pre><code> mod1b = aov(yield ~ nf + topo, data=dat)  <br>   <br> summary(mod1b)  <br>   <br>        Df Sum Sq Mean Sq F value Pr(>F)    <br> nf       5 23987  4797  23.21 &lt;2e-16 ***  <br> topo      3 620389 206796 1000.59 &lt;2e-16 ***  <br> Residuals  3434 709721   207            <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br></code></pre>
<p>
<div>From the summary table it is clear that both factors have a significant effect on yield, but just by looking at this it is very difficult to identify clearly which levels are the significant ones. Top do that we need the Tukey’s test:</div>
<div></div>
<div></div>
<pre><code> TukeyHSD(mod1b, conf.level=0.95, which=c(&quot;topo&quot;))  <br>   <br>  Tukey multiple comparisons of means  <br>   95% family-wise confidence level  <br>   <br> Fit: aov(formula = yield ~ nf + topo, data = dat)  <br>   <br> $topo  <br>       diff    lwr    upr p adj  <br> HT-LO -36.240955 -38.052618 -34.429291   0  <br> W-LO -18.168544 -19.857294 -16.479794   0  <br> E-LO  -6.206619 -8.054095 -4.359143   0  <br> W-HT  18.072411 16.326440 19.818381   0  <br> E-HT  30.034335 28.134414 31.934257   0  <br> E-W  11.961925 10.178822 13.745028   0  <br>   <br></code></pre>
<p>
<div>The zero p-values indicate a large significance for each combination, as it was clear from the plot. With the function <span>model.table</span> you can easily obtain a table of means or effects, if you are interested.</div>
<div></div>
<div></div>
<h3>Two-Way ANOVA with Interactions</h3>
<h2></h2>
<div>
<div>One step further we can take to get more insights into our data is add an interaction between nitrogen and topo, and see if this can further narrow down the main sources of yield variation. Once again we need to start our analysis by formulating an hypothesis. Since we are talking about an interaction we are now concern in finding a way to plot yield responses for varying nitrogen level and topographic position, so we need a 3d bar chart. We can do that with the function <span>bar3d.ade</span> from the package <span>epade</span>, so please install this package and load it. </div>
<div></div>
<div>Then please look at the following R code:</div>
<div></div>
</div>
<pre><code> dat$topo = factor(dat$topo,levels(dat$topo)[c(2,4,1,3)])  <br>   <br> means.INT = tapply(dat$yield, INDEX=list(dat$nf, dat$topo), FUN=mean)  <br>   <br> bar3d.ade(means.INT, col=&quot;grey&quot;)  <br>   <br></code></pre>
<p>
<div>The first line is only used to reorder the levels in the factorial variable topo. This is because from the previous plot we clearly saw that HT is the one with the lowest yield, followed by W, E and LO. We are doing this only to make the 3d bar chart more readable.</div>
<div></div>
<div>The next line applies once again the function tapply, this time to calculate the mean of yield for subgroups divided by nitrogen and topographic factors. The result is a matrix that looks like this:</div>
<div></div>
<div></div>
<pre><code> > means.INT  <br>      LO    HT    W    E  <br> N0 81.03027 41.50652 62.08192 75.13902  <br> N1 83.06276 48.33630 65.74627 78.12808  <br> N2 85.06879 48.79830 66.70848 78.92632  <br> N3 85.23255 50.18398 66.16531 78.99210  <br> N4 87.14400 52.12039 70.10682 80.39213  <br> N5 87.94122 51.03138 69.65933 80.55078  <br>   <br></code></pre>
<p>
<div>This can be used directly within the function <span>bar3d.ade</span> to create the 3d bar chart below:</div>
<div></div>
<div><a rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-0_VEwH2P8KI/WVN3DKShKQI/AAAAAAAAXOo/dJjnKnOpfrYQejYpSLZhP-uNSYXwTbncQCLcBGAs/s1600/Fig5.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i1.wp.com/3.bp.blogspot.com/-0_VEwH2P8KI/WVN3DKShKQI/AAAAAAAAXOo/dJjnKnOpfrYQejYpSLZhP-uNSYXwTbncQCLcBGAs/s400/Fig5.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>From this plot we can see two things very clearly: the first is that there is an increase in yield from HT to LO in the topographic factor, the second is that we have again and increase from N0 to N1 in the nitrogen levels. These were all expected since we already noticed them before. What we do not see in these plot is any particular influence from the interaction between topography and nitrogen. For example, if you look at HT, you have an increase in yield from N0 to N5 (expected) and overall the yield is lower than the other bars (again expected). If there was an interaction we would expect this general pattern to change, for example with relatively high yield on the hilltop at high nitrogen level, or very low yield in the low east side with N0. This does not happen and all the bars follow an expected pattern, so we can hypothesise that the interaction will not be significant.</div>
<div></div>
<div>To test that we need to run another ANOVA with an interaction term:</div>
<div></div>
<pre><code> mod1c = aov(yield ~ nf * topo, data=dat)  <br></code></pre>
<p>
<div>This formula test for both main effects and their interaction. To see the significance we can use the summary table:</div>
<div></div>
<pre><code> > summary(mod1c)  <br>        Df Sum Sq Mean Sq F value Pr(>F)    <br> nf       5 23987  4797 23.176 &lt;2e-16 ***  <br> topo      3 620389 206796 999.025 &lt;2e-16 ***  <br> nf:topo    15  1993   133  0.642 0.842    <br> Residuals  3419 707727   207            <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br></code></pre>
<p>
<div>From this we can conclude that our hypothesis was correct and that the interaction has no effect on yield.</div>
<div></div>
<div>We can have a better ides of the interaction effect by using some functions in the package phia:</div>
<div></div>
<div></div>
<pre><code> library(phia)  <br>   <br> plot(interactionMeans(mod1c))  <br></code></pre>
<p>This function plots the effects of the interactions in a 2 by 2 plot, including the standard error of the coefficients, so that we can readily see which overlap:</p>
<div><a rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-2DjcxqOa3nM/WVOC8Tc7BsI/AAAAAAAAXO4/kQyPZ5IVJQM64VQe8FY7Rw6g9r2oijTLwCLcBGAs/s1600/Fig6.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i0.wp.com/3.bp.blogspot.com/-2DjcxqOa3nM/WVOC8Tc7BsI/AAAAAAAAXO4/kQyPZ5IVJQM64VQe8FY7Rw6g9r2oijTLwCLcBGAs/s400/Fig6.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>We already knew from the 3d plot that there is a general increase between N0 and N5 that mainly drives the changes we see in the data. However, from the top-right plot we can see that topo plays a little role between N0 and the other (in fact the black line only slightly overlap with the other), but it has no effect on N1 to N5.</div>
<div></div>
<div>We can look at the numerical break-out of what we see in the plot with another function:</div>
<div></div>
<pre><code> > testInteractions(mod1c)  <br> F Test:   <br> P-value adjustment method: holm  <br>         Value  Df Sum of Sq   F Pr(>F)  <br> N0-N1 : HT-W -3.1654  1    377 1.8230   1  <br> N0-N2 : HT-W -2.6652  1    267 1.2879   1  <br> N0-N3 : HT-W -4.5941  1    784 3.7880   1  <br> N0-N4 : HT-W -2.5890  1    250 1.2072   1  <br> N0-N5 : HT-W -1.9475  1    140 0.6767   1  <br> N1-N2 : HT-W 0.5002  1     9 0.0458   1  <br> N1-N3 : HT-W -1.4286  1    76 0.3694   1  <br> N1-N4 : HT-W 0.5765  1    12 0.0604   1  <br> N1-N5 : HT-W 1.2180  1    55 0.2669   1  <br> N2-N3 : HT-W -1.9289  1    139 0.6711   1  <br> N2-N4 : HT-W 0.0762  1     0 0.0011   1  <br> N2-N5 : HT-W 0.7178  1    19 0.0924   1  <br> N3-N4 : HT-W 2.0051  1    149 0.7204   1  <br></code></pre>
<p>The table is very long so only the first lines are included. However, from this it is clear that the interaction has no effect (p-value of 1), but if it was this function can give us numerous details about the specific effects.</p>
<p>
<h3>ANCOVA with lm</h3>
<h2></h2>
<div>
<div>The Analysis of covariance (ANCOVA) fits a new model where the effects of the treatments (or factorial variables) is corrected for the effect of continuous covariates, for which we can also see the effects on yield.</div>
<div></div>
<div>The code is very similar to what we saw before, and again we can perform an ANCOVA with the <span>lm</span> function; the only difference is that here we are including an additional continuous explanatory variable in the model:</div>
<div></div>
<div></div>
</div>
<pre><code> > mod3 = lm(yield ~ nf + bv, data=dat)  <br> > summary(mod3)  <br>   <br> Call:  <br> lm(formula = yield ~ nf + bv, data = dat)  <br>   <br> Residuals:  <br>   Min   1Q Median   3Q   Max   <br> -78.345 -10.847 -3.314 10.739 56.835   <br>   <br> Coefficients:  <br>        Estimate Std. Error t value Pr(>|t|)    <br> (Intercept) 271.55084  4.99308 54.385 &lt; 2e-16 ***  <br> nfN0     -3.52312  0.95075 -3.706 0.000214 ***  <br> nfN2     1.54761  0.95167  1.626 0.103996    <br> nfN3     2.08006  0.94996  2.190 0.028619 *   <br> nfN4     3.82330  0.95117  4.020 5.96e-05 ***  <br> nfN5     4.47993  0.94994  4.716 2.50e-06 ***  <br> bv      -1.16458  0.02839 -41.015 &lt; 2e-16 ***  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> Residual standard error: 16.12 on 3436 degrees of freedom  <br> Multiple R-squared: 0.3406,  Adjusted R-squared: 0.3394   <br> F-statistic: 295.8 on 6 and 3436 DF, p-value: &lt; 2.2e-16  <br>   <br></code></pre>
<p>
<div>By printing the summary table we can already see some differences compared to the model we only nitrogen as explanatory variable. The first is related to the Adjusted R-squared (which is simply the R-squared corrected for the number of predictors so that it is less affected by overfitting), which in this case is around 0.3. If we look back at the summary table of the model with only nitrogen, the R-squared was only 0.01. This means that by adding the continuous variable bv we are able to massively increase the explanatory power of the model; in fact, this new model is capable of explaining 33% of the variation in yield. Moreover, we can also see that other terms become significant, for example N3. This is because the inclusion of bv changes the entire model and its interpretation becomes less obvious compared to the simple bar chart we plotted at the beginning.</div>
<div></div>
<div></div>
<div>The interpretation of the ANCOVA model is more complex that the one for the one-way ANOVA. In fact, the intercept value has changed and it is not the mean of the reference level N1. This is because the model now changes based on the covariate bv. The slope can be used to assess the relative impact of each term; for example, N0 has a negative impact on yield in relation to its reference level. Therefore, shifting from a nitrogen level N1 to N0 decreases the yield by -3.52, if bv is kept constant. </div>
<div></div>
<div></div>
<div>Take a look at the following code:</div>
<div></div>
<div></div>
<pre><code> > test.set = data.frame(nf=&quot;N1&quot;, bv=150)  <br> > predict(mod3, test.set)  <br>     1    2   <br> 96.86350 38.63438   <br> >   <br> > test.set = data.frame(nf=&quot;N0&quot;, bv=150)  <br> > predict(mod3, test.set)  <br>     1       <br> 93.34037   <br>   <br></code></pre>
<p>
<div>Here we are using the model (mod3) to estimate new values of yield based on set parameters. In the first example we set nf to N1 (reference level) and bv constant at 150. With the function <span>predict</span>we can see estimate these new values using mod3. For N1 and bv equal to 150 the yield is 96.86.</div>
<div></div>
<div>In the second example we did the same but for nitrogen level N0. The result is a yield equal to 93.34, that is a difference of exactly 3.52, which is the slope of the model.</div>
<div></div>
<div>For computing the ANOVA table, we can again use either the function <span>anova</span> (if the design is balanced) or <span>Anova</span> with type III (for unbalanced designs).</div>
<div></div>
<div></div>
<h3>Two-factors and one continuous explanatory variable</h3>
<h2></h2>
<div>
<div>Let’s look now at another example with a slightly more complex model where we include two factorial and one continuous variable. We also include in the model the variable topo. We can check these with the function levels:</div>
<div></div>
</div>
<div></div>
<pre><code> > levels(dat$topo)  <br> [1] &quot;E&quot; &quot;HT&quot; &quot;LO&quot; &quot;W&quot;  <br>   <br></code></pre>
<p>
<div>Please notice that E is the first and therefore is the reference level for this factor. Now let’s fit the model and look at the summary table:</div>
<div></div>
<div></div>
<pre><code> > mod4 = lm(yield ~ nf + bv + topo, data=dat)  <br> >   <br> > summary(mod4)  <br>   <br> Call:  <br> lm(formula = yield ~ nf + bv + topo, data = dat)  <br>   <br> Residuals:  <br>   Min   1Q Median   3Q   Max   <br> -46.394 -10.927 -2.211 10.364 47.338   <br>   <br> Coefficients:  <br>        Estimate Std. Error t value Pr(>|t|)    <br> (Intercept) 171.20921  5.28842 32.374 &lt; 2e-16 ***  <br> nfN0     -3.73225  0.81124 -4.601 4.36e-06 ***  <br> nfN2     1.29704  0.81203  1.597  0.1103    <br> nfN3     1.56499  0.81076  1.930  0.0537 .   <br> nfN4     3.71277  0.81161  4.575 4.94e-06 ***  <br> nfN5     3.88382  0.81091  4.789 1.74e-06 ***  <br> bv      -0.54206  0.03038 -17.845 &lt; 2e-16 ***  <br> topoHT   -24.11882  0.78112 -30.877 &lt; 2e-16 ***  <br> topoLO    3.13643  0.70924  4.422 1.01e-05 ***  <br> topoW    -10.77758  0.66708 -16.156 &lt; 2e-16 ***  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> Residual standard error: 13.75 on 3433 degrees of freedom  <br> Multiple R-squared: 0.5204,  Adjusted R-squared: 0.5191   <br> F-statistic: 413.8 on 9 and 3433 DF, p-value: &lt; 2.2e-16  <br>   <br></code></pre>
<p>
<div>The adjusted R-squared increases again and now we are able to explain around 52% of the variation in yield.</div>
<div></div>
<div></div>
<div>The interpretation is similar to what we said before, the only difference is that here both factors have a reference level. So for example, the effect of topoHT is related to the reference level, which is the one not shown E. So if we change the topographic position from E to HT, while keeping everything else in the model constant (meaning same value of bv and same nitrogen level), we obtain a decrease in yield of 24.12.</div>
<div></div>
<div></div>
<div>Another thing we can see from this table is that the p-value change, and for example N3 becomes less significant. This is probably because when we consider more variables the effect of N3 on yield is explained by other variables, maybe partly bv and partly topo.</div>
<div></div>
<div></div>
<div>One last thing we can check, and this is something we should check every time we perform an ANOVA or a linear model is the normality of the residuals. We already saw that the summary table provides us with some data about the residuals distribution (minimum, first quartile, median, third quartile and maximum) that gives us a good indication of normality, since the distribution is centred around 0. However, we can also use other tools to check this. For example a QQ plot:</div>
<div></div>
<pre><code> RES = residuals(mod4)  <br> qqnorm(RES)  <br> qqline(RES)  <br>   <br></code></pre>
<p>
<div>The function residuals automatically extract the residuals from the model, which can then be used to create the following plot:</div>
<div></div>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-wqJOePymTGk/WVOF7wTloXI/AAAAAAAAXPQ/8Eik1bnTP6cOKcSn3RQRTtdf1aIbfpwlACLcBGAs/s1600/Fig7.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i0.wp.com/4.bp.blogspot.com/-wqJOePymTGk/WVOF7wTloXI/AAAAAAAAXPQ/8Eik1bnTP6cOKcSn3RQRTtdf1aIbfpwlACLcBGAs/s400/Fig7.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>It looks approximately normal, but to have a further confirmation we can use again the function <span>skewness</span>, which returns a value below 0.5, so we can consider this a normal distribution.</div>
<div></div>
<div></div>
<h3>ANCOVA with interactions</h3>
<h2></h2>
<div>
<div>Let’s now add a further layer of complexity by adding an interaction term between bv and topo. Once again we need to formulate an hypothesis before proceeding to test it. Since we are interested in an interaction between a continuous variable (bv) and a factorial variable (topo) on yield, we could try to create scatterplots of yield versus bv, for the different levels in topo. We can easily do that with the package ggplot2:</div>
<div></div>
</div>
<pre><code> qplot(yield, bv, data=dat, geom=&quot;point&quot;, xlab=&quot;Yield&quot;, ylab=&quot;bv&quot;) +  <br> facet_wrap(~topo)+  <br> geom_smooth(method = &quot;lm&quot;, se = TRUE)  <br>   <br></code></pre>
<p>
<div>Explaining every bit of the three lines of code above would require some time and it is beyond the scope of this tutorial. In essence, these lines create a scatterplot yield versus bv for each subgroup of topo and then fit a linear regression line through the points. For more info about the use of ggplot2 please start by looking here: <a rel="nofollow" href="http://www.statmethods.net/advgraphs/ggplot2.html" target="_blank">http://www.statmethods.net/advgraphs/ggplot2.html</a></div>
<div></div>
<div>This create the plot below:</div>
<div></div>
<div><a rel="nofollow" href="https://i0.wp.com/4.bp.blogspot.com/-hem7V4jeALQ/WVOGvXMjO2I/AAAAAAAAXPY/TMkCez-2MPoQXdg4D-HWpqVtI1_4-kuJQCLcBGAs/s1600/Fig8.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i1.wp.com/4.bp.blogspot.com/-hem7V4jeALQ/WVOGvXMjO2I/AAAAAAAAXPY/TMkCez-2MPoQXdg4D-HWpqVtI1_4-kuJQCLcBGAs/s400/Fig8.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>From this plot it is clear that the four lines have different slopes, so the interaction between bv and topo may well be significant and help us further increase the explanatory power of our model. We can test that by adding this interaction:</div>
<div></div>
<pre><code> mod5 = lm(yield ~ nf + bv * topo, data=dat)  <br></code></pre>
<div></div>
<div>We can use the function Anova to check the significance:</div>
<div></div>
<pre><code> > Anova(mod5, type=c(&quot;III&quot;))  <br> Anova Table (Type III tests)  <br>   <br> Response: yield  <br>       Sum Sq  Df F value  Pr(>F)    <br> (Intercept) 20607  1 115.225 &lt; 2.2e-16 ***  <br> nf      23032  5 25.758 &lt; 2.2e-16 ***  <br> bv      5887  1 32.920 1.044e-08 ***  <br> topo     40610  3 75.691 &lt; 2.2e-16 ***  <br> bv:topo   36059  3 67.209 &lt; 2.2e-16 ***  <br> Residuals  613419 3430             <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br></code></pre>
<p>
<div>As you can see this interaction is significant. To check the details we can look at the summary table:</div>
<div></div>
<pre><code> > summary(mod5)  <br>   <br> Call:  <br> lm(formula = yield ~ nf + bv * topo, data = dat)  <br>   <br> Residuals:  <br>   Min   1Q Median   3Q   Max   <br> -46.056 -10.328 -1.516  9.622 50.184   <br>   <br> Coefficients:  <br>        Estimate Std. Error t value Pr(>|t|)    <br> (Intercept) 93.45783  8.70646 10.734 &lt; 2e-16 ***  <br> nfN1     3.96637  0.78898  5.027 5.23e-07 ***  <br> nfN2     5.24313  0.79103  6.628 3.93e-11 ***  <br> nfN3     5.46036  0.79001  6.912 5.68e-12 ***  <br> nfN4     7.52685  0.79048  9.522 &lt; 2e-16 ***  <br> nfN5     7.73646  0.79003  9.793 &lt; 2e-16 ***  <br> bv      -0.27108  0.04725 -5.738 1.04e-08 ***  <br> topoW    88.11105  12.07428  7.297 3.63e-13 ***  <br> topoE    236.12311  17.12941 13.785 &lt; 2e-16 ***  <br> topoLO   -15.76280  17.27191 -0.913  0.3615    <br> bv:topoW   -0.41393  0.06726 -6.154 8.41e-10 ***  <br> bv:topoE   -1.21024  0.09761 -12.399 &lt; 2e-16 ***  <br> bv:topoLO   0.28445  0.10104  2.815  0.0049 **   <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> Residual standard error: 13.37 on 3430 degrees of freedom  <br> Multiple R-squared: 0.547,   Adjusted R-squared: 0.5454   <br> F-statistic: 345.1 on 12 and 3430 DF, p-value: &lt; 2.2e-16  <br>   <br></code></pre>
<p>
<div>The R-squared is a bit higher, which means that we can explain more of the variability in yield by adding the interaction. For the interpretation, once again everything is related to the reference levels in the factors, even the interaction. So for example, bv:topoW tells us that the interaction between bv and topo changes the yield negatively if we change from HT to W, keeping everything else constant.</div>
<div></div>
<div>For information about individual changes we would need to use the model to estimate new data as we did for mod3.</div>
<div></div>
<div></div>
<h3>GLS – For violations of independence</h3>
<div>
<div>As we mentioned, there are certain assumptions we need to check before starting an analysis with linear models. Assumptions about normality and equality of variance can be relaxed, particularly if sample sizes are large enough. However, other assumptions for example balance in the design and independence tend to be stricter, and we need to be careful in violating them.</div>
<div></div>
<div>We can check for independence by looking at the correlation among the coefficient directly in the summary table:</div>
<div></div>
</div>
<pre><code> > summary(mod5, cor=T)  <br> […]  <br> Correlation of Coefficients:  <br>      (Intercept) nfN1 nfN2 nfN3 nfN4 nfN5 bv  topoW topoE topoLO  <br> nfN1   -0.05                                 <br> nfN2   -0.04    0.50                           <br> nfN3   -0.05    0.50 0.50                        <br> nfN4   -0.05    0.50 0.50 0.50                     <br> nfN5   -0.05    0.50 0.50 0.50 0.50                  <br> bv    -1.00    0.01 -0.01 0.01 0.00 0.00               <br> topoW   -0.72    0.00 0.00 0.00 0.00 0.00 0.72            <br> topoE   -0.51    0.01 0.02 0.03 0.01 0.02 0.51 0.37         <br> topoLO  -0.50    -0.02 -0.01 0.02 -0.01 0.02 0.50 0.36 0.26      <br> bv:topoW  0.70    0.00 0.00 0.00 0.00 0.00 -0.70 -1.00 -0.36 -0.35   <br> bv:topoE  0.48    -0.01 -0.02 -0.03 -0.01 -0.02 -0.48 -0.35 -1.00 -0.24   <br> bv:topoLO 0.47    0.02 0.01 -0.02 0.01 -0.02 -0.47 -0.34 -0.24 -1.00   <br>      bv:topoW bv:topoE  <br> nfN1              <br> nfN2              <br> nfN3              <br> nfN4              <br> nfN5              <br> bv               <br> topoW             <br> topoE             <br> topoLO             <br> bv:topoW            <br> bv:topoE  0.34        <br> bv:topoLO 0.33   0.23   <br>   <br></code></pre>
<p>
<div>If we exclude the interaction, which would clearly be correlated with the single covariates, the rest of the coefficients are not much correlated. From this we may conclude that our assumption of independence holds true for this dataset. In cases where from this table we see a relatively high correlation among coefficients, we would need to use a more robust method of maximum likelihood (ML) and residuals maximum likelihood (REML) for computing the coefficients. This can be done with the function <span>gls</span> in the package <span>nlme</span>, using the same syntax as for <span>lm</span>:</div>
<div></div>
<pre><code> mod6 = gls(yield ~ nf + bv * topo, data=dat, method=&quot;REML&quot;)  <br>   <br> Anova(mod6, type=c(&quot;III&quot;))  <br>   <br> summary(mod6)  <br>   <br></code></pre>
<p>
<div>As you can see despite the different function (<span>gls</span> instead of <span>lm</span>), the rest of the syntax is the same as before. We can still use the function <span>Anova</span> to print the ANOVA table and summary to check the individual coefficients.</div>
<div></div>
<div>Moreover, we can also use the function <span>anova</span>to compare the two models (the one from <span>gls</span>and <span>lm</span>) and see which is the best performer:</div>
<div></div>
<pre><code> > anova(mod6, mod5)  <br>    Model df   AIC   BIC  logLik  <br> mod6   1 14 27651.21 27737.18 -13811.61  <br> mod5   2 14 27651.21 27737.18 -13811.61  <br>   <br></code></pre>
<p>
<div>The indexes AIC, BIC and logLik are all used to check the accuracy of the model and should be as low as possible. For more info please look at the appendix about assessing the accuracy of our model. </div>
<div></div>
<div></div>
<div></div>
<h2>Linear Mixed-Effects Models</h2>
<div>
<div>This class of models are used to account for more than one source of random variation. For example, assume we have a dataset where again we are trying to model yield as a function of nitrogen level. However, this time the data were collected in many different farms. In this case would need to be consider a cluster and the model would need to take this clustering into account. Another common set of experiments where linear mixed-effects models are used is repeated measures where time provide an additional source of correlation between measures. For these models we do not need to worry about the assumptions from previous models, since these are very robust against all of them. For example, for unbalanced design with blocking, probably these methods should be used instead of the standard ANOVA.</div>
<div>
<div>At the beginning on this tutorial we explored the equation that supports linear model:</div>
<div></div>
<div><a rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/--lzwHrvVjz8/WVNw_gZyFCI/AAAAAAAAXOc/6ciw0sMZQ3Ew52VHkiK_R_Rq74-hqt7FQCEwYBhgL/s1600/Eq1.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i0.wp.com/3.bp.blogspot.com/--lzwHrvVjz8/WVNw_gZyFCI/AAAAAAAAXOc/6ciw0sMZQ3Ew52VHkiK_R_Rq74-hqt7FQCEwYBhgL/s640/Eq1.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>This equation can be seen as split into two components, the fixed and random effects. For fixed effect we refer to those variables we are using to explain the model. These may be factorial (in ANOVA), continuous or a mixed of the two (ANCOVA) and they can also be the blocks used in our design. The other component in the equation is the random effect, which provides a level of uncertainty that it is difficult to account in the model. For example, when we work with yield we might see differences between plants grown from similar soils and conditions. These may be related to the seeds or to other factors and are part of the within-subject variation that we cannot explain.</div>
<div></div>
<div>There are times however where in the data there are multiple sources of random variation. For example, data may be clustered in separate field or separate farms. This will provide an additional source of random variation that needs to be taken into account in the model. To do so the standard equation can be amended in the following way:</div>
<div></div>
<div><a rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-st-1bJKWNGE/WVOJsdVpU3I/AAAAAAAAXPk/8dORgWAEb6Q5S2ZyjmXFKf2eIs_ZjDzkwCLcBGAs/s1600/Eq6.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i1.wp.com/3.bp.blogspot.com/-st-1bJKWNGE/WVOJsdVpU3I/AAAAAAAAXPk/8dORgWAEb6Q5S2ZyjmXFKf2eIs_ZjDzkwCLcBGAs/s640/Eq6.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>This is referred to as a random intercept model, where the random variation is split into a cluster specific variation <i>u</i> and the standard error term. A more complex form, that is normally used for repeated measures is the random slope and intercept model:</div>
<div></div>
<div><a rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-YUy5HOqbfQ0/WVOJz-id0lI/AAAAAAAAXPo/GKiItdls6FslNHw23EtyiZAGu8AAzklEQCLcBGAs/s1600/Eq7.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i0.wp.com/1.bp.blogspot.com/-YUy5HOqbfQ0/WVOJz-id0lI/AAAAAAAAXPo/GKiItdls6FslNHw23EtyiZAGu8AAzklEQCLcBGAs/s640/Eq7.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>Where we add a new source of random variation <i>v</i> related to time <i>T</i>.</div>
<div></div>
<div></div>
<h3>Random Intercept Model for Clustered Data</h3>
<div>
<div>Just to explain the syntax to use linear mixed-effects model in R for cluster data, we will assume that the factorial variable rep in our dataset describe some clusters in the data. To fit a mixed-effects model we are going to use the function <span>lme</span> from the package <span>nlme</span>. This function can work with unbalanced designs:</div>
<div></div>
</div>
</div>
</div>
<pre><code> lme1 = lme(yield ~ nf + bv * topo, random= ~1|rep, data=dat)  <br></code></pre>
<p>
<div>The syntax is very similar to all the models we fitted before, with a general formula describing our target variable yield and all the treatments, which are the fixed effects of the model. Then we have the option random, which allows us to include an additional random component for the clustering factor rep. In this case the ~1 indicates that the random effect will be associated with the intercept.</div>
<div></div>
<div></div>
<div>Once again we can use the function summary to explore our results:</div>
<div></div>
<div></div>
<pre><code> > summary(lme1)  <br> Linear mixed-effects model fit by REML  <br>  Data: dat   <br>     AIC   BIC  logLik  <br>  27648.36 27740.46 -13809.18  <br>   <br> Random effects:  <br>  Formula: ~1 | rep  <br>     (Intercept) Residual  <br> StdDev:  0.798407 13.3573  <br>   <br> Fixed effects: yield ~ nf + bv * topo   <br>         Value Std.Error  DF  t-value p-value  <br> (Intercept) 327.3304 14.782524 3428 22.143068    0  <br> nfN1      3.9643 0.788049 3428  5.030561    0  <br> nfN2      5.2340 0.790104 3428  6.624449    0  <br> nfN3      5.4498 0.789084 3428  6.906496    0  <br> nfN4      7.5286 0.789551 3428  9.535320    0  <br> nfN5      7.7254 0.789111 3428  9.789976    0  <br> bv      -1.4685 0.085507 3428 -17.173569    0  <br> topoHT   -233.3675 17.143956 3428 -13.612232    0  <br> topoLO   -251.9750 20.967003 3428 -12.017693    0  <br> topoW    -146.4066 16.968453 3428 -8.628162    0  <br> bv:topoHT   1.1945 0.097696 3428 12.226279    0  <br> bv:topoLO   1.4961 0.123424 3428 12.121624    0  <br> bv:topoW    0.7873 0.097865 3428  8.044485    0  <br>   <br></code></pre>
<p>
<div>We can also use the function <span>Anova</span>to display the ANOVA table:</div>
<div></div>
<pre><code> > Anova(lme2, type=c(&quot;III&quot;))  <br> Analysis of Deviance Table (Type III tests)  <br>   <br> Response: yield  <br>        Chisq Df Pr(>Chisq)    <br> (Intercept) 752.25 1 &lt; 2.2e-16 ***  <br> nf     155.57 5 &lt; 2.2e-16 ***  <br> bv     291.49 1 &lt; 2.2e-16 ***  <br> topo    236.52 3 &lt; 2.2e-16 ***  <br> year    797.13 1 &lt; 2.2e-16 ***  <br> bv:topo   210.38 3 &lt; 2.2e-16 ***  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br></code></pre>
<p>
<div>We might be interested in understanding if fitting a more complex model provides any advantage in terms of accuracy, compared with a model where no additional random effect is included. To do so we can compare this new model with mod6, which we created with the <span>gls</span>function and includes the same treatment structure.  We can do that with the function anova, specifying both models:</div>
<div></div>
<pre><code> > anova(lme1, mod6)  <br>    Model df   AIC   BIC  logLik  Test L.Ratio p-value  <br> lme1   1 15 27648.36 27740.46 -13809.18              <br> mod6   2 14 27651.21 27737.18 -13811.61 1 vs 2 4.857329 0.0275  <br>   <br></code></pre>
<p>
<div>As you can see there is a decrease in AIC for the model fitted with <span>lme</span>, and the difference is significant (p-value below 0.05). Therefore this new model where clustering is accounted for is better than the one without an additional random effect, even though only slightly. In this case we would need to decide if fitting a more complex model (which is probably more difficult to explain to readers) is the best way.</div>
<div></div>
<div></div>
<h3>Random Intercept and Slope for repeated measures</h3>
<div>
<div>If we collected data at several time steps we are looking at a repeated measures analysis. The code to create such a model is the following:</div>
<div></div>
</div>
<pre><code> lme2 = lme(yield ~ nf + bv * topo + year, random= ~year|rep, data=dat)  <br>   <br> summary(lme2)  <br>   <br> Anova(lme2, type=c(&quot;III&quot;))  <br>   <br></code></pre>
<p>
<div>The syntax is very similar to what we wrote before, except that now the random component includes both time and clusters. Again we can use <span>summary</span> and <span>Anova</span>to get more info about the model. We can also use again the function <span>anova</span> to compare this with the previous model:</div>
<div></div>
<pre><code> > anova(lme1, lme2)  <br>    Model df   AIC   BIC  logLik  Test L.Ratio p-value  <br> lme1   1 15 27648.36 27740.46 -13809.18              <br> lme2   2 18 26938.83 27049.35 -13451.42 1 vs 2 715.5247 &lt;.0001  <br> Warning message:  <br> In anova.lme(lme1, lme2) :  <br>  fitted objects with different fixed effects. REML comparisons are not meaningful.  <br>   <br></code></pre>
<p>
<div>From this output it is clear that the new model is better that the one before and their difference in highly significant.</div>
<div></div>
<div>We can extract only the effects for the random components using the function ranef:</div>
<div></div>
<pre><code> > ranef(lme2)  <br>   (Intercept)     year  <br> R1 -0.3468601 -1.189799e-07  <br> R2 -0.5681688 -1.973702e-07  <br> R3  0.9150289 3.163501e-07   <br>   <br></code></pre>
<p>
<div>This tells us the changes in yield for each cluster and time step. We can also do the same for the fixed effects, and this will return the coefficients of the model:</div>
<div></div>
<pre><code> > fixef(lme2)  <br>  (Intercept)     nfN1     nfN2     nfN3     nfN4   <br> -1.133614e+04 3.918006e+00 5.132136e+00 5.368513e+00 7.464542e+00   <br>      nfN5      bv    topoHT    topoLO     topoW   <br>  7.639337e+00 -1.318391e+00 -2.049979e+02 -2.321431e+02 -1.136168e+02   <br>      year   bv:topoHT   bv:topoLO   bv:topoW   <br>  5.818826e+00 1.027686e+00 1.383705e+00 5.998379e-01   <br>   <br></code></pre>
<p>
<div>To have an idea of their confidence interval we can use the function intervals:</div>
<div></div>
<pre><code> > intervals(lme2, which = &quot;fixed&quot;)  <br> Approximate 95% confidence intervals  <br>   <br>  Fixed effects:  <br>           lower     est.     upper  <br> (Intercept) -1.214651e+04 -1.133614e+04 -1.052576e+04  <br> nfN1     2.526139e+00 3.918006e+00 5.309873e+00  <br> nfN2     3.736625e+00 5.132136e+00 6.527648e+00  <br> nfN3     3.974809e+00 5.368513e+00 6.762216e+00  <br> nfN4     6.070018e+00 7.464542e+00 8.859065e+00  <br> nfN5     6.245584e+00 7.639337e+00 9.033089e+00  <br> bv     -1.469793e+00 -1.318391e+00 -1.166989e+00  <br> topoHT   -2.353450e+02 -2.049979e+02 -1.746508e+02  <br> topoLO   -2.692026e+02 -2.321431e+02 -1.950836e+02  <br> topoW    -1.436741e+02 -1.136168e+02 -8.355954e+01  <br> year     5.414742e+00 5.818826e+00 6.222911e+00  <br> bv:topoHT  8.547273e-01 1.027686e+00 1.200644e+00  <br> bv:topoLO  1.165563e+00 1.383705e+00 1.601846e+00  <br> bv:topoW   4.264933e-01 5.998379e-01 7.731826e-01  <br> attr(,&quot;label&quot;)  <br> [1] &quot;Fixed effects:&quot;   <br>   <br></code></pre>
<h2>Dealing with non-normal data – Generalized Linear Models</h2>
<div>
<div>As you remember, when we first introduced the simple linear model we defined a set of assumptions that need to be met to apply this model. We already talked about methods to deal with deviations from the assumption of independence, equality of variances and balanced designs and the fact that, particularly if our dataset is large, we may reach robust results even if our data are not perfectly normal. However, there are datasets for which the target variable has a completely different distribution from the normal, in these cases we need to change our modelling method and employ generalized linear models. Common scenarios where this model should be considered are for example researchers where the variable of interest is binary, for example presence or absence of a species, or where we are interested in modelling counts, for example the number of insects present in a particular location. In these cases, where the target variable is not continuous but rather discrete or categorical, the assumption of normality is usually not met. In this section we will focus on the two scenarios mentioned above, but GLM can be used to deal with data distributed in many different ways, and we will introduce how to deal with more general cases.</div>
<div></div>
<div></div>
<h3>Count Data</h3>
</div>
<div>
<div>Data of this type, i.e. counts or rates, are characterized by the fact that their lower bound is always zero. This does not fit well with a normal linear model, where the regression line may well estimate negative values. For this type of variable we can employ a Poisson Regression, which fits the following model:</div>
<div><a rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-7q2Q5nEaijk/WVOM2GhetxI/AAAAAAAAXP0/QErolvs0FSQU_gRuSz-oIxatpDMz3hZFACLcBGAs/s1600/Eq8.png?ssl=1" target="_blank"><img border="0" data-original-height="79" src="https://i1.wp.com/2.bp.blogspot.com/-7q2Q5nEaijk/WVOM2GhetxI/AAAAAAAAXP0/QErolvs0FSQU_gRuSz-oIxatpDMz3hZFACLcBGAs/s640/Eq8.png?resize=450%2C79&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>As you can see the equation is very similar to the standard linear model, the difference is that to insure that all Y are positive (since we cannot have negative values for count data) we are estimating the log of <i>Y</i>.</div>
<div></div>
<div>In R fitting this model is very easy. For this example we are going to use another dataset available in the package <span>agridat</span>, named <span>beall.webworms</span>, which represents counts of webworms in a beet field, with insecticide treatments:</div>
<div></div>
</div>
<pre><code> > dat = beall.webworms  <br> > str(dat)  <br> 'data.frame':  1300 obs. of 7 variables:  <br>  $ row : int 1 2 3 4 5 6 7 8 9 10 ...  <br>  $ col : int 1 1 1 1 1 1 1 1 1 1 ...  <br>  $ y  : int 1 0 1 3 6 0 2 2 1 3 ...  <br>  $ block: Factor w/ 13 levels &quot;B1&quot;,&quot;B10&quot;,&quot;B11&quot;,..: 1 1 1 1 1 6 6 6 6 6 ...  <br>  $ trt : Factor w/ 4 levels &quot;T1&quot;,&quot;T2&quot;,&quot;T3&quot;,..: 1 1 1 1 1 1 1 1 1 1 ...  <br>  $ spray: Factor w/ 2 levels &quot;N&quot;,&quot;Y&quot;: 1 1 1 1 1 1 1 1 1 1 ...  <br>  $ lead : Factor w/ 2 levels &quot;N&quot;,&quot;Y&quot;: 1 1 1 1 1 1 1 1 1 1 ...  <br>   <br></code></pre>
<p>
<div>We can check the distribution of our data with the function <span>hist</span>:</div>
<div></div>
<pre><code> hist(dat$y, main=&quot;Histogram of Worm Count&quot;, xlab=&quot;Number of Worms&quot;)  <br></code></pre>
<p>
<div><a rel="nofollow" href="https://i0.wp.com/4.bp.blogspot.com/-d-sUDOe3Xv0/WVONPkxmY3I/AAAAAAAAXP4/8KOtQckbBu0P2hT18aKPwxGKETJY9ZemACLcBGAs/s1600/Fig9.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i2.wp.com/4.bp.blogspot.com/-d-sUDOe3Xv0/WVONPkxmY3I/AAAAAAAAXP4/8KOtQckbBu0P2hT18aKPwxGKETJY9ZemACLcBGAs/s400/Fig9.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>We are going to fit a simple model first to see how to interpret its results, and then compare it with a more complex model:</div>
<div></div>
<pre><code> pois.mod = glm(y ~ trt, data=dat, family=c(&quot;poisson&quot;))  <br></code></pre>
<p>
<div>Once again the function summary will show some useful details about this model:</div>
<div></div>
<pre><code> > summary(pois.mod)  <br>   <br> Call:  <br> glm(formula = y ~ trt, family = c(&quot;poisson&quot;), data = dat)  <br>   <br> Deviance Residuals:   <br>   Min    1Q  Median    3Q   Max   <br> -1.6733 -1.0046 -0.9081  0.6141  4.2771   <br>   <br> Coefficients:  <br>       Estimate Std. Error z value Pr(>|z|)    <br> (Intercept) 0.33647  0.04688  7.177 7.12e-13 ***  <br> trtT2    -1.02043  0.09108 -11.204 &lt; 2e-16 ***  <br> trtT3    -0.49628  0.07621 -6.512 7.41e-11 ***  <br> trtT4    -1.22246  0.09829 -12.438 &lt; 2e-16 ***  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> (Dispersion parameter for poisson family taken to be 1)  <br>   <br>   Null deviance: 1955.9 on 1299 degrees of freedom  <br> Residual deviance: 1720.4 on 1296 degrees of freedom  <br> AIC: 3125.5  <br>   <br> Number of Fisher Scoring iterations: 6  <br>   <br></code></pre>
<p>
<div>The first valuable information is related to the residuals of the model, which should be symmetrical as for any normal linear model. From this output we can see that minimum and maximum, as well as the first and third quartiles, are similar, so this assumption is confirmed. Then we can see that the variable trt (i.e. treatment factor) is highly significant for the model, with very low p-values. Its effect are all negative and referred to the first level T1, meaning for example that a change from T1 to T2 will decrease the count by 1.02. We can check this effect by estimating changes between T1 and T2 with the function <span>predict</span>, and the option <span>newdata</span>:</div>
<div></div>
<pre><code> > predict(pois.mod, newdata=data.frame(trt=c(&quot;T1&quot;,&quot;T2&quot;)))  <br>      1     2   <br>  0.3364722 -0.6839588  <br>   <br></code></pre>
<p>
<div>Another important piece of information are the Null and Residuals deviances, which allow us to compute the probability that this model is better than the Null hypothesis, which states that a constant (with no variables) model would be better. We can compute the p-value of the model with the following line:</div>
<div></div>
<pre><code> > 1-pchisq(deviance(pois.mod), df.residual(pois.mod))  <br> [1] 1.709743e-14  <br>   <br></code></pre>
<p>
<div>This p-value is very low, meaning that this model fits the data well. However, it may not be the best possible model, and we can use the AIC parameter to compare it to other models. For example, we could include more variables:</div>
<div></div>
<pre><code> pois.mod2 = glm(y ~ block + spray*lead, data=dat, family=c(&quot;poisson&quot;))  <br></code></pre>
<p>
<div>How does this new model compare with the previous?</div>
<div></div>
<pre><code> > AIC(pois.mod, pois.mod2)  <br>      df   AIC  <br> pois.mod  4 3125.478  <br> pois.mod2 16 3027.438  <br>   <br></code></pre>
<p>
<div>As you can see the second model has a lower AIC, meaning that fits the data better than the first.</div>
<div></div>
<div>One of the assumptions of the Poisson distribution is that its mean and variance have the same value. We can check by simply comparing mean and variance of our data:</div>
<div></div>
<pre><code> > mean(dat$y)  <br> [1] 0.7923077  <br> > var(dat$y)  <br> [1] 1.290164  <br>   <br></code></pre>
<p>
<div>In cases such as this when the variance is larger than the mean (in this case we talk about overdispersed count data) we should employ different methods, for example a quasipoisson distribution:</div>
<div></div>
<pre><code> pois.mod3 = glm(y ~ trt, data=dat, family=c(&quot;quasipoisson&quot;))  <br></code></pre>
<p>
<div>The summary function provides us with the dispersion parameter, which for a Poisson distribution should be 1:</div>
<div></div>
<pre><code> > summary(pois.mod3)  <br>   <br> Call:  <br> glm(formula = y ~ trt, family = c(&quot;quasipoisson&quot;), data = dat)  <br>   <br> Deviance Residuals:   <br>   Min    1Q  Median    3Q   Max   <br> -1.6733 -1.0046 -0.9081  0.6141  4.2771   <br>   <br> Coefficients:  <br>       Estimate Std. Error t value Pr(>|t|)    <br> (Intercept) 0.33647  0.05457  6.166 9.32e-10 ***  <br> trtT2    -1.02043  0.10601 -9.626 &lt; 2e-16 ***  <br> trtT3    -0.49628  0.08870 -5.595 2.69e-08 ***  <br> trtT4    -1.22246  0.11440 -10.686 &lt; 2e-16 ***  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> (Dispersion parameter for quasipoisson family taken to be 1.35472)  <br>   <br>   Null deviance: 1955.9 on 1299 degrees of freedom  <br> Residual deviance: 1720.4 on 1296 degrees of freedom  <br> AIC: NA  <br>   <br> Number of Fisher Scoring iterations: 6  <br>   <br></code></pre>
<p>
<div>Since the dispersion parameter is 1.35, we can conclude that our data are not terrible dispersed, so maybe a Poisson regression would still be appropriate for this dataset.</div>
<div></div>
<div>However, there are cases where the data are very overdispersed. In those cases, when we see that the distribution has lots of peaks we need to employ the negative binomial regression, with the function glm.nb available in the package MASS:</div>
<div></div>
<pre><code> library(MASS)  <br>   <br> NB.mod1 = glm.nb(y ~ trt, data=dat)  <br></code></pre>
<h3>Logistic Regression</h3>
<div>
<div>Another popular for of regression that can be tackled with GLM is the logistic regression, where the variable of interest is binary (0 and 1, presence and absence or any other binary outcome). In this case the regression model takes the following equation:</div>
<div></div>
<div><a rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-MMHaVdfYz8E/WVOQtOwNsjI/AAAAAAAAXQE/8nKApuboGCAyk15CqypjlO27RlYnf9spgCLcBGAs/s1600/Eq9.png?ssl=1" target="_blank"><img border="0" data-original-height="131" src="https://i2.wp.com/1.bp.blogspot.com/-MMHaVdfYz8E/WVOQtOwNsjI/AAAAAAAAXQE/8nKApuboGCAyk15CqypjlO27RlYnf9spgCLcBGAs/s640/Eq9.png?resize=450%2C131&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div>Again, the equation is identical to the standard linear model, but what we are computing from this model is the log of the probability that one of the two outcomes will occur.</div>
<div></div>
<div>For this example we are going to use another dataset available in the package <span>agridat</span>called <span>johnson.blight</span>, where the binary variable of interest is the presence or absence of blight (either 0 or 1) in potatoes:</div>
<div></div>
</div>
<pre><code> > dat = johnson.blight  <br> > str(dat)  <br> 'data.frame':  25 obs. of 6 variables:  <br>  $ year  : int 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 ...  <br>  $ area  : int 0 0 0 0 50 810 120 40 0 0 ...  <br>  $ blight : int 0 0 0 0 1 1 1 1 0 0 ...  <br>  $ rain.am : int 8 9 9 6 16 10 12 10 11 8 ...  <br>  $ rain.ja : int 1 4 6 1 6 7 12 4 10 9 ...  <br>  $ precip.m: num 5.84 6.86 47.29 8.89 7.37 ...  <br>   <br></code></pre>
<p>
<div>In R fitting this model is very easy:</div>
<div></div>
<pre><code> mod9 = glm(blight ~ rain.am, data=dat, family=binomial)  <br></code></pre>
<p>
<div>we are now using the binomial distribution for a logistic regression. To check the model we can rely again on summary:</div>
<div></div>
<pre><code> > summary(mod9)  <br>   <br> Call:  <br> glm(formula = blight ~ rain.am, family = binomial, data = dat)  <br>   <br> Deviance Residuals:   <br>   Min    1Q  Median    3Q   Max   <br> -1.9395 -0.6605 -0.3517  1.0228  1.6048   <br>   <br> Coefficients:  <br>       Estimate Std. Error z value Pr(>|z|)   <br> (Intercept) -4.9854   2.0720 -2.406  0.0161 *  <br> rain.am    0.4467   0.1860  2.402  0.0163 *  <br> ---  <br> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  <br>   <br> (Dispersion parameter for binomial family taken to be 1)  <br>   <br>   Null deviance: 34.617 on 24 degrees of freedom  <br> Residual deviance: 24.782 on 23 degrees of freedom  <br> AIC: 28.782  <br>   <br> Number of Fisher Scoring iterations: 5  <br>   <br></code></pre>
<p>
<div>This table is very similar to the one created for count data, so a lot of the discussion above can be used here. The main difference is in the way we can interpret the coefficients, because we need to remember that here we are calculating the logit function of the probability, so 0.4467 (coefficient for rain.am) is not the actual probability associated with an increase in rain. However, what we can say by just looking at the coefficients is that rain has a positive effect on blight, meaning that more rain increases the chances of finding blight in potatoes.</div>
<div></div>
<div>To estimate probabilities we need to use the function predict:</div>
<div></div>
<pre><code> > predict(mod9, type=&quot;response&quot;)  <br>      1     2     3     4     5     6     7   <br> 0.19598032 0.27590141 0.27590141 0.09070472 0.89680283 0.37328295 0.59273722   <br>      8     9     10     11     12     13     14   <br> 0.37328295 0.48214935 0.19598032 0.69466455 0.19598032 0.84754431 0.27590141   <br>     15     16     17     18     19     20     21   <br> 0.93143346 0.05998586 0.19598032 0.05998586 0.84754431 0.59273722 0.59273722   <br>     22     23     24     25   <br> 0.48214935 0.59273722 0.98109229 0.89680283  <br>   <br></code></pre>
<p>
<div>This calculates the probability associated with the values of rain in the dataset. To know the probability associated with new values of rain we can again use predict with the option newdata:</div>
<div></div>
<pre><code> >predict(mod9,newdata=data.frame(rain.am=c(15)),type=&quot;response&quot;)   <br>     1   <br> 0.8475443  <br>   <br></code></pre>
<p>
<div>This tells us that when rain is equal to 15 we have 84% chances of finding blight (i.e. chances of finding 1) in potatoes.</div>
<div></div>
<div>To assess the accuracy of the model we can use two approaches, the first is based on the deviances listed in the summary. The Residual deviance compares this model with the one that fits the data perfectly. So if we calculate the following p-value (using the deviance and df in the summary table for residuals):</div>
<div></div>
<pre><code> > 1-pchisq(24.782, 23)  <br> [1] 0.3616226  <br>   <br></code></pre>
<p>
<div>We see that because it is higher than 0.05 we cannot reject that this model fits the data as well as the perfect model, therefore our model seems to be good.</div>
<div></div>
<div>We can repeat the same procedure for the Null hypothesis, which again tests whether this model fits the data well:</div>
<div></div>
<pre><code> > 1-pchisq(34.617, 24)  <br> [1] 0.07428544  <br>   <br></code></pre>
<p>
<div>Since this is again not significant it suggests (contrary to what we obtained before) that this model is not very good.</div>
<div></div>
<div>An additional and probably easier to understand way to assess the accuracy of a logistic model is calculating the pseudo R2, which can be done by installing the package <span>pscl</span>:</div>
<div></div>
<pre><code> install.packages(&quot;pscl&quot;)  <br> library(pscl)  <br>   <br></code></pre>
<p>
<div>Now we can run the following function:</div>
<div></div>
<pre><code> > pR2(mod9)  <br>     llh   llhNull     G2  McFadden    r2ML    r2CU   <br>   -12.3910108 -17.3086742  9.8353268  0.2841155  0.3252500  0.4338984  <br>   <br></code></pre>
<p>
<div>From this we can see that our model explains around 30-40% of the variation in blight, which is not particularly good. We can use this index to compare models, as we did for AIC.</div>
<div></div>
<div></div>
<h3>Dealing with other distributions and transformation</h3>
<div>
<div>As mentioned, GLM can be used for fitting linear models not only in the two scenarios we described above, but in any occasion where data do not comply with the normality assumption. For example, we can look at another dataset available in agridat, where the variable of interest is slightly non-normal:</div>
<div></div>
</div>
<pre><code> > dat = hughes.grapes  <br> > str(dat)  <br> 'data.frame':  270 obs. of 6 variables:  <br>  $ block  : Factor w/ 3 levels &quot;B1&quot;,&quot;B2&quot;,&quot;B3&quot;: 1 1 1 1 1 1 1 1 1 1 ...  <br>  $ trt   : Factor w/ 6 levels &quot;T1&quot;,&quot;T2&quot;,&quot;T3&quot;,..: 1 2 3 4 5 6 1 2 3 4 ...  <br>  $ vine  : Factor w/ 3 levels &quot;V1&quot;,&quot;V2&quot;,&quot;V3&quot;: 1 1 1 1 1 1 1 1 1 1 ...  <br>  $ shoot  : Factor w/ 5 levels &quot;S1&quot;,&quot;S2&quot;,&quot;S3&quot;,..: 1 1 1 1 1 1 2 2 2 2 ...  <br>  $ diseased: int 1 2 0 0 3 0 7 0 1 0 ...  <br>  $ total  : int 14 12 12 13 8 9 8 10 14 10 ...  <br>   <br></code></pre>
<p>
<div>The variable total presents a skewness of 0.73, which means that probably with a transformation it should fit with a normal distribution. However, for the sake of the discussion we will assume it cannot be transformed. So now our problem is identify the best distribution for our data, to do so we can use the function descdist in the package fitdistrplus we already loaded:</div>
<div></div>
<pre><code> descdist(dat$total, discrete = FALSE)  <br></code></pre>
<p>
<div>this returns the following plot:</div>
<div></div>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-jguhE3Xltvk/WVOg3uPDklI/AAAAAAAAXQU/-tZlDEwsD2QSeuWO4R9EOt2vC1qSq4_qwCLcBGAs/s1600/Fig10.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i1.wp.com/4.bp.blogspot.com/-jguhE3Xltvk/WVOg3uPDklI/AAAAAAAAXQU/-tZlDEwsD2QSeuWO4R9EOt2vC1qSq4_qwCLcBGAs/s400/Fig10.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>Where we can see that our data (blue dot) are close to normal and maybe closer to a gamma distribution. So now we can further check this using another function from the same package:</div>
<div></div>
<pre><code> plot(fitdist(dat$total,distr=&quot;gamma&quot;))  <br></code></pre>
<p>
<div>which creates the following plot:</div>
<div></div>
<div><a rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-24SmeOxiAh4/WVOhKAx6nyI/AAAAAAAAXQY/df2GW1rBeicJwzKoPevnC3_hGa1YjjTpACLcBGAs/s1600/Fig11.png?ssl=1" target="_blank"><img border="0" data-original-height="671" src="https://i2.wp.com/2.bp.blogspot.com/-24SmeOxiAh4/WVOhKAx6nyI/AAAAAAAAXQY/df2GW1rBeicJwzKoPevnC3_hGa1YjjTpACLcBGAs/s400/Fig11.png?resize=450%2C671&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>From this we can see that in fact our data seem to be close to a gamma distribution, so now we can proceed with modelling:</div>
<div></div>
<pre><code> mod8 = glm(total ~ trt * vine, data=dat, family=Gamma(link=identity))  <br></code></pre>
<p>
<div>in the option family we included the name of the distribution, plus a link function that is used if we want to transform our data (in this case the function identity is for leaving data not transformed).</div>
<div></div>
<div>This is what we do to model other types of data that do not fit with a normal distribution. Other possible families supported by GLM are:</div>
<div></div>
<div>binomial, gaussian, Gamma, inverse.gaussian, poisson, quasi, quasibinomial, quasipoisson</div>
<div></div>
<div>Other possible link functions (which availability depends on the family) are:</div>
<div></div>
<div>logit, probit, cauchit, cloglog, identity, log, sqrt, 1/mu^2, inverse.</div>
<div></div>
<div></div>
<div></div>
<h3>Generalized Linear Mixed Effects models</h3>
<div>
<div>As linear model, linear mixed effects model need to comply with normality. If our data deviates too much we need to apply the generalized form, which is available in the package lme4:</div>
<div></div>
</div>
<pre><code> install.packages(&quot;lme4&quot;)  <br> library(lme4)  <br></code></pre>
<p>
<div>For this example we will use again the dataset <span>johnson.blight</span>:</div>
<div></div>
<pre><code> dat = johnson.blight  <br></code></pre>
<p>
<div>Now we can fit a GLME model with random effects for area, and compare it with a model with only fixed effects:</div>
<div></div>
<pre><code> mod10 = glm(blight ~ precip.m, data=dat, family=&quot;binomial&quot;)  <br>   <br> mod11 = glmer(blight ~ precip.m + (1|area), data=dat, family=&quot;binomial&quot;)   <br>   <br> > AIC(mod10, mod11)  <br>    df    AIC  <br> mod10 2 37.698821  <br> mod11 3 9.287692  <br>   <br></code></pre>
<p>
<div>As you can see this new model reduces the AIC substantially.</div>
<div></div>
<div>The same function can be used for Poisson regression, but it does not work for quasipoisson overdispersed data. However, within lme4 there is the function glmer.nb for negative binomial mixed effect. The syntax is the same as glmer, except that in glmer.nb we do not need to include family.</div>
<div></div>
<div></div>
<h2>References and Further Reading</h2>
<p>Finch, W.H., Bolin, J.E. and Kelley, K., 2014. <i>Multilevel modeling using R</i>. Crc Press.</p>
<p>Yan, X. and Su, X., 2009. <i>Linear regression analysis: theory and computing</i>. World Scientific.</p>
<p>James, G., Witten, D., Hastie, T. and Tibshirani, R., 2013. <i>An introduction to statistical learning</i> (Vol. 6). New York: springer. http://www-bcf.usc.edu/~gareth/ISL/ISLR%20Sixth%20Printing.pdf</p>
<p>Long, J. Scott. 1997. <i>Regression Models for Categorical and Limited Dependent Variables</i>. Sage. pp104-106. [For pseudo R-Squared equations, page available on google books]</p>
<p>Webster, R. and Oliver, M.A., 2007. <i>Geostatistics for environmental scientists</i>. John Wiley &amp; Sons.</p>
<p>West, B.T., Galecki, A.T. and Welch, K.B., 2014. <i>Linear mixed models</i>. CRC Press.</p>
<p>Gałecki, A. and Burzykowski, T., 2013. <i>Linear mixed-effects models using R: A step-by-step approach</i>. Springer Science &amp; Business Media.</p>
<p>Williams, R., 2004. <i>One-Way Analysis of Variance</i>. URL: https://www3.nd.edu/~rwilliam/stats1/x52.pdf</p>
<p>Witte, R. and Witte, J. 2009. <i>Statistics. 9th ed. </i>Wiley.<br>
<h2></h2>
<div></div>
<h2>Appendix 1: Assessing the accuracy of our model</h2>
<div>
<div>There are several ways to check the accuracy of our models, some are printed directly in R within the summary output, others are just as easy to calculate with specific functions.</div>
<div></div>
<div></div>
<h3>R-Squared</h3>
</div>
<div>
<div>This is probably the most commonly used statistics and allows us to understand the percentage of variance in the target variable explained by the model. It can be computed as a ratio of the regression sum of squares and the total sum of squares. This is one of the standard measures of accuracy that R prints out through the function summary for linear models and ANOVAs.</div>
<div></div>
<div></div>
<h3>Adjusted R-Squared</h3>
</div>
<div>
<div>This is a form of R-squared that is adjusted for the number of terms in the model. It can be computed as follows:</div>
<div><a rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-Qp_42uVuVOU/WVOkk-uaD9I/AAAAAAAAXQk/VDJTT2NxqUkwd16SKsZurq9aT59A24_OgCLcBGAs/s1600/Eq10.png?ssl=1" target="_blank"><img border="0" data-original-height="129" src="https://i0.wp.com/2.bp.blogspot.com/-Qp_42uVuVOU/WVOkk-uaD9I/AAAAAAAAXQk/VDJTT2NxqUkwd16SKsZurq9aT59A24_OgCLcBGAs/s640/Eq10.png?resize=450%2C129&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>Where R2 is the R-squared of the model, n is the sample size and p is the number of terms (or predictors) in the model. This index is extremely useful to determine possible overfitting in the model. This happens particularly when the sample size is small, in such cases if we fill the model with predictors we may end up increasing the R-squared simply because the model starts adapting to the noise in the data and not properly describing the data. </div>
<div></div>
<div></div>
<h3>Root Mean Squared Deviation or Root Mean Squared Error</h3>
</div>
<div>
<div>The previous indexes measure the amount of variance in the target variable that can be explained by our model. This is a good indication but in some cases we are more interested in quantifying the error in the same measuring unit of the variable. In such cases we need to compute indexes that average the residuals of the model. The problem is the residuals are both positive and negative and their distribution should be fairly symmetrical. This means that their average will always be zero. So we need to find other indexes to quantify the average residuals, for example by averaging the squared residuals:</div>
<div><a rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/-1N_pvQGyNqs/WVOk1b-TmZI/AAAAAAAAXQo/IA_EdL3QRt89SRDKyVTWL39uHY2Ik1nBQCLcBGAs/s1600/Eq11.png?ssl=1" target="_blank"><img border="0" data-original-height="172" src="https://i2.wp.com/3.bp.blogspot.com/-1N_pvQGyNqs/WVOk1b-TmZI/AAAAAAAAXQo/IA_EdL3QRt89SRDKyVTWL39uHY2Ik1nBQCLcBGAs/s640/Eq11.png?resize=450%2C172&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>This is the square root of the mean the squared residuals, with <span></span> being the estimated value at point t, <span></span> being the observed value in t and <span></span> being the sample size. The RMSE has the same measuring unit of the variable y.</div>
<div></div>
<div></div>
<h3>Mean Squared Deviation or Mean Squared Error</h3>
</div>
<div>This is simply the numerator of the previous equation, but it is not used often. The issue with both the RMSE and the MSE is that since they square the residuals they tend to be more affected by large residuals. This means that even if our model explains the large majority of the variation in the data very well, with few exceptions; these exceptions will inflate the value of RMSE.</div>
<div></div>
<div></div>
<div></div>
<h3>Mean Absolute Deviation or Mean Absolute Error</h3>
<div>
<div>To solve the problem with large residuals we can use the mean absolute error, where we average the absolute value of the residuals:</div>
<div><a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-5pkJOaONGjM/WVOlLlRQneI/AAAAAAAAXQs/-KXevZgMNoIKnM6Tx9PUCL9L6nPTfoYPgCLcBGAs/s1600/Eq12.png?ssl=1" target="_blank"><img border="0" data-original-height="124" src="https://i2.wp.com/1.bp.blogspot.com/-5pkJOaONGjM/WVOlLlRQneI/AAAAAAAAXQs/-KXevZgMNoIKnM6Tx9PUCL9L6nPTfoYPgCLcBGAs/s640/Eq12.png?resize=450%2C124&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div>This index is more robust against large residuals. Since RMSE is still widely used, even though its problems are well known, it is always better calculate and present both in a research paper.</div>
<div></div>
<div></div>
<div></div>
<h3>Akaike Information Criterion</h3>
</div>
<div>
<div>This index is another popular index we have used along the text to compare different models. It is very popular because it corrects the RMSE for the number of predictors in the model, thus allowing to account for overfitting. It can be simply computed as follows:</div>
<div></div>
<div><a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-ppqORBZ87as/WVOlcbQ4keI/AAAAAAAAXQw/EIM1N146Zi85fLz5j-_U6URtliWbTTnLACLcBGAs/s1600/Eq13.png?ssl=1" target="_blank"><img border="0" data-original-height="81" src="https://i1.wp.com/1.bp.blogspot.com/-ppqORBZ87as/WVOlcbQ4keI/AAAAAAAAXQw/EIM1N146Zi85fLz5j-_U6URtliWbTTnLACLcBGAs/s640/Eq13.png?resize=450%2C81&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<div>Where again p is the number of terms in the model.</div>
</div>
<div></div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-video-tutorial.blogspot.com/2017/06/linear-models-anova-glms-and-mixed.html"> R tutorial for Spatial Statistics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/g2yIsPv4wCY" height="1"></a></div>