Statistics in environmental sciences 9781786305077, 1786305070

Statistical tools are indispensable for the environmental sciences. They have become an integral part of the scientific

788 119 18MB

English Pages 281 [315] Year 2019

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Statistics in environmental sciences
 9781786305077, 1786305070

Table of contents :
Content: 1. Working with the R Software. 2. Fundamental Concepts in Statistics. 3. Developing a Sampling or Experimental Plan. 4. Principle of a Statistical Test. 5. Key Choices in Statistical Tests. 6. Comparison Tests of Unilateral and Bilateral Parameters. 7. Classical and Generalized Linear Models. 8. Non-parametric Alternatives to Linear Models.

Citation preview

Statistics in Environmental Sciences

Series Editor Françoise Gaill

Statistics in Environmental Sciences

Valérie David

First published 2019 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc.

Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms and licenses issued by the CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the undermentioned address: ISTE Ltd 27-37 St George’s Road London SW19 4EU UK

John Wiley & Sons, Inc. 111 River Street Hoboken, NJ 07030 USA

www.iste.co.uk

www.wiley.com

© ISTE Ltd 2019 The rights of Valérie David to be identified as the author of this work have been asserted by her in accordance with the Copyright, Designs and Patents Act 1988. Library of Congress Control Number: 2019940675 British Library Cataloguing-in-Publication Data A CIP record for this book is available from the British Library ISBN 978-1-78630-507-7

Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ix

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xi

Chapter 1. Working with the R Software . . . . . . . . . . . . . . . . . . .

1

1.1. Working with the R software . . . . . . . . . . . . . . . 1.1.1. Why and how to work with the R software? . . . . 1.1.2. Recommended working method . . . . . . . . . . . 1.1.3. Finding help with R . . . . . . . . . . . . . . . . . . 1.2. Basic operations for statistics in R . . . . . . . . . . . . 1.2.1. Manipulating the data set in R . . . . . . . . . . . . 1.2.2. Format the data set . . . . . . . . . . . . . . . . . . . 1.2.3. Arranging the data set . . . . . . . . . . . . . . . . . 1.2.4. Pivot tables. . . . . . . . . . . . . . . . . . . . . . . . 1.3. A few graphs to summarize the data set . . . . . . . . . 1.3.1. Display of graphs . . . . . . . . . . . . . . . . . . . . 1.3.2. Simple graphs (plots). . . . . . . . . . . . . . . . . . 1.3.3. Histograms . . . . . . . . . . . . . . . . . . . . . . . . 1.3.4. Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . 1.3.5. Barplots. . . . . . . . . . . . . . . . . . . . . . . . . . 1.3.6. Pair representations . . . . . . . . . . . . . . . . . . . 1.3.7. Graphical representation of contingency diagrams 1.3.8. Study of the dispersion and interaction of data on a balanced and crossed model . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

1 1 2 3 6 6 12 13 14 16 16 17 19 20 23 25 27

. . . . . . . . . .

29

Chapter 2. Fundamental Concepts in Statistics . . . . . . . . . . . . . .

31

2.1. Basic statistical vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.1. Element, population and sample . . . . . . . . . . . . . . . . . . . . . 2.1.2. Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31 31 33

vi

Statistics in Environmental Sciences

2.2. Summarizing a sample . . . . . . . . . . . . . . . . 2.2.1. Main trend parameters . . . . . . . . . . . . . . 2.2.2. Dispersion parameters . . . . . . . . . . . . . . 2.3. The laws of probability . . . . . . . . . . . . . . . . 2.3.1. Normal distribution or Laplace Gauss’s law . 2.3.2. Other useful laws of probability . . . . . . . .

. . . . . .

35 35 38 39 42 48

Chapter 3. Developing a Sampling or Experimental Plan . . . . . . . .

51

3.1. Sampling plans . . . . . . . . . . . . . . . . . . . . 3.1.1. Principles and criteria to be respected . . . . 3.1.2. Generalization and reliability of the plan . . 3.1.3. Variants of random sampling . . . . . . . . . 3.2. Experimental plans . . . . . . . . . . . . . . . . . 3.2.1. Principles and criteria to be respected . . . . 3.2.2. Generalization and sensitivity of the plan. . 3.2.3. Different experimental plans . . . . . . . . . . 3.2.4. Experimental plans and reality in the field: crucial choices for statistical analysis . . . . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . .

. . . . . . . .

. . . . . . . .

51 52 55 56 60 61 63 64

. . . . . . . . . . . . . .

67

Chapter 4. Principle of a Statistical Test . . . . . . . . . . . . . . . . . . .

73

4.1. The usefulness of statistics . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2. Decision theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3. The statistical approach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3.1. Concepts of error risk, power and robustness . . . . . . . . . . . . . . 4.3.2. Independent and paired samples . . . . . . . . . . . . . . . . . . . . 4.4. Example of the application of a statistical test . . . . . . . . . . . . . . 4.4.1. Case of a Student’s t-test of two independent bilateral samples. . 4.4.2. Case of a Student’s t-test of two independent unilateral samples . 4.4.3. Case of a Student’s t-test for comparing a sample to a standard . 4.4.4. Case of a Student’s t-test of two paired samples . . . . . . . . . . . 4.4.5. Power test and research for the minimum number of elements to be considered per sample for an imposed power . . . . . . .

. . . . . . . . . .

73 74 76 77 82 82 84 87 88 90

.

94

Chapter 5. Key Choices in Statistical Tests . . . . . . . . . . . . . . . . .

95

5.1. How are keys chosen? . . . . . . . . . . . . 5.1.1. Key 1 . . . . . . . . . . . . . . . . . . . . 5.1.2. Key 2 . . . . . . . . . . . . . . . . . . . . 5.1.3. Key 3 . . . . . . . . . . . . . . . . . . . . 5.1.4. Key 4 . . . . . . . . . . . . . . . . . . . . 5.2. Verification tests of application conditions 5.2.1. Cochran’s rule . . . . . . . . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

95 95 96 96 96 103 103

Contents

5.2.2. Kolmogorov–Smirnov test . . . . . . . . 5.2.3. Shapiro–Wilk test . . . . . . . . . . . . . . 5.2.4. Fisher–Snedecor test . . . . . . . . . . . . 5.2.5. Bartlett’s test. . . . . . . . . . . . . . . . . 5.2.6. Levene’s test . . . . . . . . . . . . . . . . . 5.2.7. Goldfeld–Quandt test . . . . . . . . . . . 5.2.8. Durbin–Watson test . . . . . . . . . . . . 5.2.9. Homogeneity test of betadisp variances .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

104 106 107 109 110 112 113 114

Chapter 6. Comparison Tests of Unilateral and Bilateral Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

115

6.1. Comparisons of numbers and proportions . . . . . . . . . . . . . . 6.1.1. Fisher’s exact test . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1.2. The chi-squared conformity test . . . . . . . . . . . . . . . . . 6.1.3. Chi-squared homogeneity test . . . . . . . . . . . . . . . . . . 6.1.4. McNemar’s test . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1.5. Binomial test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1.6. Cochran–Mantel–Hantzel test . . . . . . . . . . . . . . . . . . 6.2. Comparisons of means . . . . . . . . . . . . . . . . . . . . . . . . . 6.2.1. Student’s t-test . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2.2. Wilcoxon–Mann–Whitney U test . . . . . . . . . . . . . . . . 6.3. Correlation test of two quantitative variables . . . . . . . . . . . . 6.3.1. Linear correlation: the Bravais–Pearson coefficient . . . . . 6.3.2. Monotonous correlation: Spearman or Kendall coefficients .

. . . . . . . .

. . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . .

. . . . . . . .

vii

. . . . . . . . . . . . .

. . . . . . . . . . . . .

115 115 118 120 123 124 125 128 128 139 146 146 149

Chapter 7. Classical and Generalized Linear Models. . . . . . . . . . .

155

7.1. Principle of linear models . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.1. Classical linear models: the least squares method . . . . . . . . . . 7.1.2. Generalized linear models: the principle of maximum likelihood 7.2. Conditions of application of the model . . . . . . . . . . . . . . . . . . 7.2.1. Classic linear models . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2.2. Generalized linear models . . . . . . . . . . . . . . . . . . . . . . . . 7.3. Other useful analyses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3.1. Transformations, linearization . . . . . . . . . . . . . . . . . . . . . 7.3.2. Post hoc tests for ANOVA or equivalent GLM . . . . . . . . . . . 7.3.3. The variance partition . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3.4. Selection criteria for linear models: AIC or BIC criteria . . . . . . 7.3.5. From model to prediction . . . . . . . . . . . . . . . . . . . . . . . . 7.4. Example of the application of different linear models . . . . . . . . . . 7.4.1. Simple linear regression . . . . . . . . . . . . . . . . . . . . . . . . . 7.4.2. Multiple linear regression . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

157 157 166 170 170 173 174 174 177 179 181 182 183 183 191

viii

Statistics in Environmental Sciences

7.4.3. One-factor ANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4.4. Multifactorial ANOVA . . . . . . . . . . . . . . . . . . . . . . . . 7.4.5. Covariance analysis (ANCOVA) . . . . . . . . . . . . . . . . . . 7.5. Examples of the application of GLMs . . . . . . . . . . . . . . . . . . 7.5.1. GLM for proportion or binary data under binomial distribution 7.5.2. GLM for discrete data under Poisson distribution . . . . . . . . . 7.5.3. GLM for true positive data under gamma distribution . . . . . . 7.5.4. GLM for true data under Gaussian law . . . . . . . . . . . . . . .

. . . . . . . .

199 206 226 230 230 236 243 249

Chapter 8. Non-parametric Alternatives to Linear Models . . . . . . .

253

8.1. Principle of non-parametric tests . . . . . . . . . . . . . . . 8.2. ANOVA alternatives . . . . . . . . . . . . . . . . . . . . . . 8.2.1. Rank-based ANOVAs . . . . . . . . . . . . . . . . . . . 8.2.2. Non-parametric alternative to a one-factor ANOVA: the Kruskal–Wallis test and associated post hoc . . . . . . . 8.2.3. Non-parametric alternative to a two-factor ANOVA: Scheirer–Ray–Hare test . . . . . . . . . . . . . . . . . . . . . . 8.2.4. Non-parametric alternative to a matching ANOVA: the Friedman test . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3. Non-parametric ANOVAs based on permutation tests (PERMANOVA) . . . . . . . . . . . . . . . . 8.3.1. Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3.2. Example of application in R . . . . . . . . . . . . . . . 8.4. Nonlinear models . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

253 254 254

. . . . . . . .

254

. . . . . . . .

257

. . . . . . . .

259

. . . .

. . . .

262 262 264 271

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

273

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

275

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

277

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

Preface Statistics: Essential Tools to be Carefully Considered

“Don’t interpret what you don’t understand... call for an understanding of the methods, the supporters and outcomes before making sense of the conclusions”. Nicolas Gauvrit, Statistiques, méfiez-vous ! (2014)

In the 19th Century, the British Prime Minister, Benjamin Disraeli, defined three kinds of lies: “lies, damned lies and statistics”, already underlining the controversy surrounding these tools. Within the scientific community, pro-statistics and anti-statistics are in conflict and this is manifested by disciplines that are still reluctant to use them. Many media use statistics to sedate public opinion and advance results that ultimately make no sense without some prior clarification. More recently, in the article published in Le Monde on October 4, 2017, “Publier ou périr : la Science poussée à la faute” (“Publish or perish: Science pushed to a fault”), journalists highlight the misuse of statistics as one of research’s bad practices in the race to publish. These misuses are related to a poor understanding of how this tool works with biased sampling, simplistic experiments limiting reproducibility, exaggerated results in terms of the statistical population used or their significance, or an underestimation of the risks of error associated with statistical tests.

x

Statistics in Environmental Sciences

Despite these criticisms, it is clear that once these tools begin to be used within a scientific discipline, they quickly become essential. The use of statistics is the only way to generalize sample results to the population level due to problems related to sampling fluctuations and the inherent variability of “natural” objects. However, it can undoubtedly lead to biased results if they are not carried out accurately. Statistics require a kind of “calibration”. It would not be appropriate for a biogeochemist to use an oxygen probe without first calibrating it according to environmental parameters such as temperature, or for a system ecologist to identify species without using rigorous and expertly recognized determination keys. The “calibration” of statistical tools is done through verifying the conditions under which the tests used to meet a specific objective are applied (e.g. comparison of population averages, existence of trends, etc.). These tests are based on the use of mathematical equations according to certain hypotheses. Failure to respect these hypotheses makes the application of the equations used in the test flawed due to the mathematical properties used for its design. Thus, these tools are essential for an objective scientific approach, but their use requires particular accuracy in their implementation and interpretation. The objective of this book is therefore to understand the use of statistics by explaining the spirit behind their design, and to present the most commonly used analyses in environmental sciences, their principles, their advantages and disadvantages, their implementation via the R software and their interpretations in the field of environmental sciences. Valérie David May 2019

Introduction1

I.1. What is the relevance of statistical analysis in environmental sciences? I.1.1. The intrinsic variability of “natural” objects “Artificial” objects from industrial manufacture, such as Bijou branded madeleines, are characterized by distinctive features, in accordance with precise specifications, in other words, weight, height, calorie intake, carbohydrate content, etc. This calibration is particularly monitored during the production chain and any deviation from the imposed standard leads to the removal of the object from sale. For example, the weighing of a “sample” of 1,000 madeleines (Bijou brand) shows only very small fluctuations in weight between 24.85 and 25.19 g (Figure I.1). In contrast, “natural” objects, alive or not, are marked by strong interindividual variability for a given species, or between hydrological or pedological samples for, respectively, a theoretically homogeneous water body or soil. Thus, although classified by size according to weight, a sample of 1,000 size-5 oysters that correspond to the same age group shows a fairly broad distribution curve with an average of 37 g and a range of variation mainly fluctuating between 30 and 45 g (Figure I.1). This high variability is inherent in “natural” objects and distinguishes them from “artificial” objects. 1 This introduction is a simplified and summarized overview of Scherrer’s books (1984) and online books such as Poinsot’s (2004).

xii

Statistics in Environmental Sciences

Figure I.1. Weight distribution for 1,000 industrially produced Bijou brand madeleines and 1,000 individual living “natural” objects, hollow oysters (size-5)

This intrinsic variability requires several elements/individuals to be considered in order to characterize a “natural” object, called replicas. The greater the variability, the greater the number of elements to be considered will be in order to better understand the characteristics of the object under consideration. I.1.2. Describing a natural population Let us take an oyster population present in the Arcachon Bay (SouthWest of France) with a number that amounts to 30 in total. It is simple to determine which of them are infested by the parasitic worm Polydora spp. (Figure I.2). If the number of parasitized oysters is a total of 12, the actual parasitic prevalence (in other words, the proportion of infested oysters) is therefore 40% at the bay level. However, it is completely utopian to consider an exhaustive analysis of the oyster population for a given environment. Indeed, the number of individuals is often much higher so taking a total census would be very

Introduction

xiii

time-consuming. In addition, the study of oyster parasitism involves dissection and therefore the euthanasia of hosts (like most studies conducted on living beings), and it is totally unacceptable to decimate a population under the pretext of scientific research. The analysis of a natural population, living or not (e.g. oysters, water bodies), will therefore require representative sampling: only a few elements will be sampled, but in such a way as to represent the composition and complexity of the entire population.

Figure I.2. Hypothetical example 1: all oysters in the Arcachon Bay were counted (N = 30 in total) and analyzed; it is possible to observe the true parasitic prevalence value of the Polydora worm, which is 40% here

Let us consider the prevalence obtained from a first sample of eight oysters taken in the Arcachon bay. This is 25% (two out of eight infested oysters; sample 1, Figure I.3). This value is different from that obtained from the population as a whole (40%) due to sampling fluctuations (in other words, only eight oysters are considered out of 30). In addition, the consideration of a second sample on this same population gives a prevalence of 50% (four out of eight infested oysters; sample 2, Figure I.3). This value is not only different from the value obtained at the population level, but also from that of sample 1. Thus, two samples giving different values can come from the same initial population.

xiv

Statistics in Environmental Sciences

Figure I.3. Comparison of two geographically distinct hypothetical oyster populations (Arcachon Bay and Marennes-Oléron Bay). The total number of oysters in each population is 30, but the actual prevalence is different, 40% and 20%, respectively. Three samples were taken, two from the Arcachon population and one from the Marennes-Oléron Bay. For a color version of this figure, see www.iste.co.uk/ david/statistics.zip

Let us now compare the parasitic prevalence of two oyster populations between two renowned oyster farming bays, the Arcachon Bay and the Marennes-Oléron Bay, each composed of a total of 30 individuals (Figure I.3). The real prevalence is 40% for Arcachon and 20% for MarennesOléron. A sample taken from the population of the Marennes-Oléron Bay gives a prevalence of 25%, which is not only different from the actual prevalence of the system, but also identical to that of sample 1 of the Arcachon Bay (Figure I.3). Thus, two samples from two different populations can give identical results. In conclusion, going through samples does not give fair values and thus complicates the generalization that we could make from these samples at the population level. Although sampling is mandatory in environmental sciences because of the variability inherent in any “natural” object, the difference observed between two samples from two populations to be compared may be related to (1) a “real” difference between these two populations and (2) in part, to

Introduction

xv

sampling fluctuations. The respective part of one or the other, the real effect versus the random effect, is impossible to quantify because the value of the variable considered at the level of the entire population is not accessible. Using the “statistical” tool is the only way to distinguish between assessing the risk of being wrong when making the decision to consider these two populations as different, or not (Figure I.4).

Figure I.4. Comparison of two hypothetical oyster populations that are geographically separated (Arcachon Bay and Marennes-Oléron Bay). Only statistics can be used to distinguish between assessing the risk of being wrong when making the decision to consider that these two populations are different, by properly generalizing the results from the consideration of the samples taken within each population

Thus, statistics refer to the set of scientific methods from which data are collected, organized, summarized, presented and analyzed, and which allow conclusions to be drawn and sound decisions to be made regarding the populations they represent. I.2. The statistical mind: from the representative sample of the population In general, the scientific approach consists of taking a sample (or samples), describing it and “extrapolating” the results obtained from the population(s) using a statistical approach. The sample(s) must therefore be representative of the population.

xvi

Statistics in Environmental Sciences

I.2.1. The representativeness of a sample A representative sample must best reflect the composition and complexity of the entire population. It is therefore a question of giving each element of the population an equal opportunity to be sampled. Only randomly carried out sampling in the population, in other words, random sampling, allows this. However, being carried out randomly does not just mean it can be carried out in any way. Box I.1 shows that a human mind is too sophisticated to deal with chaos appropriately. While this may seem surprising, only a precise methodology can provide access to random sampling. Once again, using the population of oysters in the Arcachon Bay, this time with a probable number of individuals (N = 8,000). In theory, all elements of the population should be identified and numbered from 1 to 8,000 (Figure I.5). If the sample consists of eight elements, eight numbers should be drawn at random (by computer or using a random number table) and only the oysters corresponding to these numbers should be considered in the sample (Figure I.5).

Figure I.5. Precise procedure for a simple random sampling of eight oysters out of a population of 8,000. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

This is a simple random sampling, but it is the only one on which the statistical tests available in the study can be applied as they are. Other types exist, such as those that particularly consider a spatial or temporal variation factor known to affect the variability of the elements (see Chapter 3).

Introduction

xvii

The experiment2 consists of (randomly) drawing a number between 1 and 10. Although the task is entrusted to a computer for, for example, two draws of 100 individuals (A), the number leaving the majority or minority will not necessarily be the same and the form of the final distribution will not be identical from one draw to another.

This same experiment was carried out repeatedly from one year to the next for Master’s students and shows similar distributions with a number 7, often in the majority, and a bimodal form with extreme (1 and 10) or median (5) values, often in the minority.

This experiment shows that despite the effort made to produce a random number, the human mind is often influenced by its reasoning or culture. The number 7 has a strong symbolic meaning in the Western world and the numbers 1, 5 and 10 are ignored because they are not representative of a number given at random with regard to the mind (too extreme or too median). Thus, by contrast, a random draw made by a computer will be more arbitrary. Box I.1. An aside on random sampling and the human mind

2 This experiment is based on Poinsot’s book (2004), Statistics for Statophobes, available at: https://perso.univ-rennes1.fr/denis.poinsot/statistiques_%20pour_statophobes/STATISTIQUES %20POUR%20STATOPHOBES.pdf.

xviii

Statistics in Environmental Sciences

I.2.2. How to characterize the sample for a good extrapolation of the population Some characteristics of the sample will allow us to obtain/extrapolate the properties of the population. Although the average is the first factor that comes to mind when summarizing the sample, it is not enough. In order to demonstrate this, let us put ourselves in the shoes of an Arcachon oyster farmer, who has just lost a large part of his production due to the development of a pathogen. He decides to go to the wild oyster reefs of the bay to ensure the supply of his stall at the Arcachon Christmas market. This will ensure he earns a revenue, which will certainly be less than with cultivated oysters (as the price per dozen is lower), but will allow him to limit the damage to his company from an economic perspective. He is looking for size-5 oysters, weighing about 37 g (30–45 g), which are particularly affected by the pathogen on his own production. In order to facilitate his work, he contacts the Comité régional de la conchyliculture (Regional Shellfish Farming Committee) (CRC) to get an idea of the average weights of wild oysters in different parts of the bay and thus target his samples. In fact, the average weight of oysters has recently been assessed at three sites in the bay: Cap Ferret (on average 37 g), Comprian (36 g) and Jacquets (45 g). He therefore moves away from the Jacquets site, which clearly had oysters that were too large, and finally decides to take the oysters from Cap Ferret because of the accessibility to the reefs. After hours of sorting, his hands bloody, he is only able to harvest about 30 size-5 oysters. He angrily returns to the CRC, who show him the graphic results of the movement carried out in these three sites. The average weight of oysters has indeed been assessed on the basis of 100 oysters for each site and the averages are indeed accurate, but the oyster farmer kicks himself following the observation of the distribution curves of the weights obtained per site (Figure I.6). It is clear from the curves relating to Cap Ferret and Comprian that wild oysters have very similar average weights, but that they have a very different weight structure. Most of the population has a weight close to the average weight of 36 g in Comprian and corresponds to size-5 oysters, whereas the population of Cap Ferret has two groups of sizes, one smaller and the other larger than the average weight: few oysters in Cap Ferret actually have the characteristics of the size desired by the oyster farmer. Paradoxically, the oyster farmer could have collected many more size-5 oysters with a

Introduction

xix

sampling effort comparable to Jacquets, although the overall average would have been higher. Thus, this example illustrates that the description of a sample by its average alone is not sufficient. It requires the consideration of a factor reflecting interindividual variability. This variability will be addressed in statistics by so-called dispersion parameters such as variance, standard deviation, standard error, etc. Moreover, the average is not the only factor describing a central value of a population, although it is the factor that is mainly used. Parameters such as the median or mode may sometimes be more appropriate (see section 2.2.1).

Figure I.6. Distribution curve of the weights of wild oysters taken from Cap Ferret, Comprian and Jacquets by the CRC. The calculated averages are shown in gray. The gray box corresponds to the size-5 oysters

xx

Statistics in Environmental Sciences

I.2.3. Effect of sample size Contrary to popular belief, the size of a sample does not influence its representativeness: it is not the number of individuals considered that will determine whether the sample reflects the complexity of the population, but its type of sampling. As described above, only random sampling can be representative of this. Let us look at the distribution of the oyster population at Comprian. This is in the form of a bell curve (Gaussian curve) centered on an average of 36 g with a high weight dispersion between 20 and 49 g (Figure I.7).

Figure I.7. Curve of the weight distribution of wild oysters in Comprian for the entire population (N = 8,000 oysters) and two samples of n = 8 oysters and n = 3,000 oysters. As the sample size increases, the confidence interval of the average calculated from the sample’s characteristic parameters decreases. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Two random samples of different sizes (sample 1 of eight oysters and sample 2 of 3,000 oysters) were taken from this population. The average value of the samples is likely to be unequal to the true population average due to sampling fluctuations, but we will see later that the dispersion parameters (in other words, standard error) calculated from these samples allow us to estimate a confidence interval in which there is a 95% chance of finding the true average of the population (see section 2.3.1). The larger the sample, the smaller this confidence interval is and therefore the estimate of the improved “true” average. For the small sample, the population average is

Introduction

xxi

likely to be between 33.5 and 38.5 g (a variation of 5 g), while this range is almost halved for the large sample (2.8 g; Figure I.7). The estimate will therefore be more accurate for a large sample. In environmental science, the choice of the sample size (in other words, sampling effort) will be the best compromise between feasibility and the power of the statistical test implemented. It is not a matter of spending too much time analyzing the sample or decimating a population. However, the sample must be large enough to make the estimate as accurate as possible in order to conclude on the objective set. For example, if it is a question of comparing two populations, the smaller the confidence intervals, the more likely it will be possible to determine that they do not overlap (or only overlap a little) and that the populations are different if this is the case. The ability of a statistical test to determine a difference between two sample populations is known as a “power test” (see section 4.3.1). I.3. The “statistical” tool in environmental research I.3.1. Environmental research: from description to explanation Scientific research is not limited to the study of a single parameter, in the sense that the researcher’s objective will not only be to describe the spatial and temporal variability of a parameter of interest (e.g. parasitic prevalence of oysters), but to also understand how other factors control its fluctuations (e.g. size, condition indicators, site contamination, parasite life cycle, etc.). Thus, many parameters related to the interest parameter will be sampled simultaneously during a study. For example, looking for environmental factors influencing parasite prevalence may be done by analyzing the joint evolution of parasite prevalence with factors proposed by graphs and statistical analyses by considering parameters in pairs in order to determine whether the relationships are “real” (called significance). However, it is unlikely that a single factor will affect the interest parameter. For example, the parasitic prevalence could be higher if the oysters are older (longer contact time with parasites) and/or if the oysters are physiologically weakened due to the presence of a contaminant. A global approach that considers all the potentially explanatory parameters will therefore have to be used and the human mind is too limited to grasp it solely on the basis of the global data table.

xxii

Statistics in Environmental Sciences

An analysis of the parameters in pairs is not only time-consuming but also biased by the set of “hidden” variables and the covariation between explanatory factors. For example, a parameter pair approach would reveal a relationship between prevalence and condition indicators, between prevalence and contamination, between size and prevalence and a last one between contamination and condition indicators. A global statistical approach would make it possible to prioritize the effect of these factors by highlighting, for example, that the relationship with size is not due to prolonged contact with the parasite, but rather due to an indirect impact of the contaminant on the physiology of the oyster. I.3.2. The place of statistics in the scientific process The scientific approach is comparable in some respects to that discussed in a criminal investigation. Let us take the example of investigations conducted as part of a well-known television series (Figure I.8). This series always begins with a crime scene where the team picks up clues and interviews witnesses. Samples are sent to the laboratory for further investigation. Witnesses or suspects are questioned on their alibis at the police station. Finally, all the evidence is combined to form evidence and to draw a conclusion on the murderer. In comparison, any scientific approach begins with field sampling to identify variables of interest. For example, in the case of a study analyzing the effect of factors controlling the state of health of oysters in the Arcachon Bay, variables reflecting the physiological state of the species, as well as others characterizing its physico-chemical environment, would be recorded at different facilities (temperature, salinity, metal contamination, etc.). These field variables are complemented by laboratory analyses (e.g. metal assays) with or without experimentation (e.g. to determine the specific impact of the copper contaminant on the survival of oysters). Lastly, all data are processed in order to meet the objectives set. The overall “field–laboratory–analysis” approach is therefore similar to a police investigation with variables measured in the field or in the laboratory, which is similar to the “clues” collected during a criminal investigation, and the processing of data collected can be compared to the investigators’ reflection on evidence.

Introduction

xxiii

Figure I.8. Comparison of the scientific approach with criminal investigations. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The fundamental difference, however, is that investigators must explore all possible leads, whereas scientists cannot access the entire population and will therefore use statistics to fill this gap. Sampling plans or experimental plans should be able to generalize the results of the target population and consider the knowledge acquired by the literature on the subject under study (see Chapter 3). For example, facilities sampled in the field could be randomly selected from known oyster aquaculture areas. The scientific approach is therefore divided into three stages (Figure I.9): 1) the field with a sampling strategy that is representative of the population in order to highlight the relationships between variables; 2) the laboratory to analyze certain variables or experimentally approach certain causal relationships between variables; 3) modeling to compare the relationships observed in the field with those obtained through experimentation and thus better understand these relationships.

xxiv

Statistics in Environmental Sciences

Figure I.9. The synergy of research approaches

I.3.3. The importance of the conditions of application and the statistical strategy Statistical tests are based on the use of mathematical equations whose properties are based on certain assumptions. These assumptions must therefore be verified: these are the conditions of application. If the performance of a test is conditioned, for example, by the fact that the data follow a known distribution law, such as the normal distribution law (see Chapter 2), then failure to comply with this condition makes the application of the equations used in this test difficult because of the mathematical properties used for its design. In addition, the scientific objective requires a statistical strategy on which the sampling plan or experimental design depends. Although the statistical tool only comes into play at the end of the scientific process, it must be considered beforehand in order to guide choices and digital analyses: number of replicas, sampling strategy, experimental approaches. The traditional statistical tests

Introduction

xxv

developed in textbooks are designed for a simple random sampling strategy. Any other strategy requires an adjustment of the tests (see Chapter 3). It is a mistake to think that statistics only come into play during the data processing phase. Indeed, for the latter to be effective, certain choices must be made upstream, starting from the design of the sampling plan or the experimental design, whether it is in the type of approach, the choice of variables, the number of replicas, etc. It is indeed frustrating to think that the objective could have been better understood “if...”, after the analysis (Figure I.10). All the stages of the scientific process are interconnected by the choice of statistical tests that will be ultimately used. In theory, it would be necessary to already know the type of test that will be implemented when the study objective is considered.

Figure I.10. Successive/interconnected approaches through the “choices” made throughout the scientific process

I.4. Book structure The statistics presented in this book are the most commonly used in the field of environmental sciences to compare parameters and to carry out linear models of a Y variable to be explained by one or more explanatory variables with parametric or non-parametric tests. Their descriptions are made through concrete examples in environmental sciences. They are presented using the R software, which is widely used in the scientific world. The book is composed as follows:

xxvi

Statistics in Environmental Sciences

– Chapter 1 introduces the use of the R software with some recommendations for its correct use, basic operations, data import, design of pivot tables and graphs; – Chapter 2 presents the fundamental concepts in statistics: the basic vocabulary, how to summarize a sample with the parameters of central tendency and dispersion, as well as the main probability laws on which the statistical tests are based; – Chapter 3 describes the design of sampling and experimental designs based on the objectives and assumptions made prior to the study; – Chapter 4 introduces the main principles of statistical tests, including decision theory and the global approach adopted using the example of the most traditional tests; – Chapter 5 proposes keys for choosing statistical tests according to the objective and available data, as well as statistical tools useful for testing the conditions under which these tests are applied; – Chapter 6 presents the tests for comparing parameters with bilateral or unilateral alternatives such as two averages, two or more distributions or proportions, etc.; – Chapter 7 focuses on traditional and generalized linear models describing a quantitative Y variable by proposing equations from several other explanatory variables with their principles, application conditions and different application examples according to the plan considered; – Chapter 8 describes alternatives to these linear models in the case of non-compliant conditions, alternatives based on rank or permutation tests; – the Conclusion describes in particular how to introduce statistics into a scientific report or publication.

1 Working with the R Software1

1.1. Working with the R software 1.1.1. Why and how to work with the R software? The R software is both a computer programming language and a working environment for statistical data processing. It is used to manipulate data, plot graphs and perform statistical analyses on these data. Among its advantages, R is: – a “free” software that can be downloaded and installed on personal computers; – a “cross-platform” software that runs on Windows, Linux, Mac OS, etc. As a result, many scientists use it internationally and do not hesitate to share their statistical knowledge by developing new functions and communicating through forums. Everyone can contribute to its improvement by integrating new functionalities or analysis methods that have not yet been implemented. This therefore makes it a software that is in rapid and constant evolution. Thus, many statistical analyses that are both simple and complex are available (descriptive and inferential statistics, parametric or non-parametric tests, linear or nonlinear models, multivariate analyses, etc.). No commercial 1 This chapter is a simplified and summarized vision of the books by Cornillon (2012), Crawley (2012), Bertrand and Mauny-Bertrand (2014), Grolemund and Wickam (2015), and Wickam (2016), supplemented by functions found in R forums and applied to examples in environmental sciences.

Statistics in Environmental Sciences, First Edition. Valérie David. © ISTE Ltd 2019. Published by ISTE Ltd and John Wiley & Sons, Inc.

2

Statistics in Environmental Sciences

software is able to provide such a choice of methods. It also has many useful and professional-quality graphic functions. Lastly, the tool is very effective because, when we master the R language, we are able to create our own tools (or “scripts”). This makes it possible to perform very sophisticated analyses by writing a script with a series of functions on the data (functions that are already incremented or created by ourselves) and to reproduce them very quickly on other data sets. In this book, we will be using the integrated development environment “Rstudio”, which is more user-friendly and powerful. It should be noted that not all functions are available in the basic software and that it will often be necessary to use additional libraries (or packages) that are also free. For example, the MASS package. When first used, the installation of the package requires an Internet connection. There are two steps when using it for the first time: installation with the install.package() function, then loading it into the working session with the library() function: >install.package(‘MASS’) >library(MASS)

Once installed, each new session can just be loaded without an Internet connection: >library(MASS)

1.1.2. Recommended working method The environment is frugal: everything is done through command lines (the “functions”). Working with the R software therefore requires a working method. If it is respected, it makes it possible to be more efficient and faster in analyses. 1.1.2.1. Creating a working directory First of all, it is advisable to create our own working directory (called the current directory). All analyses will be centralized there (or even all libraries could be centralized there by copy-pasting zip files from the website). This will make it easier to search for files (data, scripts, sessions, etc.). To do this, we must create a folder in the session called “R”, for example. The first thing to do each time the R software is opened is to change the current directory:

Working with the R Software

3

– either via File/Change the current directory in the toolbar (at the top); – or via setwd() by specifying the address of the current directory (this is a fake address). The getwd() function is used to check that the working session is the right one: >setwd("C:/users/R") >getwd()

The dir() function allows us to view all the files available in the directory: >dir()

1.1.2.2. Using and classifying our work in the form of scripts Scripts are also very useful and it is strongly recommended to work from them exclusively. They allow us to create a set of R command lines (in the form of functions) and keep them in memory for later use. In this way, we will be able to access an old script saved during a previous job. It is therefore advised that we create scripts for any new use of statistical tools, which will be available when we need to perform the same analysis again (without wasting time again trying to find information on their use). However, it is important to think about commenting on the lines of code (comments are after the # at the end of the line of code) for a better understanding later. To create a script, simply press File/New script to open a new script or File/Open script to access an old script. A new window appears in the console. It should be noted that this script should be regularly saved under an explicit name if possible in order to find it easily. 1.1.3. Finding help with R There are several possibilities depending on the research. 1.1.3.1. Starting and perfecting our skills in specific areas Documents can be downloaded free of charge, as can links and examples available on the website: http://www.r-project.org/, on the left, in the Documentation section by clicking on Manuals, then Contributed, for example, R for Beginners, Fitting Distributions with R, Practical Regression and Anova using R, etc.

4

Statistics in Environmental Sciences

1.1.3.2. Finding out how to do a particular test or graph 1.1.3.2.1. Without any idea of the function to use The help.search() function with a keyword of the test in quotation marks can be used. For example, for the Fisher’s exact test: >help.search("Fisher")

A window opens displaying all packages installed on the computer where the term in quotation marks appears in the help files. On the right is the title of the function, on the left are two elements separated by the sign “::”, on the left is the library concerned and on the right is the function. In the case presented here, the proposal seems to correspond to Fisher’s exact test. This test is therefore in the stats library under the fisher.test() function. This library is included in the basic software (by default).

Figure 1.1. Search window of the help.search() function

If we click on the function name, the full help on the function is displayed. It is also possible to launch the online help by typing: >help.start()

Working with the R Software

5

Then by clicking on Search Engine & Keywords and typing the keyword in the field at the top left, in the case described here, the same results as above are displayed. 1.1.3.2.2. The function to be used is known, but not its use Just type “?” followed by the name of the function: >?fisher.test()

The help window appears at the bottom right containing all the information necessary to use the function. All help files are built in the same way:

Summary description of the function

Box 1.1. Help file for a function

6

Statistics in Environmental Sciences

The Usage and Examples sections are the two most important. 1.1.3.3. Usage In the Usage section, the syntax of the function is specified: fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE, control = list(), or = 1, alternative = "two.sided", conf.int = TRUE, conf.level = 0.95, simulate.p.value = FALSE, B = 2000)

All arguments are separated by commas. Only a few arguments are required to be filled in. Here, for example, it is the argument x. All arguments followed by = something have default values and are therefore optional. However, it will be necessary to inform them if one of these options needs to be changed. For example, to change the limit of the confidence interval to 99%, simply change the conf.level argument. Lastly, it is only possible to omit the names of arguments when they are given in order. 1.1.3.4. Other possibilities If nothing conclusive appears, performing a Google search with keywords such as “Fisher R CRAN test”, can be effective. With the number of forums in vogue on R or TP of online statistics, it would be quite unfortunate if nothing were available on the desired function. It is thus possible to ask the question on a forum, such as the one at the following address: https://informatique-mia.inra.fr/r4ciam/node/54. The R community is very open and there will always or almost always be a volunteer to answer. 1.2. Basic operations for statistics in R 1.2.1. Manipulating the data set in R 1.2.1.1. Operations for a sequence of numbers, i.e. a variable The c() function allows the creation of a sequence of numbers, and the factor() function allows the creation of a multimodality factor that corresponds to text, which in turn matches a qualitative variable. It is useful to store it in an object, while paying attention to the spelling. Here, we have a temperature sequence or weather data. Note that Temp is not recognized in the same way as temp: they are not the same objects for the software. Lastly,

Working with the R Software

7

the “;” allows two operations to be carried out on the same line. Here, it is the creation of Temp then the calling of Temp, and the creation of meteo then the calling of meteo (although the terms in this code are in French, it will of course still work with terms in English!): >Tempmeteolevels(meteo)

The levels() function is used to identify the different conditions of the factor. It is possible to convert a numeric variable to factor format with the as.factor() function. Some tests require this format in order to correctly consider a qualitative variable in statistical language: >Temp_factorlevels(Temp_factor)

Number sequences can also be used to facilitate the creation of a variable. Here are the most common ones, seq(), rep(): >4:19 # Series of integers between the numbers 4 and 19 [1] 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 >seq(from = 0, to = 12, by = 2) # sequence from 0 to 12 every 2 values [1] 0 2 4 6 8 10 12 >rep(c(15,18,23), each = 3, times = 2) # Series of the numbers 15, 18 and 23 repeated 3 times each, then the sequence repeated twice [1] 15 15 15 18 18 18 23 23 23 15 15 15 18 18 18 23 23 23

All operations are possible on the components of this vector. In the following example, the operation transforms the temperatures expressed in degrees Celsius into degrees Fahrenheit. Each component of the vector will undergo the operation: >(Temp* 9+160)/5 # Formula for transforming temperature in degrees Celsius into Farenheit [1] 59.0 64.4 73.4 66.2 75.2

8

Statistics in Environmental Sciences

1.2.1.2. Observation of a data set Let us take the crabs data set available in the MASS library. These are five morphological variables measured on 200 crabs of the Leptograpsus variegatus species that have been collected in Fremantle, Australia: >install.packages(« MASS ») >library(MASS) >data(crabs)

The str() function allows us to explore the data set by reporting the number of observations and the type of variables: .>str(crabs) ‘data.frame’: 200 obs. of 8 variables: $ sp : Factor w/ 2 levels "B","O": 1 1 1 1 1 1 1 1 1 1... $ sex : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2... $ index: int 1 2 3 4 5 6 7 8 9 10... $ FL : num 8.1 8.8 9.2 9.6 9.8 10.8 11.1 11.6 11.8 11.8... $ RW : num 6.7 7.7 7.8 7.9 8 9 9.9 9.1 9.6 10.5... $ CL : num 16.1 18.1 19 20.1 20.3 23 23.8 24.5 24.2 25.2... $ CW : num 19 20.8 22.4 23.1 23 26.5 27.1 28.4 27.8 29.3... $ BD : num 7 7.4 7.7 8.2 8.2 9.8 9.8 10.4 9.7 10.3...

The data set contains 200 rows or observations corresponding to the 200 crabs measured (200 sampling units) for eight variables listed in columns. “Variety” (sp) and sex (sex) appear as factors with two conditions each. Each crab in each variety/sex cross treatment was numbered from 1 to 50 (index variable, integer status). The other variables are numeric: FL corresponds to the size of the frontal lobe (in mm), RW to the width of the abdomen (in mm), CL to the length of the shell (in mm), CW to the width of the shell (in mm) and BD to the body depth (in mm). The crabs data set is stored in the form of a data frame, which can be easily used to perform operations. If this was not the case, it is possible to transform a matrix into a data.frame with the function as.data.frame(): .>crabsnames(crabs) [1] "sp" "sex"

"index" "FL"

"RW"

"CL"

"CW"

"BD"

Working with the R Software

9

The following operations can be used to explore the data set – functions *head()*, *View()*, *dim()* and *summary()*: >head(crabs) # Display of the first few lines sp sex index FL RW CL CW BD 1 B M 1 8.1 6.7 16.1 19.0 7.0 2 B M 2 8.8 7.7 18.1 20.8 7.4 3 B M 3 9.2 7.8 19.0 22.4 7.7 4 B M 4 9.6 7.9 20.1 23.1 8.2 5 B M 5 9.8 8.0 20.3 23.0 8.2 6 B M 6 10.8 9.0 23.0 26.5 9.8 >View(somlit) # Displays the dataset in a separate window >dim(crabs) # Data-set dimensions (rows and columns) [1] 200 8 >summary(crabs) # summary of variables

With the summary() function, the factors give the amount by method while basic statistics are given for the quantitative variables (minimum, maximum, first and third quartiles, median and average). 1.2.1.3. Operations on variables It is often useful to isolate a variable in order to make it easier to handle, for example, the length of the shell CL. To call a variable contained in a data.frame, several possibilities are available. For an operation on a matrix or a (two-dimensional) data frame, the square brackets are used with two values separated by a comma, which involve the lines before the comma and the columns after the comma: .>crabs$CL # The most used >crabs[,c(“CL”)]# The text is in quotation marks >crabs[,6]# CL being the 6th column of the table

If the same object will be used for the rest of the analyses (such as crabs, in this case), it is of interest to “fix” the object in order to directly access its columns. In this case, the attach() function can be used. The syntax for calling a column is lighter afterwards, just call the variable (crabs becomes useless). However, it is dangerous to attach several objects simultaneously. It is then necessary to detach object 1 with the detach() function, before attaching the next one: >attach(crabs) >CL >detach(crabs)

10

Statistics in Environmental Sciences

Basic operations are possible by specific functions on each variable: >length(crabs$CL) # number of values >sum(crabs$CL) # sum of values >mean(crabs$CL) # mean >min(crabs$CL) # minimum value >max(crabs$CL) # maximum value >range(crabs$CL) # min-max variation range >log (crabs$CL) # natural logarithm >log10(crabs$CL) # base 10 logarithm >sd(crabs$CL) # standard deviation >sd(crabs$CL)/sqrt(length(crabs$CL)) # standard error >median(crabs$CL )# median: 50% of the values below the given value >quantile(crabs$CL) # different quantiles >scale(crabs$CL) # Centering and reduction/normalization/standardization

Many functions can be hindered by the presence of missing data (write NA in order to be recognized in R). The argument na.rm=TRUE usually eliminates this problem: >mean(crabs$CL, na.rm=TRUE) # mean without considering NA

If the data table has missing data, it may be useful to remove all lines with this type of data with the na.omit() function: >na.omit(crabs)

1.2.1.4. Selection and merging of data-sets It is possible to just select a few lines or columns of the data table because of the bracket, the selection of lines is done before the comma and the selection of columns after the comma: >crabs[1 :50, c(« FL », « CL »)] # Selection of lines 1 to 50 for variables FL and CL

The selection on criteria can be done with the subset() function using the different types of relationships > (strict superior), >= (equal or superior), < (strict inferior), subset(crabs, sp== "O" & sex== "F", select=c("FL","CL"))

– crabs indexed from 1 to 10 and from 25 to 30, with the FL variable alone: >subset(crabs, index %in% c(1:10,25:35), select=c("FL"))

Similarly, tables can be glued together. If they have the same column names, they are merged on top of one another with the rbind() function (rows merge). If they have the same line names, the merging is done table by table with the cbind() function: >subset(crabs, sp== ‘O’) ‐>spO # Sub-table of orange crabs >subset(crabs, sp== ‘B’) ‐>spB # Sub-table of blue crabs >rbind(spO,spB) # Merging the two tables below each other with the same column headers

1.2.1.5. Import/export of a data set To export the R file to a spreadsheet such as Excel, it is easier to use the txt format with the write.table() function: >write.table(crabs,"crabs.txt",dec=",",sep="\t",row.names=F)

It is necessary to specify the name of the object, the name wanted for the file with .txt, the decimal separators (by default they will be “.”) and the column separator (preferably tabs with “\t”) and the absence (or presence) of the name of the lines with row.names. Be careful, row.names requires that each line has a different name or number! To import the file, two formats are possible: – text format with a table separator; it will appear with the extension .txt; – CSV format (separator: semicolon); it will appear with the extension .csv. This last format avoids import errors when spaces are present in the text (column names, qualitative variable conditions). In addition, this format is accessible through more spreadsheet software than .txt. The file under the name crabs with a semicolon separator for a .csv format or table separator for .txt must be available in the current directory, and commas as decimal separators in the base file must be able to be imported.

12

Statistics in Environmental Sciences

For the .txt format, the read.table() function is used: >crabscrabscrabs[,c("FL","CL")]->dat

Working with the R Software

13

The stack() function allows us to put the variables one below the other by specifying the variable in a column called ind, with the associated values in a column called values: >stack(dat)->dat_stack ; dat_stack values ind 1 8.1 FL 2 8.8 FL 3 9.2 FL 4 9.6 FL 5 9.8 FL 6 10.8 FL 7 11.1 FL 8 11.6 FL … 395 43.8 CL 396 41.2 CL 397 41.7 CL 398 42.6 CL 399 43.0 CL 400 46.2 CL

From a file of this type, it is possible to return to the “1 column = 1 variable” format with the unstack() function: >unstack(dat_stack) CL FL 1 16.1 8.1 2 18.1 8.8 3 19.0 9.2 4 20.1 9.6 5 20.3 9.8

The transposition of a data table (in other words, the lines become columns and vice versa) is done with the function t(): >t(crabs)

1.2.3. Arranging the data set The arrange() function of the [dplyr] package allows us to arrange a table by an ascending or descending sorting process of qualitative or quantitative variables. It is possible to sort by one or more variables. The order is important: the sorting is first done according to the first variable,

14

Statistics in Environmental Sciences

then for two identical values according to the second variable, etc., desc() allows us to specify a decreasing order within arrange(). By default, the sorting is in ascending order. For example, the table is sorted here by species, then by sex and then by shell length, in descending order: >library(dplyr) >arrange(crabs,sp, sex, desc(CL)) sp sex index FL RW CL CW 1 B F 50 19.2 16.5 40.9 47.9 2 B F 49 17.5 16.7 38.6 44.5 3 B F 48 17.4 16.9 38.2 44.1 4 B F 47 16.7 16.1 36.6 41.9 5 B F 44 15.8 15.0 34.5 40.3 …

BD 18.1 17.0 16.6 15.4 15.3

1.2.4. Pivot tables The count() function of the [dplyr] package allows us to obtain numbers according to qualitative factor classes by using one or more variables. It is then necessary to specify the database with .(,), noting that the factors concerned are separated by the commas. For example, let us take the species sp and sex sex. Here are 50 individuals that are between the cross-conditions of the factor: >library(dplyr) >count(crabs, .(sp,sex)) sp sex freq 1 B F 50 2 B M 50 3 O F 50 4 O M 50

The apply() function allows us to perform an operation on a dimension of the data table. It is necessary to specify the table, the dimension (one for the rows and two for the columns) and then the operation to be carried out, for example, the mean or standard deviation on the variables FL and CL that are stored in dat: >apply(dat, 2, mean) # mean for all columns FL CL 15.5830 32.1055 >apply(dat, 2, sd) # standard deviation for all columns FL CL

Working with the R Software

15

3.495325 7.118983

The with() function allows us to specify which data set the analyses are performed on before writing the function, without having to specify the name of the object in which the variables are stored. For example, to only select crabs of the blue variety, to apply a function, here the average length of the shell: >with(subset(crabs,sp=="B"),mean(CL))

The tapply() function allows us to establish operations according to the classes of one or more combined qualitative factors. Combined with the with() function to specify the database, we need to specify the variable of interest (which is CL here), then the factors in a list argument, and finally, the desired operation (which here, is the average). Note that with two factors, the conditions of one appear in rows and those of the other in columns: >with(crabs, tapply(CL, list(sp, sex), mean)) F M B 28.102 32.014 O 34.618 33.688

The aggregate() function allows calculations to be made on numeric variables according to the conditions of one or more qualitative factors, and lists the results in a table, such as a pivot table. The syntax involves “~”. Under R, the tilde means “according to” and is used by several functions. It must be indicated of the variable on which the calculation is made, the factor or factors determining the terms, and then the interest function. Unlike the tapply() function, the results appear in rows and will be easier to reuse later: >aggregate(CL~sp+sex, data=crabs, mean) sp sex CL 1 B F 28.102 2 O F 34.618 3 B M 32.014 4 O M 33.688

The each() function allows us to apply several operations to the same variable. The operations, then the variable of interest, are specified in brackets. Here is the mean and standard deviation of CL:

16

Statistics in Environmental Sciences

>each(mean,sd)(crabs$CL) mean sd 32.105500 7.118983

The ddply() function of the [dplyr] package allows us to apply an operation according to the conditions of one or more qualitative factors for several variables. The with() function here allows us to specify the starting table from which the data set containing only the quantitative variables of interest is derived. It is necessary to specify the factor(s) – note its particular rating: (Variable 1, Variable 2) – then the operation (for example, colMeans for an average per column). The results are returned in the form of a data table: >with(crabs,ddply(crabs[,c("FL","CL")], .(sp,sex), colMeans)) sp sex FL CL 1 B F 13.270 28.102 2 B M 14.842 32.014 3 O F 17.594 34.618 4 O M 16.626 33.688

The summarize() function of the [dplyr] package allows us to summarize operations on a data table in the same way as the summary() function, with the operations of our choice. The basis followed by different arguments for the different calculations with name=operation must be specified. Here, for example, the mean, the standard deviation and the standard error of the shell length: >summarize(crabs, CL_mean=mean(CL), CL_ET=sd(CL), CL_ES=sd(CL)/sqrt(length(CL))) CL_mean CL_ET CL_ES 1 32.1055 7.118983 0.5033881

1.3. A few graphs to summarize the data set 1.3.1. Display of graphs Some functions create a window before displaying the graph (example: plot() functions); others do not create new graphs, but instead add information about the existing graph: axis(), title(), text(), points(), and lines() functions. By default, creating a new graph erases the previous graph window and therefore the graph it contains. The windows() or x11() functions (written

Working with the R Software

17

like this, without arguments) allow us to display a new graph window in addition to the existing one: the new graph will be displayed in addition to the existing window. It is also possible to display several graphs in the same window using the par(mfrow=c(number1,number2)) function. The “number 1” defines the number of rows, in other words, the number of graphs placed vertically in the window, and the “number 2” the number of columns, in other words, the number of graphs placed horizontally. For example, par(mfrow=c(3,2)) will display a window of six graphs: two graphs side by side on three rows. 1.3.2. Simple graphs (plots) Let us take the crabs data-set, for which the length of the frontal lobe will be represented as a function of the shell length for all crabs in the data set (section 1.2.1.2). This involves using the plot() function, for which many options are available (Figure 1.3(A)). In the plot() function, it is necessary to specify Y~X (or X,Y), then the database (crabs). Finally, the following arguments are optional: – main = adds a title at the top of the graph to specify between quotation marks; – xlab = and ylab = adds captions to the x and y axes, respectively; – type = specifies symbols in points “p”, lines “l” or the two “b”; – pch = specifies the type of symbols used (Figure 1.2); – col = specifies the color of the symbols; – cex = specifies the size of the symbol (by default 1, if < 1, the symbol will be smaller than the default size and vice versa); – xlim = (nbermax; nbermax) and ylim = (nbermax; nbermax) specify the variation range of the x or y axes, respectively, with a vector c(), where the minimum and maximum values are separated by commas: >plot(FL~CL,crabs, main="Crabs", xlab="Shell length (in mm)", ylab="Front lobe length (in mm)",col="black", type= « p », pch=21, cex=0.8, ylim=c(0,30))

18

Statistics in Environmental Sciences

Figure 1.2. Pch symbol for the plot function. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

It is possible to represent the elements according to subgroups on the graph with different symbols, different colors, etc. For example, representing crabs according to their varieties. All that needs to be done is to give different elements (as well as groups) for each desired argument and to specify the distinguishing factor in square brackets. Here, for example, the sp variety (Figure 1.3(B)): >plot(FL~CL,crabs, pch=c(16,15)[crabs$sp], col=c("blue","grey")[crabs$sp], xlab="Shell length (in mm)", ylab="Front lobe length (in mm)")

Figure 1.3. A) Length of the frontal lobe as a function of shell length with a plot() function for the 200 crabs in the crab data-set. B) Differentiation by species. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Working with the R Software

19

It is also possible to add points or text to an existing graph with the points() and text() functions. For example, the graph in Figure 1.3(B) could be made by first projecting the blue crabs with plot() and superimposing the orange crabs with dots. For each one, different arguments like col and pch are differentiated: >with(subset(crabs,sp=="B"),plot(FL~CL,col="blue",pch=16,main="Crabs", xlab="Shell length (in mm)",ylab="Front lobe length (in mm)")) >with(subset(crabs,sp=="O"),points(FL~CL,col="orange",pch=15))

The legend() function then allows us to add a legend by specifying the location (e.g. bottomright, bottomleft, topright, topleft). Here, for example, with bottomright and the different useful arguments with c() functions, as shown here with col for color and pch for numbers for symbols according to alphabetical order: >legend("bottomright",legend=c("Blue","Orange"), col=c("blue","orange"), pch=c(16,15))

1.3.3. Histograms A histogram represents the values of a quantitative variable by classifying them in intervals. The choice of the range of values is not always easy. Indeed, with an interval that is too small, too many often-insignificant variations appear, whereas with an interval that is too high, the variations in the distribution are erased in favor of a distribution that is not very “discriminatory” and does not reveal a potentially normal, bimodal distribution, etc. By default, R calculates an interval over which h=range(x)/ log2(n)+1 (Sturges option). There are also other estimates of the optimal size of the class interval based on other optimization algorithms. The hist() function allows the implementation of a histogram under R. It is necessary to specify the variable of interest (which is CL here) and the type of representation desired, in gross frequencies (proba=FALSE; Figure 1.4(A)) or relative frequencies (proba=TRUE): >hist(crabs$CL,main="Histogram",xlab="Shell length", ylab="Gross amount",proba=F)

It is also possible to choose intervals so that they are heterogeneously (Figure 1.4(B)) or homogeneously finer, for example with the breaks argument and a c() function freezing the boundaries of the interval:

20

Statistics in Environmental Sciences

Gross amount

>hist(crabs$CL,main="Manual histogram",breaks=c(10,20,40,50), xlab="Shell length", ylab="Gross amount",proba=F)

Figure 1.4. Histograms for the shell length of crabs: A) the number of classes (8) is determined by the Sturges method; a unimodal bell curve is observed; B) the number of classes has been manually fixed from heterogeneous intervals

This method of representation is very useful for observing the distribution of variables. It is remarkable if a representation is unimodal with a maximum amount for the class [30–35 mm]. 1.3.4. Boxplots Boxplots provide a lot of information on the dispersion of a variable of interest (Figure 1.5): – the median by a bold line; – a rectangle designating the position of half of the data between the 25% and 75% quartile of the data, designated, respectively, by Q1 and Q3; – the whiskers represent deviations from the maximum high values or the deviation of Q3+1.5 (Q3-Q1) if they are less than the maximum, and from the minimum low values or the deviation of Q1+1.5 (Q3-Q1) if they are more than the minimum;

Working with the R Software

21

– extreme values if there are any corresponding values deviating from the limit of the whiskers. ‘Lower whisker’

MAX value between Minimum OR Q1-1.5(Q3-Q1)

‘Upper whisker’

MIN value between Maximum OR Q3+1.5(Q3-Q1) Q3+1.5*(Q3-Q1) = 53 < MAX

Q1-1.5*(Q3-Q1) = 21 < MIN

Figure 1.5. Illustrations of a boxplot from a series of raw data (gray dots). For a color version of this figure, see www.iste.co.uk/david/statistics.zip

These boxplots obviously only make sense if more than 10 values need to be represented. Otherwise, a bar chart is more appropriate (section 1.3.5). The boxplot() function allows the representation of box plots in R. The variable(s) of interest (e.g. FL and CL; Figure 1.6(A)) should be specified. The same graph parameters as plot() can be used as long as their use is justified: >boxplot(crabs[,c("FL","CL")],xlab="Variables",ylab="Values")

It is also possible to represent the dispersion of a variable according to a qualitative factor. For example, the shell length by species with ~factor (Figure 1.6(B)): >boxplot(CL~sp,crabs, xlab="Varieties", ylab="Shell length")

Averages are not represented on a boxplot. However, it is possible to superimpose them on graphs with a points() function combined with tapply()). Here, they are represented by red dots and are very close to the median (Figure 1.6(B)): >points(tapply(crabs$CL, crabs$sp, mean), col="red",pch=16,cex=2)

22

Statistics in Environmental Sciences

Figure 1.6. Illustrations of boxplots: A) for the length of the frontal lobe and shell on the same graph; B) for the length of the shell according to the variety. The averages for the two varieties have been superimposed by red dots. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

It might be interesting to represent these boxplots according to two factors, such as variety and sex. The representations available in basic libraries do not allow such graphs to be created clearly. It is better to use the [ggplot2] library, which is a little more difficult to understand but provides more complex graphs. With ggplot2, graphs are built in a structured way. First in an object called a graph, the ggplot() function specifies the factor that will be considered on the x-axis (which is sp here) and the quantitative variable (which is CL) on the y-axis. The factor that will be superimposed is then specified in geom_boxplot, and the x and y legend is specified in labs(). The graph is finally launched with the desired theme (Figure 1.7(A)). The theme_bw is one of the simplest and most unlimited: >library(ggplot2) >graphegraphe+theme_bw()

It is also possible to choose a graph in a facet form that corresponds to the factor with the facet_wrap(~factor) function (here, it is sex; Figure 1.7(B): >graphe+facet_wrap(~sex)+theme_bw()

Working with the R Software

23

Figure 1.7. Illustrations of boxplots with two qualitative factors: A) the two factors are crossed on the same graph; B) the secondarily chosen factor separates the graph into facets so that only the conditions of one factor appear per facet. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

1.3.5. Barplots Barplots can also be used when representing the mean with a dispersion estimator such as standard deviations or standard errors (“standard deviation of the mean”). The standard error is an estimate of the dispersion of the data in terms of the number of elements contained in the sample (standard deviation of the mean, section 2.2.2). It is generally represented on barplots in publications. The bars represent the mean, and the whiskers represent the dispersion parameter. For a clear representation, a function called my.barplot() found in an R forum and implemented in R has been used here. The following function has been launched in order to use it: >my.barplot meansemy.barplot(mean,se,xlab="sp",ylab="Shell length", col=c("blue","orange"),beside=T,ylim=c(0,40)) >legend("top", legend=c("F","M"),pch=c(15), col=c("blue","orange"))

Figure 1.8. Barplot illustrations: A) for a single factor; B) for two factors. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Working with the R Software

25

It can be seen that orange crabs have a priori longer shell lengths (Figure 1.7(A)), but it can also be seen that this difference is essentially only apparent in female crabs (Figure 1.8(B)). However, the results are only visual and should be supported by a statistical test. These graphs remain a good visual illustration of statistical test results or data mining. 1.3.6. Pair representations It can be very useful to observe the types of relationships between quantitative variables (absent, linear, unimodal, etc.). The aim is to explore the data in order to make hypotheses before applying linear regressions or correlations between quantitative variables, for example. Let us take the quantitative morphological variables contained in crabs, for example variables 4–8. The pairs() function allows us to explore the relationships between each of these variables in pairs: >pairs(crabs[,c(4:8)])

Figure 1.9. Illustration of representations of relationships of quantitative variables in pairs for the crabs database

26

Statistics in Environmental Sciences

Relationships are explored by taking the variable given on the line as the ordinate and the variable given in the column as the abscissa. For example, the graph on the first row, second column corresponds to FL as a function of RW (Figure 1.9). Each relationship appears twice (although reversed) since the illustration is symmetrical: the relationship between RW as a function of FL is located in the second row of the first column. It is obvious that all the relationships seem linear, but this representation gives no indication of their real (or significant) existence. The following function found on an R forum, called panel.cor.pearson(), completes this representation by adding this upper.panel argument to the pairs() function and allows us to give Pearson correlation coefficients and their level of significance in parallel (see section 6.3.1). Just launch the following lines of code in order to use it: >panel.cor.pearson c(27,25,30,38,32,34,34,35,38,32,39,38,43,39,41,36,41,42,43,55)>data

The output() function allows us to sort the values in ascending order with decreasing=FALSE or descending order with decreasing=TRUE. Sorting the values in ascending order provides a better understanding of the calculations of the main parameters (Figure 2.4): >sort(data, decreasing=FALSE) [1] 25 27 30 32 32 34 34 35 36 38 38 38 39 39 41 41 42 43 43 55

2.2.1. Main trend parameters The average is calculated according to the equation being the values and n being the number of elements in the sample.

with xi

EXAMPLE.– (27+25+30+... +55)/20=37.1 µMol The mean()function allows us to obtain the same result in R. Note that adding na.rm=T allows the calculation to be done even if the variable has missing values: >mean (data) 37.1

The round() function allows us to obtain the same result, but one that is rounded to the desired decimal point, by specifying the latter after the comma. Here, the data are rounded to the unit, in other words, 0 decimal points, for a significant figure corresponding to the data: >round(mean (data),0) 37

Figure 2.4. Method of calculating means, modes, medians and quantiles on a lead content sample in the wings of wild birds (sample of n = 20 individuals). For a color version of this figure, see www.iste.co.uk/david/statistics.zip

36 Statistics in Environmental Sciences

Fundamental Concepts in Statistics

37

The median is calculated according to the values sorted in ascending order. It corresponds to the value located in the middle of the distribution with 50% of the values before and 50% of the values after the distribution (Figure 2.4). If the number of data is odd, it is the central value, if the number of values is even, like in the example, the average of the two central values is calculated. EXAMPLE.– (38+38)/2=38 µMol The median() function allows you to obtain the same result in R: >median(data) 38

Although the mean is the most commonly used parameter, the median is sometimes more appropriate for summarizing a sample. Indeed, the mean has the disadvantage of being susceptible to extreme values, unlike the median (Box 2.1). The mean and median size of 10 master students are calculated here for two situations. For distribution A, a rather small range of values between 155 and 172 cm, the mean and median have very close values that are at the center of the data.

In contrast, for distribution B, where a student stands out (extreme value) with a size of 239 cm, the mean is much higher than the median and only three students have values above it. The mean is therefore driven by the extreme value while the median, by its method of calculation, is always at the center of the values. In this last distribution, the median is therefore more suitable than the mean in order to summarize the sample. Box 2.1. Mean versus median to summarize the sample. For a color version of this box, see www.iste.co.uk/david/statistics.zip

38

Statistics in Environmental Sciences

The mode corresponds to the value with the maximum amount. There may be none, just as there may be several (Figure 2.4). EXAMPLE.– The peak distribution of lead levels is 38 µMol with three values. 2.2.2. Dispersion parameters Quantiles correspond to the values at x% of the data arranged in ascending order. They are calculated with the quantile() function in R. The most common are presented below. – Quartiles at 25% (first quartile) and quartiles at 75% (third quartile): separate the number of elements into four equal portions. The median is the second quartile: >quantile(data,0.25) 25% 33.5 >quantile(data,0.75) 75% 41

– The interquartile range: the difference between the third and first quartiles: >quantile(data,0.75) - quantile(data,0.25) 7.5

– The 10% and 90% deciles: >quantile(data,0.1) 10% 29.7 >quantile(data,0.9) 90% 43

– The variance (noted down as s2) s2 = Ʃ(xi – X)2/(n – 1): corresponds to the sum of the deviations from the square mean and thus measures the dispersion around the mean. It is expressed in the unit of the squared variable and is therefore not very expressive:

Fundamental Concepts in Statistics

39

> var(data) [1] 43.67368

– The standard deviation (noted down as s) s = √s2: expressed in the same unit as the variable and the mean, and is therefore more intuitive to use (~sum of deviations from the mean). We are talking about the standard deviation of the data: >sd(data) [1] 6.608607

– Standard error (noted down as es): this is the standard deviation weighted by the number of sample elements that makes it possible to adjust the accuracy of the estimate of the population mean by the sample size; the larger n is, the smaller es is and the precision is high. Here we are talking about standard deviation of the mean. It is more specific than the standard deviation for graphical representations of data dispersion. - For a mean, es = s/√n: >sd(data)/sqrt(length(data)) [1] 1.477729

- For a percentage (p), es = √(p(1 – p)/(n – 1)). Let us consider, for example, the percentage of values greater than 42 µMol, in other words, three values out of 20: >pn=20 >sqrt(p*(1-p)/(n-1)) [1] 0.0819178

2.3. The laws of probability Laws of probability describe the random behavior of a phenomenon that depends on chance. These laws will be very important for statistical tests since the results obtained from the samples will be compared with the results that would have been obtained at random, in other words, due to fluctuations in sampling (see decision theory in section 4.2). Let us look into the size of 100 white oaks measured in a forest of the Gascony heathlands. The heights can be divided by class and represented

40

Statistics in Environmental Sciences

according to an absolute or relative frequency histogram. The variable here is of the continuous type. The values range from 18.17 to 44.29 m. For this simple population, it is possible to distribute oaks according to intervals and calculate absolute frequencies (number of individuals per class) and relative frequencies (number of individuals in relation to the total number). The probability of an event corresponds to the number of favorable cases out of the number of possible cases (relative frequency), that in turn correspond to the question asked. For example, for the population of 100 oaks, the probability of obtaining oaks in the 25-30 m range is 0.38 (Table 2.1). The characters adopted are: P (20plot(function(x) dnorm(x,30,5), xlim=c(10,50), col="red",add=TRUE,lwd=2) # Add the gaussian law

The dnorm() function is used to obtain the probability density. NOTE.– It is possible to obtain slightly different histograms given that the 100 sizes are randomly drawn. However, the distribution of the corresponding normal distribution will remain the same. First, the probability of obtaining oaks less than 38 m in size in the forest of 25,000 oaks from the sample of 100 oaks will be calculated (Figure 2.10(A)). For example, let us suppose that pruning these trees is mandatory for this size and that we want to know the number of oaks to be pruned for the entire population.

Figure 2.10. Areas of normal distribution corresponding to oaks that are smaller than 38 m (A) and between 27 and 35 m (B) in height. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The first step consists of centering and reducing the data of interest to us, which is 38 m here. Let us take z = 38 – 30/5 = 1.6 for its reduced centered

Fundamental Concepts in Statistics

47

value. We therefore want to obtain the probability of the event P(z < 1.6). The table of standard normal distribution areas gives the values of P(z < x) with the unit and tenth of x in the row and the following decimals in column. Thus, P(z < 1.60) = 0.9452, in other words, there are approximately 94.5% of white oaks for the population of 25,000 individuals and 0.9452×25,000 = 23,630 pruning oaks for the entire population. This is the easiest way to use this table. Now, let us suppose that we want to estimate the probability of oaks between 27 and 35 m in size, and therefore the number of oaks corresponding to the population included in this interval. We must center and reduce the data of interest to us, in other words, z1 = (27 – 30)/5 = –0.60 for the lower limit and z2 = (35 – 30)/5 = 1 for the upper limit. We therefore wish to estimate the area P(z < 1.00) -P(z < –0.60). However, since the curve is symmetrical, the area table only gives the areas below z to the left of the limit. To find the areas on the right, simply perform 1 – P(z < x), given that the total area is equal to 1, so the calculation becomes: P(z < 1) – P(z < –0.60) = P(z < 1) – (1 – P(z < 0.60)) = 0.8413 – 1 + 0.7257 = 0.567

In other words, a probability of events calculated on the basis of the sample of 56.7%. This would represent 0.567 × 25,000 = 14,175 trees for the entire population. These hand calculations can quickly become tedious. In R, it is possible to go directly through the corresponding distribution function (Figure 2.6) with the pnorm() function, which reports the values lower or higher than the desired gross value from the moment the mean and standard deviation of the sample are known. The lower.tail argument is used to specify whether you want the lower probability with lower.tail=TRUE or the value above the threshold set with lower.tail=FALSE. For example A, for values below the fixed x: P(z < 38 m). The desired threshold value (which is 38 here), the mean μ and the standard deviation SD of the sample must be specified. There is no longer even a need to center or reduce data. Here, the mean and standard deviation values of the hypothetical population are considered for simplicity. lower.tail=TRUE specifies that the desired probability is located to the left of the threshold

48

Statistics in Environmental Sciences

value. Note that the results obtained by hand from the standard normal distribution area table are identical to those obtained here: >pnorm(38, mean=30,sd=5, lower.tail=TRUE) 0.9452007

The following example is still example A, but this time for values greater than the fixed x: P(z > 38 m). lower.tail=FALSE specifies that the desired probability is located to the right of the threshold value: >pnorm(38, mean=30,sd=5, lower.tail=FALSE) 0.05479929

For example B, upper limit – lower limit, with the fixing of the lower values for the two arguments: >pnorm(35, mean=30,sd=5, lower.tail=TRUE)-pnorm(27, mean=30,sd=5, lower.tail=TRUE) 0.9452007

Similarly, it is possible to use the qnorm() function in order to estimate the threshold value (size) under which x% of individuals are located from the time the mean and standard deviation of the sample are known. For example, F = 40% of trees: >qnorm(0.40, mean=30,sd=5, lower.tail=TRUE) 28.73326

We can say here that if the population of oak sizes follows a normal distribution, 40% of the trees are less than 28.73 m in size. 2.3.2. Other useful laws of probability Other laws of probability besides the normal law of distribution exist and are likely to be used in some statistical tests. For the majority of them, Table 2.2 shows the cases and tests in which they are involved, the shape of their density curves (because they do not necessarily present the same appearance according to the parameters used), the functions in R to find the probability densities, distribution and the thresholds corresponding to a particular event probability F.

Fundamental Concepts in Statistics

Distribution law

Student’s tdistribution

Binomial distribution

Poisson distribution

Situations

For tests corresponding to the comparison of 2 means of small samples pop sample(pop,20) [1] 4 3678 6004 1302 10190 1461 7110 9551 10429 2680 4757 8692 75 9640 6437 9308 9012 3138

7807

1378

Developin ng a Sampling or o Experimental Plan

53

In thhe example of o the Arcachhon Bay, thee elements coorrespond too stations from which w the benthic b maccrofauna will be samppled. The sstatistical populatiion thereforee correspondds to all posssible stations in the aquaatic area of the bay. b This num mber is infinnite: it referss to all possiible combinaations of longituddes-latitudes distributed over the aq quatic enviroonment of tthe area. These tw wo quantitattive variablees can take any a value beetween 2 values: for examplee, 44°35’ N and 44°455’ N in deccimal degreees or 44.583333 and 44.750000 for the laatitude. The population is i said to bee infinite. A random samplinng of 20 stattions would consist of a random drraw of 2 vaalues per elementt: one value correspondiing to the lo ongitude andd one to the latitude (Figure 3.1).

Figure e 3.1. Examplle of a random m draw of 20 stations in the Arcachon A Bayy. For a color version n of this figure e, see www.istte.co.uk/david/ d/statistics.zip

For example, e for a random sampple of 20 latitu udes: > Lat< samp ple(Lat,20)

Twenty stations are then defined: d statiion 1 with latitude 44.593 and 632 and longgitude 1.08233, etc. longitudde 1.1333, sttation 2 with latitude 44.6 The projection of o the 20 ranndomly seleccted stations can give, byy way of examplee, the result shown in Fiigure 3.1. Th he second drraw would obbviously not givee the same reesult (Figuress 3.2A and 3.2B). Whaat may seem disruptive inn these two figures is thhat some areaas of the bay seeem over-reprresented by random r sam mpling (Westt for 2A, Soouth-East

54

Statistics in Environmental Sciences

for 2B) and others, on the other hand, seem under-represented (North for 2A, South-West for 2B). Researchers may therefore be tempted to standardize the positioning of the stations so that they are more evenly distributed over the study area (Figure 3.2C). Similarly, access to some areas of the bay may be difficult and researchers may want to make it easier for themselves by positioning their stations according to their accessibility or the time required to reach them (walking access, therefore favoring stations near the shore; Figure 3.2D). In both cases, the sampling is no longer random: the positioning of the stations is arbitrarily chosen, each possible positioning no longer has an equal chance of belonging to the sample. The sampling is therefore biased.

Figure 3.2. Positioning of 20 stations distributed according to two random (A and B) or non-random samplings: the stations are chosen so as to be homogeneously distributed from a spatial perspective (C) or according to their accessibility for researchers (D). For a color version of this figure, see www.iste.co.uk/david/ statistics.zip

While it may seem obvious that positioning stations according to their accessibility may tilt representativeness, it is indeed clear that some areas are

Developing a Sampling or Experimental Plan

55

over-sampled or under-sampled. The positioning of stations homogeneously placed over the area may initially seem more “representative” than the area to be studied. Well, that isn’t the case! The term “to think” is precisely that which should be banned because it is the best way to introduce bias into sampling. The danger of reflection is that sampling will be oriented towards expected results and one will therefore have a better chance of finding what is expected: the work will lose its objectivity, which is incidentally the leading quality it must properly demonstrate. It should be kept in mind that not everything is known on the subject covered (which here, is the benthic macrofauna). The distribution that seems “representative” to us, which from a spatial perspective appears to be a homogeneous distribution, implies that it is the environmental variables related to latitude and longitude that structure communities (e.g. temperature, salinity, etc.). If this were the case, distributing the stations in this way would provide the widest possible range of diversity across the bay. However, if this were not the case (and indeed it is not the case!), the sample would have been unintentionally oriented in this direction when other factors could have been more important to consider. This type of procedure could completely cause sampling bias so that the variability induced by these other factors is visible. The best solution is therefore to trust in chance, making it possible to approach the study without a priori knowledge. 3.1.2. Generalization and reliability of the plan The simple random sampling described here is the only way to ensure that each element has the same chance of finding its way into the sample: it is therefore totally fair. Consequently, this is the only way to directly generalize the results to the population as a whole and thus meet the external validity criterion. Any other type of sampling will not allow it or will only allow it after a few corrections. The main advantage of simple random sampling is that position parameters, such as the average number of species at the bay scale, can be simply calculated by averaging the number of species recorded at the 20 stations and the confidence interval of this average with the formulas described in all statistical handbooks. Contrary to popular belief, the size of the sample, and therefore the act of increasing the sampling effort, do not affect its representativeness. As seen above, although it allows a more accurate estimation of population

56

Statistics in Environmental Sciences

parameters from the sample (narrower confidence interval at the mean), precision does not necessarily imply reliability (see section I.2.3). If sampling has not been carried out randomly, greater accuracy of the mean is not useful if its estimate is not reliable. 3.1.3. Variants of random sampling However, most of the time, researchers have ideas about the factors that structure their communities, based on existing literature. It therefore seems appropriate to take this into account. For example, it is known that benthic habitats are the main factors structuring macrofauna communities. Thus, the fauna present on sand will be different from that found in mud or in eelgrass beds (aquatic herbaceous plants). During the sampling conducted in the Arcachon Bay, the researchers chose to consider the “habitat” factor in order to conduct their study more precisely. Thus, they first mapped these habitats and estimated the relative part of each one over the aquatic area considered, before establishing their sampling plan. Five habitats were identified over a total area of 156 km2: channel areas (26%), dwarf eelgrass beds (30%), sea eelgrass beds (30%), sandy seabed (9%) and muddy seabed (6%; Figure 3.3). 3.1.3.1. Disadvantage of simple random sampling The simple random sampling shown in Figure 3.3A was on 8 stations in channels, 11 stations in dwarf eelgrass beds, 4 in marine eelgrass beds, 2 on sandy sea floors and none on muddy sea floors. It is therefore clear that it overestimates the first two to the detriment of the “sand” and “mud” habitats. It was already a foregone conclusion that some habitats could be oversampled compared to others, because of their large geographical area (e.g. meadows or channels versus sand or mud). However, the random nature of the stations sampled does not necessarily reflect the proportion of the areas occupied by these same habitats. In any case, this sampling, although representative of the entire bay, will not give a diverse description of muddy sea floors and will therefore be very limited to sandy sea floors, due to the small number of stations sampled on this habitat. 3.1.3.2. Stratified random sampling Although researchers want to be able to consider the “habitat” factor in their interpretation, it is within their interest to over-represent in terms of the

Developing a Sampling or Experimental Plan

57

number of elements that have sampled the habitats with a small surface area (e.g. mud, sand), even if it means reducing the number of stations with larger habitats. This is stratified random sampling. The population, which in this case is the bay, is subdivided into strata, the habitats, and an independent random sampling is carried out within each stratum. Here, the researchers selected a sample from 4 stations whose location is randomly selected for each of the five habitats considered (Figure 3.3B) for a total number of 20 stations. This sampling makes it possible to balance the number of replicas according to a structuring factor, while considering the random aspect. However, the consideration of a fixed number of stations per stratum means that weighting calculations will be required in order to estimate the average specific abundance at the bay scale, as well as for other statistical calculations, such as the confidence interval: the specific abundance of each stratum must be reported in proportion to each stratum.

Figure 3.3. The 4 most used random samples. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

58

Statistics in Environmental Sciences

3.1.3.3. Random cluster sampling If the number of strata as well as the variability in terms of diversity within each stratum is too large, a larger number of stations per stratum will be required. If at least ten stations per stratum are required for a proper study, only two habitats can be considered for a total of 20 stations. These habitats will be randomly selected from the 5 available habitats and then 10 stations will be sampled within each stratum by simple random sampling. This is a random sampling called cluster sampling (Figure 3.3C). Here, the draw has designated the “herbarium at Zostera marina” stratum and the “sand” stratum. In this type of sampling, the plan is characterized by a hierarchical branched system of elements. In this case, we only have one hierarchical level: the habitat. This is called first degree sampling (Figure 3.4A). The strategy could be more complex with, for example, 4 substrata per habitat. Similarly, random sampling can be done at the substrata level. The draw refers to two habitats and then two substrata to be considered, within which the positioning of stations (e.g. 5) is designated by simple random sampling: this is second degree sampling. And so on for 3 or 4 hierarchical levels. Extrapolation of the mean of the bay’s specific abundance becomes more complex than that of a stratified sample. However, the fact that the habitats were randomly selected from those available (and each of them was therefore given an equal chance to constitute the final sample), makes it possible to generalize the results in terms of the variability of the entire bay. 3.1.3.4. Random systematic sampling Finally, another widely used strategy is systematic random sampling (Figure 3.3D). It is particularly used when trying to distribute the stations over a defined area, but in a more objective way than if it was entirely controlled by the operator, as previously shown (Figure 3.2C). The aim is to identify the number of stations that can be positioned in the area, which in this case, is N (Figure 3.5). For an infinite population, it is possible to place a large number of stations on a very tight network and then number them continuously (imagine 10,000). The sampling step must then be calculated, in other words, N/n = 10,000/20 = 500. Random sampling is based on the positioning of the first station among the 10,000. All that will need to be

Developing a Sampling or Experimental Plan

59

done is to sample all 500 numbered stations in order to obtain 20 stations (restarted at 1 as soon as the 10,000 is exceeded). The final network of stations is regular, so the stations are very homogeneously distributed throughout the space. However, the sampling plan remains partly random. It is therefore particularly well-suited for mapping. Furthermore, it is this type of strategy that is adopted during temporal monitoring, with a rate of constant sampling (monthly or annual macrofauna sampling for a station, etc.).

Figure 3.4. Process for first and second degree random cluster sampling. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

60

Statistics in Environmental Sciences

Figure 3.5. Procedure for random systematic sampling. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

On the other hand, “perfect” scheduling means that the results can be tainted by spatial or temporal autocorrelation, depending on whether the sampling is spatial or temporal: the measurement of a parameter at a station/date can be influenced and therefore not be independent of the one measured at the previous or next station/date. However, this independence between elements is often a prerequisite for some statistical tests. 3.2. Experimental plans2 Let us look at a study on the contamination of chicken eggs with polychlorinated biphenyls (PCBs). These persistent organic pollutants are found in egg yolks in different types of farming, including private farms. The measures proposed to reduce this contamination consist of reducing soil ingestion in hens during rearing, in particular by replacing the earthy or grassy surfaces outside the poultry house with hard surfaces.

2 This section is a simplified and summarized vision of Dagnelie’s book (2012) and is applied to examples in environmental sciences.

Developing a Sampling or Experimental Plan

61

3.2.1. Principles and criteria to be respected This type of study requires an experimental approach that consists of simplifying the complex system of causal relationships observed in nature through simplified systems, in which only a limited number of factors vary: for example, contamination of the soil used to raise poultry and the presence or absence of an external earthy/hard surface versus a concrete surface. It is based on the difference method: different situations are analyzed corresponding to the variation of a factor or the combination of several factors called variant factors. Other factors that may vary in nature are invariant factors (which will be identical in all experimental situations) in the experiment, so as not to interfere with the results of the experiment. Let’s take a look at the simple example of a plan to answer the question “does egg contamination with PCBs depend on the degree of soil contamination?” (Figure 3.6). Chicks are distributed in different poultry houses with different levels of PCB contamination and the variables of interest are measured at the end of their reproduction. – The experimental elements or units are the eggs obtained from hens in the studied poultry houses, in other words, entities of the population on which all the variables of the study are measured. – The statistical population here represents all the eggs present in a given type of territory with the same type of land-based farming. For example, all the eggs in the region over which the experimental structures are spread out. – The hens are raised in two scenarios or treatments: with and without PCB contamination on the ground. The varying factor is a quantitative or qualitative controlled variable corresponding to the types of treatment chosen a priori (e.g. degree of PCB contamination on the ground, which is either 0 or 1). – For each treatment, replicas (e.g. 5 replicas) must be considered in order to integrate the variability in an intra-treatment way. – The measured or response variable must make it possible to answer the question asked, in other words, the contamination of egg yolk with PCBs. It represents a random variable of any value that couldn’t be predicted in advance.

62

Statistics in Environmental Sciences

Figure 3.6. Example of an experimental plan put in place to determine whether the PCB contamination of eggs depends on the degree of soil contamination

Three validity criteria must be respected: – the general validity criterion, in other words, the plan’s ability to specifically answer the question asked, - it is obvious that the choice of the variant factor(s) must be in line with the objective: if the question concerns the relationship between egg and soil contamination, the variant factor will not concern the amount of feed fed to the hens during rearing but will indeed provide breeding environments for PCB contamination in different soils, - similarly, the choice of response variables must make it possible to answer the question asked: here, it is one involving the contamination of egg yolks and also the thickness of the latter’s shell if this variable, measured on the experimental unit, is likely to respond differently to the varying factor. Factors that do not provide any information for the question and/or are not measured on the experimental unit, such as the color of the egg or the weight of hens, will not be considered; – the internal validity criterion, in other words, the plan’s ability to prevent the emergence of rival hypotheses; there must be no more than one possible explanation for the results obtained: - the other factors that may explain a fluctuation in the measured variable to be explained must be controlled, so as not to vary (invariant

Developing a Sampling or Experimental Plan

63

factors). If the contamination observed in egg yolks is likely to vary depending on the age of the laying hens, then the latter must be considered in the same way in all considered situations and replicas, - similarly, the use of evidence that is unaffected by the explanatory factor must be considered in order to relativize the response to fluctuations in the explanatory factor. Thus, rearing without soil contamination should be considered in order to relativize the fact that it can be caused by another source: rain, feed or the gene pool of laying hens (developed themselves in a contaminated or uncontaminated environment), etc.; – the external validity criterion, in other words, the plan’s ability to present results that are as generalizable as possible. The concern here involves the representativeness of the experimental study: - the experimental elements or units present in the different treatments (conditions of the cross-variant factors) must be derived from a “representative” sample, that is to say, a random sample. For an experimental plan, we speak of randomization. All the chicks used for the study must be distributed in the different situations considered (example: concentrations 0 or 1) by random drawing, - similarly, in the case of environmental studies, it is recommended to take treatment ranges that are consistent with fluctuations in the varying factor(s) encountered in the natural environment in order to be able to interpret the results in relation to the latter. If the maximum contamination identified in the natural environment is 1 ng/kg, higher concentrations are usually not worth considering. 3.2.2. Generalization and sensitivity of the plan Consideration of invariant factors will determine the sensitivity and the generalization of the study (Figure 3.7). Some factors can be: – eliminated factors (e.g. no vitamins can be given to hens during rearing); – fixed factors (e.g. all hens are raised with the same number of vitamins, are of the same breed, etc.); – randomized factors (e.g. different breeds of hens are considered and randomly distributed in the different experimental structures).

64

Statistics in Environmental Sciences

If the factors are eliminated or fixed, the sensitivity of the study, in other words, the variability of the response variable, will be lower within each treatment, so it will be easier to observe fluctuations between treatments. Conversely, the statistical population to which the results of the study can be generalized will be limited (e.g. hens of a given breed; Figure 3.7). In contrast, if the factors are randomized, then the results will be generalizable to a larger statistical population (e.g. hens of all breeds) but the response within each treatment will be more fluctuating (e.g. hens of different breeds are likely to respond differently to contamination) and highlighting a variation between situations will be less influential due to a variation in the more fluctuating response variable within each situation (Figure 3.7).

Figure 3.7. Sensitivity versus generalization of the experimental plan according to a consideration of invariant factors. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

3.2.3. Different experimental plans 3.2.3.1. Experimental plans with a controlled factor If one factor alone varies, the plans may differ: – depending on the number of replicas considered: the more important it is, the greater the sensitivity of the experiment will be; – depending on the number of conditions of the factor considered: only 1 contamination versus 1 piece of evidence (Figure 3.6) or various levels of contamination versus 1 piece of evidence (Figure 3.8); – depending on the independence or matching of the situations (for example, if it is a question of looking at the evolution of the contamination over time, a control should be considered for each contamination at the initial time (Figure 3.8).

Developing a Sampling or Experimental Plan

65

Figure 3.8. Examples of multi-treatment experimental plans with 1 controlled factor, with or without temporal matching

3.2.3.2. Experimental plans with several controlled factors When the number of factors that are controlled increases, the conditions of a factor are analyzed according to one or more conditions of one or more factors (Figure 3.9). In the simplest case of considering 2 factors, “PCB concentrations in the soil” (4 conditions) and “soil type” (2 conditions), each factor varies according to one condition from the other: for example, the two types of soil used for poultry farming are considered at maximum PCB concentrations. However, the response obtained from the number of PCBs in egg yolks regarding the soil type will only be valid for this concentration. But it is totally conceivable that the difference in the quantity of PCBs in eggs is much higher in earthy environments than in hard environments for high PCB concentrations, and almost non-existent for very low concentrations. With this experimental plan, it is therefore not possible to see if the response of the measured variable is the same for different PCB concentrations of the “concentration” factor between “earthy soil” and “hard soil” conditions of the “soil type” factor. Although this answer is different, we describe this as an interaction between significant factors. Analyzing the interaction between the two factors considered will only be possible if all the conditions of one factor vary according to all the conditions of the other factor. The plan is then more comprehensive, but the

66

Statistics in Environmental Sciences

number of experimental structures rapidly increases. In the case of considering 5 replicas per condition, this represents 25 structures in the simple plan and 40 in the comprehensive plan. This number increases exponentially by adding factors and replicas, and affects experimental feasibility (Figure 3.9).

Figure 3.9. Involvement of a multi-factorial type of plan concerning the number of structures to consider

3.2.3.3. Experimental block plans In in situ experiments, not all theoretically invariant factors can be determined and are likely to cause the results to be biased or will significantly restrict the statistical population. Imagine that all the environmental structures considered in this case study have different grasslands that may influence the bioavailability of the contaminant when ingested by chickens. This theoretically invariant factor can be considered by a block effect. In the case of a full block experiment, the different replicas correspondding to a given concentration of PCBs in the soil will be divided into different classes that in turn correspond to different grassy areas called

Developing a Sampling or Experimental Plan

67

blocks (Figure 3.10), and will allow this effect to be considered in terms of intra-treatment variability. However, this can considerably increase the number of replicas and therefore the number of experimental structures to consider. The use of an incomplete block plan reduces this number by considering only a portion of the treatments per block and distributing them randomly within each block. In the example, the draw of 3 out of 4 treatments per block omitted the highest concentration for block 1 and the lowest for block 3 (Figure 3.10). In a Latin square, the different structures (replicas) per treatment are randomly distributed per block (Figure 3.10).

Figure 3.10. Examples of blocks and Latin square experimental plans

3.2.4. Experimental plans and reality in the field: crucial choices for statistical analysis The same scientific question can lead to the implementation of different experimental plans depending on the reality of the field. Let us consider two proposed plans to answer the questions: 1) does a PCB contamination of eggs depend on the degree of soil contamination? 2) Does the type of farmed soil influence this contamination? Researchers set up experimental structures in environments contaminated with PCBs and for two different types of soil. They are able to choose the

68

Statistics in Environmental Sciences

fixed concentrations used as a condition of the controlled factor from a wide range of PCB concentrations, but will only be able to study three for feasibility. In addition, the type of soil encountered in each farm is not always the same. Some have earthy surfaces and others have grassy surfaces, for soft surface cases. Lastly, the number of farms available for the experiment is not necessarily identical between the different cross-conditions (X concentrations × soil types).

Figure 3.11. Examples of experimental plans in the face of two concrete realities

All choices will condition the type of plan because the statistical analyses and mathematical formulations will not be strictly identical in each case.

Developing a Sampling or Experimental Plan

69

3.2.4.1. Balanced and unbalanced plans The so-called “balanced” plan corresponds to a strictly identical number of replicas for each situation considered (e.g. proposed Plan B with 5 replicas; Figure 3.11). Unfortunately, this ideal situation is often not possible: the plan is therefore called “unbalanced”. Indeed, often the implementation of experimental structures on the same number of farms by cross-conditions of controlled factors is not necessarily possible, although a large number of replicas bring more power to the statistical analyses used (e.g. proposed Plan B). In addition, a posteriori analyses may show that one or more replicas do not provide results and cannot be considered in statistical analyses (e.g. loss of samples). The maximum number of replicas is therefore considered when possible, but this number is lower for some crossconditions. 3.2.4.2. Fixed and random factors Although different PCB concentrations are possible depending on the availability of farms for the study, the conditions of controlled factor “concentrations” can be “chosen” in two different ways. If they are willingly chosen by the experimenter to correspond to a proportional increasing range or to have a maximum number of replicas per cross-condition, the factors are called “fixed” (e.g. PCB concentrations are fixed by the experimenter to only have 5 replicas for each situation, proposed Plan A; Figure 3.11). Conversely, if the conditions of the controlled factor are randomly determined by drawing lots from those that are available, they are random factors. This “randomization” brings about a broader generalization of the results. 3.2.4.3. Crossover plans and prioritized plans In proposed Plan A, two strictly identical soil types are available for each selected concentration: “earth” and “hard” soil. The soil effect in clay versus hard soil can be tested independently for each PCB concentration. Each varying factor has significance that is independent of the other: it is a crossfactor plan (Figure 3.12). It is possible to analyze the effect of PCB concentration alone, the effect of soil type alone and the interaction between the two factors. In some situations, the conditions of one of the two factors (prioritized factor) do not have any tangible meaning until the condition associated with the other factor (prioritizing factor) is known. For example, if the two types

70

Statistics in Environmental Sciences

of soil are different for each PCB concentration in the soil, then it is a prioritized plan (Figure 3.12). For example, for some concentrations, earth soils are not available but are instead replaced by grassy soils for some PCB concentrations within soft surfaces to be considered. However, these types of surfaces are likely to give different answers. It is therefore not possible to assimilate grass surfaces to earth surfaces. The prioritizing factor is the PCB concentration and the prioritized factor is the soil type. In this case, only the effect of the prioritizing “concentrations” factor and interaction (in other words, different responses of soil types to concentrations) make sense and can therefore be tested.

Figure 3.12. Example of experimental plans with crossed and prioritized factors

3.2.4.4. Supplementary plans and plans with interaction Interaction can only be studied when a comprehensive plan is implemented: each condition of a controlled factor (concentrations) is studied for each condition of another controlled factor (e.g. soil type). It will then be necessary to study the effect of each factor independently and the interaction (concentration effect + soil type effect + interaction). If this is not the case, the plan is said to be strictly supplementary with the sole consideration of the two controlled factors taken independently of one another: for example, the effects of the “concentrations” and “soil type” factors alone. Interaction reflects the difference in response to a factor in relation to the other; the experiment is then a full crossover plan, each condition of a factor is studied according to the conditions of the other factor. If this response is different (e.g. stable PCB concentration response in eggs for hard soil and increasing PCB concentrations for those in earth soil), the interaction is significant (Figure 3.13). Otherwise, this interaction is not significant.

Developing a Sampling or Experimental Plan

71

All these characteristics must be defined before the statistical analyses are chosen. For the two plans proposed here, Plan A is a balanced experimental plan with two crossed fixed factors with interaction and Plan B is an unbalanced prioritized plan with two mixed fixed factors.

Figure 3.13. Interaction in a crossover plan: type of response associated with a significant or insignificant interaction

4 Principle of a Statistical Test1

4.1. The usefulness of statistics Inferential statistics refers to the set of statistical tests used to make decisions about a population or to compare populations based on the information provided by one or more representative sample(s). Let us take the example of the weight of two oyster populations to be compared (Figure 4.1). These two populations are quite different: they are in very large numbers – 8,563 Jacquet oysters and 10,055 Comprian oysters – and have different average weights – 33 g for the Jacquets and 41 g for the Comprian. These two populations are therefore different in reality. It is worth noting that these two populations follow a Gaussian law (see section 2.3.1). For feasibility and ethical reasons (we will not decimate the population at the two stations to measure them in the laboratory), it is impossible to collect all the oysters to weigh them. Thus, it is not possible to know their characteristics directly and they can therefore only be found through a representative sample (simple random sampling, see section 3.1.2 for an example illustrating 20 oysters for each of two populations; Figure 4.1). The distribution and means of the samples are not identical to those of the populations due to sampling fluctuations (see section I.2). However, it is these samples that will allow us to estimate whether or not these two populations are different (or

1 This chapter is a simplified and summarized version of the books by Frontier (1983) and Gonick and Huffman (2016), and is also applied to examples in environmental sciences.

Statistics in Environmental Sciences, First Edition. Valérie David. © ISTE Ltd 2019. Published by ISTE Ltd and John Wiley & Sons, Inc.

74

Statistics in Environmental Sciences

rather, if they have a high probability of being different) using a statistical test.

Figure 4.1. For example, statistical tests make it possible to compare two populations whose parameters are unknown (mean weight of oysters between two stations in the Arcachon Bay), using two samples that have been randomly taken from these populations. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

4.2. Decision theory Have you ever noticed that in the crime series Criminal Minds, Agent Spencer often used incomprehensible babble? When, following an analysis of evidence, his director would ask him if the witness could be guilty, and he would reply with the following type of sentence: “According to the evidence, there is very small probability that he is not be guilty”. This convoluted sentence could very well have been simplified by “there is a good chance that he is guilty”, which is much more understandable. However, Spencer holds a PhD degree and thinks according to the decision theory used in statistics. It focuses on the probability that the results obtained can be linked to chance. In order to understand this theory, let us take the example illustrated in Gonick and Huffman’s book Les statistiques en BD (2016). In the 1960s, several verdicts were challenged in the southern United States on charges of racial bias in the constitution of jury trials. Although 50% of the population was African American, only four African American members were selected for a jury from a panel of 80 members. In theory, the selection of jury members was supposedly random. However, this figure seems very low: can it be the result of chance? (Figure 4.2).

Principle of a Statistical Test

75

To verify this, a law of known probability called binomial distribution can be used (see section 2.3.2). This law makes it possible to estimate the random number of successes (k = 4 African American juries) when several experiments are repeated (n = 80 members) with a known probability (P = 50% of the population being African American). To do this, the binomial distribution law is implemented by setting k = 4, n = 80, p = 0.5 and lower.tail=TRUE to determine the probability of obtaining four or fewer African American jurors from 80 juries (Figure 4.2). >pbinom(4,80,0,5,lower.tail=TRUE) 1.38e-18

The probability of obtaining the observed result at random is therefore very low. It is therefore “very unlikely” that the selection was made at random and therefore, the selection of juries is indeed jeopardized by racial bias. Decision theory consists of comparing the results obtained from the samples (inevitably affected by problems related to fluctuations in random sampling) with those that would have been obtained at random using known probability laws (see section 2.3). This makes it possible to estimate whether or not what is obtained can be linked to mere chance. If the probability is very low, chance is not the only explanation; a real effect is likely.

Figure 4.2. An illustrated example of decision theory based on the example of African American juries by Gonick and Huffman (2016)

76

Statistics in Environmental Sciences

The difficulty in understanding the approach arises from the fact that we usually try to demonstrate a real effect, but statistics ultimately calculates the probability linked to chance: the P-value. The lower this probability is, the more the real effect is likely. 4.3. The statistical approach In order for decision theory to work, it is necessary to make two possible choices that will be designated by working hypotheses. The choice of one or the other of these hypotheses will be made according to the results obtained. – Null hypothesis or H0 hypothesis: sampling fluctuations explain the result. Indeed, we saw in section I.1.2 that two samples from the same population could give different results, simply because of sampling fluctuations. In mathematical terms, this would give the following in the case of comparing the weight of two oyster populations: H0: μjacquets = μcomprian (μ refers to the mean of the population) – Alternative hypothesis or H1: sampling fluctuations alone cannot explain the result obtained. For example, the difference observed between two samples cannot be the result of chance alone; it is real. For oyster populations, this would result in: H1: μjacquets ≠ μcomprian Other, more specific hypotheses can be made, such as: H1: μjacquets < μcomprian or H1: μjacquets > μcomprian In the first case, no presupposition is made on the objective regarding the meaning of the difference; it is called a bilateral test. Whereas in the second case, an assumption is made on the meaning of the difference: it is a unilateral test. The probability of obtaining the observed result at random is then calculated using the laws of known probability, in other words, the probability of having H0 with the sample (in the example of African American juries, this is binomial distribution). This comparison involves calculating a statistic that will be compared with the probability law corresponding to the types of data and objectives set. This probability is the

Principle of a Statistical Test

77

famous P-value. It is this probability, moreover, that Spencer refers to in the series Criminal Minds (see section 4.2). The question now is whether or not this probability is low enough for a decision to be made (1) on the verdict of the witness in Criminal Minds or (2) on the weight of oysters, for example. The decision will then be made based on the P-value. If the latter is very low, the alternative hypothesis will be chosen, and the real effect will be accepted (the H0 prospect will be rejected). By convention, if the P-value is less than 5% or 0.05, then the result is considered significant. In most cases, significant levels are distinguished by *** for a P-value < 0.001, ** for a P-value < 0.01 and * for a P-value < 0.05. In conclusion, in statistics, it is possible to estimate the risk of error by concluding that the result obtained from the sample is done at random. However, there is still a risk since the whole population is not considered. 4.3.1. Concepts of error risk, power and robustness There are two possible types of errors illustrated here that are based on the example of Gonick and Huffman’s smoke detectors (2016). Intended to alert sleepers if a fire breaks out at night, they must be perfectly reliable. However, they may inadvertently be triggered if a scallop burns in the pan or, on the contrary, they may sometimes not be triggered at the start of a fire. The first type of sensor error is to activate when there is no fire, or to detect a result when it does not exist: this is the α error (Figure 4.3(A)). The second type of error is to not detect a fire, a result that is real: it is the β error (Figure 4.3(A)). The α risk corresponds to the risk of rejecting H0 if it is true, whereas the β risk corresponds to the risk of accepting H0 if it is false. Let us look at the weight of apples from different orchards, those from Mrs. PinkLadies and Mr. Golden. The objective is to compare the weight of apples between these two orchards. This comparison is based on representative samples taken in each of the orchards due to the excessive number of apples in each (1,582 and 2,341 apples, respectively). In order to answer this question, a sample of eight apples is taken from each of the orchards. Having access to the weight of apples for samples and not those of orchards considered as a whole (populations) will not allow us to have a frank and categorical answer to the question asked, and it will be tainted with a risk of error. Indeed, the

78

Statistics in Environmental Sciences

statistics will only allow us to test whether or not the differences obtained from the samples in terms of apple weight are the result of chance alone.

Figure 4.3. Illustrated example of the risks of error in statistics through: A) the example of smoke detectors (Gonick and Huffman 2016); B) the application of the comparison between two apple populations. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The working hypotheses are: – H0: mean of Papples (Mrs. PinkLadies) = mean of Papples (Mr. Golden) (which I will accept if there is a high probability that the observed differences are due to chance); – H1: mean of Tapples (Mrs. PinkLadies) ≠ mean of Tapples (Mr. Golden) (which I will accept if H0 is too unlikely). In order to properly formalize the risks involved, let us assume that we know two populations perfectly (for example, the weight of apples in two orchards): a total number N of 1,582 apples, a mean size of 150 g and a dispersion measured by the standard deviation of 20 g for Mrs. PinkLadies’ orchard, versus N = 2,341 apples, a mean of 120 g and a standard deviation of 25 g for Mr. Golden’s orchard. A representative sample of eight apples is taken from each of the orchards. The most well-known test for comparing the means of two samples is the Student’s t-test. If the probability that the

Principle of a Statistical Test

79

differences observed between two samples are due to chance (translated by the P-value of the test), then H0 is accepted, otherwise H0 is rejected and H1 is accepted. It will then be possible to conclude that the two orchards have a different apple weight. The risk of error taken here is 5% or a P-value < 0.5, the highest risk generally recognized in statistics. Forty re-samplings and tests on these samples are performed and the conclusion is noted each time (Figure 4.4). In theory, it would be normal to conclude that H0 is rejected since the populations are different. This is the case for 31 attempts, but in nine of 40 cases, or in 23% of the attempts, H0 is accepted due to sampling fluctuations and other factors. This is the β error: the risk of accepting H0 while H0 is false (in other words, the fire without an alarm according to the example of the smoke detector, as defined by Gonick and Huffman 2012). In addition, if another test is used, such as the Wilcoxon–Mann–Whitney test, which is also appropriate for comparing means, then the error occurs 32 times out of 40, or in 33% of the attempts. The risk of the β error is therefore higher. The difference between the two tests is that the Student’s t-test is based on a known probability law, the Student’s t-distribution (see section 2.3.2), which is called a parametric test, and the Wilcoxon–Mann–Whitney test is based on the ranking principle, and is called a non-parametric test. In the vast majority of objectives, it will almost always be possible to have a parametric test and a non-parametric test to apply. The advantage of the parametric test is that it minimizes the β error, but in contrast, it is subject to application conditions that may be difficult to achieve because it is based on a mathematically known probability law. Lastly, it should also be noted that the β risk varies according to the number of elements in the sample: for example, three of 40 errors would be obtained for n = 20 apples per sample or one of 40 errors for n = 50, as well as the α error, which is set at the beginning. The same work is done by considering two samples of the same size (n = 8) from Mrs. PinkLadies’ orchard, this time from the same population (Figure 4.4). The decision should therefore theoretically lead to the acceptance of H0, because the observed differences can only be the result of the randomness of the sampling. This is what is obtained for 38 attempts, but in two cases out of 40, H0 is rejected and H1 is accepted for both tests. This is the α error: the risk of rejecting H0 when H0 is true. In contrast to the previous error, this risk is identical regardless of the test considered or the n amount considered, there are always two cases out of 40 or a 5% of error if it is an error made at the beginning.

80

Statistics in Environmental Sciences

Figure 4.4. Carrying out 40 test attempts following different sampling of eight apples taken from two different populations (left) or the same population with a Student’s t-test (parametric test) or a Wilcoxon–Mann–Withney test (non-parametric test) and error risk. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Principle of a Statistical Test

81

To formalize this experience: – the error probability of rejecting H0 when H0 is true (saying that there is a difference if there is not one) is called a first-type risk or the α risk. This risk of error is set by the researcher and is effectively the acceptance threshold of H0. By default, it is set at 5%, but some research does not tolerate such a high risk of error. The P-value provided by all statistical software gives the probability related to the rejection of H0 and is fixed by the α risk. The results are conventionally noted by * if P < 5%, ** if P < 1% or *** if P < 0.1% in the scientific literature; – the probability of error of accepting H0 when H0 is false, in other words, not concluding that there is a difference when this difference is real at the population level, is called a second-type risk or the β risk. The lower the β risk is, the more likely it is to conclude that there is a difference if this is the case: it reflects the test power. The latter therefore corresponds to the opposite of β or 1-β. There is no need to fool ourselves: most of the time, when a researcher performs a test to compare two populations, he or she tries to show that there is a difference (otherwise he or she will feel frustrated!). So, we might as well add that this risk of error must also be minimized. Test power is determined by several factors: – the number considered per sample: the stronger the number, the stronger the power. In the example of apples, for n = 8, β = 23% (9/40) or 87% power, whereas for n = 50, β = 2.5% (1/40) or a higher power of 97.5%; – the type of test: in general, parametric tests based on known theoretical distributions are more powerful than alternative non-parametric tests for answering the same question. Consequently, the risks of error are inevitable due to sampling fluctuations, but must be minimized as much as possible. The margin of maneuver consists of: – choosing the α risk that we give ourselves, and this depends on the type of research; – choosing as much as it is possible the use of a parametric test based on a known theoretical distribution law, as long as its application conditions are respected (which may not be the case!);

82

Statistics in Environmental Sciences

– increasing sample sizes as much as possible (as far as experimental or sampling constraints are concerned). Parametric tests will always be more powerful than non-parametric tests. However, since the observations are based on known probability laws, they require verification of the conditions under which these tests are applied. Since there are no application conditions, non-parametric tests will be called robust tests. Here, we are talking about the robustness of the test. We will always try to minimize the β risk and therefore maximize the test power by choosing a parametric test as a priority. It will therefore be necessary to check beforehand whether the application conditions are respected. For example, for the comparison between oyster weights in two stations, the first step will be to check if the samples, and therefore the populations, follow a normal distribution before applying the corresponding parametric test. If this is not the case, the test will be biased, so non-parametric tests will be more appropriate, even if it means losing power and therefore having less chance of showing that there is an effect, if there is one (Figure 4.5). 4.3.2. Independent and paired samples In some cases, samples are taken from two independent populations, such as different sites, for example. The elements are therefore completely independent of each other. This can be exemplified by oysters collected at two stations: Jacquets and Comprian. These are independent samples. In other situations, the samples to be compared are taken from the same elements. For example, cadmium concentrations in muscle and liver for the same batch of oysters: these are “paired” samples. 4.4. Example of the application of a statistical test Here, we will detail the principle of the statistics most commonly used for a comparison of means from samples according to a normal distribution, the Student’s t-test. In section 6.2.1, these tests will be applied in R functions.

Principle of a Statistical Test

83

Figure 4.5. Concepts of power and robustness of a test, and parametric and non-parametric tests. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Let us take a look at the crabs data set (see section 1.2.1.1.1), which is available in the MASS library, in order to try to compare the shell length between orange and blue female crabs. Observation of the histograms, density curves and means suggests that two samples follow (approximately) a normal distribution and that blue female crabs are smaller than orange female crabs (Figure 4.6). Below are the lines of code needed to get this graph with ggplot2: >library(ggplot2) >crabs2mean(subset(crabs2,sp=="B")$CL)->B >mean(subset(crabs2,sp=="O")$CL)->O >ggplot(crabs2, aes(x=CL,y=.. density.., fill=sp,color=sp)) + geom_histogram(position="identity",alpha=0.1)+xlab(label="Shell lengths (in mm)")+ylab(label="Relative amounts")+ scale_color_manual(values=c("Blue", "Orange"))+geom_density(alpha=0.3)+ scale_fill_manual(values=c("Blue", "Orange"))+ theme(legend.position="right",panel.background=element_rect(fill= "white", colour="grey50"))+ geom_vline(aes(xintercept=B),color="blue", size=2)+ geom_vline(aes(xintercept=O),color="orange", size=2)

84

Statistics in Environmental Sciences

Figure 4.6. Histograms of the shell lengths of female crabs, distinguishable by blue and orange. The density curves of each sample were superimposed and the vertical lines correspond to the means of the samples obtained on the 50 crabs of each species. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

4.4.1. Case of a Student’s t-test of two independent bilateral samples The two working hypotheses are stated: – H0: μblue=μorange → the mean lengths are identical between the two populations; – H1: μblue≠μorange → the mean lengths are different between the two populations. Hypotheses are made at the population level, and these samples will be the ones used in order to make a decision at this same level. This is a bilateral test given that no presupposition is made about the meaning of the difference. The parametric mean comparison test (for samples according to normal distribution) is called the Student’s t-test. It is based on the Student’s t-distribution (see section 2.3.2). Its statistics are calculated as follows:

Principle of a Statistical Test

t calculated 

85

XA  XB es

with: – XA and XB being the means of the two samples; – es being the global standard error. On reflection, it corresponds to the clash between two means. The closer they are, the closer t will be to 0. The means are therefore very close and there is little chance of them being different. In contrast, the greater t is, the greater the tcalculated is, and the more likely the means will be different. In addition, the value is weighted by the standard error so the larger the samples are, the smaller es will be and the larger t will be. In reality, the hand-written formula is more complicated, especially when trying to accurately calculate the standard error and the degrees of freedom that will be useful for the test: t calculated 

XA  XB  es

with s 2 

XA  XB 2

(s / n A  s 2 / n B

(n A  1) s A 2  (n B  1) s B2 nA  nB  2

and ddl  n A  n B  2

The values are calculated by taking blue crabs as sample A and orange crabs as sample B: > mean(subset(crabs2,sp=="B")$CL)->B;B #Mean B [1] 28.102 > mean(subset(crabs2,sp=="O")$CL)->O;O #Mean O [1] 34.618 > s2 tcalcul=(B-O)/sqrt(s2/50+s2/50);tcalcul # Calculation of the calculated t [1] -5.54218 > ddltobs abline(v=tobs, col="blue") abline(v=-tobs, col="blue")

The tcalculated is much lower than 0 and comes out of the lower boundary. The decision is made to reject H0 and accept H1, in other words, to accept that the mean shell lengths are significantly different at the threshold α = 5%.

Figure 4.7. Student probability density for female crab samples to be compared with a unilateral (A) and a bilateral (B) test. The limits corresponding to the risk α/2 = 2.5% of the values are reported on each side, due to the unilateral test for (A). The limit corresponding to the risk α = 1 ‰ of the values is shown on the low value side for (B), given that a negative difference value for H0 is assumed. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Principle of a Statistical Test

87

We could have taken a threshold α = 1% by taking an F2 = 0.005 or 1 ‰ with an F3 of 0.0005. However, we can obtain the exact P-value value by using the distribution function of the Student’s t-distribution with the pt function and the arguments tcalcul, ddl and lower.tail=TRUE to specify F(tpt(tcalcul,ddl, lower.tail=TRUE) [1] 1.260477e-07

This probability is much lower than 1‰. The mean shell lengths are therefore significantly different with a P-value of 10–7 (or by convention ***). In addition, the Student’s t-distribution is similar to standard normal distribution. It can effectively be compared to it for samples larger than 30 elements, which is the case here (n = 50 per sample). For smaller samples, the shape remains the same, but flatter: the limits are higher in the high values and lower in the low values due to a mathematical correction made for smaller samples (see section 2.3.2). 4.4.2. Case of a Student’s t-test of two independent unilateral samples In Figure 4.5, blue female crabs appeared to be smaller than orange female crabs. It is therefore possible to assume a significance, given the difference. The two working hypotheses become: – H0: μblue = μorange → the mean lengths are identical between two populations; – H: μblue < μorange or μblue - μorange < 0 → the mean lengths of blue crabs are lower than those of orange crabs. The same test will be used, and the calculation and its projection of the tcalculated is done in the same way on the probability density function: > tcalcul=(B-O)/sqrt(s2/50+s2/50);tcalcul # Calculation of the calculated t [1] -5.54218 > ddlx=seq(-7,7,0.01) # Creating a value sequence for x

88

Statistics in Environmental Sciences

> plot(x, dt(x,ddl),type="l", ylab="Density f(t)",xlab="Continuous variable t") # Plot the density function of the Student’s tdistribution >abline(v=tcalcul, col="red") # Deferral of the calculated t on the density function

For a unilateral test, the α risk considered (here, for example, α = 1‰) is only distributed on one side: here, it is on the lower side of values since a negative value for H1 is assumed. Let us take an α risk here of 1‰ for a change: >F1=0.0001;qt(F1, ddl,lower.tail=FALSE)->tobs >abline(v=-tobs, col="blue")

The tcalculated is much lower than 0 and therefore leaves the lower limit of the α risk. H0 is therefore rejected and H1 is accepted, in other words, the mean shell length of blue crabs is significantly lower than that of orange crabs at the α threshold of 1‰ (in other words, by convention ***). We will later see that all this unraveling is useless because the t.test() function allows this to be performed directly in R. However, these examples illustrate the practical application of a statistical test (section 5.3.8). 4.4.3. Case of a Student’s t-test for comparing a sample to a standard Crabs are only allowed to be fished if they are greater than 33 mm (shell length). We are therefore trying to determine whether the crab population as a whole is larger than this value. The two working hypotheses become: – H0: μcrabs = 33 → the mean lengths are not different from 33 mm; – H1: μcrabs > 33 or μcrabs – 33 > 0 → the mean lengths of crabs are greater than 33 mm. The same test will be used even if the calculation of the tcalculated is done by comparing the mean of the sample to the standard imposed here, 33 mm: t calculated =

X  standard es

Principle of a Statistical Test

89

with: – degree of freedom ddl = n – 1; – X being the mean of the sample; – the standard being the value to be compared with mu = (here, mu = 33); – es being the standard error; – n being the number of elements in the sample (which is 200 here). The t also follows the Student’s t-distribution (or normal distribution for n > 30). Below are the lines of code needed to obtain the values of the tcalculated, the ddl and the Student’s probability density graph corresponding to the test (see Figure 4.8(A)): > (mean(crabs$CL)-33)/(sd(crabs$CL)/sqrt(length(crabs$CL)))->tcalcul > ddl x=seq(-7,7,0.01) # Creating a value sequence for x > x11(); plot(x, dt(x,ddl),type="l", ylab="Density f(t)",xlab=" Continuous variable t") # Plot the density function of the Student's t-distribution > abline(v=tcalcul, col="red")

However, for a unilateral test, the α risk considered (here, for example, α = 1%) is only distributed on one side; on the low values side, since a negative value for H1 is assumed (Figure 4.8). Thus: > F1=0.01;qt(F1, > abline(v=tobs, > F1=0.01;qt(F1, [1] 2.718079 > abline(v=tobs,

ddl,lower.tail=FALSE)->tobs col="blue") ddl,lower.tail=FALSE)->tobs;tobs col="blue")

The tcalculated is less than 0, but does not leave the lower limit. We will therefore take the decision to accept H0 and reject H1, in other words, to say that the mean shell lengths are not significantly different from 33 mm at the α = 1% threshold.

90

Statistics in Environmental Sciences

Figure 4.8. Student probability density for samples of crabs facing the permitted fishing size of 33 mm with a risk α = 1% A) and female crabs compared with a unilateral test with a risk α = 1‰ B). The limits corresponding to the α risk are only positioned on one side because of the unilateral test for A) and B): the negative side for A) given that a negative difference for H1 is assumed and vice versa for B). The values of the tcalculated values are positioned in red. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

However, we can obtain the exact P-value by using the distribution function of the Student’s t-distribution with the pt function and the tcalcul, ddl and lower.tail=FALSE arguments in order to specify F(t > tcalcul). > pt(tcalcul,ddl, lower.tail=TRUE) [1] 0.03855129

This probability is less than 5%, so H1 could have been accepted at the α threshold of 5%, while not at the 1% threshold. 4.4.4. Case of a Student’s t-test of two paired samples In a study conducted by Ifremer, a species of pearl oysters known as Pinctada margaritifera was used as a bioindicator to quantify the level of cadmium (heavy metal) present in Polynesian lagoon waters. The stock of pearl oysters represents about 30,000 individuals in Polynesia. Twelve oysters were collected. Cadmium concentrations in the muscle and liver were analyzed for each oyster at each site.

Principle of a Statistical Test

91

In paired samples, it is useful to compare each element to see if there is a difference. It is this difference that will be tested. The two working hypotheses for a unilateral test become: – H0: μliver – μmuscle = 0 → the differences between concentrations in the muscle and the liver are no different; – H1: μliver – μmuscle < 0 → concentrations in the muscle are lower than those in the liver. Observation of the histograms showing the differences, density and mean curve suggests that the sample follows (approximately) a normal distribution and that concentrations in the liver are higher than those in the muscles (Figure 4.9). Below are the lines of code to get this graph: >Cd_liver-Cd_muscle->diff >hist(diff,main="Histogram",xlab="Difference Cd_liverCd_muscle",ylab="Relative amount", proba=T,ylim=c(0,0.008),xlim=c(200,700)) >density lines(density,col="blue",lwd=2) >mean(diff) 398.6417 >abline(v=mean(diff), col="red",lwd=2)

The parametric mean comparison test for paired samples (here, it is the differences between elements according to a normal distribution) focuses this time on the differences of each element taken two by two. This is called the Student’s t-test for paired samples. It is based on the Student’s probability law. The Student’s t-test statistic for paired samples is: t calculated  ddl  n  1

d 0 sd / n



d sd / n

92

Statistics in Environmental Sciences

with: – d being the mean of the differences between elements; – n being the number of elements; – sd being the standard deviation of these same differences.

Figure 4.9. Histogram of the differences between cadmium concentrations in the liver and muscle for each of the 12 oysters sampled. The density curve was superimposed and the vertical lines correspond to the means of the samples obtained on the 50 crabs of each species. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Upon reflection, this corresponds to the comparison of the means of the differences to 0. The lower it is, the closer t will be to 0, so the differences are very close and unlikely to be different. In contrast, the larger (or smaller) t is, the larger the tcalculated and differences are more likely to be. In addition, the value is weighted by the standard error and therefore, the larger the samples, the smaller es will be and the larger t will be.

Principle of a Statistical Test

93

We will calculate all these values here by taking the differences between those in the liver and those in the muscle, which are assumed to be positive: > tcalcul ddl x=seq(-7,7,0.01) # Creating a value sequence for x > x11(); plot(x, dt(x,ddl),type="l", ylab="Density f(t)", xlab="Continuous variable t") # Plot the density function of the Student's t-distribution > abline(v=tcalcul, col="red")

However, for a unilateral test, the α risk considered (here, for example, α = 1%) is only distributed on one side: here on the high value side, given that a positive value for H1 is assumed: > F1=0.01;qt(F1, > abline(v=tobs, > F1=0.01;qt(F1, [1] 2.718079 > abline(v=tobs,

ddl,lower.tail=FALSE)->tobs col="blue") ddl,lower.tail=FALSE)->tobs;tobs col="blue")

The tcalculated is much lower than 0 and comes out of the upper limit. H0 is therefore rejected and H1 is accepted, in other words, the concentrations in the muscle and liver are significantly different at the threshold α = 1%. Nevertheless, it is possible to obtain the exact P-value by using the distribution function of the Student’s t-distribution with the pt function and the tcalcul, ddl and lower.tail=FALSE arguments in order to specify F(t > tcalcul): > pt(tcalcul,ddl, lower.tail=FALSE) [1] 1.542693e-05

This probability is much lower than 1‰. The mean shell lengths are therefore significantly different with a P-value of 2 × 10–5 (or by convention ***).

94

Statistics in Environmental Sciences

4.4.5. Power test and research for the minimum number of elements to be considered per sample for an imposed power When a statistical test, such as the Student’s parametric test, leads to the acceptance of H0 (that is to say, there is no difference between the parameters tested), it may be of value to calculate the test power, in other words, its ability to draw a conclusion on H1 if H1 is true (real difference). Indeed, even if this test is more powerful than the corresponding nonparametric test, the fact remains that this power depends on the α risk and the number of samples. It can be limited, even for a parametric test. In addition, it may be interesting to find out how much power is required for a sampling or experiment plan a posteriori. For non-parametric tests, knowing the test power can be useful in commenting on the results obtained. 4.4.5.1. Calculation of power There are functions in R to calculate the power and the n for a given power, and are also distributed in many libraries and not always easy to understand (example of the chi-squared test in sections 6.1.2 and 6.1.3; the Student’s t-test in section 6.2.1.5). When these functions do not exist, it is still possible to calculate it with a simple approach based on the same principle as the one detailed in section 4.3.1 (such as the Wilcoxon–Mann– Whitney test, see section 6.2.2.4). Many re-sampling efforts of the data are carried out (noted as N), the corresponding test is carried out each time from the number of n1 and n2 samples (n2 being a standard), the risk of error β is calculated (a number of times when the P-value is greater than 0.05) and lastly, the estimated power (1 – β) is calculated. 4.4.5.2. Calculation of the number of samples required for a high power Similarly, in order to calculate the n required per sample for a given power, it is possible to perform the same operations: n re-sampling, testing and power calculation for an increasing n number. Then, you must find the n minimum in order to obtain the power looked for. Furthermore, functions exist for example for the Student’s t-test (section 6.2.1.5) and can be built for the Wilcoxon–Mann–Whitney test (section 6.2.2.4).

5 Key Choices in Statistical Tests1

5.1. How are keys chosen? Statistical tests are based on known probability laws (which represent chance), notably for parametric tests. This is also often the case for nonparametric tests, but only after the transformation of quantitative data in ranks. However, depending on the type of parameters to be compared (means, proportions, etc.), different tests will be used and therefore different application conditions will have to be checked in order to decide whether or not a parametric or non-parametric test should be used. This chapter guides the choice of analysis to be implemented within the framework of the objectives and the type of data available. Table 5.1 allows us to target our parameter of interest and find the key that corresponds to it. 5.1.1. Key 1 The main tests are used, or at least most commonly used, to test the application conditions of all the tests that will be detailed later, their application conditions and their associated R functions (the sections refer to the parts that use these same tests throughout the work) (Table 5.2).

1 The keys were developed based on those by Scherrer (1984), Millot (2016), the books by Azaïs and Bardet (2012), Hawi (2011), Anderson (2001) and Chapman and Wong (2001).

Statistics in Environmental Sciences, First Edition. Valérie David. © ISTE Ltd 2019. Published by ISTE Ltd and John Wiley & Sons, Inc.

96

Statistics in Environmental Sciences

5.1.2. Key 2 The main tests are used, or at least most commonly used, to compare numbers and proportions, their conditions of application and their associated R functions (the sections refer to the parts that use these same tests throughout the work) (Table 5.3). 5.1.3. Key 3 The main tests are used, or at least the most commonly used, to compare means, their application conditions and their associated R functions (the sections refer to the parts that use these same tests throughout the work) (Table 5.4). 5.1.4. Key 4 The main tests are used, or at least the most commonly used, to link quantitative variables, their application conditions and their associated R functions (the sections refer to the parts that use these same tests throughout the work) (Table 5.5). Parameters

Associated key

Distributions

Key1

Variances

Key 1

Dispersion

Key 1

Independence of observations

Key 1

Medians

Key 2

Amount

Key 2

Binary proportions/data

Key 2

Means

Key 3

Correlation of quantitative variables

Key 4

Regression between quantitative variables

Key 4

Table 5.1. Parameters of interest in the analysis and associated keys

All situations

A quantitative variable explained by a factor

Two qualitative variables with two or more conditions

Dispersions

Comparison of numbers in a contingency table

Table 5.2. Key 1

All situations

All situations

Quantitative variables

5.2.1. Cochran’s rule

5.2.9. Betadisp test

5.2.8. Durbin–Watson test

5.2.7. Goldfeld–Quandt test

5.2.6. Levene’s test

5.2.5. Bartlett’s tests

Distribution following a normal distribution and as many repetitions between samples With or without as many repetitions between samples

5.2.6. Levene’s test

5.2.4. Fisher-Snedecor test

6.1.3. Chi-squared conformity test

5.2.3. Shapiro–Wilk test

Distribution not following a normal distribution

Distribution following a normal distribution

Qualitative variables with at least two classes

Ordinal qualitative variables

Two qualitative variables with at least 6.1.3. Chi-squared two classes homogeneity test

Tests 5.2.2. Kolmogorov–Smirnov test

Conditions of application One quantitative variable and one qualitative variable with two classes



One or more variances observed

Two variances observed

A distribution with one theoretical distribution (normal distribution)

Two distributions

Situations

Independence of observations

Variances

Distributions

Parameters



library(vegan) ; beatdisper()

library(lmtest) ; dwtest()

library(lmtest) ; gqtest()

library(car) ; leveneTest()

library(car) ; lbartlett.test()

leveneTest()

var.test()

chisq.test()

shapiro.test()

chisq.test()

ks.test()

R functions

Key Choices in Statistical Tests 97

Proportions

Amount

Medians

Parameters

Homogeneity of dispersion variances between samples

With R2 and a post hoc comparison test

With R2 and a comparison test post hoc

With the equation Y~ X, R2 and a comparison test post hoc

Several numbers observed compared to several theoretical ones; unilateral or bilateral

Several amounts observed; unilateral or bilateral

Conditions of application of GLMs

With the equation Y~ X, R2 and a post hoc comparison test

Homogeneity of dispersion variances between samples

Conditions of application of GLMs

Cochran’s rule not respected

Cochran’s rule

Cochran’s rule not respected

Cochran’s rule





Two medians observed; unilateral or bilateral

Several medians observed

Conditions of application

Situations

8.3. PERMANOVA

7.5.1. GLM under binomial distribution

6.1.1. Fisher’s exact test

6.1.3. Chi-squared conformity test

6.1.1. Fisher’s exact test; Kruskal– Wallis test

6.1.2. Chi-squared homogeneity test

8.3. PERMANOVA

7.5.2. GLM under Poisson distribution

8.2.1. Kruskal–Wallis test

6.2.2. Wilcoxon–Mann–Whitney test

Tests

library(vegan) ; adonis()

glm(..., family = binomial(link=logit))

fisher.test()

chisq.test()

kruskal.test()

fisher.test()

chisq.test()

library(vegan) ; adonis()

glm(…, family = poisson(link=log))

kruskal.test()

wilcox.test()

R functions

98 Statistics in Environmental Sciences

unilateral or bilateral

Several observed compared to several theoretical ones;

Several proportions observed; unilateral or bilateral

Two proportions observed, one of which induces a pairing; unilateral or bilateral

One proportion observed compared to a theoretical one

Two proportions observed; unilateral or bilateral

binom.test()

6.1.5. Binomial test

6.1.2. Chi-squared test

6.1.6. Cochran–Mantel–Hantzel test

Table 5.3. Key 2

A qualitative variable with two classes to one qualitative variable with several classes while respecting Cochran’s rule

Two qualitative variables with two classes to one qualitative variable with several classes

chisq.test()

mantelhaen.test()

fisher.test()

chisq.test()

mcnemar()

6.1.4. McNemar’s test

chisq.test() binom.test()

6.1.5. Binomial test

fisher.test()

binomial(link=logit)

6.1.2. Chi-squared conformity test

6.1.1. Fisher’s exact test

6.1.3. Chi-squared homogeneity test

A qualitative variable with two classes to one qualitative 6.1.3. Chi-squared homogeneity variable with several classes: test – Cochran’s rule; 6.1.1. Fisher’s exact test – Cochran’s rule not followed.

– Cochran’s rule not followed.

– Cochran’s rule;

One qualitative variable with two classes and one to two classes with pairing:

1 qualitative variable with two classes

- Cochran’s rule not followed.

- Cochran’s rule;

Two qualitative variables with two classes:

Key Choices in Statistical Tests 99

Two paired means; unilateral or bilateral

A means compared to a standard; unilateral or bilateral test

Two independent sample means; unilateral or bilateral test

Situations

Actual values with Two or more means required equation Y ~ X and test post hoc

Two means

Parameters

wilcox.test(…, paired=TRUE) lm(Y~ X, data); anova() lm(Y~X1*X2, data); anova() lm(Y ~X1*X2, data); library(car) ; Anova(…, type=« III ») lm(Y~X1+Xl:X2, data); library(car); Anova(..., type=« III »)

6.2.1.2. Welch’s t-test 6.2.2.1. Wilcoxon–Mann–Whitney test 6.2.1.3. Student’s conformity t-test 6.2.2.2. Wilcoxon–Mann–Whitney conformity t-test 6.2.1.4. Student’s t-test for paired samples 6.2.2.3. Wilcoxon test for paired samples 7.4 3. One-factor ANOVA 7.4 4.1. Multifactorial ANOVA (studies of pure effects and interaction); type I 7.4.4.2. Multifactorial ANOVA (studies of pure effects and interaction); type III 7.4.4.3. Multifactorial ANOVA (studies of the effect of the

Normal distribution of the two samples; Non-homogeneous variances Non-normal distribution of at least one of the two samples Normal distribution of the sample Non-normal distribution of the sample Normal distribution of differences between sample pairs Non-normal distribution of differences between sample pairs Conditions for the application of linear models on residues: – one explanatory factor; – several intersecting factors, balanced plan; – several intersecting factors, unbalanced plan;

t.test(..., paired=TRUE)

wilcox.test(..., mu=)

t.test(..., mu=)

wilcox.test()

t.test(..., var.equal=TRUE)

t.test(..., var.equal = FALSE)

6.2.1.1. Student’s t-test for independent samples (n > 30: test Z; n < 30 ; t-test)

Normal distribution of the two samples; homogeneous variances

R functions

Tests

Conditions of application

100 Statistics in Environmental Sciences

8.2.2. Kruskal–Wallis test 8.2.4. Scheirer–Ray–Hare test 8.2.5. The Friedman test

Conditions of application not met: – one-factor explanatory plan; – two-factor explanatory plan; – plan with pairing.

Real values

Table 5.4. Key 3

8.3. PERMANOVA

Homogeneity of dispersion variances between samples

7.5.4. GLM under Gaussian distribution 7.5 3. GLM under gamma distribution

With R2 and a comparison test post hoc

Conditions for applying GLM to observations; different situations that are comparable to ANOVA plans but for: – real values; –real values that are not zero.

– several hierarchical factors; hierarchical factor and interaction) – mixed effect with a pairing or 7.4.4.4. ANOVA with repeated block effect. measures

Kruskal.test() library(rcompanion); scheirerRayHare() friedman.test()

library(vegan); adonis()

glm(…, family = gaussian(link=identity) glm(…, family = Gamma(link=log)

lme(Y ~ X, random = ~ 1/ Block, data)

Key Choices in Statistical Tests 101

Situations

Two quantitative variables

Quantitative variable explained by an explanatory variable

Quantitative variable explained by several explanatory variables

Parameters

Correlations

Regression with the equation Y~X

Regression with the equation Y~X1+…+Xn

Tests

6.3.2. Spearman’s correlation 6.3.2. Kendall’s correlation

7.5.4. GLM on Gaussian distribution 7.5.1. GLM on binomial distribution 7.5.2. GLM on Poisson distribution 7.5.3. GLM on gamma distribution

7.5.4. GLM on Gaussian distribution 7.5.1. GLM on binomial distribution 7.5.2. GLM on Poisson distribution 7.5.3. GLM on gamma distribution

Table 5.5. Key 4

Conditions of application for GLM: – real Y data; – proportion data; – discrete Y data; – real non-zero data.

Conditions of application of linear 7.4.2. Multiple linear models on residues regression

Conditions of application for GLM: – real Y data; – proportion data; – discrete Y data; – real non-zero data.

Conditions of application of linear 7.4.1. Simple linear models on residues regression

Conditions not met

Conditions of application of linear 6.3.1. Pearson’s correlation models on residues

Conditions of application

glm(…, family = gaussian(link= identity) glm(…, family =binomial(link=logit) glm(…, family = poisson(link=log) glm(…, family = Gamma(link=log)

lm(Y~X1+…+Xn, data)

glm(…, family = gaussian(link=identity) glm(…, family =binomial(link=logit) glm(…, family = poisson(link=log) glm(…, family = Gamma(link=log)

lm(Y~X, data)

cor.test(…, method=‘spearman’) cor.test(…, method=‘kendall’)

cor.test(…, method=‘pearson’)

R functions

102 Statistics in Environmental Sciences

Key Choices in Statistical Tests

103

5.2. Verification tests of application conditions Only a few tests will be analyzed in order to become familiar with the statistical analysis procedure (e.g. Fisher–Snedecor test, section 5.2.4). 5.2.1. Cochran’s rule Cochran’s rule is useful for comparing observed numbers with theoretical numbers or two sets of numbers (chi-squared test or Fisher’s exact test). It requires that a minimum of five numbers be met in each class of the contingency table used for these tests. This is the comparison of oyster parasitism between the Jacquets and Comprian stations. Let us consider 20 oysters sampled per station and the presence of the parasite listed in each station. A data table with two columns is constructed, with one column listing the station and the other listing whether or not the oyster in question is parasitic: > Par dim(Par) [1] 40 2

It has 40 lines, as many as the oysters sampled, 20 for Jacquets (J) and 20 for Comprian (C). The head() function allows us to see what the data table looks like: > head(Par) Stations Parasitism 1 J Y 2 J N 3 J Y 4 J N 5 J N 6 J Y

The table() function makes it possible to create a contingency table, in other words, to calculate the numbers for each condition of two crossed factors by specifying two qualitative variables, Par$Stations with two

104

Statistics in Environmental Sciences

conditions (J for Jacquets and C for Comprian) and Par$Parasitism (N for non-parasitic and O for parasitic): > TCF ks.test(F,"pnorm",mean(F),sd(F)) One-sample Kolmogorov-Smirnov test data: F D = 0.19111, p-value = 0.7294 alternative hypothesis: two-sided

The P-value is higher than the α threshold, H0 is accepted: the oak population therefore follows a normal distribution to the risk α = 5%. Note that this function can be used with any type of probability distribution. 5.2.2.2. In the case of comparing two population distributions The two working hypotheses are: – H0: the two distributions are identical in the target population; – H1: the two distributions are different in the target population. Let us take the example of 10 vine leaves sampled on the Chevalier estate and 10 on the Couhins–Lurton estate, located in the Appellation controlled provenance of Pessac–Leognan. For each leaf, the quantity of minerals was measured. Do these quantities of mineral salts have different distributions, between the two domains? Thus: >CheCL ks.test(Che,CL) Two-sample Kolmogorov-Smirnov test data: Che and CL D = 0.6, p-value = 0.05465 alternative hypothesis: two-sided

The P-value is higher than the α threshold (but not by much), H0 is accepted. Mineral salt distributions are not significantly different between the two domains of risk α = 5%.

106

Statistics in Environmental Sciences

5.2.3. Shapiro–Wilk test The Shapiro–Wilk test is the most powerful test for testing the normality of the data. It is based on quantiles of values. The W statistic can be interpreted as the R2 obtained between the series of quantiles generated from the normal distribution and the one obtained from the data (Figure 5.1).

Figure 5.1. Principle of the Shapiro–Wilk normality test. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

In R, it can be applied by the shapiro.test() function by specifying the sample values. Let us take a look at a sample from a forest population consisting of 13 elements. Does the population follow a normal distribution? So: >FA shapiro.test(FA) # Population A normality test Shapiro-Wilk normality test data: FA W = 0.92201, p-value = 0.267 # The p-value > 5%, I accept H0 and the sample follows a normal distribution

Key Choices in Statistical Tests

107

The P-value is higher than the α threshold, H0 is preserved, in other words, the population follows a normal distribution. 5.2.4. Fisher–Snedecor test The Fisher–Snedecor test is the most powerful test for testing the homogeneity (or equality) of two variances. It is based on the ratio between two variances concerned: Fcalculated 

s 2A 2

s B

by convention, s 2A  s 2B ddlA  n  1 ddlB  n  1

The statistics of the F test are based on the Fisher–Snedecor probability distribution. Let us take the example of a researcher who is interested in the variability of sea lion jaws and tries to determine if this variability can be applied to both males and females. He, therefore, measures the length of jaws on 10 males and 10 females. The two working hypotheses are: – H0: s2A=s2B => s2F=s2M; – H1: s2A≠s2B => s2F≠s2M. Thus: > F M data.frame(Sex=c(rep("F",times=10),c(rep("M",times=10))),Mandibule=c( F,M) )->data # Sharing of F1 and F2 data in a table (cf )

The variance for each of these samples is calculated with tapply(): > var var(M)/var(F)->Fcalcul;Fcalcul [1] 14.14603 > ddltobs abline(v=tobs, col="blue")

Figure 5.2. Projection on the probability density of the Fcalculated and the 5% risk limit. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

This dissection is not necessary in R since the var.test() function allows us to obtain the same results by just specifying the two samples. It gives the P-value directly:

Key Choices in Statistical Tests

109

> var.test(M,F) F test to compare two variances data: M and F F = 14.146, num df = 9, denom df = 9, p-value = 0.0005327 alternative hypothesis: true ratio of variances is not equal to 1 95 percent confidence interval: 3.513675 56.951848 sample estimates: ratio of variances 14.14603

Given that the P-value is less than 0.001, H1 is accepted and the variances are not homogeneous between the two male and female populations in sea lions with a risk α = 0.001. 5.2.5. Bartlett's test Bartlett’s test is used to test the homogeneity (or equality) of two or more variances when there is much repetition per sample. It is one of the most widely used, although Levene’s test is more powerful. The two working hypotheses are: – H0: s2A=s2B=…=S2n; – H1: at least one of the variances is different from the others. Researchers want to highlight that oat production depends on the nitrogen concentration contained in the fertilizer used in agricultural fields. First, they want to know if the variances of production are homogeneous between concentrations. They consider four nitrogen concentrations (0cwt, 0.2cwt, 0.4cwt and 0.6cwt). The data are available in the oats database of the MASS library: >library(MASS) >data(oats) >str(oats) ‘data.frame’: 72 obs. of 4 variables: $ B: Factor w/ 6 levels "I","II","III",.. : 1 1 1 1 1 1 1 1 1 1... $ V: Factor w/ 3 levels "Golden.rain",..: 3 3 3 3 1 1 1 1 2 2 ... $ N: Factor w/ 4 levels "0.0cwt","0.2cwt",.. : 1 2 3 4 1 2 3 4 1 2... $ Y: int 111 130 157 174 117 114 161 141 105 140...

110

Statistics in Environmental Sciences

This database contains in particular the quantitative variable production to be explained (Y) and the variable nitrogen concentration in the qualitative N fertilizer, also with four conditions (0.0cwt, 0.2cwt, 0.4cwt and 0.6cwt) representing four samples. The contingency table shows the equivalent number of replicas per sample: >table(oats$N) 0.0cwt 0.2cwt 0.4cwt 0.6cwt 18 18 18 18

Bartlett’s test is performed with the bartlett.test() function by specifying the quantitative variable, tilde, the factor defining the samples and then the database: > bartlett.test(Y ~ N, data = oats) Bartlett test of homogeneity of variances data: Y by N Bartlett’s K-squared = 0.53378, df = 3, p-value = 0.9114

The P-value is greater than the risk α = 0.05, H0 is accepted and the variances are not significantly different from the risk α = 0.05. 5.2.6. Levene’s test Levene’s test is the most powerful test for testing the homogeneity (or equality) of two or more variances, whether the number of repetitions between samples is the same or not. The two working hypotheses are: – H0: s2A=s2B=…=S2n; – H1: at least one of the variances is different from the others. Researchers want to test the hypothesis that plant organs adapt to a limitation of light for their photosynthesis by increasing their absorption area. According to this hypothesis, plants would adapt by increasing the surface area of their leaves in an environment where light would be a

Key Choices in Statistical Tests

111

limiting factor for the same species. The researchers are therefore setting up a sampling plan in order to test the morphological response of the leaves of a species, the holm oak, to light limitation. One of the factors that may influence the availability of light in forestry is the position of the leaf on the tree: the leaves at the bottom of the tree are likely to be more shaded than those at the top of the tree. The data are available in the database on the file foretchenes.txt: Les données sont disponibles dans la base sur le fichier foretchenes.txt : >read.table("foretchenes.txt", header=T,dec=",")->data >str(data) ‘data.frame’: 202 obs. of 5 variables: $ Type : Factor w/ 2 levels "Entretenue","Mixte": 1 1 1 1 1 1 1 1 1 1... $ Holmoak: Factor w/ 4 levels "BG","CX","FR",.. : 2 2 2 2 2 2 2 2 2 2... $ Holmbis : Factor w/ 2 levels "P1","P2": 1 1 1 1 1 1 1 1 1 1 ... $ Strata : Factor w/ 3 levels "haut1","haut2",..: 1 1 1 1 1 1 1 1 1 1 ... $ Lmax : num 4.7 6.08 5.28 5.5 5.83...

This database contains in particular the Lmax quantitative variable and the stratum factor that defines three samples (haut1 for high, haut2 for intermediate and haut3 for low). The contingency table shows the different number of replicas per sample: > table(data$Strata) haut1 haut2 haut3 70 69 63

As the plan is unbalanced, Levene’s test is more appropriate. It is done with the leveneTest() function of the car library by specifying the quantitative variable and the factor defining the samples: > library(car) > leveneTest(data$Lmax, data$Strata) Levene’s Test for Homogeneity of Variance (center = median) Df F value Pr(>F) group 2 4.1515 0.01712 * 199 –-

112

Statistics in Environmental Sciences

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

The P-value is lower than the risk α = 0.05, H0 is rejected and H1 is accepted. At least one of the variances is different from the others with the risk α = 5%. 5.2.7. Goldfeld–Quandt test The Goldfeld–Quandt test is used to test the homogeneity of variances for quantitative variables. The two working hypotheses are: – H0: the variances are homogeneous; – H1: the variances are not homogeneous. Let us take the famous example of Anderson’s irises. Morphological measurements were carried out on flowers belonging to three iris species: Iris setosa, I. virginica and I. versicolor (50 for each). The data are accessible in the iris data object. It consists of 150 observations (50 per species), four quantitative morphological variables as mentioned above and a qualitative variable in the form of a factor, the species: >data(iris) > str(iris) ‘data.frame’: 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

The available variables include sepal and petal lengths for 150 individual irises. The researchers first want to find out if the variances of the “Sepal lengths” linear model, according to petal lengths, are homogeneous.

Key Choices in Statistical Tests

113

The Goldfeld–Quandt test is accessible via the lmtest library and must be applied to the “Sepal lengths” linear model according to petal lengths: > LM library(lmtest) > gqtest(LM) Goldfeld-Quandt test data: LM GQ = 0.92953, df1 = 73, df2 = 73, p-value = 0.6221 alternative hypothesis: variance increases from segment 1 to 2

The P-value is higher than the risk α = 0.05, H0 is accepted and the variances are not significantly homogeneous to the risk α = 0.05. 5.2.8. Durbin–Watson test The Durbin–Watson test makes it possible to test the independence (versus autocorrelation) between data, particularly during systematic random sampling. The two working hypotheses are: – H0: data independence; – H1: autocorrelation in the data. On the same iris data set in section 5.2.7 regarding the same “Sepal length” relationship according to petal length, the researchers analyze whether or not the data are independent. The Durbin–Watson test is accessible via the lmtest library and must be applied to the “Sepal length” linear model according to petal lengths: > LM library(lmtest) > dwtest(LM) Durbin-Watson test data: LM DW = 1.8673, p-value = 0.1852 alternative hypothesis: true autocorrelation is greater than 0

The P-value is greater than the risk α = 0.05, H0 is accepted and the data are independent of the risk α = 0.05.

114

Statistics in Environmental Sciences

5.2.9. Homogeneity test of betadisp variances The betadisp variance homogeneity test is used to test the homogeneity of dispersion between samples based on a matrix of associations, and measures the squared distances to centroids in each group and compares them with the global centroid of data. The two working hypotheses are: – H0: the dispersion of data is identical between groups; – H1: at least one of the dispersions is different from the others. On the same iris data set in section 5.2.7, the researchers analyze the differences in dispersion of petal length between three iris species. The betadisp test on the qualitative factor defining the samples is accessible by the betadisper() function of the vegan library. It is based on a matrix of associations, constructed from Euclidean distances. It is necessary to specify the matrix, followed by the factor tested, then the method. The most used is the centroid method. It is followed by an anova() function upon the results of the betadisper() function: library(vegan) betadisper(dist(iris$Petal.Length), iris$Species, type = "centroid")->CA;anova(CA) Analysis of Variance Table Response: Distances Df Sum Sq Mean Sq F value Pr(>F) Groups 2 2.6700 1.33502 20.683 1.216e-08 *** Residuals 147 9.4881 0.06454 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

The P-value is lower than the risk α = 0.05, H0 is rejected and H1 is accepted. At least one of the groups has a different dispersion from the others.

6 Comparison Tests of Unilateral and Bilateral Parameters1

6.1. Comparisons of numbers and proportions 6.1.1. Fisher’s exact test Fisher’s exact test consists of comparing observed numbers with theoretical numbers or probabilities, as well as comparing observed numbers or probabilities with each other, particularly when Cochran’s rule is not respected. This test is known as being accurate because the probabilities can be accurately calculated instead of using an approximation, like in the chisquared tests. Let us look at the comparison of oyster parasitism between the Jacquets and Comprian stations, for which Cochran’s rule was not respected (see section 5.2.1). Twenty oysters were sampled per station and the presence of the parasite was listed in each station. The underlying question is: are there more parasitized oysters in one of the two stations? The two working hypotheses are: – H0: the proportions or numbers are identical between the two target populations => oysters are equally parasitic at Jacquets and Comprian stations;

1 This chapter is a simplified and summarized vision of Millot’s book (2016) and applied to examples in environmental sciences.

Statistics in Environmental Sciences, First Edition. Valérie David. © ISTE Ltd 2019. Published by ISTE Ltd and John Wiley & Sons, Inc.

116

Statistics in Environmental Sciences

– H1: the proportions or numbers are different between the two target populations => oysters are more parasitic at one of the stations. Let us take the TC contingency table built in section 5.2.1 with the table() function: > TCmosaicplot(TC,cex.axis=2)

No

Yes

Figure 6.1. Illustration of the results of the contingency table constructed from the population of parasitic oysters at the Comprian and Jacquets stations

The graph shows gray rectangles that are proportional to the number per class (see section 1.3.7). The largest number here is of oysters that are not parasitic at Comprian, and the smallest number is of oysters that are parasitic at Comprian. From the graphs, it appears that parasitic oysters are more

Comparison Tests of Unilateral and Bilateral Parameters

117

present at Jacquets than at Comprian, but there is only a small difference (Figure 6.1). Fisher’s exact test is performed with the fisher.test() function and by specifying the contingency table: > fisher.test(TC) Fisher’s Exact Test for Count Data data: TC p-value = 0.3008 alternative hypothesis: true odds ratio is not equal to 1 95 percent confidence interval: 0.5388583 14.7603748 sample estimates: odds ratio 2.600904

The P-value is greater than 5%, H0 is accepted: the parasitic prevalence is not significantly different between Jacquets and Comprian. This test can also be used in a unilateral way by adding the alternative argument with two possibilities: less or greater. By default, the bilateral way is used (alternative=“two-sided”). For example, to test the following working hypotheses: – H0: oysters are equally parasitic at both Jacquets and Comprian; – H1: oysters are less parasitic at Comprian than at Jacquets. For a table corresponding to Comprian/non-parasitic on the upper left box and Comprian/parasitic on the lower left box, the alternative argument will be greater: > fisher.test(TC, alternative="greater") Fisher’s Exact Test for Count Data data: TC p-value = 0.1504 alternative hypothesis: true odds ratio is greater than 1 95 percent confidence interval: 0.6653218 Inf sample estimates: odds ratio 2.600904

118

Statistics in Environmental Sciences

The P-value is greater than 5%, H0 is accepted and Jacquet’s parasitic prevalence is not significantly higher than that of Comprian. 6.1.2. The chi-squared conformity test The chi-squared conformity test consists of comparing observed numbers with theoretical numbers or probabilities. Its condition of application is Cochran’s rule. Suppose we wanted to know if the sample of the 20 oyster weights measured at Jacquets station followed a normal distribution. The mean sample size is 32.4 mm and the standard deviation is 5.5 mm. The two working hypotheses are: – H0: the theoretical proportions are the real proportions in the population => the frequencies observed in each class correspond to those given by standard normal distribution; – H1: the theoretical proportions are not the real proportions in the population => the frequencies observed in each class do not correspond to those given by standard normal distribution. The histogram representing the weight dispersion shows five weight classes [20–25 mm], [25–30 mm], [30–35 mm], [35–40 mm], [40–45 mm] (Figure 6.2). The probability for each class interval can be calculated using standard normal distribution. For example, for the [30–35 mm] weight class: >pnorm(35, mean=32.4,sd=5.5, lower.tail=TRUE)-pnorm(27, mean=32.4,sd=5.5, lower.tail=TRUE) 0.3508

It is then possible to calculate the corresponding frequencies for 20 individuals: >0.3508*20 7.016

Comparison Tests of Unilateral and Bilateral Parameters

Jacquets

0.0122 0.0763 0.2415 0.3508 0.2354 0.082

0.244 1.526 4.83 7.016 4.708 1.64

119

X = 32.4 s = 5.5; n = 20

0 2 4 8 4 2

Figure 6.2. Illustrations of the chi-squared conformity comparison Figure 6.2. Illustrations of the chi-squared conformity distribution on thedistribution comparisononofthe oyster weight class sizes from of oyster weight class sizes from withofstandard normal distribution. For a a histogram with standard normal distribution. Foraahistogram color version this figure, see www.iste.co.uk/david/statistics.zip color version of this figure, see www.iste.co.uk/david/statistics.zip

The comparison of the theoretical frequencies of each of the six classes with the observed frequencies is done using the chi-squared conformity test. Test statistics: 2  

(freqobs  freqt heo )2 with the degree of freedom freq theo

ddl  n  1  r where n = frequency values and k = parameter of the theoretical law used (two for normal distribution).

The chisq.test() function allows the application of the chi-squared conformity test by specifying the vector of theoretical and absolute frequencies as argument: > Ft Fo chisq.test(Fo,Ft) Pearson’s Chi-squared test data: Ft and Fo X-squared = 18, df = 15, p-value = 0.2627

The P-value is greater than 5%, H0 is accepted: the frequencies observed in each class correspond to those given by the standard normal distribution. Our sample does indeed follow a normal distribution. The chisq.test() function can also be used directly with theoretical probabilities instead of theoretical frequencies with the p argument:

120

Statistics in Environmental Sciences

>Ptchisq.test(Fo,p=Pt)

This test can also be used with more conditions for each factor, as detailed by the chi-squared homogeneity test (section 6.1.3). Similarly, it can also be used in a unilateral way, like Fisher’s exact test (section 6.1.1) by adding the alternative argument. 6.1.2.1. Power H0 being accepted, it may be of value to calculate the test power. The power of the chi-squared conformity test is obtained with the pwr.chisq.test() function of the pwr library. The effect of the sample size in w must be specified with the ES.w1 function, containing the theoretical proportions P0 and observed P1, and the degree of freedom in df = n – 1: > P0 P1 pwr.chisq.test(w=ES.w1(P0,P1),N=100,df=(length(P1)-1)) Chi squared power calculation w = 0.2070463 N = 100 df = 5 sig.level = 0.05 power = 0.31205 NOTE: N is the number of observations

At 32%, the power is quite low, so there is about a one in three chance of making a mistake by accepting H0 if H1 is true. 6.1.3. Chi-squared homogeneity test The chi-squared homogeneity test consists of comparing observed numbers from several populations. Its condition of application is Cochran’s rule. Let us use the example of 80 fish caught in three different alpine lakes: Annecy, Le Bourget and Lake Geneva. A fishing expedition was conducted for each lake and all fish in the nets were analyzed. Three species of fish were preferentially fished for: pike, char and trout. Are the fish communities the same between the lakes?

Comparison Tests of Unilateral and Bilateral Parameters

121

The two working hypotheses are: – H0: the proportions observed between the lakes are identical; – H1: at least one of the communities has different proportions. The contingency table is constructed as follows and shows that Cochran’s rule is accepted: > nom TCmosaicplot(t(TC),main=NULL,cex.axis=1.5) # The table must be transposed in order to obtain the results in the same way as the contingency table

Figure 6.3. Illustration of the results of the contingency table based on the numbers of the three fish species caught in three alpine lakes

122

Statistics in Environmental Sciences

Observation of this graph suggests that the communities are different between the three lakes (Figure 6.3). The chi-squared homogeneity test, using the chisq.test() function on the contingency table, can therefore be applied: > chisq.test(TC) Pearson’s Chi-squared test data: TC X-squared = 9.7031, df = 4, p-value = 0.04574

The result gives us the chi-squared test statistics as well as the P-value = 0.04, so it is lower than the α risk by 5% and H0 is rejected and H1 is accepted, in other words, at least one of the communities is different from the others with α = 5% risk. It is possible to build scripts in order to perform two-by-two comparison tests, but these are more difficult to understand, so we will not discuss them here. For more details, refer to Millot (2016). 6.1.3.1. Power If H0 is retained, it may be of value to calculate the test power. The power of the chi-squared homogeneity test is obtained with the pwr.chisq.test() function of the pwr library. The effect of the sample size in w must be specified with the ES.w2 function, containing the contingency table in proportion, the degree of freedom in df=(number of conditions of a factor – 1) × (number of conditions of the other factor – 1) and the number N of total elements: >Prob pwr.chisq.test(w=ES.w2(TC),df=(3-1)*(3-1),N=80) Chi squared power calculation w = 79.00077 N = 80 df = 4 sig.level = 0.05 power = 1 NOTE: N is the number of observations

The power here is equal to 1 since H1 was accepted.

Comparison Tests of Unilateral and Bilateral Parameters

123

6.1.4. McNemar’s test McNemar’s test consists of comparing two proportions in paired sets. In this test, only conflicting values are considered, in other words, those that have a different value between the two measurements. The test statistic is then compared to the threshold value in the chi-squared distribution table with a degree of freedom of 1. A study is carried out on the effect of a fungicide on pheasant egg laying. The number of viable and non-viable eggs is reported for a batch of 10 hens over 5 days. Fungicide treatment is administered to the hens and the number of viable and non-viable eggs from those hens is then counted for 5 days after 30 days of treatment. The contingency table resulting from this experience is as follows: >nomTCmosaicplot(t(TC),main=NULL,cex.axis=1.5)

Observation of this graph suggests that the proportion of viable eggs appears to be higher for untreated hens (Figure 6.4). McNemar’s test can be applied with mcnemar.test() function on the contingency table: > mcnemar.test(TC) McNemar’s Chi-squared test data: t(TC) McNemar’s chi-squared = 1.35, df = 1, p-value = 0.2453

124

Statistics in Environmental Sciences

Non viable

Treatment

No treatment

Viable

Figure 6.4. Illustration of the results of the contingency table, constructed from the numbers between viable and non-viable eggs in female pheasants treated with fungicide

In the end, the P-value is 5% higher than the α risk, H0 is accepted and the proportions between viable and non-viable eggs are not significantly different before and after fungicide treatment. 6.1.5. Binomial test The binomial test consists of comparing one observed proportion with a theoretical proportion. It is based on a qualitative variable with two classes, one of which is used to define a tested proportion. It can be used in a unilateral or bilateral way. The statistics are based on binomial distribution. According to Mendel’s principle of inheritance, crossing two plants of particular genotypes leads to offspring composed of ¼ dwarf plants and ¾ giant plants. An experiment was carried out in order to test this hypothesis on plants of this species. The offspring is made up of 243 dwarf plants and 682 giant plants. The two working hypotheses are: – H0: the proportion of giant plants is 0.75;

Comparison Tests of Unilateral and Bilateral Parameters

125

– H1: the proportion of giant plants is different from 0.75. The binomial test is applied with the binom.test() function by specifying the number of successes, the total number of tests, the theoretical probability P and the alternative argument (two-sided, less, greater). By default, this last argument is set to two.sided, a bilateral test: > binom.test(682, 682 + 243, p = 3/4) Exact binomial test data: 682 and 682 + 243 number of successes = 682, number of trials = 925, p-value = 0.3825 alternative hypothesis: true probability of success is not equal to 0.75 95 percent confidence interval: 0.7076683 0.7654066 sample estimates: probability of success 0.7372973

The P-value is 5% higher than the α risk, H0 is accepted and the proportion of giant plants is not significantly different from 0.75. 6.1.6. Cochran–Mantel–Hantzel test The Cochran–Mantel–Hantzel test compares observed numbers as well as observed pairs of proportions. It is based on two qualitative variables with two classes and one qualitative variable with n classes. A farmer would like to know if there is a relationship between lodging (lodged ear or not) and insect attacks (yes or no) depending on the color of the ear of wheat. The ear of wheat can be three colors: yellow, orange and red. The two working hypotheses are: – H0: in harvesting, lodging and insect attacks are independent, regardless of the color of the ear or the proportions of their lodged bases. Whether they are upright or lodged, they are identical in harvesting regardless of the color of the ear;

126

Statistics in Environmental Sciences

– H1: on all the colors of the ear, the two variables are linked or the two proportions are different, taking the color of the ear into account. This time, the contingency table has three dimensions; the third dimension is the factor that is being tested, in other words, the color of the ear. The table is therefore constructed as follows: > nom names(nom) TC3par(mfrow=c(2,2)) > for(i in 1:dim(TC3)[3]){mosaicplot(t(TC3[,,i]), main=paste(names(dimnames(TC3))[3],"=",dimnames(TC3)[[3]][i]),cex.axi s=1)}

Observation of the graphs suggests that these proportions are different: indeed, the columns are not split up in the same places in the orange color, for example (Figure 6.5). A slight inversion of the proportions between the yellow and red colors can also be observed.

Comparison Tests of Unilateral and Bilateral Parameters

127

Figure 6.5. Illustration of the results of three contingency tables (one per color), constructed from the numbers of ear that are lodged or not lodged, ear that have been attacked or not attack, all in harvest

Cochran’s test is applied with the mantelhaen.test() function and specifies the three-dimensional contingency table. The correct=TRUE argument is strongly recommended for a continuous correction of the test power in the case of a three-dimensional table: > mantelhaen.test(TC3, correct=TRUE) Mantel-Haenszel chi-squared test with continuity correction data: TC3 Mantel-Haenszel X-squared = 3.1421, df = 1, p-value = 0.0763 alternative hypothesis: true common odds ratio is not equal to 1 95 percent confidence interval: 0.1856335 1.0043144

128

Statistics in Environmental Sciences

sample estimates: common odds ratio 0.4317805

The P-value is higher than the α risk by 5% (by a narrow margin!), H0 is accepted. When harvesting, lodging and insect attacks are independent regardless of the color of the ear. 6.2. Comparisons of means 6.2.1. Student’s t-test The Student’s t-test is the most commonly used parametric test because it is the most powerful test to compare two means. The application of this test has already been detailed (see section 4.4). We will talk about the t-test for small samples based on the Student’s t-distribution and the Z-test for large samples based on normal distribution. In R, in both cases, the t.test() function will be used by specifying several arguments: >t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, ...)

with two samples x and y or mu for y if it is a comparison with a standard, alternative with two-sided for a bilateral test, less and greater for a unilateral test: less for H1 if it is assumed that x < y and greater for H1 if it is assumed that x > y. The Paired argument specifies whether the samples are independent (paired=FALSE) or paired (paired=TRUE), and finally var.equal specifies whether the homoskedasticity is respected: var.equal=FALSE for Welch’s t-test and var.equal=TRUE for a t-test or Z-test. Indeed, when the variances are not homogeneous between samples, a correction is made to the Student’s t-distribution. The conf.level argument makes it possible to estimate the confidence interval at the desired level, which is 95% here, in other words, a α risk of 5% (it can be changed by 0.99, for example, for a α risk of 1%).

Comparison Tests of Unilateral and Bilateral Parameters

129

6.2.1.1. For independent samples For two randomly and independently selected oak populations, the heights (in dm) of 13 and 14 trees, respectively, were measured. Are the heights significantly different between the two forests? The two working hypotheses – the samples are independent and the test is bilateral – are: – H0: µA = µB => the sizes of the trees between the two populations are the same; – H1: µA ≠ µB => the sizes of the trees between the two populations are different: >FAFB shapiro.test(FA) # Normality test of population A Shapiro-Wilk normality test data: FA W = 0.92201, p-value = 0.267 # It follows a normal distribution > shapiro.test(FB) Shapiro-Wilk normality test data: FB W = 0.97278, p-value = 0.9115 # It follows a normal distribution

In both cases, the P-value is higher than the α risk. The two populations follow a normal distribution. Are the variances homogeneous? With the Fisher–Snedecor test: > var.test(FA,FB)# Homogeneous var F test to compare two variances data: FA and FB F = 0.58696, num df = 12, denom df = 13, p-value = 0.3647 # The variances are homogeneous alternative hypothesis: true ratio of variances is not equal to 1 95 percent confidence interval:

130

Statistics in Environmental Sciences

0.1861495 1.9013248 sample estimates: ratio of variances 0.5869621

The P-value is higher than the α risk. The variances of two populations are not significantly different. The Student’s t-test for independent samples can therefore be used. The t.test() function is used here with paired=FALSE because the samples are independent, and alternative=“two.sided” because no assumption is made about the meaning of the difference and var.equal=TRUE because the variances are homogeneous: > t.test(FA,FB,alternative="two.sided",paired=FALSE, var.equal=TRUE,conf.level = 0.95) Two Sample t-test data: F1 and F2 t = 0.95416, df = 25, p-value = 0.3491 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.6759945 1.8430275 sample estimates: mean of x mean of y 25.96923 25.38571

The P-value is greater than α = 0.5, H0 is accepted. There is no significant difference in height between the two populations. Finally, since we have 13 and 14 elements per population, a box plot or bar chart can be used to illustrate the results (Figure 6.6): >data.frame(Populations=c(rep("F1",times=13),c(rep("F2",times=14))), Height=c(F1,F2) )->data # Sharing F1 and F2 data in a table >boxplot(Height~Populations,data, xlab="Populations", ylab="Height (in mm)") # representation described in Section 2.3.3. >meansemy.barplot(mean,se,xlab="Varieties",ylab="Shell length",ylim=c(0,max(mean+se)))# # representation described in Section 2.3.4.

Comparison Tests of Unilateral and Bilateral Parameters

B. Bar chart representation

Height (in m)

A. Box plot representation

131

F1 Populations F2

F1

Populations F2

Figure 6.6. Box plot and bar chart representation (mean ± standard error) of tree heights in two populations

6.2.1.2. Welch’s t-test A researcher is interested in the lengths of sea lion mandibles and tries to determine if these lengths are different between males and females. He therefore measures the length of mandibles on 10 males and 10 females. The two working hypotheses – the samples are independent and the test is bilateral – are: – H0: µF = µM => the mandible lengths are identical; – H1: µF ≠ µM => the mandible lengths are different: >FM shapiro.test(F) Shapiro-Wilk normality test data: F W = 0.95308, p-value = 0.705 # It follows a normal distribution > shapiro.test(M)

132

Statistics in Environmental Sciences

Shapiro-Wilk normality test data: M W = 0.94846, p-value = 0.6503 # It follows a normal distribution

In both cases, the P-value is higher than the α risk. The two populations follow a normal distribution. Are the variances homogeneous? With the Fisher–Snedecor test: > var.test(F,M) F test to compare two variances data: F and M F = 0.0418, num df = 9, denom df = 9, p-value = 6.119e-05 # The variances are not homogeneous alternative hypothesis: true ratio of variances is not equal to 1 95 percent confidence interval: 0.01038249 0.16828595 sample estimates: ratio of variances 0.04179985

The P-value is lower than the α risk. The variances of two populations are different. Welch’s t-test var.equal=FALSE:

is

therefore

appropriate

to

use

by

specifying

> t.test(F,M,alternative="two.sided",paired=FALSE, var.equal=FALSE,conf.level = 0.95) Welch Two Sample t-test data: F and M t = 0.34503, df = 9.7511, p-value = 0.7374 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -11.00428 15.02028 sample estimates: mean of x mean of y 29.657 27.649

The P-value is greater than α = 0.5, H0 is accepted. There is no significant difference in mandible lengths between males and females. For a graphical representation (Figure 6.7):

Comparison Tests of Unilateral and Bilateral Parameters

133

Mandible lengths

>FMdata.frame(Sex=c(rep(c("F"),length(F)),rep(c("M"),length(M))),mandib ules=c(F,M))->otarie >boxplot(mandibles~Sex,otarie,xlab="Sex",ylab="Mandible lengths" )

Sex Figure 6.7. Box plot and bar chart representation (mean ± standard error) of female and male sea lion mandibles

6.2.1.3. For the comparison of a sample to a standard The rate of a harmful gas was collected 30 times during 2010 by Météo France at the observatory of the Pic du Midi d’Ossau. Is the rate significantly above the tolerable threshold of 50?

134

Statistics in Environmental Sciences

The two working hypotheses – the samples are independent and the test is unilateral – are: – H0: µG-50 = 0 => the level of harmful gas is not different from 50; – H1: µG-50 > 0 => the level of harmful gas is above the tolerated threshold: >G shapiro.test(G) Shapiro-Wilk normality test data: G W = 0.95988, p-value = 0.3077 distribution

# The sample follows a normal

The P-value is higher than the α risk. The population follows a normal distribution. A t-test comparing a sample to a standard will therefore be appropriate to use. The t.test() function is used here by specifying the sample G, the standard is used with the argument mu and alternative=“less” because a negative difference between the gas content and the standard is assumed: > t.test(G,mu=50, alternative="less") One Sample t-test data: G t = 2.2735, df = 29, p-value = 0.9847 alternative hypothesis: true mean is less than 50 95 percent confidence interval: -Inf 57.40881 sample estimates: mean of x 54.24

The P-value is greater than α = 0.5 and H0 is accepted. There is no significant difference between the harmful gas content and the standard.

Comparison Tests of Unilateral and Bilateral Parameters

135

For a graphical representation (Figure 6.8): >boxplot(G,ylab="Harmful gas level") >points(50, pch=16,col="red",cex=1.5) >legend("bottomright",50,c("Standard"),pch=16,col="red")

Figure 6.8. Box plot representation of the harmful gas levels measured at the weather station of the Pic du Midi d’Ossau; the standard is superimposed in red

6.2.1.4. For the comparison of paired samples The study reports that the trigonometric measurement of trees standing upright tends to underestimate tree size. Twelve trees were measured by this first method and then measured on the ground after felling (method 2). Based on the results obtained, is this hypothesis confirmed? The two working hypotheses – the samples are paired and the test is unilateral – are: – H0: d(µ1-µ2)=0 => the sizes of the trees before and after felling are similar;

136

Statistics in Environmental Sciences

– H1: d(µ1=µ2) the sizes of the trees before felling are smaller than after felling: > M1 M2 M=M1-M2 > shapiro.test(M) Shapiro-Wilk normality test data: M W = 0.95248, p-value = 0.6735

# The difference follows a normal

distribution

The P-value is higher than the α risk, H0 is accepted. The difference between populations follows a normal distribution. A t-test of the comparison of paired samples will therefore be appropriate. The t.test() function is used here by specifying samples M1 and M2, paired=TRUE to specify that the samples are paired and alternative=“less” because a negative difference between method 1 and method 2 is assumed: > t.test(M1,M2, paired=TRUE, alternative="less") Paired t-test data: M1 and M2 t = -1.9566, df = 11, p-value = 0.03813 alternative hypothesis: true difference in means is less than 0 95 percent confidence interval: -Inf -0.06640138 sample estimates: mean of the differences -0.8083333

The P-value is less than α = 0.5, H1 is therefore accepted. Method 1 therefore significantly underestimates the height of trees with a threshold of α = 5%.

Comparison Tests of Unilateral and Bilateral Parameters

137

For a graphical representation (Figure 6.9): >boxplot(M,ylab="Differences between tree size before and after felling") >abline(h=0,lty=2,col="red", lwd=2)

Figure 6.9. Box plot representation of the difference in tree sizes before and after felling; the red dotted line indicates 0. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

6.2.1.5. Student’s t-test power When Student’s t-test leads to the acknowledgment of H0, in other words, there is no difference between the parameters tested, it may be of value to calculate the test power, that is to say, its ability to conclude H1 if H1 is true (real difference) and to assess, if necessary, the number of elements per sample required to conclude H1. Let us look once more at the example of the two oak populations comparison (section 6.2.1.1), which was made for the samples of the heights of 13 and 14 trees, respectively, and for which H0 had been upheld.

138

Statistics in Environmental Sciences

The two working hypotheses – the samples are independent and the test is bilateral – are: – H0: µA = µB => the sizes of the trees of the two populations are the same; – H1: µA ≠ µB => the sizes of the trees of the two populations are different. The test power can be addressed using the power.t.test() function by specifying n the number of elements of the smallest sample (here n = 13), the result of the t statistic for d (here, it is 0.95416), the level of significance with sig.level = 0.05, the test type with type, here two.sample is used for two independent samples (one.sample is used for comparing a sample to a standard, paired for paired samples) and the test type with alternative=“two.sided” (one.sided for a unilateral test): >power.t.test(n=13,d=0.95416,sig.level=0.05,type="two.sample",alterna tive="two.sided") Two-sample t test power calculation n = 13 delta = 0.95416 sd = 1 sig.level = 0.05 power = 0.6459966 alternative = two.sided NOTE: n is number in *each* group

The power is given by power, here it is 0.65. It is therefore quite low, the β risk is at 35% or about ⅓ of not accepting H1 if H1 is true. If we wanted a high power, we would need more elements in the samples. For example, for a power of 0.9, the same function can be used by specifying the result of the t statistic with d (here, 0.95416), the power with power (here, 0.9) and the same arguments as before, except n of course: > power.t.test(d=0.95416,sig.level=0.05,power=0.9,type="two.sample", alternative="two.sided") Two-sample t test power calculation n = 24.08473 delta = 0.95416 sd = 1 sig.level = 0.05 power = 0.9 alternative = two.sided NOTE: n is number in *each* group

Comparison Tests of Unilateral and Bilateral Parameters

139

In order to obtain such a power, at least 24 elements per sample (specified by n) would be required. 6.2.2. Wilcoxon–Mann–Whitney U test The Wilcoxon–Mann–Whitney U test is the most commonly used nonparametric test to compare means, although it is still less powerful than the parametric alternative, the Student’s t-test. This test seeks to verify whether the elements of two samples classified on the same ordinal scale occupy equivalent positions (ranks) (thus revealing the similarity of two distributions; Figure 6.7). The test statistic corresponds to the number of times the values of one sample precede those of the other. Sample A 8 11 15 Sample B 6 9 10 13 “Balanced” distribution between the 2 samples

Case 1

6 8 9 10 11 B A B B A

13 15 =>Distribution not different B A

Case 2

6 8 9 10 11 B B B B A

13 15 =>Distribution different A A

Figure 6.10. Example of two cases in which the values are arranged by rank before their distributions are compared, according to the Wilcoxon–Mann–Whitney lower element number test. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The statistics of the U test are more difficult to understand; they will not be discussed here. However, it follows a Student’s t-distribution for samples that have more than 20 individuals and tables have been specially designed for smaller numbers of elements. In R, the wilcox.test() function will be used in all three cases, specifying the same arguments as for the t.test function: >wilcox.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, conf.int = FALSE, conf.level = 0.95, ...)

with two samples x and y, or mu if it is a comparison with a standard, alternative with two-sided for a bilateral test, less and greater for a unilateral test: less for H1 if it is assumed that x < y and greater for H1 if it is assumed that x > y. The Paired argument is used to specify whether the samples are independent (paired=FALSE) or paired (paired=TRUE). The conf.level

140

Statistics in Environmental Sciences

argument makes it possible to estimate the confidence interval at the desired level, here it is 95%, in other words, an α risk of 5% (it can be changed by 0.99, for example for an α risk of 1%). 6.2.2.1. Wilcoxon–Mann–Whitney U test for independent samples The gray cuckoo is a bird that lays its eggs in nests of other species so that they can incubate their eggs in their place. The size of the gray cuckoo eggs was measured for two samples, one consisting of eggs collected from wren nests (small nest size) and the other from warbler nests (large nest size). Does the gray cuckoo adapt the size of its eggs to the size of the nest in which it lays? The two working hypotheses – the samples are independent and the test is unilateral – are: – H0: µR = µF => egg sizes in wren and warbler nests have similar values; – H1: µR < µF => egg sizes in wren nests have lower values than those in warbler nests: >RFshapiro.test(R)# Normality test of the wren sample Shapiro-Wilk normality test data: R W = 0.83597, p-value = 0.01106 # The sample does not follow a normal distribution >shapiro.test(F)# Normality test of the warbler sample Shapiro-Wilk normality test data: F W = 0.97366, p-value = 0.9214 # The sample follows a normal distribution

At least one of the two populations does not follow a normal distribution, given that one of the two P-value is lower than the α risk.

Comparison Tests of Unilateral and Bilateral Parameters

141

The Wilcoxon–Mann–Whitney test is appropriate to use. The wilcox.test() function is used here with paired=FALSE because the samples are independent and alternative=“less” is used because of an R < F difference: >wilcox.test(R,F,paired=FALSE, alternative="less") Wilcoxon rank sum test with continuity correction data: R and F W = 8.5, p-value = 1.349e-05 alternative hypothesis: true location shift is less than 0

The p-value is well below 0.001. The size of cuckoo eggs in wren nests is significantly smaller than in warbler nests at the threshold α = 1‰. The gray cuckoo adapts the size of its eggs to the size of the nest in which it lays. For a graphical representation (Figure 6.10):

Size of Cuckoo’s eggs

>data.frame(Bird=c(rep(c("Wren"),length(R)),rep(c("Warbler"), length(F))),Size=c(R,F))->cuckoo >x11();boxplot(Size~Bird,cuckoo,xlab="Bird",ylab="Size of Cuckroo's eggs")

Warbler

Wren Bird

Figure 6.11. Box plot representation of the size of gray cuckoo eggs in warbler and wren nests

142

Statistics in Environmental Sciences

6.2.2.2. Wilcoxon–Mann–Whitney conformity test After 10,000 years of effort, geologists of planet W of the alpha Centauri star obtained four measurements of the hyperfine sedimentation constant. Are these values significantly different from the theoretical pi value? The two working hypotheses – the samples are independent and the test is unilateral – are: – H0: µG-pi = 0 => the values are not different from pi; – H1: µG-pi > 0 => the values are different from pi. >Gshapiro.test(G) # does not follow a normal distribution Shapiro-Wilk normality test data: G W = 0.67091, p-value = 0.005041 # The sample does not follow a normal distribution

The P-value is lower than the α risk: the population does not follow a normal distribution. The Wilcoxon—Mann–Whitney conformity test is appropriate. The wilcox.test() function is used here with mu = 3.14 because the sample is compared to a standard, and alternative=“two.sided” because no assumption is made about the difference between the sample and the standard: >wilcox.test(G,mu=3.14,alternative="two.sided") Wilcoxon signed rank test with continuity correction data: G V = 10, p-value = 0.09751 alternative hypothesis: true location is not equal to 3.14

The P-value is greater than 0.05, H0 is accepted. There is therefore no significant difference between the sample and the pi value.

Comparison Tests of Unilateral and Bilateral Parameters

143

For a graphical representation, it is better to use a bar graph (Figure 6.11): >my.barplot(mean(G),sd(G)/sqrt(length(G)),ylab="Hyperfine sedimentation constant",ylim=c(0,max(mean(G)+sd(G)/length(G)))) >points(pi, pch=16,col="red",cex=1.5) >legend("bottomright",pi,c("pi"),pch=16,col="red")

Figure 6.12. Representation of hyperfine sedimentation constants (mean ± standard error); the pi value has been superimposed in red. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

6.2.2.3. Wilcoxon U test for paired samples The researchers assume that mercury concentrations are higher than cadmium concentrations in eels. Concentrations of these two metals were measured in five fish. The two working hypotheses – the samples are paired and the test is unilateral – are: – H0: µhg = µcd => cadmium and mercury concentrations are identical;

144

Statistics in Environmental Sciences

– H1: µhg > µcd => mercury concentrations are higher than those of cadmium. > Cd HgHg-Cd->C > shapiro.test(C)# does not follow a normal distribution Shapiro-Wilk normality test data: C W = 0.76012, p-value = 0.03683 # The difference does not follow a normal distribution

The element by element difference between mercury and cadmium concentrations does not follow a normal law because the P-value is lower than the α risk. Wilcoxon’s U test for paired samples is appropriate. The wilcox.test() function is used here by specifying two samples Hg and Cd, the greater alternative is used because an assumption is made regarding a positive difference between Hg and Cd for H1 and paired=TRUE is used because the samples are paired: > wilcox.test(Hg,Cd,alternative="greater",paired=TRUE) Wilcoxon signed rank test with continuity correction data: Hg and Cd V = 8.5, p-value = 0.4451 alternative hypothesis: true location shift is greater than 0

The P-value is greater than 0.05. Mercury concentrations are therefore not significantly higher than those of cadmium with a risk of α = 5%. 6.2.2.4. Wilcoxon–Mann–Whitney test power Let us look at the example of the comparison of paired samples of mercury and cadmium concentrations in eels (section 6.2.2.3). A Wilcoxon comparison test was used, for which there is no function available to obtain the power. The power and number of samples that would have been required

Comparison Tests of Unilateral and Bilateral Parameters

145

for a power of 0.9 will be merely approached on the same principle as detailed in section 4.3.1. The two working hypotheses – the samples are paired and the test is unilateral – are: – H0: µhg = µcd => cadmium and mercury concentrations are identical; – H1: µhg > µcd => mercury concentrations are higher than those of cadmium. Hg and Cd are noted as samples 1 and 2, respectively. The mean, standard deviation and number of elements are listed per sample: >Cd Hgmean1pwr pwr->power[i,2] }

The table is sorted in ascending order of power with the order() function and the first results that are greater than or equal to 0.9 are displayed with head(subset()): > power[order(power$pwr),]->power > head(subset power,pwr>=0.9)) n puis 316 0.903 356 0.905 382 0.905 350 0.907 355 0.907 357 0.908

It would take more than 316 elements to obtain such a power here. 6.3. Correlation test of two quantitative variables 6.3.1. Linear correlation: the Bravais–Pearson coefficient The Pearson correlation coefficient, usually denoted as r, is only valid for the relationship between two quantitative variables under the assumption that this relationship is linear. The Pearson correlation coefficient is calculated according to: N

rp 

 ( xi  x )  ( yi  y ) i=1

N

 i=1

( xi  x )2 

N

( yi  y )2 i=1

Comparison Tests of Unilateral and Bilateral Parameters

147

It is a measure of the relationship between two quantitative variables and varies between –1 and 1. If the relationship is very weak or even nil, r = 0. If the relationship is very strong or even perfect, r will be very close to –1 for a negative relationship and +1 for a positive relationship (Figure 6.12). The Pearson correlation coefficient makes it possible to estimate the sense and intensity of the relationship, but in no case does the correlation quantify the variation of a variable explained by an explanatory variable in a very particular sense, as the equation of the right-hand model and R2 does. However, its application is subject to the same conditions of application as linear models (Chapter 7). The correlation coefficient significance test presents a statistic based on the Student’s probability law. It can be used in a unilateral or bilateral way: tr 

r (n  2) 2

(1  r )

with ddl  n  2

The working hypotheses are: – H0: r = 0 → the two variables are not related to each other; – H1: r ≠ 0 → for a bilateral test, the two variables are related to each other; – or H1: r > 0 → for a unilateral test, the two variables are positively related to each other; – or H1: r < 0 → for a unilateral test, the two variables are negatively related to each other.

‘Perfect’ positive connection r=1

‘Perfect’ negative connection r = -1

Strong connection

Zero connection

r close to 1

r=0

Figure 6.13. Different types of relationships between two variables

148

Statistics in Environmental Sciences

6.3.1.1. Application in R The correlation coefficient and its related test are obtained with the cor.test() function by giving the two variables x and y, the method (pearson) and the type of test with alternative: a bilateral test with two-sided, less (negative relationship) or greater (positive relationship) for a unilateral test. Let us look at the study of the relationship between abdominal length and shell length in crabs in the crabs data set of the MASS library (see section 1.2.1). First, a graphical representation shows the plausibility of this relationship (Figure 1.1). The working hypotheses are: – H0: r = 0 => the two variables are not related to each other; – H1: r ≠ 0 => for a bilateral test, the two variables are linked to each other. > cor.test(crabs$RW,crabs$CL,method="pearson", alternative="two.sided")) Pearson’s product-moment correlation data: crabs$RW and crabs$CL t = 27.88, df = 198, p-value < 2.2e-16 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.8605954 0.9178047 sample estimates: cor 0.892743

The P-value is much lower than the risk α = 0.001, H0 is rejected: a significant relationship exists between these two variables. Note that the correlation coefficient cor is given at the bottom, cor = 0.893. It is positive and very close to 1, so the relationship is growing and of high intensity. 6.3.1.2. Power If H0 had been retained, the power of such a test could have been obtained with the pwr.r.test function of the pwr library with n (the number of elements) and r (the correlation coefficient obtained) and the alternative

Comparison Tests of Unilateral and Bilateral Parameters

149

argument to specify if the test is bilateral or unilateral (two.sided, less, greater): >library(pwr) >pwr.r.test(n = 200, r = 0.892743 , sig.level = 0.05, alternative = c("two.sided")) approximate correlation power calculation (arctangh transformation) n = 200 r = 0.892743 sig.level = 0.05 power = 1 alternative = two.sided

Here, the power is 1 because H1 was kept during the test. To get the minimum number of elements in order to obtain a power of 0.9, all we have to do is not specify the n with the same function, and add the power argument: > pwr.r.test(r = 0.892743, sig.level = 0.05, power = 0.9, alternative = c("two.sided")) approximate correlation power calculation (arctangh transformation) n = 7.681961 r = 0.892743

sig.level = 0.05 power = 0.9 alternative = two.sided

It would only take eight elements to obtain a power of 0.9. 6.3.2. Monotonous correlation: Spearman or Kendall coefficients The mathematical principle of Pearson’s linear correlation, in other words, the formulation of the equations and the properties that result from them to measure this relationship, is based on the fact that the variables or rather the difference between the observed data and the line follow a known theoretical law of distribution: normal distribution. This will not be the case for a nonlinear monotonous relationship such as an exponential curve. It is therefore necessary to find another way to measure this relationship, without making any prior assumptions about the distribution of variables.

150

Statistics in Environmental Sciences

Let us look at the study of the relationship between the weight and length of cod. This relationship is exponential and does not follow a law (Figure 6.14(A)). To overcome this condition, the raw values are transformed into ranks (Figure 6.14(A)): the data for each variable are placed in ascending order and replaced by their ranks (beware of draws; section 2.1.2).

Figure 6.14. Case of the weight–height relationship of cod; comparison of the distribution of ranks per variable between the case of an existing relationship and the case of a non-existent relationship

In an increasing effective relationship such as that of cod, fish with the shortest lengths (lowest ranks) also have the lowest weights, those with the longest lengths (highest ranks) have the highest weights (Figure 6.14(A)). In contrast, if the relationship did not exist, the shorter lengths would be associated with scattered values of weight in terms of rank. You then need only formalize a term that reflects this intensity of the relationship through the differences in rankings between variables (Figure 6.14(B)). 6.3.2.1. For the Spearman correlation coefficient It is assumed that the smaller the difference for all values is, the stronger the relationship is. It is a little more complicated than that, because a strong but negative relationship can also involve low ranks for one variable and high ranks for the other, but the idea is there. Mathematicians have

Comparison Tests of Unilateral and Bilateral Parameters

151

developed a mathematical formula to take these aspects into account and this gives the Spearman’s rho correlation coefficient. For the coefficient test, a t r statistic is calculated that is supposed to follow the Student’s probability law with a degree of freedom of ddl = n – 2: rho  1 

 d12

6

3

n n

ts 

rho n  2 1  rs 2

with: – d being the differences in rank between the elements; – n being the number of samples. 6.3.2.2. For the Kendall correlation coefficient The calculation is more complex. A first set is sorted and the ranks of the values of the second set are compared to the first. Therefore, only the second set is used. For each observation, the number of subsequent values that are higher (a +1 is assigned) and lower (–1) are recorded. Hence the emergence of a new set of numbers that are balanced between positive and negative numbers. The total balance S is equal to n(n – 1)/2 if the order is completely respected, given that it is the sum of the first n natural integers. If the order is completely reversed, S = –n(n – 1)/2. In the event of total independence, S = 0. The value of S is then observed through Kendall’s tau coefficient. This is the observed balance in relation to the maximum balance. The z-test statistic follows standard normal distribution: 

S 2S  n(n  1) n(n  1) 2

z

 ( R1 ,R 2 ) 2  (2.n  5) 9  n(n  1)

These two coefficients evolve between –1 and 1, like the Pearson coefficient, with a sign reflecting the meaning of the relationship and an absolute value reflecting the intensity, once again, just like the Pearson coefficient: the stronger its absolute value, the stronger this intensity is and vice versa.

152

Statistics in Environmental Sciences

In both cases, the working hypotheses are: – H0: r = 0 => the two variables are not related to each other; – H1: r ≠ 0 => for a bilateral test, the two variables are related to each other; – or H1: r > 0 => for a unilateral test, the two variables are positively related to each other; – or H1: r < 0 => for a unilateral test, the two variables are negatively related to each other. Some authors mention a link between Kendall’s tau and Spearman’s rho (Siegel and Castellan’s formula). The main hurdle for this type of correlation is the handling of draws. If there are many, the result may only depend on an agreement of how to handle them. According to specialists, this disadvantage would be better handled by Kendall’s tau than by Spearman’s rho. It should be noted that in the case of a nonlinear monotonous relationship, non-parametric alternatives to correlation may be more powerful than Pearson’s coefficient because they are more adapted to the shape of the data. The analysis of Pearson’s coefficient, where applicable, is often followed by a simple linear regression in order to formalize the relationship, a simple equation to be used later to predict weight data according to length data, but if only the measurement of the latter variable was performed in the field (Chapter 7). Unfortunately, the non-parametric alternatives do not provide any equation for quantifying the explained variation or for predicting subsequent data. The formalization of other types of relationships is often more complex because it is necessary to know the name associated with the shape of the curve (exponential curve, etc.). However, most statistical software has developed functions to help determine the type of equation to be apprehended for each type of curve. NOTE.– The case of an exponential relationship, as used here, is a little particular because it is possible to transform the data for a straight-line relationship outcome. A log-transformation of weights makes it possible to “linearize” the relationship between weight and height and thus apply

Comparison Tests of Unilateral and Bilateral Parameters

153

Pearson’s correlation and a fortiori a linear regression or a Gaussian distribution glm (see section 7.3.1 and Figure 6.15).

A

B

Figure 6.15. Establishing the weight–height relationship equation for cod: with an exponential adjustment A) and a linear adjustment after prior transformation into a logarithm of the weights B)

6.3.2.3. Application in R The correlation coefficients and their associated tests are obtained with the cor.test() function, just like Pearson’s correlation coefficient (see section 6.3.1), by giving the two variables x and y, the method (Spearman or Kendall) and the type of test with alternative: bilateral with two-sided, less (negative relationship) or greater (positive relationship) for a unilateral test. Let us look at the first cod data set: > Size Weight cor.test(Size, Weight, method="spearman", alternative="two.sided") Spearman’s rank correlation rho data: Size and Weight S = 2, p-value = 4.96e-05 alternative hypothesis: true rho is not equal to 0 sample estimates: rho 0.9833333

154

Statistics in Environmental Sciences

The P-value is much lower than the risk α = 0.001, H0 is rejected: a significant relationship exists between these two variables. Note that the rho correlation coefficient is given at the bottom, rho = 0.983. It is positive and very close to 1, so the relationship is growing and of high intensity. For a test on Kendall’s correlation with a unilateral test assuming an increasing relationship between the two variables: > cor.test(Size, Weight, method="kendall", alternative="greater") Kendall’s rank correlation tau data: Size and Weight T = 35, p-value = 2.48e-05 alternative hypothesis: true tau is greater than 0 sample estimates: tau 0.9444444

The P-value is much lower than the risk α = 0.001, H0 is rejected: a significant and positive relationship exists between these two variables. The tau correlation coefficient is given at the bottom, tau = 0.944. It is positive and very close to 1, so the relationship is growing and of high intensity. However, these functions do not manage rank values equally – if there are any in the samples, a warning message will appear. Many draws can bias the results. For data mining, as shown in Figure 6.14(A): >plot(Weight~Size, pch=16,xlab="Weight",ylab="Weight")

7 Classical and Generalized Linear Models1

A model is a simplified representation of reality. A statistical model is an approximate mathematical description of the mechanism that generated the observations. This involves explaining the evolution of one variable by one or more other variables – from X1 to Xn (Figure 7.1). This explanation will never be perfect: a compromise is made between reality and what can be done. The approximation of this mathematical description is manifested by an error term in the model, generally recorded as Ɛ: Y = f (X1, X2,… Xn ….+ Ɛ)

Figure 7.1. Different types of statistical models. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

1 This chapter is a simplified and summarized overview of the books by Azaïs and Bardet (2012) and Hawi (2011) applied to examples in environmental sciences.

Statistics in Environmental Sciences, First Edition. Valérie David. © ISTE Ltd 2019. Published by ISTE Ltd and John Wiley & Sons, Inc.

156

Statistics in Environmental Sciences

A linear model is a statistical model for expressing a quantitative Y variable linearly, depending on one or more x variables (quantitative and/or qualitative): Yi = b0 + b1X1 + b2X2 …bnXn + Ɛ with: – b1, b2 and bn being the coefficients associated with variables X1, X2 and Xn, respectively; – b0 being the ordinate of origin. There are several types of linear models: – linear regression: it explains a quantitative variable by one or more quantitative variables, which is equivalent to defining lines between quantitative variables; – variance analysis (ANOVA): it explains a quantitative variable by one or more qualitative variable(s), which is equivalent to comparing several means in a bilateral test; – covariance analysis (ANCOVA): it explains a quantitative variable by a mixture of qualitative and quantitative variable(s), which is equivalent to comparing regression slopes according to the qualitative variable conditions. Let us take the example of Anderson’s irises (section 5.2.7). Morphological measurements were carried out on flowers belonging to three iris species: Iris setosa, I. virginica and I. versicolor (50 for each). The variables are the length and width of the petals and sepals. This database consists of 150 observations (50 per species), four quantitative morphological variables mentioned above and a qualitative variable in the form of a factor, the species. It makes it possible to tackle the main types of questions that can lead to the three main types of linear models, the simplest ones being: – do the lengths of the sepals depend on (or relate to) the lengths of the petals? Y is a dependent quantitative variable (sepal length) that will be explained by another independent quantitative variable (petal length). Here it is a question of linking a quantitative variable to another quantitative variable: it is therefore a linear regression for a classical linear model (Figure 7.2(A));

Classical and Generalized Linear Models

157

– are the lengths of the sepals different between species? The goal here is to link a dependent quantitative variable, Y (sepal length), to an independent qualitative variable X (species): it is therefore a variance analysis (Figure 7.2(B)); – is the relationship between sepal and petal lengths the same for one species as it is for another? This involves linking two quantitative variables and one qualitative variable (the species): it is therefore a covariance analysis (Figure 7.2(C)). Y is a dependent quantitative variable (sepal length) that will be explained by an independent quantitative variable (petal length) and a qualitative variable (species). The idea is to compare the slopes of sepal lengths against petal lengths from one species to another in order to analyze if there are similar regressions between species.

Figure 7.2. Different types of classical linear statistical models. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.1. Principle of linear models 7.1.1. Classical linear models: the least squares method 7.1.1.1. General principle for simple regression and single-factor ANOVA All linear models are based on the least squares method. The adjustment of the points responds to:



ˆ Residual variation   Yi  Y i i

with

^

Yi



2

or minimum

being the value estimated by the model.

158

Statistics in Environmental Sciences

According to the least squares method, the total deviation (deviation from a given point, y, to the mean of y) is equal to the sum of the explained deviation (deviation from the mean of y to y given by the model for that point) and the residual deviation (deviation from a given y to y given by the model for that point) (Figure 7.3).

Figure 7.3. Illustration of the equality of the total deviation to the sum of explained and residual deviations for (A) linear regression and (B) variance analysis. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.1.1.1.1. For linear regression Let us look at the length of the sepals compared to those of the petals for the I. versicolor species, according to a line by the least squares method: this is a linear regression because the explanatory variable is quantitative. The least square method adjustment ensures that the sum of the point deviations on the line is as small as possible (Figure 7.3(A)). For each point x i, the line gives the corresponding yi with a deviation that corresponds to the noise, that is to say, residual deviation. The equation of the line is:

Classical and Generalized Linear Models

159

with: – β0 being the y-intercept; – β1 being the slope of the line. Long and tedious formulas exist to calculate β0 and β1, but they will not be detailed here, because all statistical software and even spreadsheet-type software are able to calculate them. For the example, they give: Lsepal =2.4 + 0.83 × Lpetal 7.1.1.1.2. For a variance analysis (ANOVA) Let us look at the study of the length of petals according to species. This time it is a variance analysis because the explanatory variable is qualitative with three species. The least squares method adjustment ensures that the sum of point deviations of the value for the corresponding species given by the model is as small as possible (Figure 7.3(B)). The equation is:

with: – β0 being the mean length; – β1, β2 and β3 being the three coefficients for each condition of the qualitative variable/factor (e.g. species): they correspond to the difference between the mean and the value given by the model for the given species. In reality, statistical software does not take the mean of the y as its origin. They set β0 on one of the coefficients corresponding to the conditions of the qualitative variable and calculate the others in relation to it. This removes a member from the equation, because its coefficient will be equal to 0:

160

Statistics in Environmental Sciences

For example, it is I. setosa which is taken as reference (the first in alphabetical order of the conditions of the qualitative factor). This gives the following equation for the example shown: Lpetal = 5.01 + 0.93 × Iversicolor + 1.58 × Ivirginica This attribute for the total deviations makes it possible to construct the variance analysis table of the linear model: calculation of the sum of squares of deviations (SC) , for which equality is also true and the calculation of the mean squares (CM) = sum of squares/degree of freedom (Table 7.1).

Table 7.1. Variance analysis table for linear regression and a variance analysis (ANOVA). For a color version of this table, see www.iste.co.uk/david/statistics.zip

The significance test of any linear model, whether it be regression, ANOVA or ANCOVA, is the Fisher–Snedecor test based on the mean squares of the variance analysis table. Its statistics are F with: F = CMexp/CMres which follows the Fisher–Snedecor method (see section 2.3.2). The two working hypotheses are: – H0: R2 = 0 => the dependent variable is not explained linearly by the independent variable; – H1: R2≠0 => the dependent variable is explained linearly by the independent variable. In the case of an ANOVA, this means that at least one of the conditions (e.g. one of the three species) is different from the others. Only a post hoc test will make it possible to determine the real difference between the two

Classical and Generalized Linear Models

161

conditions in detail. In our example of the length of petals related to the species, the P-value gives 2.2 × 10–6, H1 is accepted and at least one of the species is different from the others with an R2 of 0.94. The coefficient of determination, known as R2, is calculated from the sum of squares of the variance analysis table: R2 = SCexp/SCtot It corresponds to the amount of variation (in other words, information) explained by the model. It varies between 0 and 1: 0 when the independent variable used explains nothing and 1 when it explains all the fluctuations of the dependent Y variable. In the ANOVA example, the R2 is 0.94: 94% of the fluctuations in petal lengths are explained by the species with a P-value of 2.2 × 10–6. In the regression example, which is also significant, the R2 is 0.76: 76% of the fluctuations in sepal lengths are explained by fluctuations in petal lengths with a P-value of 2.2 × 10–6. The coefficient of determination should not be confused with the Bravais–Pearson correlation coefficient, which only reflects the meaning and intensity of the model without quantification (see section 6.3.1). For a simple linear regression, the R2 significance test corresponds to the test of the slope of the equation and R2 = r2. 7.1.1.2. Multiple regression A single variable may not be sufficient to satisfactorily explain fluctuations in the variable that needs to be explained. Multiple linear regression consists of measuring the dependence of a dependent quantitative variable on several independent quantitative variables, always based on the least squares method adjustment. 7.1.1.2.1. General principle For two variables, a regression plane is adjusted (Figure 7.4). For n variables, a regression hyperplane with a dimension of n – 1 is adjusted. The equation then becomes more complex. For example: Yi = 0 + 1.x1i + 2.x2i +…+  n.xni + Ɛi

162

Statistics in Environmental Sciences

with: – 0 being the y-intercept; – 1, 2…  n being the partial regression coefficients. The two working hypotheses are: – H0: R2 = 0 => the Y variable is linearly independent of the variables x1, 2,…; – H1: R2 ≠ 0 => the Y variable is explained linearly by at least one of the independent variables x1, 2,… j.

Figure 7.4. Regression plane design linking the explained Y variable to two X1 and X2 variables

7.1.1.2.2. Concept of partial regression coefficient versus slope of a simple regression The coefficients of a multiple regression no longer correspond to the slopes that would have been individually obtained by independently making the lines of Y with X1, Y with X2, etc. This is referred to as a partial regression coefficient for 1, 2…  n. Partial regression is the process of judging the effect of one variable after removing the effect of one or more others. Let us take a look at the fluctuations in Bidonia spp. abundance according to temperature and time. The regression is significant when explaining fluctuations in Bidonia abundance based on the effect of time alone (Figure 7.5(B)), but not so when it comes to explaining fluctuations in Bidonia

Classical and Generalized Linear Models

163

according to temperature (Figure 7.5(A)). On the other hand, when the effect of time is removed (the residual, in other words, what is not explained, from the Bidonia model depending on time), the residual regression depending on temperature becomes significant (Figures 7.5(C) and (D)). In reality, the effect of time has masked the effect of temperature in the raw data, but this effect exists. The “residual as a function of temperature” regression coefficient is called the partial regression coefficient in a multiple regression, because it considers the effect of temperature after removing the effect of other explanatory/independent variables included in the model (here, it is 1 variable: time).

Figure 7.5. Decomposition of a partial regression. The regression is not significant between Bidonia and temperature, but it is significant between Bidonia and time. On the other hand, the residual of the Bidonia model depending on time shows a significant regression with temperature, reflecting a real effect of temperature once the effect of time has been removed. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The regression considering the effect of time and temperature gives the following equation: Bidonia = 0.478Temperature + 0.876Time – 2.263

164

Statistics in Environmental Sciences

It should be noted that the slope of the Bidonia regression according to time (0.893) is different from that given by the 2-variable explanatory model, because this new slope considers the effect of time after removing the effect of temperature. Although the Bidonia-Temperature regression was not significant, the partial regression coefficient of the 2-variable explanatory model reveals a significant temperature effect. 7.1.1.3. Multifactorial ANOVA This is the generalization of a factor to the ANOVA in order to explain a dependent quantitative variable by several independent qualitative variables. For example, explaining the length of sepals by species and the color of the iris. The variance analysis table is becoming more complex and the calculation of mean squares (residual and explained CM) for tests is also becoming more complex, especially since the calculations change according to different factors that have already been discussed in section 3.2: – the type of factors: fixed or random factors (see section 3.2.4.2); – the type of experimental plan: cross-factors or hierarchical factors (see section 3.2.4.3); – the type of experimental plan: independent or paired factors, block effect and Latin square (see sections 3.2.3.1 and 3.2.3.3); – a balanced or unbalanced plan (see section 3.2.4.1); – the type of model: additive plan with or without interactions (see section 3.2.4.4). Three types of ANOVA models will be distinguished according to the equations set up and therefore the impact on the calculation of the statistical tests involved: – type I ANOVA model: fixed-effects model for the 1-factor ANOVA on balanced and unbalanced design, ANCOVA on balanced design; – type II ANOVA model: random effect(s) model, very rare in environmental sciences; – type III ANOVA model: mixed effects model, multifactorial ANOVA on unbalanced cross-design or hierarchical design, ANCOVA on unbalanced design.

Classical and Generalized Linear Models

165

7.1.1.4. Covariance analyses (ANCOVA) This is the analysis of the effect of both quantitative and qualitative independent variables on a dependent quantitative variable. For example, explaining the length of sepals by the length of petals and the species of iris. It is possible to analyze the effect of each independently-taken variable (petal lengths and species) in addition to the interaction. It is often the interaction that is interesting in ANCOVA. A significant interaction means that at least one of the slopes of regression between the two quantitative variables is different for a condition of the qualitative variable. Such as an example is the linear regression between sepal and petal lengths being different for at least one of the species. It would seem that this is the case here because the lines made for each species are not parallel. Indeed, in the case of an absence of interaction, the lines would appear (at least approximately) parallel (Figure 7.6), but the point dispersion can make this interaction insignificant.

Figure 7.6. Graphical representation of the covariance analysis, analyzing the joint effect of petal length (quantitative variable) and species (qualitative variable) on sepal length. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Indeed, the variability around the regression reveals that there is an effect on the length of petals taken independently (P-value = 2.2 × 10–16 lower than the risk α = 0.001), as well as the species taken

166

Statistics in Environmental Sciences

independently (P-value = 5.2 × 10–13 lower than the risk α = 0.001), but that the interaction is not significant (P-value = 0.19 higher than the risk α = 0.05). 7.1.1.5. Key to choosing the ANOVA designs to be implemented Figure 7.7 shows a key for choosing the classic linear model to be implemented, according to the different cases.

Figure 7.7. Key for choosing the linear model design to be implemented and main steps. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.1.2. Generalized linear models: the principle of maximum likelihood The classical linear model is based on the normal residuals hypothesis, which implies that the variable to be explained is also considered as being normally distributed, in other words, in a symmetrical manner. This is not the case for many data (in other words, counts, percentages). In addition, it also assumes that the effects of the explanatory variables on the variable to be explained are linear, which is quite often not the case.

Classical and Generalized Linear Models

167

Generalized linear models (GLMs) make it possible to explain larger types of variables: continuous quantitative, binary or discrete integer variables from quantitative and/or qualitative variables based on the assumption that observations follow known distribution laws, but not necessarily a normal distribution like in conventional linear models. They will consider the non-normality of the error term, and choose another distribution to model it. GLMs are therefore a flexible generalization of the latter. The method used to adjust the model is no longer the least squares method, but the maximum likelihood method. The classic linear model: Y=β0 + β1*X1 + β2*X2 +…+ βn*Xn is instead considered as: Y=g(β0 + β1*X1 + β2*X2 +…+ βn*Xn) whose inverse function becomes: Lien(Y)=β0 + β1*X1 + β2*X2 +…+ βn*Xn and Link() is called a “link function”. As for linear models, the explanatory/dependent variable(s) X 1, X2… Xn can be qualitative and/or quantitative. Thus, the GLM allows the linear model to be linked to the response variable via a link function and allows the amplitude of the variance of each measure to be a function of its expected value. The model seeks the most likely observations by estimating unknown parameters, and does so by maximizing an amount that measures the likelihood of observing the sample (the likelihood function; Figure 7.8). This linking function depends on the type of data that will determine the probability law to be used (Table 7.2), for example, in Figure 7.8, the normal distribution for a Gaussian GLM.

168

Statistics in Environmental Sciences

Figure 7.8. Differences in methods between the least squares adjustment (A) and the likelihood method (B) (example of a Gaussian model) to form a line in the middle of a point cloud. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The percentage of explanation of the model is no longer made by the R2 (in other words, the variance part explained by the model), which was calculated from the ratio of squares of explained deviations to the square of total deviations in linear models (see section 7.1.1). For GLMs, deviance is used: it measures the difference in likelihood between the model and the “perfect” or saturated model, in other words, the model that has as many parameters as it does observations and therefore estimates the data accurately (providing zero deviance). The deviance analysis table will not be detailed here because it is much more complex than that of conventional linear models. However, it is important to note that the deviance test depends on the model: the Fisher– Snedecor test will be used for Gaussian or Gamma GLMs, whereas the chi-squared test will be used for binomial and Poisson GLMs (Table 7.2). However, it is possible to estimate an R2 equivalent by: R2equiv=1 – (explained deviance/zero deviance) which expresses the part of deviance explained by the model. The final equations depend on the type of model and the link used (see Table 7.2).

Y = exp(ß0 + ß1*X1 + ß2*X2 +…+ ßn*Xn)

Chi-squared

log

Equal to the mean

Tukey for a balanced plan Bonferroni for unbalanced plan Dunnet for comparison to a standard

exp(β0 + β1 *X1 + β2 *X2 +... + βn *Xn ) (1+ exp(β0 + β1 *X1 + β2 *X2 +... + βn *Xn

Chi-squared

Fisher Y=

logit

Identity or log

Y =ß0 +ß1*X1 + ß2*X2 +…+ ßn*Xn

np(1-p)

Constant

1/Y = ß0 + ß1/X1 + ß2/X2 +…+ ßn/Xn)

Fisher

log

Constant square

Table 7.2. The different generalized linear models compared to the linear model, common use, hypotheses, liaison functions, associated tests and corresponding equations

Post hoc

Final equation

Hypothesis on the variance Function of usual connection Tests for deviance

Example of use

Classical use of the law of probability

Binomial The observations follow a binomial distribution

Generalized linear models

Poisson Gamma The observations follow a Observations follow a Poisson distribution gamma distribution Positive integer data; number of Actual data; according to a Binary data; number of successes for events occurring during a time Real non-zero positive data normal distribution several experiments and a known interval, the mean frequency of with high dispersion (symmetrical distribution) probability of success occurrence being constant Proportions Many situations Counting, specific wealth.... Density of individuals Presence/Absence

Gaussian The observations follow a Hypothesis normal distribution

Classic linear model

Classical and Generalized Linear Models 169

170

Statistics in Environmental Sciences

7.2. Conditions of application of the model 7.2.1. Classic linear models The application of linear models is subject to four preconditions related to the model residuals and not to the raw data used for the model. It therefore requires that the model be carried out (estimate the line, the ANOVA or the ANCOVA) before verifying the application conditions. Tests directly performed on the raw data give much less powerful results than those applied to the residuals. However, graphical residual analyses remain more reliable and powerful than the use of inferential tests, although the use of inferential tests remains more appropriate for making decisions, especially at the beginning. The conditions of application are drastic and often difficult to comply with: – the centering of the residuals, in other words, residuals that are homogeneously distributed on either side of zero over the entire variation range of the values of the explained variable; – the independence of the residuals: this hypothesis is based on a simple random sampling. Any spatial (with non-random station positioning) or temporal (with constant sampling) sampling can generate spatial or temporal autocorrelation. If this condition is not met, the “space” or “time” variable can be introduced into the model as a variable that includes a matching (random variable); – the normality of the residuals; however, linear models are robust beyond 20–30 pieces of data; – the homogeneity of the variances of the residuals over the entire range of variation of the values of the explained variable. If the conditions are not met, it is possible to transform the quantitative variable to be explained (see section 7.3.1). If this is not enough to meet the application conditions, other approaches such as GLMs, permutation ANOVAS (PERMANOVA) or non-parametric rank-based tests (Spearman/Kruskal–Wallis; see Chapter 8) should be considered. For a graphical analysis (Figure 7.9), the centering, homogeneity of variances and independence of residuals can be analyzed by the standardized residual graph according to the predicted values, while normality is analyzed

Classical and Generalized Linear Models

171

on the Henry curve (QQ-plot). A “banana” shape of the residual graph that depends on the predicted values indicates autocorrelation in the data, whereas a trumpet shape indicates a heterogeneity of variances (Figure 7.9). On the QQ-plot, the points must not move too far from the diagonal at either limit.

Figure 7.9. Analysis of graphs to verify the application conditions of classical or generalized linear models. What you need and what you do not need, etc.

For the example of the length of the sepals as a function of the length of the Iris versicolor petals, only a small “trumpet” shape appears to be visible on the residual graph based on predicted values suggesting non-homogeneity of the data (Figure 7.10(A)). The Henry curve is, in contrast, well in agreement with the bisector reflecting residuals that follow a normal distribution (Figure 7.10(B)). This data set is therefore well suited for the application of a linear model.

Figure 7.10. Graphs that can be used to analyze the application conditions of the linear model sepal length versus petal length for I. versicolor: residuals versus predicted values (A), Henry or Q-Q plot curve (B) and Cook’s distance curve (C). For a color version of this figure, see www.iste.co.uk/david/statistics.zip

172 Statistics in Environmental Sciences

Classical and Generalized Linear Models

173

The inferential tests that are available and recognized as powerful for testing application conditions are for: – the homogeneity of residual variances: the Goldfeld–Quandt test for a regression or Levene’s test for an analysis of variance. For the example of sepal length regression according to petal length for I. versicolor, there was doubt about this hypothesis. The application of the Goldfeld–Quandt test allowed this hypothesis to be verified and gives a P-value of 0.66, which is higher than the α risk, H0 is accepted and the homogeneity of the variances is verified. Bartlett’s test is often proposed in the literature, but is only valid for balanced ANOVA plans (as many repetitions per condition). Levene’s test remains the most powerful, although it is not available in all statistical software. The biases generated by the least squares method are often very big when this condition is not met; – the normality of the residuals: the Shapiro–Wilk test on the model residuals remains the most powerful test for this condition. However, linear models are recognized as being robust for large samples (n > 30) when this condition is not met; – the independence of the residuals: can be tested via the Durbin–Watson test. This condition may be difficult to achieve when the sampling strategies used imply that the data are dependent on each other (random systematic sampling in space and time for example). Lastly, Cook’s distance curve gives an idea of the aberrant points considered in the model, given that for each of the points, it returns the distance between the parameters estimated by the regression with and without this point (Figure 7.10(C)). If the significance of the role of each point is concentrated onto a few values, the linear model is not good and it is better to remove these points. Here, no aberrant points are found. However, not all researchers agree on the interpretation of this graph. The simple graphical analysis of the linear model allows us to quickly detect the presence of aberrant points. A detailed analysis of the application conditions is described for regression in section 7.4.1 and for ANOVA in section 7.4.3. 7.2.2. Generalized linear models The application conditions are:

174

Statistics in Environmental Sciences

– the independence of the variables; – the type of distribution; – the observations are distributed around the predicted value; – the hypothesis on variances that will be modified from one distribution law to another (see Table 7.2). The verification of the application conditions is done by the graphical method described for linear models (see section 7.2.1). The distribution of standardized residuals around the predicted values for the linear model is observed, but here we will be talking about standardized deviance residuals for GLMs. The Henry curve is analyzed in the same way, even if a less extreme distribution on the line has to be used for a classical linear model, and care should be taken with aberrant values on Cook’s distance graph. 7.3. Other useful analyses 7.3.1. Transformations, linearization In a study on the impact of fishing on cod stocks, researchers want to determine if there is a relationship between fish weight and size in order to determine if the information carried by these two variables is “redundant”, in other words, if the evolution of its length automatically indicates the evolution of its weight. In the latter case, it would be unnecessary to measure the two “length” and “weight” variables during the scientific study and this would reduce the number of variables that need to be considered: only one of the two variables is necessary. Ideally, this relationship, if it exists, should be representable in the form of a mathematical equation that would allow information to be obtained a posteriori without the need of measuring it in the field. Observation of the “weight” curve according to “length” (Figure 7.11) indicates that this relationship exists: the larger the fish, the heavier the fish. However, it is not in the form of a straight line, but rather in the form of an exponential growth. The relationship is monotonous, in other words, it has a specific direction (an increase here), but it is not linear. Therefore, we cannot

Classical and Generalized Linear Models

175

use the linear correlation coefficient or a regression to measure this relationship directly.

Figure 7.11. Weight representation (in grams) according to height (length in cm) of North Atlantic cod (fictitious data). Two types of trend were fitted to the data: a straight line and an exponential curve

However, in the case of a monotonous relationship, certain transformations can allow Y to be linearized according to the X(s) and thus allow the application of linear models. But this method does not always work. In the latter cases, where transformations are not effective in linearizing the data, some other methods can be used to process the data (non-parametric alternatives for the case of a monotonic relationship, Chapter 8). Among the most popular: – logarithmic transformation, when the relationship of Y to X is exponential or when the data are in the form of integers representing large categories, or when solving residual dependence problems; there are several possible logarithmic transformations to test: >log(x) >log10(x) >log2(x)

# Neparian logarithm # log base 10 # log base 2

– the square root transformation, used when the variance is proportional to the mean, which is visible on a graph of group variances and group means:

176

Statistics in Environmental Sciences

>sqrt (Y)

– the arc-sine transformation for percentages obtained experimentally: >asin (Y)

– the power normalization of Box Cox, useful when the variances are not homogeneous and which allows the normalization of residuals. It is a form of power transformation. It is more difficult to implement because it requires finding the best power parameter ƛ for transformation based on the maximum likelihood method. Here is an example of an application found on a forum (https://stackoverflow.com/questions/33999512/how-to-use-thebox-cox-power-transformation-in- r) from the library(MASS): >library(MASS) # Generation of fictitious data >set.seed(1) >n x y LM par(mfrow=c(2,2)) ; plot(LM) # The application conditions are not met # Search for the lambda parameter that will optimize the Box-Cox transformation >bc lambda powerTransform summary(glht(glm,linfct = mcp(X = "Tukey")))

where: glm refers to the result of the GLM; X is the explanatory variable; – Bonferroni post hoc test for an unbalanced plan (as many repetitions per condition): - for a classic linear model: >pairwise.t.test(Y, X, p.adjust="bonferroni", pool.sd = T) >pairwise.t.test(Y, interaction(X1,X2), p.adjust="bonferroni", pool.sd = T)

- for a generalized linear model: >summary(glht(glm,linfct = mcp(azote = "Tukey")), test = adjusted(type = "bonferroni"))

– Dunnett post hoc test model, when one of the conditions is chosen as a reference and the others must be compared to it. It is necessary to construct contrasts in order to freeze the condition of reference. For example, for an X variable with three treatments: b0, b1 and b2 including one piece of evidence, b0. Note that in the contr object, the evidence is noted as –1 and variable is noted as 1 for all comparisons that need to be made between conditions and bystander: >library(multcomp) >contr summary(glht(LM,linfct=mcp(X = contr)),test = adjusted(type = "free"))

In our example showing 50 values, strictly per species, the Tukey test would give us P-value < 2.2 ×10–16 for each pair of species I. setosa–I virginica, I. setosa–I. versicolor and I. virginica–I. versicolor. All species are therefore different in terms of petal lengths.

Classical and Generalized Linear Models

179

Most of the time, the results of the post hoc tests are noted by the letters a, b, c, expressing different values (Figure 7.10).

Figure 7.13. Box plots representing the lengths of petals according to their species. The letters represent the significance: three species are different with the lowest value noted as (a) for I. setosa and the highest value noted as (c) for I. virginica

If there had been no differences between I. setosa–I. versicolor and I. versicolor–I. virginica, the letters would have been doubled: a, ab, b. 7.3.3. The variance partition Let us suppose that we want to analyze the effect of two variables, for example two quantitative variables, humidity and time, on fluctuations in Bidonia species abundance and that a significant effect of two variables is highlighted independently. This means that two variables, taken independently, explain a part of the fluctuations in Bidonia abundance (R2 = 0.4 or 40% for time and R2 = 0.35 or 35% for humidity; Figure 7.14). However, the consideration of both variables through a multiple regression only shows that an R2 of 0.55 is less than the sum of the two effects of time and humidity on Bidonia abundance variations (0.35 + 0.40 = 0.75). The

180

Statistics in Environmental Sciences

variance partition will allow a better understanding of this result by quantifying the relative importance of each of these two factors in the explanatory power: the pure proportion of each (without effect of the other) and the combined proportion linked to an existing relationship between the two (Figure 7.14). The variance partition only applies to multiple linear models (at least two explanatory variables): multiple regressions or ANOVA with at least two factors. It makes it possible to quantify part of the explanation related to the pure effect of each variable considered (in other words, independently of the others), as well as the combined effects related to the redundancy of information between these variables (related to an existing relationship between them).

Figure 7.14. Example of a variance partition of the effect of time and humidity (two quantitative variables) on Bidonia abundance. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

To understand how it works, it must first be carried out by hand. These are four quadrants: (a) the proportion purely related to time, (b) the proportion related to the combined effect of time and humidity, (c) the proportion purely related to humidity and (d) the proportion not explained by these two variables (Figure 7.14). The simple regressions and the multiple regression are carried out and the R2 corresponds to a proportion of the overall variance explained each time: – a + b = 0.40 = R2time => R2 simple regression by time; – b + c = 0.35 = R2humidity => R2 simple regression by humidity;

Classical and Generalized Linear Models

181

– a + b + c = 0.55 = R2time × humidity => R2 multiple regression time + humidity; – a + b + c + d = 1. The resolution of the system with four equations and four unknown factors then makes it possible to estimate a, b, c and d: – proportion of humidity => c = R2time × humidity-R2time = 0.55–0.40 = 0.15; – proportion purely related to time => a = R2time × humidity-R2humidity = 0.55-0.35 = 0.20; – proportion related to the combined effect => b = d–c–a = 0.55–0.15– 0.20 = 0.20; – unexplained proportion => d = 1-R2time×humidity = 0.45. Depending on the design used, this system may be overdetermined and lead to different estimates for the different coefficients, and sometimes negative values. Some corrections are sometimes proposed in the functions in order to ensure that the sum a + b + c + d is equal to 1 in the end. For multiple regression, the use of the varpart() function of the vegan library is possible and integrates these corrections. Unfortunately, for ANOVA, ANCOVA or GLM, the varpart() function is not usable; we have to do the calculations by hand like before. Corrections are more difficult to integrate. 7.3.4. Selection criteria for linear models: AIC or BIC criteria When returning from the field or after an experiment, different factors or variables are potentially explanatory regarding the fluctuations of the variable of interest. The choice of explanatory variables to be kept is not simple: we have seen that with the set of partial regressions, a single variable which is considered to explain a variable of interest may not be significant because its effect is hidden by another variable (see section 7.1.1.1). Similarly, a variable can be very explanatory when this explanation is linked to the effect of a variable that is very redundant. According to the principle of parsimony, if two theories both explain a result well, it is necessary to “settle” for the simplest explanation. A model

182

Statistics in Environmental Sciences

based on a large number of variables will focus on explaining the observed values at the expense of explaining the general phenomenon (the more coefficients estimated, the less precise they will be). There are mathematical optimization models for the variables to be retained. They are based on minimizing a mean distance (quadratic risk/Kullback difference) between the “true” theoretical model and different possible combinations of predictor variables. For example, both the Akaike information criterion (AIC) and the Bayesian information criterion (BIC) allow models to be penalized according to the number of parameters in order to satisfy the parsimony criterion. The model with the lowest AIC or BIC is then chosen. These two criteria are the most commonly used for classical linear models and GLMs in environmental sciences. Be careful, however, because these are mathematical models to be viewed with a critical eye: knowledge of the data will make it possible to better analyze the combination of variables to be retained. If, among the 10 initial variables, the presence of any variables already appear to be scientifically unjustified (e.g. X10 or X2), it is not necessary to use them when applying the optimization model. 7.3.5. From model to prediction Naturally, the model equations can be reused later to predict the value that Y would give for given Xs. However, it should be noted that this prediction is subject to an error risk given that the models never explain 100% of the fluctuations of the variable explaining Y. In addition, care must be taken regarding the ranges of X-values on which the model was performed, because the model is only valid for these ranges. The model gives a confidence interval, as well as a wider interval for prediction. In the classical linear model, it is assumed that the ε error is independent of xi and is distributed according to the normal distribution, with 0 as the mean and a constant variance. For a given x i value, the confidence interval is estimated on the mean of values of the variable to be explained, while the prediction interval is estimated on the xi given. It is therefore preferable to use the prediction interval so that the risk of error is reduced in the calculations.

Classical and Generalized Linear Models

183

7.4. Example of the application of different linear models 7.4.1. Simple linear regression 7.4.1.1. With verification of detailed application conditions The Arcachon Bay is characterized by its high oyster farming activity and the presence of large shoals of wild oysters that are distributed throughout the bay in the intertidal zones. The factors conditioning the physiological state of oysters are therefore particularly studied by researchers. This species feeds on microalgae that is suspended in the water column and the availability of these organisms is therefore likely to strongly influence its state of health (growth, reproduction). A sampling at 64 stations was therefore carried out during the summer (breeding period of the species). For each station, the mean condition index was calculated from a random sample of 30 oysters. Initial observations have shown an increasing gradient in terms of condition indices toward the northwest of the bay: oysters are therefore much better off in the outer areas at the edge of the bay, than in inland waters influenced by the Leyre River. Seeing as water turnover time is longer in external areas, work was carried out to model the condition index depending on water turnover time and to verify whether this parameter could explain this gradient. Indeed, a shorter turnover time would allow the food to be renewed more regularly and thus have a positive impact on the physiological condition of the oyster. The aim here is therefore to analyze to what extent the water turnover time could explain the variation in the condition index and to therefore test the type of linear regression Condition = aTurnover + b (a = slope, b = ordinate of origin): >datastr(data) ‘data.frame’: 64 obs. of 7 variables: $ Point : int 1 2 3 4 5 6 7 8 9 10... $ DatePrel : Factor w/ 5 levels "23/06/2013","24/06/2013",..: 1 1 1 1 1 1 1 1 1 2 ... $ LatWGS84 : num 44.7 44.7 44.7 44.7 44.7... $ LonWGS84 : num -1.14 -1.15 -1.15 -1.15 -1.13... $ Condition : num 4.7 5.98 5.22 7.52 5.72... $ Turnover : num 20.2 18.9 19.7 19.2 20.2... $ Altitude : num -0.71 0.06 -0.02 -0.09 -0.55 -1.2 -0.85 -0.34 -0.27 -0.68...

184

Statistics in Environmental Sciences

The database presents seven variables for 64 stations in the Arcachon Bay, including the condition index (Condition) and the turnover time in days (Turnover), both quantitative. 7.4.1.1.1. Observation of the plausibility of the linear relationship to be tested The first step is to check whether this relationship is graphically possible. NOTE.– The condition index data were log-transformed (section 7.3.1) because an initial analysis that was carried out, as shown below, showed that the application conditions were not met and that the relationship was exponential. >plot(log(Condition)~Turnover time (in days, data,xlab="Turnover", ylab ="log(Condition index)",pch=16,col="dark grey")

It is clear that log(Condition) evolves linearly and negatively according to the turnover time. The study of this regression can therefore be considered (Figure 7.15).

Figure 7.15. Graph showing the log(Condition index) according to turnover time. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.4.1.1.2. Implementation of the model to obtain the residuals to be tested The lm() function is used for simple regression by specifying the variable to explain log(Condition), tilde, the Turnover explanatory variable and the object containing the two variables (data).

Classical and Generalized Linear Models

185

The resulting object (LMR) allows residuals to be obtained using LMR$res: >lm(log(Condition)~Turnover,data)->LMR >LMR$res # Model residuals

7.4.1.1.3. Graphical analysis of application conditions (section 7.2.1) Applying the plot() function to the model allows the visualization of four graphs, with a 4-way separation of the graphical window using par(mfrow): >par(mfrow=c(2,2));plot(LMR)

C

D

Figure 7.16. Graphs given by R to analyze the application conditions of the log(Condition) according to the turnover time. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Only the first two graphs are to be used, the other two can give an incorrect view of the results: residuals according to predicted values and Henry’s curve (QQ-plot; Figures 7.16(A) and (B)). It is not advised to interpret the square root graph of standardized residuals according to the

186

Statistics in Environmental Sciences

predicted values because the interpretation may be biased (Figure 7.16(C)). As for Cook’s distance curve (Figure 7.16(D)), it is difficult to interpret, but there are no values that are obviously outside the red intervals that can be considered as being aberrant. The graph representing the residuals according to the predicted values allows us to check the centering, independence and homogeneity of the residuals. The points must be evenly distributed around 0 (centering). There should be no “banana” shape (reflecting the dependence or autocorrelation of residuals) or “trumpet” shape (reflecting the heterogeneity of variances). The Henry curve is used to check normality: the points must be distributed over the bisector of the graph without moving too far away from its endpoints. Obviously, all the conditions seem to be met in graphical terms, but the use of inferential tests can allow a more objective analysis if necessary. 7.4.1.1.4. Inferential decision support tests The application of the Goldfeld–Quandt test, which is accessible by the gqtest() function of the lmtest library, allows us to check the homogeneity of the variances of the residuals with H0; the variances are homogeneous: >library(lmtest) >gqtest(LMR) Goldfeld-Quandt test data: LM GQ = 1.6633, df1 = 30, df2 = 30, p-value = 0.08463 alternative hypothesis: variance increases from segment 1 to 2

The application of the test on the results of the LMR model gives a P-value > 0.05, H0 is preserved: the homogeneity of the variances of the residuals is respected. The application of the Shapiro–Wilk test on model residuals (LMR$res), which is accessible by the shapiro-test() function, allows us to check the normality of residuals with H0; the residuals follow a normal distribution: >shapiro.test(LMR$res) Shapiro-Wilk normality test data: LM$res W = 0.98478, p-value = 0.6171

Classical and Generalized Linear Models

187

The application of the Shapiro–Wilk test on the residuals of the LMR$res model gives a P-value > 0.05, H0 is preserved; the normality of the residuals is respected. Finally, the application of the Durbin–Watson test on the residuals, which is accessible by the dwtest() function of the lmtest library, allows us to check the independence of the residuals with H0; the residuals are independent: >library(lmtest) >dwtest(LMR) Durbin-Watson test data: LM DW = 1.7837, p-value = 0.1699 alternative hypothesis: true autocorrelation is greater than 0

The application of the Durbin–Watson test on the LM model gives a P-value > 0.05, H0 is maintained; the independence of the residuals is respected. The conditions of application are therefore all respected and the model can be analyzed. However, it should be noted that the mere observation of the graphs leads us to the same conclusions. Inferential tests are especially useful when there is doubt about one of the conditions. 7.4.1.1.5. Observation of the results of the simple regression The summary() function allows us to obtain the value of the Fisher test F statistic for the regression, the value of the R2 (to be read as “multiple R-squared” for a simple linear regression), the P-value of the regression test, as well as the coefficients of the line in the Estimate column, which is the y-intercept for the intercept and the slope for the tested variable: >summary(LMR) Call: lm(formula = log(Condition) ~ Turnover, data = data) Residuals: Min 1Q Median 3Q Max -0.35340 -0.11186 -0.01016 0.12179 0.39031 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 3.97779 0.24301 16.369 < 2e-16 ***

Renouvellement -0.10989

0.01222

-8.993 7.63e-13 ***

188

Statistics in Environmental Sciences

Residual standard error: 0.1653 on 62 degrees of freedom (4 observations deleted due to missingness) Multiple R-squared: 0.5661, Adjusted R-squared: 0.5591 F-statistic: 80.88 on 1 and 62 DF, p-value: 7.634e-13

The F statistic for the Fisher test used to test the regression gives 80.88 and the associated P-value 7.634 × 10–13 (P-value anova(LMR) Analysis of Variance Table Response: log(Condition) Df Sum Sq Mean Sq F value Pr(>F) Turnover 1 2.2088 2.20876 80.877 7.634e-13 *** Residuals 62 1.6932 0.02731

The existence of the regression allows the projection of the line on the graph with the abline() function by specifying the result of the model (Figure 7.17(A)): >plot(log(Condition)~Turnover,data,xlab="Turnover time (in days)", ylab ="log(Condition indices)",pch=16,col="dark grey"); abline(LMR,lwd=2,lty=2)

Classical and Generalized Linear Models

189

Figure 7.17. Graphs representing the log(Condition index) according to the turnover time A) and altitude B) with lines from simple superimposed linear regressions

In addition, another explanatory factor is also possible: the immersion time of the oyster. The oyster can filter the water and therefore can only feed itself when it is underwater. The altitude (noted as Height in the database) at which it is located may therefore be a determining factor in its accessibility to food. The higher it is, the longer it emerges at ebb tide and the less able it is to feed over a long period of time. The aim here is therefore to analyze to what extent height could explain the variation in the condition index and thus to test the type of linear regression: Log(Condition) = Altitude + c The same simple regression work performed on the Log(Condition) regression according to Height also shows a significant regression that respects the application conditions, this time with an equation (Figure 7.13(B)): Log(Condition) = –0.18Altitude + 1.69 (R2 = 0.08, P-value < 0.05) For a graphical representation such as the one in Figure 7.17(B): >plot(log(Condition)~Altitude,data,xlab="Altitude (in meters)", ylab ="log(Condition indices)",pch=16,col="dark grey"); abline(LMH,lwd=2,lty=2)

190

Statistics in Environmental Sciences

7.4.1.2. Using an example of prediction from the model equation Let us take the example of the sepal lengths according to petal lengths of the I. versicolor species in Anderson’s iris database (see section 7.1.1): >data(iris) > dataLM plot(Sepal.Length~Petal.Length,data, xlab="Petal length", ylab="Sepal length") abline(LM,col="black", lwd=2)

It is possible to obtain the confidence and prediction intervals with the predict() function from a data.frame. Here, using the new object, is a sequence on the variation range of petal lengths (see section 7.3.5 for the definition of these intervals): >newpchead(pc) fit lwr upr 1 4.478226 4.097061 4.859390 2 4.561054 4.200093 4.922015 3 4.643882 4.303035 4.984728 4 4.726710 4.405872 5.047547 5 4.809538 4.508583 5.110493 6 4.892366 4.611141 5.173591 >pphead(pp) fit lwr upr 1 4.478226 3.691056 5.265395 2 4.561054 3.783466 5.338642 3 4.643882 3.875424 5.412339 4 4.726710 3.966916 5.486504 5 4.809538 4.057924 5.561152 6 4.892366 4.148432 5.636300

It provides a three-column data.frame: fit for the value given by the model, lwr for the lower limit of the desired interval and upr for the upper limit of the interval. Here, the results can be represented on the graph with

Classical and Generalized Linear Models

191

the matlines() function that allows the columns of a matrix to be graphically represented (Figure 7.18): >matlines(new, pc[,2:3], lty=c(2,2), lwd=2,col="blue") >matlines(new, pp[,2:3], lty=c(3,3), lwd=2,col="red") >legend("bottomright",c("confidence","prediction"),lty=c(2,3), col=c("blue","red"))

Figure 7.18. Simple linear regression of sepal lengths according to petal lengths in I. versicolor; confidence and prediction regression intervals are superimposed. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.4.2. Multiple linear regression 7.4.2.1. Using an example of a variance partition Let us take another look at the example of the study on the condition index of oysters in the Arcachon Bay (see section 7.4.1). The plausibility and significance of a linear relationship between log(Condition) according to turnover time and altitude (noted as Height in the database; see section 7.3.1) has previously been verified. This is therefore the case of a multiple linear regression that analyzes to what extent turnover time and height could explain the variation in the condition index, and thus test the type of linear regression:

192

Statistics in Environmental Sciences

Log(Condition) = aTurnover + bHeight + c The condition indices were log-transformed in order to meet the application conditions of the linear model. The function to be used is the same, lm(), as well as the same application conditions: >LM2lm(log(Condition)~Turnover+Altitude,data)->LM

7.4.2.1.1. Verification of application conditions Thus: >par(mfrow=c(2,2));plot(LM)

There may be doubts about the conditions for applying residual normality to Henry’s curve and about the independence of the residuals (“banana” shape) on the residual graph according to the predicted values (Figure 7.19). Inferential verification tests are therefore recommended (see details of the process in section 7.3.1).

Figure 7.19. Graph given by R to analyze the application conditions of the multiple regression log(Condition) according to the turnover time and height. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Classical and Generalized Linear Models

193

7.4.2.1.2. Checking the normality of residuals: Shapiro–Wilk test Thus: >shapiro.test(LM$res) Shapiro-Wilk normality test data: LM$res W = 0.97924, p-value = 0.3543

7.4.2.1.3. Verification of residual independence: Durbin–Watson test Thus: >dwtest(LM) Durbin-Watson test data: LM DW = 1.7599, p-value = 0.1322 alternative hypothesis: true autocorrelation is greater than 0

The application of the Shapiro–Wilk and Durbin–Watson tests on the model residuals gives P-values > 0.05, H0 is retained for both tests: the normality and independence of the residuals are therefore respected. Finally, in graphical terms, the centering of the residuals is respected. 7.4.2.1.4. Analysis of the results of the model So: >summary(LM) Call: lm(formula = log(Condition) ~ Turnover + Altitude, data = data) Residuals: Min 1Q Median 3Q Max -0.37134 -0.11388 -0.01886 0.12869 0.36274 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.78275 0.79650 6.005 1.21e-07 *** Turnover -0.15184 0.03947 -3.847 0.000293 *** Altitude 0.93338 0.82124 1.137 0.260243 Residual standard error: 0.1642 on 60 degrees of freedom Multiple R-squared: 0.5854, Adjusted R-squared: 0.5647 F-statistic: 28.24 on 3 and 60 DF, p-value: 1.637e-11

The F statistic for Fisher’s test used to test the regression gives 28.24 and the associated P-value is 1.637 × 10–11, which leads to the rejection of H0.

194

Statistics in Environmental Sciences

The coefficient of determination (R2) is therefore significantly different from 0 and is estimated at 0.585 (“Adjusted R-squared” for multiple regression). The partial regression coefficients of the model are estimated at 4.78 for the y-intercept, -0.15 for the slope relative to the turnover effect after removing the height effect, and 0.93 for the slope relative to the height effect after removing the turnover effect. The tests associated with the coefficients have P-value < 0.001 (Pr column) for the y-intercept and the partial slope of the turnover, but P-value values > 0.05 for the height value. Thus, only the effect of the turnover time is significant. The global model can be written as: Log(Condition) = -0.15 Turnover + 0.93 Altitude + 4.78 (R2 = 0.59, ***) >anova(LM) Analysis of Variance Table Response: log(Condition) Turnover Altitude Residuals

Df Sum Sq Mean Sq F value Pr(>F) 1 2.20876 2.20876 81.9188 8.105e-13 *** 1 0.03563 0.03563 1.3213 0.2549 60 1.61777 0.02696

The variance analysis table gives the detailed sum of squares and the mean squares for the part explained by each variable. It confirms the effect of turnover time alone on the log of condition indices. If one of the variables does not have a significant effect, it can be removed from the model. The equation is the one given in section 7.4.1. This means that the relationship observed regarding the effect of height alone on the oyster condition index is simply related to its interaction with water turnover: Log(Condition) = -0.11 Turnover + 3.98 (R2 = 0.57, P-value < 0.001) 7.4.2.1.5. Variance partition It was highlighted that, taken independently, the variables turnover time and height explained part of the condition indices of oysters. However, the consideration of the two variables through multiple regression highlights the only significant effect of turnover time: the height no longer has a significant effect once the effect of turnover time is removed. The variance partition will allow a better understanding of this result by quantifying the relative importance of each of these two factors in the explanatory power: both of

Classical and Generalized Linear Models

195

their pure parts (without the effect of the other) and the combined part linked to an existing relationship between the two explanatory variables. We seek to determine the part of variance related to the pure effect of turnover (a), the pure part of height (c), the collective part, which is in turn related to redundancy between the two variables (b) and the part which is unexplained (d) (Figure 7.20 and section 7.3.3).

Figure 7.20. Example of a variance partition regarding the effect of turnover time and height on the log of the oyster condition index

For multiple regression, the use of the varpart() function of the vegan library is possible and integrates these corrections. The plot() function of the result provides a graphical display of the variance partition (Figure 7.21): >library(vegan) >varpart(log(data$Condition),data$Turnover,data$Altitude)->VP >VP Partition of variance in RDA Call: varpart(Y = log(data$Condition), X = data$Turnover, data$Altitude) Explanatory tables: X1: data$Turnover X2: data$Altitude No. of explanatory tables: 2 Total variation (SS): 3.902 Variance: 0.061936 No. of observations: 64 Partition table: Df R.squared Adj.R.squared Testable [a+b] = X1 1 0.56606 0.55906 TRUE

196

Statistics in Environmental Sciences

[b+c] = X2 [a+b+c] = X1+X2 Individual fractions [a] = X1|X2 [b] [c] = X2|X1 [d] = Residuals –Use function ‘rda’ to >plot(VP)

1 2 1 0 1

0.07720 0.57519

0.06231 0.56126

TRUE TRUE

0.49895 0.06011 0.00220 0.43874

TRUE FALSE TRUE FALSE

test significance of fractions of interest

Figure 7.21. Results given by R for the graph of the variance partition regarding the effect of turnover time and height on the log of the oyster condition index

The graph shows the variance parts explained in a pure way, the parts of variables 1 and 2 (according to the order given in the varpart() function in the circles), as well as the collective part (interaction of two circles) and the residual part (Residuals). Thus, the turnover time explains 50% of the variance in a pure way (without height-related effects) and 6% according to a combined turnover-height effect. Height, on the other hand, does not explain anything without any effect related to turnover. The simple regression conducted between the condition index and height, although significant, therefore only reflects the part of this variable related to turn over time. It should be noted that the variance explained by the simple condition/turnover index regression is the sum of pure variances of the turnover and the combined variances of two variables. In conclusion, only the consideration of turnover time is necessary in order to describe 56%

Classical and Generalized Linear Models

197

of fluctuations in log-transformed condition indices. The remaining variance expressed by the residual part must be explained by other independent variables. 7.4.2.2. Using an example of model optimization for multiple regression Imagine that we wanted to know the influence of certain forest population characteristics on the development of the processionary caterpillar. To do this, various parameters are recorded in the field and the database is kept in the che.txt file: > che str(che) ‘data.frame’: 33 obs. of 11 variables: $ Nests: num 2.37 1.47 1.13 0.85 0.24 1.49 0.3 0.07 3 1.21... $ X1 : int 1200 1342 1231 1254 1357 1250 1422 1309 1127 1075... $ X2 : int 22 28 28 28 32 27 37 46 24 34... $ X3 : int 1 8 5 18 7 1 22 7 2 9 ... $ X4 : num 4 4.4 2.4 3 3.7 4.4 3 5.7 3.5 4.3 ... $ X5 : num 14.8 18 7.8 9.2 10.7 14.8 8.1 19.6 12.6 12... $ X6 : num 1 1.5 1.3 2.3 1.4 1 2.7 1.5 1 1.6... $ X7 : num 1.1 1.5 1.6 1.7 1.7 1.7 1.9 1.9 1.7 1.8... $ X8 : num 5.9 6.4 4.3 6.9 6.6 5.8 8.3 7.8 4.9 6.8... $ X9 : num 1.4 1.7 1.5 2.3 1.8 1.3 2.5 1.8 1.5 2... $ X10 : num 1.4 1.7 1.4 1.6 1.3 1.4 2 1.6 2 2...

There are several variables available. The variable of interest, which is the number of caterpillar nests per tree of a plot of nests and 10 other potentially explanatory variables of this number of nests: the altitude (X1), the slope of the ground (X2), the number of pines on the plot (X3), the height of the tree in the center of the plot (X4), its diameter (X5), the density of the forest population (X6), the direction of the plot (X7), the height of the dominant trees (X8), the number of layers of vegetation (X9) and the mixture of plant populations (X10). This is a “multiple linear regression”. Of all these explanatory variables, we would like to determine the most explanatory ones, but without any preliminary ideas on the choice of variables to be used. We will therefore develop an optimization model using the Akaike criterion (AIC, see section 7.3.4). The application of an AIC model on the example can be done with the stepAIC() function of the MASS library in R. The stepAIC() function applies

198

Statistics in Environmental Sciences

itself to the model by considering all available variables other than the explanatory variable. The criterion k = 2 indicates the use of the AIC (k = log(n), the use of the BIC): >LMche aic summary(aic) Call: lm(formula = Nests ~ X1 + X2 + X4 + X5 + X9, data = che) Residuals: Min 1Q Median 3Q Max -1.11954 -0.25116 -0.01917 0.28641 1.19584 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.2817173 1.0208616 6.153 1.41e-06 ***

Classical and Generalized Linear Models

199

X1 -0.0024906 0.0007928 -3.141 0.00405 ** X2 -0.0348229 0.0132497 -2.628 0.01399 * X4 -0.5443351 0.2432573 -2.238 0.03369 * X5 0.1298846 0.0546146 2.378 0.02473 * X9 -0.3774876 0.2143146 -1.761 0.08950 . –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 Residual standard error: 0.5144 on 27 degrees of freedom Multiple R-squared: 0.6567, Adjusted R-squared: 0.5931 F-statistic: 10.33 on 5 and 27 DF, p-value: 1.29e-05 > anova(aic) Analysis of Variance Table Response: Nests Df Sum Sq Mean Sq F value Pr(>F) X1 1 5.7190 5.7190 21.6140 7.821e-05 *** X2 1 3.4633 3.4633 13.0888 0.001205 ** X4 1 0.5523 0.5523 2.0872 0.160041 X5 1 3.1099 3.1099 11.7533 0.001963 ** X9 1 0.8209 0.8209 3.1024 0.089498. Residuals 27 7.1441 0.2646 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

The model explains 59% of the nests and is significant (P-value = 1.29 × 10–5) with the equation: Nests = –0.002 × X1 – 0.03 × X2 – 0.544 × X4 + 0.1299 × X5 – 0.377 × X9 with an R2 = 0.59 (***) Thus, five variables are kept in the end, even if two are not significant a priori: the altitude (X1), the slope of the ground (X2), the height of the tree in the center of the plot (X4), its diameter (X5) and the number of layers of vegetation (X9). It would be possible to run the multiple regression again without these two insignificant variables to see what it changes: if the R2 is not significantly changed, it would be better to keep this last model. 7.4.3. One-factor ANOVA Researchers want to show that oat production depends on the nitrogen concentration contained in the fertilizer used in agricultural fields. The expected answers would, of course, correspond to an increase in production yield with increasing nitrogen concentrations.

200

Statistics in Environmental Sciences

They are studying the effect of four nitrogen concentrations in fertilizers, between a zero concentration that would correspond to organic farming and three others spread over the range available in commercial fertilizers (four concentrations tested: 0cwt, 0.2cwt, 0.4cwt and 0.6cwt). For each variety, four concentrations are tested. This is a one-factor variance analysis. 7.4.3.1. The database: download and pre-processing The data are available in the oats database of the MASS library: >library(MASS) >data(oats) >str(oats) ‘data.frame’: 72 obs. of 4 variables: $ B: Factor w/ 6 levels "I","II","III",.. : 1 1 1 1 1 1 1 1 1 1... $ V: Factor w/ 3 levels "Golden.rain",..: 3 3 3 3 1 1 1 1 2 2 ... $ N: Factor w/ 4 levels "0.0cwt","0.2cwt",..: 1 2 3 4 1 2 3 4 1 2 ... $ Y: int 111 130 157 174 117 114 161 141 105 140...

This database contains the quantitative variable production to be explained (Y), the variable variety V factor with three conditions (Victory, Marvellous and Golden rain) and the variable nitrogen concentration in fertilizer N factor, also with four conditions (0.0cwt, 0.2cwt, 0.4cwt and 0.6cwt). Variable B identifies the blocks and six blocks provide a replication of the cross conditions. We are only interested in the explained Y and explanatory N variables. The aim here is to analyze the effect of fertilizer on production, regardless of the variety. 7.4.3.2. Determining the ANOVA plan The determination of the experimental plan is detailed in section 3.2. The contingency table allows us to see the number of replicas per condition: >table(oats$N) 0.0cwt 0.2cwt 0.4cwt 0.6cwt 18 18 18 18

There are 18 replicas here for each condition. The plan is balanced. In addition, the plan in this experiment is one on which a balanced 1-factor

Classical and Generalized Linear Models

201

ANOVA can be applied given that the concentrations are deliberately chosen by the researchers. 7.4.3.3. Prior observation of results A box plot allows us to see if the hypothesis is plausible (Figure 7.22): >boxplot(Y~N,oats, xlab="Nitrogen concentration", ylab="Production")

Figure 7.22. Graph showing production according to nitrogen concentrations; the letters indicate the significant differences between the different concentrations from the Tukey test

It seems that production increases with concentrations. 7.4.3.4. Carrying out the ANOVA and checking the application conditions In the case of a balanced plan, both lm() and aov() functions are possible, whereas only the lm() function is possible for an unbalanced plan. We would therefore prefer the latter, which can ultimately be used for all linear models: >ANANANpar(mfrow=c(2,2)); plot(AN)

The residuals are centered (Figure 7.23(A)). No particular shape is shown on the residuals graph according to the predicted values.

Figure 7.23. Graph given by R to graphically verify the application conditions in the case of an ANOVA explaining oat production according to fertilizer concentrations. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The bisector is followed on Henry’s curve (Figure 7.23(B)). In the case of any doubt, the most important conditions can be tested by the tests given in section 7.2.1. However, we will apply all inferential tests for an ANOVA as an example. The application of Levene’s test can be accessed through the leveneTest() function of the car library and it allows us to check the homogeneity of the variances of the residuals with H0; the variances are homogeneous:

Classical and Generalized Linear Models

203

>library(car) >leveneTest(AN)# Levene’s test for homogeneity of variances of residuals Levene’s Test for Homogeneity of Variance (center = median) Df F value Pr(>F) group 3 0.0662 0.9776 68

The P-value is greater than the risk α = 5%, H0 is retained; the variances are homogeneous. The application of the Shapiro–Wilk test on the model residuals (AN$res), which is accessible through the shapiro-test() function, allows us to verify the normality of the residuals with H0; the residuals follow a normal distribution: >shapiro.test(AN$res) Shapiro-Wilk normality test data: AN$res W = 0.96997, p-value = 0.08254

The application of the Shapiro–Wilk test on AN$res model residuals gives a P-value > 0.05, H0 is preserved; the normality of the residuals is respected. Finally, the application of the Durbin–Watson test on residuals, which is accessible through the dwtest() function of the lmtest library, allows us to check the independence of residuals with H0; residuals are independent: >library(lmtest) >dwtest(AN) Durbin-Watson test data: AN DW = 0.83794, p-value = 8.986e-08 alternative hypothesis: true autocorrelation is greater than 0

The P-value is less than 5%, revealing a data dependency. However, no “banana” shape appears on the residual graph according to the predicted values. Since the graphs are more reliable, we will consider an independence of the residuals.

204

Statistics in Environmental Sciences

7.4.3.5. Analysis of the ANOVA results So: > summary(AN) Call: lm(formula = Y ~ N, data = oats) Residuals: Min 1Q Median 3Q Max -37.389 -16.889 -2.306 15.486 50.611 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 79.389 5.110 15.535 < 2e-16 *** N0.2cwt 19.500 7.227 2.698 0.00879 ** N0.4cwt 34.833 7.227 4.820 8.41e-06 *** N0.6cwt 44.000 7.227 6.088 5.96e-08 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 Residual standard error: 21.68 on 68 degrees of freedom Multiple R-squared: 0.3851, Adjusted R-squared: 0.358 F-statistic: 14.2 on 3 and 68 DF, p-value: 2.782e-07

The determination coefficient test is significant with a P-value < 0.001 and an R2 that is significantly different to 0 (R2 = 0.39; value given by the “Multiple R-squared” for a 1-factor ANOVA). The coefficients noted in the estimate column are used to write the equation. It should be noted that one of the conditions of the qualitative factor, the one used as a reference in the intercept (see section 7.1.1), is missing: Production = 79.4 + 19.5 × M0.2cwt + 34.8 × M0.4cwt + 44 × M0.2cwt Here, the y-intercept has been set to M0.0cwt. To read the equation, we designate the required condition by 1 and the others by 0: > anova(AN) Analysis of Variance Table Response: Y Df Sum Sq Mean Sq F value Pr(>F) N 3 20020 6673.5 14.197 2.782e-07 *** Residuals 68 31965 470.1 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Classical and Generalized Linear Models

205

In line with our expectations, the variance analysis table reveals a significant fertilizer concentration factor (P < 0.001): production becomes stronger as concentrations increase. However, the significance of the ANOVA means that at least one of the conditions is different to the others. No information is given on which conditions are different from one another. 7.4.3.6. Tukey post hoc test The Tukey post hoc test can be used to assess what significant differences there are between the four concentrations (Tukey test) in a case where a plan is balanced, as shown here (section 7.3.2). The Tukey test is performed on a linear model built with aov(), which is formulated as an lm() test with the TukeyHSD() function: >TukeyHSD(x=aov(Y~N,oats),conf.level=0.95) Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = Y ~ N, data = oats) $N diff lwr upr 0.2cwt-0.0cwt 19.500000 0.4658368 38.53416 0.4cwt-0.0cwt 34.833333 15.7991702 53.86750 0.6cwt-0.0cwt 44.000000 24.9658368 63.03416 0.4cwt-0.2cwt 15.333333 -3.7008298 34.36750 0.6cwt-0.2cwt 24.500000 5.4658368 43.53416 0.6cwt-0.4cwt 9.166667 -9.8674965 28.20083

p adj 0.0426086 0.0000491 0.0000004 0.1566279 0.0062719 0.5860171

The P-value is reported for each crossed condition: only 0.4–0.2 and 0.4–0.6 comparisons have significant P-values lower than the risk α = 5%. Most of the time, the results of the post hoc tests are noted by letters a, b c, expressing different values. To obtain them, it is worth using the multcompLetters() function available in the multcompView library: >library(multcompView) >Letlibrary(multcomp) > contr summary(glht(AN,linfct=mcp(N = contr)),test = adjusted(type = "free")) Simultaneous Tests for General Linear Hypotheses Multiple Comparisons of Means: User-defined Contrasts Fit: lm(formula = Y ~ N, data = oats) Linear Hypotheses: Estimate Std. Error t value Pr(>|t|) N0.2cwt -N0.0cwt == 0 19.500 7.227 2.698 0.00879 ** N0.4cwt -N0.0cwt == 0 34.833 7.227 4.820 1.67e-05 *** N0.6cwt -N0.0cwt == 0 44.000 7.227 6.088 1.97e-07 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 (Adjusted p–values reported – free method)

All productions with nitrogen concentrations are significantly different from production without nitrogen concentration. 7.4.4. Multifactorial ANOVA 7.4.4.1. ANOVA on a balanced crossover plan Let us look at the example of the study of the oat production experiment (see section 7.3.3). This time, the researchers are studying three varieties known for their production properties (Victory, Marvellous and Golden rain)

Classical and Generalized Linear Models

207

and four nitrogen concentrations in fertilizers between a zero concentration that would correspond to organic agriculture and three others distributed over the range available in commercial fertilizers (four concentrations tested: 0cwt, 0.2cwt, 0.4cwt and 0.6cwt). For each variety, the four concentrations are tested. 7.4.4.1.1. The database: download and pre-processing The data are available in the oats database of the MASS library. Among the available variables are the quantitative variable production to be explained (Y), the qualitative V variable with three conditions (Victory, Marvellous and Golden Rain) and the qualitative N variable of nitrogen concentration in fertilizer, also with four conditions (0.0cwt, 0.2cwt, 0.4cwt and 0.6cwt). 7.4.4.1.2. Determining the ANOVA plan The conditions of the two factors Varieties and [Nitrogen] were deliberately chosen by the experimenter: they are therefore fixed factors. The contingency table makes it possible to not only implement the balanced nature of the experimental plan – six replicas per V×N crossover conditions – but also the crossover aspect of the plan – each crossover between the Variety condition and the [Nitrogen] condition is considered: > table(oats$V,oats$N) 0.0cwt 0.2cwt 0.4cwt 0.6cwt Golden.rain 6 6 6 6 Marvellous 6 6 6 6 Victory 6 6 6 6

There are six exclusive replicas for each crossover condition here. The plan is crossed and balanced. In this experiment, it is therefore a plan on which an ANOVA with two balanced fixed crossover factors can be applied, in other words, a type I ANOVA plan. 7.4.4.1.3. Prior observation of results The observation of mean values by condition using the interaction.plot() function and of raw data using the xyplot() function makes it possible to grasp the significance of the interaction:

208

Statistics in Environmental Sciences

>interaction.plot(oats$N,oats$V, oats$Y, fixed=TRUE,xlab="Nitrogen concentration",ylab="Production") >library(lattice) >xyplot ( Y∼N , data = oats, groups = V,pch=c(16,1,16), col=c("grey","black","black"), labels=levels(oats$N)); A. Evolution according to fertilizers by variety Marvellous Golden Rain

B. Distribution by variety Victory

Victory

Golden Rain

Nitrogen concentration

Marvellous

N

Figure 7.24. Observation of the mean lines of the production data according to nitrogen concentrations A) for three varieties and B) the corresponding raw values reflecting the dispersion

In general, it seems that production increases with nitrogen concentration in a very similar way between varieties: the interaction is therefore probably low and not significant if we consider the variability between replicas. Although production seems higher for the Marvellous variety than for the others, a difference between varieties is not necessarily expected based on the variability provided by the replicas. We choose to consider both factors and the interaction, but the expected result from the graphical observation is the only significant effect of nitrogen concentrations (Figure 7.24). 7.4.4.1.4. Carrying out the ANOVA and checking the application conditions So: >ANANAN par(mfrow=c(2,2)); plot(AN) # Graphical display

Figure 7.25. Graph given by R to graphically verify the application conditions in the case of an ANOVA explaining oat production according to fertilizer concentrations and oat variety. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

The residuals are centered. No particular shape is shown on the residual graph according to the predicted values. The bisector is well–followed on Henry’s curve (Figure 7.25). If there is any doubt, the most important conditions can be tested by the tests given in section 7.2.1 and implemented in section 7.4.3. 7.4.4.1.5. Analysis of the ANOVA results So: >summary(AN) # Details of the results

210

Statistics in Environmental Sciences

Call: lm(formula = Y ~ V * N, data = oats) Residuals: Min 1Q Median 3Q Max -38.500 -16.125 0.167 10.583 55.500 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 80.0000 9.1070 8.784 2.28e-12 *** VMarvellous 6.6667 12.8792 0.518 0.606620 VVictory -8.5000 12.8792 -0.660 0.511793 N0.2cwt 18.5000 12.8792 1.436 0.156076 N0.4cwt 34.6667 12.8792 2.692 0.009199 ** N0.6cwt 44.8333 12.8792 3.481 0.000937 *** VMarvellous:N0.2cwt 3.3333 18.2140 0.183 0.855407 VVictory:N0.2cwt -0.3333 18.2140 -0.018 0.985459 VMarvellous:N0.4cwt -4.1667 18.2140 -0.229 0.819832 VVictory:N0.4cwt 4.6667 18.2140 0.256 0.798662 VMarvellous:N0.6cwt -4.6667 18.2140 -0.256 0.798662 VVictory:N0.6cwt 2.1667 18.2140 0.119 0.905707 Residual standard error: 22.31 on 60 degrees of freedom Multiple R-squared: 0.4257, Adjusted R-squared: 0.3204 F-statistic: 4.043 on 11 and 60 DF, p-value: 0.0001964

The determination coefficient test is significant with a P-value < 0.001 and an R2 significantly different from 0 (R2 = 0.32; value given by the “adjusted R-squared” for an ANOVA with 2 or more factors). The equation of the line would be: Production = 80 + 6.67 × M VMarvellous + 8.5 × M VVictory + 18.5 × M N0.2cwt + 34.7M N0.4cwt + 44.8 × M N0.6cwt + 3; 3M VMarvellous:N0.2cwt – 0.3M VVictory:N0.2cwt – 4.2M VMarvellous:N0.4cwt + 4.7M VVictory:N0.4cwt – 4.7M VMarvellous:N0.6cwt + 2.2M VVictory:N0.6cwt In the same way as in section 7.4.3, it should be noted that conditions for each qualitative factor and the interaction are missing; those that were taken as references in the intercept (see section 7.1.1): >anova(AN) # Variance analysis table Analysis of Variance Table Response: Y Df Sum Sq Mean Sq F value Pr(>F) V 2 1786.4 893.2 1.7949 0.1750 N 3 20020.5 6673.5 13.4108 8.367e-07 *** V:N 6 321.7 53.6 0.1078 0.9952 Residuals 60 29857.3 497.6

Classical and Generalized Linear Models

–Signif. codes:

211

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

In accordance with our expectations, only the fertilizer concentration factor is a factor that explains the production variance (P < 0.001), but neither the variety nor the interaction is significant: production becomes stronger as concentrations increase. The Tukey post hoc test can be used to assess what significant differences there are between the four concentrations. The Tukey post hoc test can be used to assess what significant differences there are between the four concentrations if the plan is balanced or Dunnett’s test can be used to compare the conditions with nitrogen concentrations to the conditions without nitrogen (see section 7.4.3). For example, with the Tukey post hoc test (see section 7.3.2): > TukeyHSD(x=aov(Y~N*V,oats),conf.level=0.95)

The P-value is reported for each crossing of conditions: the results are very long and tedious to read. The multcompLetters() function available in the multcompView library (see section 7.4.3) can be useful: > LetN LetV LetNVmeansemy.barplot(mean,se,xlab="Nitrogen concentration",ylab="Production", col=c("blue","green","red"),beside=T,ylim=c(0,max(oats$Y))) >legend("top", legend=c("Golden rain","Marvellous","Victory"),pch=c(15), col=c("blue","green","red"))

Production

Golden Rain Marvellous Victory

Nitrogen concentration

Figure 7.26.Observation Observation ofofbar graphs representing production according to nitrogentoconcentrations by var Figure 7.26. bar graphs representing production according lettersconcentrations report differencesby in significance between different concentrations from the Tukey test. For a colo nitrogen variety. The lettersthe report differences in significance of this figure, see www.iste.co.uk/david/statistics.zip between the different concentrations from the Tukey test. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.4.4.2. ANOVA on an unbalanced crossover plan Researchers want to test the hypothesis according to which plant organs adapt to a limitation of light for their photosynthesis by increasing their area

Classical and Generalized Linear Models

213

of absorption. According to this hypothesis, plants would adapt by increasing the surface area of their leaves in an environment where light would be a limiting factor for a same species. The researchers are therefore setting up a sampling plan in order to test the morphological response of the leaves of a species, the holm oak, to a limitation of light. Two factors that are likely to condition the availability of light in silviculture are considered: – the position of the leaf on the tree: the leaves at the bottom of the tree are likely to be more shaded than those at the top of the tree; – the type of forest: a maintained forest is focused on oak silviculture while a mixed forest is characterized by the presence of other species such as Pinus pinaster, which are larger than oaks and therefore likely to shade the oak trees. The aim here is to test the morphological response of the leaves measured by their length according to the forest type (mixed/maintained) and the stratum (low/intermediate/high position) likely to influence light availability. Two different populations are selected for each forest type; 15–20 leaves are measured per strata within each population based on the samples available in the field. 7.4.4.2.1. The database: download and preprocessing The data are available in the database in the foretchenes.txt file: >read.table("foretchenes.txt", header=T,dec=",")->data >str(data) ‘data.frame’: 202 obs. of 5 variables: $ Type : Factor w/ 2 levels "Maintained","Mixed": 1 1 1 1 1 1 1 1 1 1... $ Population: Factor w/ 4 levels "BG","CX","FR",.. : 2 2 2 2 2 2 2 2 2 2... $ Popbis : Factor w/ 2 levels "P1","P2": 1 1 1 1 1 1 1 1 1 1 ... $ Strata : Factor w/ 3 levels "haut1","haut2",..: 1 1 1 1 1 1 1 1 1 1 ... $ Lmax : num 4.7 6.08 5.28 5.5 5.83...

This database contains the quantitative maximum leaf length variable (Lmax), the forest type variable (Type), the qualitative variable with two conditions (Mixed, Maintained) and the variable reflecting the height on the

214

Statistics in Environmental Sciences

tree (Height), the qualitative variable with three conditions (high1for high, high2 for intermediate and high3 for low). 7.4.4.2.2. Determining the ANOVA plan The conditions of the Type and Strates factors were deliberately chosen by the experimenter: they are therefore fixed factors. The contingency table not only shows the unbalanced nature of the experimental plan – a different number of replicas per crossover condition – but also the crossed nature of the plan – each stratum was considered within each forest: >table(data$Strata,data$Type) Maintained Mixed high1 40 30 high2 39 30 high3 35 28

This is therefore the case of an ANOVA with two fixed cross-factors on an unbalanced plan. 7.4.4.2.3. Prior observation of results The observation of mean values by condition using the interaction.plot() function, and of raw data using the xyplot() function, makes it possible to realize the significance of the interaction: >interaction.plot(data$Strata,data$Type, data$Lmax, fixed=TRUE, xlab="Strata", ylab="Lmax") >library(lattice) >xyplot ( Lmax∼Strata| Type, data = data, fixed=TRUE,,xlab="Strata",ylab="Maximum lengths")

It seems that the maximum length decreases with the height of the strata, with a very similar and general shape between the two forests: the two curves are parallel, so there is no interaction (see Figure 7.27). In addition, the lengths appear to be higher for a mixed forest than for a maintained forest. We will consider the two factors and the interaction, but we only expect to highlight the effects of the Type and Strates factors and not that of interaction.

Classical and Generalized Linear Models

215

Figure 7.27. Observation of the corresponding gross values showing mean lines of length data according to stratum according to A) forest type and B) dispersion by stratum and forest type

7.4.4.2.4. Implementation of the ANOVA and checking the application conditions For an ANOVA on an unbalanced plan, the contrast argument must be added to the lm() function by specifying each factor=contr.sum in a vector so that the calculations are accurate in the type III ANOVA (see section 7.1.1.1). The length data have been log-transformed to comply with the application conditions: >AN=lm(log(Lmax)~Type*Strata,data,contrasts=list(Type=contr.sum, Strata=contr.sum)) # Important contrast option for an unbalanced plan >par(mfrow=c(2,2));plot(AN) #Graphical display

The residuals are centered. No particular shape is shown on the residual graph according to the predicted values. The bisector is not well-followed on Henry’s curve, hence the need to test the normality of the residuals (Figure 7.28): >shapiro.test(AN$res) # Shapiro-Wilk test for data normality

For the Shapiro–Wilk test, the P-value is < 0.05, H0 is rejected. The residuals are not normal. However, the large amount of data makes the ANOVA resilient to this condition (n >>> 20).

216

Statistics in Environmental Sciences

Figure 7.28. Graph given by R to graphically verify the application conditions of the ANOVA explaining the maximum leaf length according to forest type and stratum. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.4.4.2.5. Analysis of the ANOVA results So: > summary(AN) # coef estimated for crossover factor by t-test are not relevant Call: lm(formula = log(Lmax) ~ Type * Strata, data = data, contrasts = list(Type = contr.sum, Strata = contr.sum)) Residuals: Min 1Q Median 3Q Max -0.233533 -0.067847 0.006956 0.069787 0.189135 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.612987 0.006944 232.280 pairwise.t.test(log(data$Lmax), as.factor(data$Type), p.adjust="bonferroni", pool.sd = T) Pairwise comparisons using t tests with pooled SD data: log(data$Lmax) and as.factor(data$Strata) high1 high2

218

Statistics in Environmental Sciences

high2 1.6e-06 high3 < 2e-16 < 2e-16 P–value adjustment method: bonferroni

The P-values are reported for each crossing of conditions: the P-value is much lower than the α risk for all crossover conditions. Note that the multcompletters() function cannot be used here, the letters must be assigned by hand. To use the function on the interaction (not necessary here), we need to create a variable that combines two factors using the interaction() function and then transform it into a factor with as.factor(). This factor can then be used in the pairwise.t.test() function: >data$inthead(data$int) [1] Maintained _ high1 Maintained _ high1 Maintained _ high1 [4] Maintained _ high1 Maintained _ high1 Maintained _ high1 6 Levels: Maintained _ high1 Maintained _ high2... Mixed _ high3

Since the Forest and Strates effects alone are significant, and not the interaction, it is better to use a representation by effect (Figure 7.29), and not by interaction, by crossing the effects. Given the numbers, box plots will be prioritized: >par(mfrow=c(1,2); boxplot(log(Lmax)~Type,data, xlab="Strata", ylab="Maximum length"); boxplot(log(Lmax)~Strata,data, xlab="Strata", ylab="Maximum length")

Figure 7.29. Box plots representing dispersion by A) forest type and B) stratum

Classical and Generalized Linear Models

219

7.4.4.3. Multifactorial ANOVA on a prioritized plan Let us look at the study on the adaptation of plant organs to light limitation. The purpose here is to use the sampling plan conducted on oak trees between mixed and maintained forests to determine whether the maximum lengths vary according to the type of forest and the population sampled (see section 7.4.4.2). Within each forest, two populations were considered: CX and HER for maintained forests, and BG and FR for mixed forests. This sampling plan adds variability, but the populations are specific to each forest and cannot be matched between forests: the CX population of the maintained forest is no more similar to BG than to FR sampled in the mixed forest, the same goes for HER. This is, therefore, a prioritized plan: the forest type being the prioritizing factor and the population being the prioritized factor (Figure 7.30). For this type of plan, it is necessary to assign an identical name between the conditions of the prioritized factor within the conditions of the prioritizing factor in order for the analysis to be performed. However, this assignment is arbitrary and makes no sense: the effect of the P1 versus P2 population cannot be tested; only the analyses of the prioritizing factor and interaction are significant because they reflect differences between populations within two forests.

Figure 7.30. Example of a prioritized plan for studying the adaptation of plant organs to light limitation; populations are specific to each forest and cannot be matched between forests

The aim here is to test the morphological response of the leaves, measured by their length according to the type of forest (mixed/maintained), which is the prioritizing factor, and the population, which is the prioritized factor.

220

Statistics in Environmental Sciences

7.4.4.3.1. The database: download and preprocessing The data are available in the database in the a.txt file, as described in section 5.2.6. This database contains, among other things, the quantitative maximum leaf length variable (Lmax), the forest type variable Type, a qualitative variable with two conditions (Mixed, Maintained) and the variable reflecting the population with four conditions: CX and HER for managed forests, BG and FR for mixed forests and an additional Popbis variable that assigns an identical name between the conditions of the prioritized factor within the conditions of the prioritizing factor: P1 versus P2. 7.4.4.3.2. Determining the ANOVA plan The conditions of the Type and Population factors were deliberately chosen by the experimenter. It is a fixed factor plan. The contingency table makes it possible to find out (1) the unbalanced nature of the experimental plan – a number of different replicas per crossover condition – and (2) the prioritized nature of the plan – only two populations in the mixed forest and two populations in the maintained forest: >table(data$Type,data$Population) BG CX FR HER Maintained 0 56 0 58 Mixed 43 0 45 0

This is therefore an ANOVA with fixed factors that is prioritized on an unbalanced plan. 7.4.4.3.3. Prior observation of results The observation of raw data using the xyplot() function makes it possible to find out whether the interaction is important or not: > xyplot ( Lmax∼Strata | Population, data = data, fixed=TRUE, xlab="Population",ylab="Maximum lengths")

There appears to be no difference between populations within the same forest, whereas the maximum lengths still appear to be higher for the mixed (black) forest than for the managed forest (gray; Figure 7.31). In a prioritized

Classical and Generalized Linear Models

221

plan, only the effect of the prioritizing factor (Type) and the interaction (Type:Population) are considered, but we only expect to highlight the effect of the Type factor and not a significant interaction.

Figure 7.31. Observation of the corresponding gross values reporting on dispersion by population

7.4.4.3.4. Implementation of the ANOVA and checking the application conditions The contrast argument must be added to the lm() function by specifying each factor=contr.sum in a vector so that the calculations are accurate in a type III ANOVA model. The length data have been log-transformed to comply with the application conditions: >ANpar(mfrow=c(2,2));plot(AN) # Graphical display

222

Statistics in Environmental Sciences

The residuals are centered. No particular shape is shown on the residual graph according to the predicted values. The bisector is not well-followed on Henry’s curve, hence the need to test the normality of the residuals (Figure 7.32): >shapiro.test(AN$res) # Shapiro-Wilk test for data normality

For the Shapiro–Wilk test, the P-value is < 0.05, H0 is rejected. The residuals are not normal. However, the large amount of data makes the ANOVA resilient to this condition (n = 204 > 20; Figure 7.28).

Figure 7.32. Graph given by R to graphically verify the application conditions. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.4.4.3.5. Analysis of the ANOVA results So: >summary(AN) # Details of the results Call: lm(formula = log(Lmax) ~ Popbis:Type + Type, data = data, contrasts = list(Popbis = contr.sum, Type = contr.sum)) Residuals: Min 1Q Median 3Q Max -0.49820 -0.17527 0.08143 0.17984 0.36014 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.622392 0.016256 99.803 < 2e-16 ***

Classical and Generalized Linear Models

223

Type1 -0.074348 0.016256 -4.574 8.44e-06 *** Popbis1:TypeMaintained 0.002517 0.021458 0.117 0.907 Popbis1:TypeMixed 0.012274 0.024425 0.503 0.616 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 Residual standard error: 0.2291 on 198 degrees of freedom Multiple R-squared: 0.09642, Adjusted R-squared: 0.08272 F-statistic: 7.042 on 3 and 198 DF, p-value: 0.0001599

ANOVA significantly explains 8% of the variance in leaf length variance (R2 = 0.08 for adjusted R-squared, P-value < 0.001). For an ANOVA on a prioritized plan, the results must be read as type=“III”. This is done with the Anova() function, available in the (car) library: >Anova(AN, type= "III") # Variance analysis table Anova Table (Type III tests) Response: log(Lmax) Sum Sq Df F value Pr(>F) (Intercept) 522.66 1 9960.6956 < 2.2e-16 *** Type 1.10 1 20.9181 8.44e-06 *** Popbis:Type 0.01 2 0.1331 0.8754 Residuals 10.39 198

Only the Forest Type effect is significant in accordance with the expected results. Thus, maximum leaf lengths are higher in a mixed forest than in a managed forest and no significant differences are observed between populations within each forest. Post hoc tests are unnecessary because there are only two conditions. The graph to be used is the one shown in Figure 7.29(A). 7.4.4.4. ANOVA with repeated measurements: mixed plan, matched plan, block effect plan This is once again the example of the study on oat production that is dependent on the nitrogen concentration in the fertilizer and its variety (see section 7.4.4.1). This is a two-factor fixed crossover design on a balanced plan with four nitrogen concentrations and three varieties used. The replication of the cross conditions comes from a block experiment (see section 7.4.3). The area available for the experiment was divided into six different blocks and each cross condition was performed in each block. It is therefore obvious that the results can be influenced by this block effect: for

224

Statistics in Environmental Sciences

example, some blocks may present more fertile land than others and help production. The variability between replicas can be dependent on the block: it is therefore of interest to consider this block effect as an additional random factor in the ANOVA design considered. It is then a mixed ANOVA design considering both fixed factors and one random factor: the block effect. The working hypotheses are – if µi is the mean of the sample i: – H0: µ1 = µ2 = … = µk; – Ha: there is at least one pair (i, j) such as µi ≠ µj. 7.4.4.4.1. Implementation of the ANOVA In the case of a mixed plan, the lme() function of the nlme package is recommended: >library(nlme) >ANM = lme(Y ~ X1*X2, random = ~ 1|Fa, data = data)

with: – Y being the variable to be explained; – X1, X2... being the fixed explanatory factors used; – Fa being the random factor; – data being the data table. The * refers to the study of interactions between variables, in addition to the independent effects of each. It can be replaced by + if the independent effects alone wish to be analyzed (without considering the interaction): >ANM = lme(Y ~ V*N, random = ~ 1|B, data = oats) >summary(ANM) # Details of the results Linear mixed-effects model fit by REML Data: oats AIC BIC logLik 564.69 594.0108 -268.345 Random effects: Formula: ~1 | B (Intercept) Residual StdDev: 15.60138 15.94425 Fixed effects: Y ~ V * N Value Std.Error DF t-value

p-value

Classical and Generalized Linear Models

(Intercept) VMarvellous VVictory N0.2cwt N0.4cwt N0.6cwt VMarvellous:N0.2cwt VVictory:N0.2cwt VMarvellous:N0.4cwt VVictory:N0.4cwt VMarvellous:N0.6cwt VVictory:N0.6cwt >anova(ANM) numDF (Intercept) 1 V 2 N 3 V:N 6

80.00000 6.66667 -8.50000 18.50000 34.66667 44.83333 3.33333 -0.33333 -4.16667 4.66667 -4.66667 2.16667

9.106977 9.205419 9.205419 9.205419 9.205419 9.205419 13.018428 13.018428 13.018428 13.018428 13.018428 13.018428

55 55 55 55 55 55 55 55 55 55 55 55

8.784473 0.724211 -0.923369 2.009686 3.765898 4.870320 0.256047 -0.025605 -0.320059 0.358466 -0.358466 0.166431

225

0.0000 0.4720 0.3598 0.0494 0.0004 0.0000 0.7989 0.9797 0.7501 0.7214 0.7214 0.8684

denDF F-value p-value 55 245.14092 F) V 2 1786.4 893.2 1.7949 0.1750 N 3 20020.5 6673.5 13.4108 8.367e-07 *** V:N 6 321.7 53.6 0.1078 0.9952

Residuals 60 29857.3 497.6 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

The results are a little different. The consideration of the block effect makes the variety effect significant. Thus, the differences between blocks conceal this effect.

226

Statistics in Environmental Sciences

7.4.4.4.2. Graphical representation So: >xyplot ( Y∼V, data = oats, groups = B,type = "a") # Graphical representation of the block effect on the variety

The graph shows that the blocks do not have the same response according to the variety, thus concealing the variety’s response. The random Bloc factor is therefore worth considering when putting this variety effect forward (Figure 7.33).

160

Production

140

120

100

80

60

Golden Rain

Marvellous

Victory

Varieties Figure 7.33. Mean variation in production according to the species, which is represented by different colors. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.4.5. Covariance analysis (ANCOVA) Let us look at the crabs data set (see section 1.2.1) containing five morphological variables measured on 200 crabs of the Leptograpsus variegatus species according to sex and color, collected in Fremantle,

Classical and Generalized Linear Models

227

Australia. Researchers have looked at the allometric relationship between shell length and abdominal width and would like to know (1) if this relationship exists and (2) if this relationship is different between the sexes. 7.4.5.1. The database: download and pre-processing The data are available in the MASS library in the crabs data frame: >library(MASS) >data(crabs) >str(data) ‘data.frame’: 200 obs. of 8 variables: $ sp : Factor w/ 2 levels "B","O": 1 1 1 1 1 1 1 1 1 1 ... $ sex : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ... $ index: int 1 2 3 4 5 6 7 8 9 10... $ FL : num 8.1 8.8 9.2 9.6 9.8 10.8 11.1 11.6 11.8 11.8... $ RW : num 6.7 7.7 7.8 7.9 8 9 9.9 9.1 9.6 10.5... $ CL : num 16.1 18.1 19 20.1 20.3 23 23.8 24.5 24.2 25.2... $ CW : num 19 20.8 22.4 23.1 23 26.5 27.1 28.4 27.8 29.3... $ BD : num 7 7.4 7.7 8.2 8.2 9.8 9.8 10.4 9.7 10.3...

7.4.5.2. Determining the ANOVA plan The objective here is to analyze and see if the width of the abdomen RW depends on a quantitative variable, the length of the shell CL, and a qualitative variable, the sex sex: > table(crabs$sex) F M 100 100

The contingency table shows that this is therefore a balanced ANCOVA design. 7.4.5.3. Prior observation of results The following code lines represent the allometric relationship between abdominal width and shell length by sex by superimposing a line for each in order to visualize the data and graphically analyze the working hypotheses (Figure 7.34): >plot(RW~CL,crabs[crabs$sex=="F",],pch=16,xlab="Shell length", ylab="Abdomen width",ylim=c(min(crabs$RW),max(crabs$RW)),xlim=c(min(crabs$CL),max(c rabs$CL)), col="red")

228

Statistics in Environmental Sciences

>points(RW~CL,crabs[crabs$sex=="M",], pch=16,col="blue") >legend("bottomright", c("F","M"),pch=16, col=c("red","blue")) >abline(lm(RW~CL,crabs[crabs$sex=="F",]),col=c("red"),lwd=2) >abline(lm(RW~CL,crabs[crabs$sex=="M",]),col=c("blue"),lwd=2)

Figure 7.34. Observation of the corresponding gross values reflecting the dispersion of abdomen width according to shell length, differentiated by sex. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

It would seem that this allometric relationship exists, but that it is not identical between the two sexes given that the lines are not parallel and the male and female points seem very different for long shell lengths. One would therefore expect that there is an effect from shell length on abdominal width and an interaction between shell length and sex (Figure 7.34). 7.4.5.4. Implementation of the ANCOVA and verification of the application conditions For an ANCOVA, it is necessary to add the contrast argument by specifying the qualitative factor=contr.sum in a vector so that the calculations are accurate in the type III ANOVA:

Classical and Generalized Linear Models

229

> ANC x11();par(mfrow=c(2,2));plot(ANC)

The residuals are centered. No particular shape is shown on the residual graph according to the predicted values. The bisector is well followed on Henry’s curve, which means that inferential tests are not required for the application conditions (Figure 7.35).

Figure 7.35. Graph given by R to graphically verify the application conditions. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

7.4.5.5. Analysis of the ANOVA results Thus: > summary(ANC)# Good significant model 71% p>0.001, Call: lm(formula = RW ~ CL * sex, data = crabs, contrasts = list(sex = contr.sum)) Residuals: Min 1Q Median 3Q Max -1.16219 -0.33650 0.00698 0.24158 1.22019 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.741147 0.139199 12.51 < 2e-16 *** CL 0.343919 0.004245 81.02 < 2e-16 *** sex1 -0.904859 0.139199 -6.50 6.49e-10 *** CL:sex1 0.059484 0.004245 14.01 < 2e-16 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 Residual standard error: 0.4214 on 196 degrees of freedom Multiple R-squared: 0.9736, Adjusted R-squared: 0.9732 F-statistic: 2408 on 3 and 196 DF, p-value: < 2.2e-16

230

Statistics in Environmental Sciences

The ANCOVA significantly explains 97% of the variance in abdominal width variance (adjusted R-squared). For an ANCOVA on a balanced plan, the variance analysis table, and therefore of the significance of factors, is done with the classical anova() function (or type I). For an ANCOVA on an unbalanced plan, the ANOVA function with type=“III” would be used (see section 7.4.4.3): > anova(ANC)# Analysis of Variance Table Response: RW Df Sum Sq Mean Sq CL 1 1050.27 1050.27 sex 1 197.83 197.83 CL:sex 1 34.88 34.88 Residuals 196 34.81 0.18 –Signif. codes: 0 ‘***’ 0.001

F value Pr(>F) 5913.44 < 2.2e-16 *** 1113.89 < 2.2e-16 *** 196.38 < 2.2e-16 ***

‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

The effect of shell length alone is significant (P-value < 2.2 × 10–16) and the interaction is also significant (P-value < 2.2 × 10–16). The allometric relationship is real as well as significantly different between sexes with a risk of α = 0.001. 7.5. Examples of the application of GLMs 7.5.1. GLM for proportion or binary data under binomial distribution Researchers are studying the influence of insecticide (carbon disulfide) dose and sex on cockroach mortality. For five different doses, they classify ldoses of the poison and the total number of dead cockroaches at the end of the experiment out of 20 initial cockroaches. The experiment is done on both males and females. The underlying questions are: Does mortality depend on the dose of the poison? Is it different between the sexes? Does this mortality respond differently between males and females? The data are available as follows: > ldose = rep(0:5, 2) >mort = c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16) >sex = factor(rep(c("M", "F"), c(6, 6)))

Classical and Generalized Linear Models

231

>SF glm par(mfrow=c(2,2)); plot(glm)

232

Statistics in Environmental Sciences

Figure 7.36. Graphs given by R to verify the application conditions of cockroach mortality according to insecticide dose and sex, with a GLM under binomial distribution. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Here, the residuals are relatively centered, no particular shape emerges from the residuals according to the predicted values and normality is well respected (Figure 7.36). Indeed, no aberrant values appear on Cook’s distances. The application conditions are respected: > summary(glm) Call: glm(formula = SF ~ sex * ldose, family = binomial(link = logit)) Deviance Residuals: Min 1Q Median 3Q Max -1.39849 -0.32094 -0.07592 0.38220 1.10375 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -2.9935 0.5527 -5.416 6.09e-08 *** sexM 0.1750 0.7783 0.225 0.822 ldose 0.9060 0.1671 5.422 5.89e-08 ***

Classical and Generalized Linear Models

233

sexM:ldose 0.3529 0.2700 1.307 0.191 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 124.8756 on 11 degrees of freedom Residual deviance: 4.9937 on 8 degrees of freedom AIC: 43.104 Number of Fisher Scoring iterations: 4

The summary() function provides the deviance data from which it is possible to calculate the equivalent of R2 from the deviance and the deviance of the “saturated” model (see section 7.1.2): > R2 anova(glm,test="Chisq") Analysis of Deviance Table Model: binomial, link: logit Response: SF Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev Pr(>Chi) NULL 11 124.876 sex 1 6.077 10 118.799 0.0137 * ldose 1 112.042 9 6.757 null.m step_aic aic R2 anova(aic,test="Chisq") Analysis of Deviance Table Model: binomial, link: logit Response: SF Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev Pr(>Chi) NULL 11 124.876 ldose 1 107.892 10 16.984 < 2.2e-16 ***

Classical and Generalized Linear Models

235

sex 1 10.227 9 6.757 0.001384 ** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Cockroach mortality depends on sex (P-value < 0.01) and insecticide dose (P-value < 0.001): > summary(aic) Call: glm(formula = step_aic$formula, family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -1.10540 -0.65343 -0.02225 0.48471 1.42944 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -3.4732 0.4685 -7.413 1.23e-13 *** ldose 1.0642 0.1311 8.119 4.70e-16 *** sexM 1.1007 0.3558 3.093 0.00198 ** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

The summary() function provides the coefficients to write the final equation like in linear models. Be careful, this equation must consider that the calculations were performed under binomial distribution with the logit as the link (see Table 7.1, section 7.1.2): Mortality = exp(1.064×ldose +1.101MSexM–3.473)/(1+exp(1.064× ldose +1.101MSexM–3.473)) The graphical representation of the raw male (M) and female (F) data in text, as well as the models obtained in continuous curves, is detailed here (Figure 7.37). Note that the probability or probabilities predicted from the equation obtained are done with the predict() function by specifying the model (here, aic), a data.frame with the X values in columns and the argument type=“response”: > plot(c(1,32), c(0,1), type = "n", xlab = "Insecticide dose",ylab = "Proportion of dead chrockcheases", log = "x") # Graphical representation without anything > text(2^ldose, numdead/20, as.character(sex)) # Projection of raw data in text > ld lines(2^ld, predict(aic, data.frame(ldose = ld,sex = factor(rep("M", length(ld)), levels = levels(sex))), type = "response")) # Projection of the model obtained for males from the ld sequence > lines(2^ld, predict(aic, data.frame(ldose = ld,sex = factor(rep("F", length(ld)), levels = levels(sex))),type = "response"),lty=2) # Projection of the model obtained for females from the ld sequence > legend("bottomright",c("F","M"),lwd=1,lty=c(2,1))

Figure 7.37. Graphical representation of raw male (M) and female (F) data in text, as well as models obtained in continuous curves

Here, there is no need for post hoc tests because the qualitative factor, “sex”, has only one condition and a significant effect of sex therefore automatically means that males are different from females. 7.5.2. GLM for discrete data under Poisson distribution The Environmental Monitoring and Assessment Program (EMAP) was conducted in order to address ecological status issues, particularly aquatic

Classical and Generalized Linear Models

237

resources in the northeastern United States. Within this framework, several hundred natural and artificial lakes have been selected from more than 10,000 lakes listed by the US Geological Survey (USGS). This study examines bird populations in 208 lakes selected to represent the diversity of regional conditions (Figure 7.38).

Figure 7.38. Location of the 208 lakes sampled for the study in the northeastern United States. The codes and location of the states concerned are reported. Representation of the importance of specific bird wealth by bubble chart according to the geographical location of the sampled lakes

In addition to the wildlife inventory in the presence/absence of the 95 species recorded at each station, the database also contains information on the environment with “geographical” variables (latitude, longitude, altitude and distance to the sea) and more local environmental variables, some of which reflect the diversity of local anthropogenic impacts on these environments (land use in the watershed, human population density, etc.; Table 7.3). All these variables are quantitative.

238

Statistics in Environmental Sciences

The objective is to study the extent to which certain environmental variables can explain the specific wealth of birds. The data are accessible in R via: >load(url("http://pbil.univ-lyon1.fr/R/donnees/pps056.rda")) >pps056$avifaune->BIO # Data in presence/absence of bird species listed in column >dim(BIO) [1] 208 95 The available data show 208 lakes in rows and 95 species in columns. The specnumber() function of the ‘vegan’ library allows you to calculate the specific wealth of these 208 lakes.

> library(vegan)

RS=specnumber(BIO)

As for the environmental data, they are available in the following subfiles and stored in the SITE and ENV objects. The codes are shown in Table 7.3: > pps056$mil->ENV;names(ENV) [1] "AG_TOT" "BAR_TOT" "ELEV" "FOR_TOT" "HOUDENKM" [6] "KM_SEA" "MINE_TOT" "POPDENKM" "PRECIP_M" "RD_DEN" [11] "URB_TOT" "WETL_TOT" > pps056$site->SITE; names(SITE) [1] "STATE" "LON_DD" "LAT_DD"

Code LON_DD LAT_DD ELEV KM_SEA AG_TOT MINE_TOT URB_TOT BAR_TOT FOR_TOT WETL_TOT

Associated environmental variable SITES database Longitude of the lake Latitude of the lake ENV database Altitude of the lake Distance to the sea from the lake Percentage of the watershed used for agriculture Percentage of the watershed used for the mining industry Percentage of the watershed that has been urbanized Percentage of the watershed occupied by the tundra Percentage of the watershed occupied by the forest Percentage of the watershed occupied by wetlands

Classical and Generalized Linear Models

HOUDENKM POPENKM PRECIP_M logRD_DEN

239

Density of homes in the watershed (houses/km2) Density of population in the watershed (inhabitants/km2) Mean annual precipitation Log(Density of the road network in the watershed)

Table 7.3. Variables and associated codes for environmental variables

Researchers begin by preselecting 10 variables that they believe are scientifically sound in order to explain specific wealth. They are (1) longitude, (2) latitude, (3) the altitude of the lake, (4) housing density in the watershed, (5) distance to the sea, (6) percentage of the watershed used for agriculture, (7) percentage of the watershed used for mining, (8) population density in the watershed, (9) percentage of the watershed that has been urbanized, and (10) percentage of the watershed used by wetlands. These are all quantitative variables. The researchers therefore seek to explain the specific bird wealth according to these 10 quantitative variables. This is equivalent to a multiple regression for a linear model, but with a discrete variable to be explained, so the researchers choose to apply a GLM based on the Poisson distribution. The researchers start by optimizing the number and type of variables to be retained in the optimization model by the AIC criterion, using the stepAIC() function as indicated in section 7.5.1, but this time using the Poisson distribution and the log link function. The interaction is not considered here because this is regression. The best model (lowest AIC) is stored in the aic object: > library(MASS) > null.m step_aic aic anova(aic,test="Chisq") Analysis of Deviance Table Model: poisson, link: log Response: RS Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev Pr(>Chi) NULL 207 859.98 SITE$LAT_DD 1 27.4323 206 832.55 1.627e-07 *** ENV$ELEV 1 23.5043 205 809.04 1.246e-06 *** ENV$URB_TOT 1 22.6167 204 786.43 1.978e-06 *** SITE$LON_DD 1 12.8470 203 773.58 0.000338 *** ENV$AG_TOT 1 16.4349 202 757.14 5.035e-05 *** ENV$MINE_TOT 1 10.5110 201 746.63 0.001187 ** ENV$WETL_TOT 1 3.1331 200 743.50 0.076716. ENV$KM_SEA 1 2.9108 199 740.59 0.087986. –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Classical and Generalized Linear Models

241

Here, eight variables are retained, two of which are not significant: > R2 glm x11();par(mfrow=c(2,2));plot(glm)

Here, the residuals are more or less centered, no particular shape emerges from the residuals according to the predicted values and normality is well respected. No particular value is outside Cook’s distance curve (Figure 7.39). The conditions are met.

Figure 7.39. Graphs given by R to verify the conditions for applying the specific wealth of birds according to six environmental variables retained by a GLM under Poisson distribution. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

242

Statistics in Environmental Sciences

Thus: > R2 anova(glm,test="Chisq") Analysis of Deviance Table Model: poisson, link: log Response: RS Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev Pr(>Chi) NULL 207 859.98 SITE$LON_DD 1 13.986 206 845.99 0.0001842 *** SITE$LAT_DD 1 13.556 205 832.44 0.0002316 *** ENV$ELEV 1 38.739 204 793.70 4.844e-10 *** ENV$MINE_TOT 1 11.943 203 781.76 0.0005486 *** ENV$AG_TOT 1 9.081 202 772.67 0.0025831 ** ENV$URB_TOT 1 26.042 201 746.63 3.341e-07 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

The chi-squared tests reveal that six variables are substantial in explaining the deviance of the specific wealth: > summary(glm) Call: glm(formula = RS ~ SITE$LON_DD + SITE$LAT_DD + ENV$ELEV + ENV$MINE_TOT + ENV$AG_TOT + ENV$URB_TOT, family = poisson(link = log)) Deviance Residuals: Min 1Q Median 3Q Max -5.6003 -1.3516 0.1215 1.1837 6.0296 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -4.0961064 1.2603030 -3.250 0.00115 ** SITE$LON_DD -0.0545718 0.0103947 -5.250 1.52e-07 *** SITE$LAT_DD 0.0897970 0.0137878 6.513 7.38e-11 *** ENV$ELEV -0.0009523 0.0001178 -8.083 6.31e-16 *** ENV$MINE_TOT -0.0140739 0.0046483 -3.028 0.00246 ** ENV$AG_TOT -0.0034575 0.0009298 -3.719 0.00020 *** ENV$URB_TOT -0.0052354 0.0010672 -4.906 9.31e-07 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Classical and Generalized Linear Models

243

(Dispersion parameter for poisson family taken to be 1) Null deviance: 859.98 on 207 degrees of freedom Residual deviance: 746.63 on 201 degrees of freedom AIC: 1847.5 Number of Fisher Scoring iterations: 4

Considering that the model was implemented under the Poisson distribution with a log link, the equation of the model can therefore be written as (see Table 7.2, section 7.1.2): RS = exp(–4.09 – 0.05 longitude + 0.08 latitude – 0.001 altitude – 0.014 mining industry – 0.005 agriculture – 0.005 urbanization) The specific wealth depends on both geographical variables (longitude, latitude and altitude) and anthropogenic impacts (agriculture, mining and urbanization), the latter having a negative effect on the variable considered. 7.5.3. GLM for true positive data under gamma distribution Millsonia and Chuniodrilus worms are recurrently found in rice crops. Before taking measures to eradicate these worms that are claimed to be harmful to rice production, researchers want to show whether or not worms have an impact on rice growth. They set up an experimental plan containing structures with and without worms and measure the bmaer aerial biomass, the bmrac root biomass and the total bmtot biomass of the plant, particularly for increasing nitrogen concentrations. They seek to explain to what extent the presence/absence of one or both worms and nitrogen concentrations explain variations in the total biomass of rice. The data are available in the plantevers.txt file and the potentially explanatory variables, Millsonia, Chuniodrilus and nitrogen concentrations, are transformed into qualitative variables, in other words, factors: > vers vers$nitrogen vers$millsonia vers$chuniodrilus str(vers) ‘data.frame’: 60 obs. of 6 variables: $ millsonia : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ... $ chuniodrilus: Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...

244

$ 3 $ $ $

Statistics in Environmental Sciences

nitrogen 3 3 4 bmaer bmrac bmtot

: Factor w/ 5 levels "0","25","100",..: 1 1 1 2 2 2 : num 4.6 5.4 3.7 4.3 3.2 4.9 3.4 3.8 4.8 5.7... : num 1.5 1.8 1.3 1.9 0.8 1.6 0.9 0.9 1.7 2... : num 6.1 7.2 5 6.2 4 6.5 4.3 4.7 6.5 7.7...

The database includes 60 observations, including three factors to be used as explanatory variables and the variable to be explained: the total biomass of bmtot rice. The latter corresponds to non-zero real values. The GLM that will be used on this cross and balanced design will therefore be a gamma GLM with log as its binding function. Be warned, the link that is used for the gamma distribution is not one chosen by default, so it must be specified! The researchers start by optimizing the number and type of variables to be retained in the optimization model using the AIC criterion, as described in section 7.5.1, but this time using the gamma distribution and the log link function: > library(MASS) > null.m step_aic aic x11();par(mfrow=c(2,2));plot(aic)

Here, the residuals are more or less centered, no particular shape emerges from the residuals according to the predicted values and normality is well respected. The conditions are respected and no value leaves Cook’s curve graph (Figure 7.40). The variance table analysis is carried out under the Fisher–Snedecor distribution.

Figure 7.40. Graphs given by R to verify the application conditions of the total biomass in rice according to the effect of the two presence/absence factors of Millsonia and nitrogen concentrations by a GLM under gamma distribution, with the log link function. For a color version of this figure, see www.iste.co.uk/david/ statistics.zip

Thus: > anova(aic,test="F") Analysis of Deviance Table Model: Gamma, link: log

246

Statistics in Environmental Sciences

Response: bmtot Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev F Pr(>F) NULL 59 5.7089 nitrogen 4 1.76957 55 3.9394 8.0936 3.454e-05 *** millsonia 1 0.58151 54 3.3579 10.6388 0.001922 ** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 > R2 summary(aic) Call: glm(formula = step_aic$formula, family = Gamma(link = log), data = vers) Deviance Residuals: Min 1Q Median 3Q Max -0.61673 -0.19859 0.04412 0.13759 0.30795 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.84761 0.07393 24.991 < 2e-16 *** nitrogen25 -0.06384 0.09545 -0.669 0.506421 nitrogen100 -0.05386 0.09545 -0.564 0.574859 nitrogen400 0.21076 0.09545 2.208 0.031501 * nitrogen1600 0.37672 0.09545 3.947 0.000231 *** millsoniaTRUE 0.19745 0.06037 3.271 0.001871 ** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 (Dispersion parameter for Gamma family taken to be 0.05465956) Null deviance: 5.7089 on 59 degrees of freedom Residual deviance: 3.3579 on 54 degrees of freedom AIC: 253.33 Number of Fisher Scoring iterations: 4

Considering that the model was implemented under gamma distribution with the log link, the equation of the GLM model is written as (see Table 7.2, section 7.1.2): 1/Biomass = 1.85 – 0.06 × 1/MN25 – 0.05 × 1/MN100 + 0.21 × 1/MN400 + 0.38 × 1/MN1600 + 0.20 × 1/Mmilsoniapresent

Classical and Generalized Linear Models

247

As simple as it is to graphically determine the differences for Millsonia (given that the variable only has only two conditions, as much as for nitrogen concentrations), a Tukey test can be applied for a balanced plan. The summary function is used on top of the glht() function in the Tukey test by specifying the following method: > summary(glht(aic,linfct = mcp(nitrogen = "Tukey"))) Simultaneous Tests for General Linear Hypotheses Multiple Comparisons of Means: Tukey Contrasts Fit: glm(formula = step_aic$formula, family = Gamma(link = log), data = vers) Linear Hypotheses: Estimate Std. Error z value Pr(>|z|) 25 - 0 == 0 -0.063842 0.095446 -0.669 0.9631 100 - 0 == 0 -0.053864 0.095446 -0.564 0.9802 400 - 0 == 0 0.210757 0.095446 2.208 0.1766 1600 - 0 == 0 0.376718 0.095446 3.947 contr summary(glht(aic,linfct=mcp(nitrogen = contr)),test = adjusted (type = "free"))

248

Statistics in Environmental Sciences

Simultaneous Tests for General Linear Hypotheses Multiple Comparisons of Means: User-defined Contrasts Fit: glm(formula = bmtot ~ millsonia + nitrogen, family = Gamma(link = log), data = vers) Linear Hypotheses: Estimate Std. Error z value Pr(>|z|) 25 -0 == 0 -0.06384 0.09545 -0.669 0.727174 100 -0 == 0 -0.05386 0.09545 -0.564 0.727174 400 -0 == 0 0.21076 0.09545 2.208 0.071159. 1600-0 == 0 0.37672 0.09545 3.947 0.000298 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 (Adjusted p values reported – free method)

In this case, only the highest nitrogen concentration is significantly different to the evidence without nitrogen concentration. The graphical representation is obtained using the following: >par(mfrow=c(1,2)); > boxplot(log(bmtot)~millsonia, vers, xlab="Presence/absence of Millsonia", ylab="ln(total rice in biomass") > boxplot(log(bmtot)~azote, vers, xlab="Nitrogen concentrations",ylab="ln(total rice in biomass")

The presence of the Millsonia worm seems to increase rice growth, as well as nitrogen concentrations (Figure 7.41). It is therefore not necessary to eradicate these two species.

Figure 7.41. Effect of the presence/absence of the Millsonia worm and nitrogen concentrations on rice biomasses; superimposed letters determine the differences between factor conditions

Classical and Generalized Linear Models

249

7.5.4. GLM for true data under Gaussian law Let us take another look at the example of the study on the oat production experiment (see section 7.4.2.2) that examines the effect of three varieties of oats (Victory, Marvellous and Golden Rain) and four nitrogen concentrations in fertilizers (0cwt, 0.2cwt, 0.4cwt and 0.6cwt). For each variety, four concentrations are tested. The previous two-factor fixed cross-design and balanced ANOVA had shown that there was only a significant effect of nitrogen concentrations on production (see section 7.4.2.2). Let us now apply a Gaussian glm with the identity link: > glm anova(glm,test="F") Analysis of Deviance Table Model: gaussian, link: identity Response: Y Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev F Pr(>F) NULL 71 51986 V 2 1786.4 69 50200 1.7949 0.1750 N 3 20020.5 66 30179 13.4108 8.367e-07 *** V:N 6 321.8 60 29857 0.1078 0.9952 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Once again, the effect of significant nitrogen concentrations alone can be observed here (P-value < 0.001). The GLM is therefore restored with this effect alone and can be compared to the one-factor ANOVA performed in section 6.2.3: > glm x11();par(mfrow=c(2,2));plot(glm)

The residuals are centered, no particular shape emerges from the residuals according to the predicted values and normality is well respected. The application conditions are met (Figure 7.42).

250

Statistics in Environmental Sciences

Thus: > anova(glm,test="F") Analysis of Deviance Table Model: gaussian, link: identity Response: Y Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev F Pr(>F) NULL 71 51986 N 3 20021 68 31965 14.197 2.782e-07 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 > R2|t|) (Intercept) 79.389 5.110 15.535 < 2e-16 *** N0.2cwt 19.500 7.227 2.698 0.00879 ** N0.4cwt 34.833 7.227 4.820 8.41e-06 *** N0.6cwt 44.000 7.227 6.088 5.96e-08 *** –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 (Dispersion parameter for gaussian family taken to be 470.0801) Null deviance: 51986 on 71 degrees of freedom Residual deviance: 31965 on 68 degrees of freedom AIC: 653.22 Number of Fisher Scoring iterations: 2

The same results in terms of R2 and the equation were found for the one-factor ANOVA (section 6.2.1). The equation is written as: Production = 79.4+19.5M0.2cwt +34.8M0.4cwt +44M0.6cwt

Classical and Generalized Linear Models

251

Figure 7.42. Graphs given by R to verify the application conditions of oat production according to nitrogen concentrations by a GLM under a Gaussian distribution, with identity as the linking function. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Indeed, a Gaussian GLM and a linear model rapidly converge toward the same solution. On a balanced design, the Tukey post hoc test can be used: > summary(glht(glm,linfct = mcp(N = "Tukey"))) Simultaneous Tests for General Linear Hypotheses Multiple Comparisons of Means: Tukey Contrasts Fit: glm(formula = Y ~ N, family = gaussian(link = identity), data = oats) Linear Hypotheses: Estimate Std. Error z value Pr(>|z|) 0.2cwt - 0.0cwt == 0 19.500 7.227 2.698 0.03481 * 0.4cwt - 0.0cwt == 0 34.833 7.227 4.820 < 0.001 *** 0.6cwt - 0.0cwt == 0 44.000 7.227 6.088 < 0.001 *** 0.4cwt - 0.2cwt == 0 15.333 7.227 2.122 0.14646 0.6cwt - 0.2cwt == 0 24.500 7.227 3.390 0.00367 ** 0.6cwt - 0.4cwt == 0 9.167 7.227 1.268 0.58315 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 (Adjusted p values reported – single-step method)

8 Non-parametric Alternatives to Linear Models

8.1. Principle of non-parametric tests Not all situations are simplified by a linear model. First and foremost, a situation can be: – monotonous (increasing or decreasing), but nonlinear: for example, the exponential relationship between the length and weight of cod in the North Atlantic (section 6.3.2); – non-monotonous: let us take the example of Bidonia according to humidity. This is a nonlinear parabolic type of regression. In the case of a non-monotonous relationship, nonlinear models should be used. When the objective is to explain a quantitative variable but the observations do not follow a normal distribution or any other known distribution law – parametric tests – there is an entire array of statistical tests that exist in order to overcome this problem. It is rare to not be able to find a non-parametric equivalent of a parametric test. Some do not rely on any preconditions: “non-parametric” tests based on ranks (see section 8.2). Some have already been discussed in Chapter 5 (e.g. section 6.2.2, the Wilcoxon–Mann–Whitney test to compare two samples, or section 6.3.2, the Spearman and Kendall correlation test). Some are based on association matrices and the application of permutation tests. These tests do have application conditions, but are less

Statistics in Environmental Sciences, First Edition. Valérie David. © ISTE Ltd 2019. Published by ISTE Ltd and John Wiley & Sons, Inc.

254

Statistics in Environmental Sciences

drastic than their parametric equivalents. They are generally more powerful than rank-based tests (see section 8.3). In this chapter, only alternatives to analysis of variance (ANOVA) or analysis of covariance will be discussed, the non-parametric alternatives to regression having already been examined in section 6.3.2 (Spearman and Kendall correlation coefficients). 8.2. ANOVA alternatives 8.2.1. Rank-based ANOVAs The general principle of these tests is to consider the ranks of the numerical values of variables assigned as a result of ranking these values in ascending order, rather than the numerical values themselves. This principle makes it possible to avoid application conditions such as the “normality” of distributions or the homogeneity of variances that are essential for the viability of parametric tests. However, it should be noted that most of the statistics for these rank-based tests follow known distribution laws, at least for large samples. Correction tables are proposed for small samples. The first remarkable feature of these tests is that no preconditions are required, as mentioned above: this is the case, for example, with the Wilcoxon–Mann–Whitney test as opposed to the Student’s t-test to compare the mean of two samples (see section 6.2.2). These are called robust tests: the parametric alternatives are sensitive and biased by non-compliance with the application conditions. In contrast, it would be more difficult to reject H0 due to the degradation of numerical values in ranks than for the parametric alternative of the same test. Thus, the non-parametric alternative gives less of a chance of accepting the alternative hypothesis if it is true: non-parametric tests are less powerful than parametric tests. 8.2.2. Non-parametric alternative to a one-factor ANOVA: the Kruskal–Wallis test and associated post hoc The non-parametric alternative to the one-factor ANOVA is the Kruskal– Wallis test. The objective is to test the effect of a controlled factor on a

Non-parametric Alternatives to Linear Models

255

continuous random variable, in other words, to compare several samples corresponding to the different conditions of the factor. The working hypotheses are: – H0: distributions between conditions are identical => no factor effect; – H1: at least one of the distributions is different => there is a factor effect. As an example, let us take the effect of three types of fertilizer on cereal productivity: the qualitative factor therefore has three conditions A, B and C (Figure 8.1). A rank must be assigned to the n values of the results for all conditions combined, the ranks for each condition must be summed up and the H statistic must be calculated. This statistic follows the chi-squared probability law with the following degree of freedom: ddl = number of conditions – 1.

Figure 8.1. Principle of the Kruskal–Wallis test regarding the relationship between cereal production and fertilizer types and calculated H statistics. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

8.2.2.1. Application in R The data set consists of a data.frame with two columns, the quantitative variable to explain Production and the explanatory qualitative variable Fertilizer as a factor, stored in PROD. The Kruskal–Wallis test is performed

256

Statistics in Environmental Sciences

with the kruskal.test() function, specifying the variable to be explained and then the explanatory variable: >PROD kruskal.test(PROD$Production, as.factor(PROD$Fertilizers)) Kruskal-Wallis rank sum test data: PROD$Production and as.factor(PROD$Fertilizers) Kruskal-Wallis chi-squared = 6.4132, df = 2, p-value = 0.04049

The P-value is lower than the risk α = 0.05, H0 is rejected: a relationship exists between production and fertilizer concentrations. However, this means that at least one of the distributions is different from the others. To highlight this, a post hoc test can be used. Dunn’s post hoc is powerful and supports unbalanced designs. It is available using the dunnTest() function of the FSA library: > library(FSA) > dunnTest(Production ~ as.factor(Fertilizers),data=PROD, method="bh") Dunn (1964) Kruskal-Wallis multiple comparison p-values adjusted with the Benjamini-Hochberg method. Comparison Z P.unadj P.adj 1 A - B 1.266219 0.20543454 0.20543454 2 A - C -1.266219 0.20543454 0.30815182 3 B - C -2.532439 0.01132721 0.03398164

The matrix returns the P-value of the two to two comparisons, and a significant difference in production between fertilizers B and C can be noticed. For a graphical representation with four conditions per replica, a bar graph is favored with the mybarplot() function, as detailed in section 1.3.4. The superimposed letters on the graph highlight the differences between fertilizers (Figure 8.2): > mean se x11();my.barplot(mean,se,xlab="Fertilizers types",ylab="Production", ylim=c(0,max(mean+se)))# CL’s maximum number in ylim corresponds to the mean + the standard error

Non-parametric Alternatives to Linear Models

257

The production of fertilizer C significantly ensures the best production (Figure 8.2).

Figure 8.2. Production according to fertilizer types; superimposed letters correspond to the level of significance identified by Dunn’s post hoc test

8.2.3. Non-parametric alternative to a two-factor ANOVA: Scheirer–Ray–Hare test Let us take the example of cockroach mortality by sex and insecticide dose (section 7.5.1): > ldose = as.factor(rep(0:5, 2)); > sex = factor(rep(c("M", "F"), c(6, 6))) > mort = c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)/20 > data str(data) ‘data.frame’: 12 obs. of 3 variables: $ sex : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 1 1 1 1 ...

258

Statistics in Environmental Sciences

$ ldose: Factor w/ 6 levels "0","1","2","3",..: 1 2 3 4 5 6 1 2 3 4 ... $ mort : num 0.05 0.2 0.45 0.65 0.9 1 0 0.1 0.3 0.5...

The data table contains the quantitative variable to be explained, the mortality in proportion from 0 to 1 and the two qualitative variables: sex and insecticide dose. The Sheyrer–Ray–Hare test is available via the scheirerRayHare() function of the rcompanion library. It is defined as a classic linear model: the variable to be explained, tilde, the explanatory variables separated by a + and then the database. Unlike linear models, it gives the result of the interaction with the +, but also works with *. It should be noted that, for this example, the effect of the interaction cannot be studied because there is only one data per cross condition: > table(sex,ldose) ldose sex 0 1 2 3 4 5 F 1 1 1 1 1 1 M 1 1 1 1 1 1

It should therefore not be taken into account: >library(rcompanion) > scheirerRayHare(mort ~ sex + ldose, DV: mort Observations: 12 D: 1 MS total: 13

data = data)

Df Sum Sq H p.value sex 1 8.333 0.6410 0.42334 ldose 5 133.000 10.2308 0.06895 sex:ldose 5 1.667 0.1282 0.99970 Residuals 0 0.000

Here, there are no significant variables (P-value > 0.05), even if the dose effect is close to that significance (P-value = 0.06). Yet, for the same data set, the GLM (section 7.5.1) showed an insecticide dose effect. The nonparametric test does not detect it because it is less powerful. However, for a significant effect, Dunn’s post hoc test is recommended (section 8.2.1). For a graphical representation, given the small amount of data, a bar graph representation is recommended (section 7.5.1).

Non-parametric Alternatives to Linear Models

259

8.2.4. Non-parametric alternative to a matching ANOVA: the Friedman test The Friedman test is a non-parametric test to be used when k samples are matched by time, block effect or other mixed designs, in order to highlight a difference between treatments. This is the non-parametric alternative to the ANOVA repeated measures (section 7.4.4.4). The working hypotheses are – if Mi is the position parameter of sample i: – H0: M1 = M2 = … = Mk; – Ha: there is at least one couple (i, j) such as Mi ≠ Mj. If n is the size of the matched k samples, the Q statistic of the Friedman test is: Q = 12/(nk(k + 1)) Σi=1.. k [Ri² – 3 n(k + 1)] where Ri is the sum of the ranks for sample i. This statistic follows a chi-squared distribution law at k-1 degrees of freedom for large samples (n = 30). For small samples, correction tables are available. For the Friedman test, the most widely used multiple comparison method is the Nemenyi post hoc (1963). This method is similar to Dunn’s post hoc method, but takes data matching into account. Let us look at the example of an insecticide-treated apple field with five different increasing doses (A, B, C, D and E) and a follow-up of the number of insects caught per hectare every week for 1 month. Here, we have a variable to explain the number of insects caught according to the insecticide dose with a matching factor: time. The database is as follows, with the quantitative variable to be explained, insectes, and two factors, one of which we want to see the effect of, traitement, and the other which induces a matching, temps: > insectes str(insectes) num [1:20] 527 633 642 623 377 604 600 708 550 408...

260

Statistics in Environmental Sciences

> traitement str(traitement) Factor w/ 5 levels "A","B","C","D",.. : 1 2 3 4 5 1 2 3 4 5... >temps str(temps) Factor w/ 4 levels "T1","T2","T3",.. : 1 1 1 1 1 2 2 2 2 2...

It should be noted that, in this example, there is only one situation according to the crossed conditions of the traitement factor and the temps factor inducing matching: > table(traitement,temps) temps traitement T1 T2 T3 T4 A 1 1 1 1 B 1 1 1 1 C 1 1 1 1 D 1 1 1 1 E 1 1 1 1

The Friedman test is performed in R with the friedman.test()function by specifying the variable to be explained, then the variable whose effect will be analyzed and lastly, the variable inducing the matching: > friedman.test(insectes,traitement,temps) Friedman rank sum test data: pomme, traitement and temps Friedman chi-squared = 9.8, df = 4, p-value = 0.04393

The P-value is lower than the risk α = 0.05, H0 is rejected, therefore at least one treatment is different from the others. To apply the Nemenyi post hoc test, the frdAllPairsNemenyiTest() function of the PMCMRplus library can be used. It is defined like the previous Friedman test: > library(PMCMRplus) > frdAllPairsNemenyiTest(insectes,traitement,temps) Pairwise comparisons using Nemenyi-Wilcoxon-Wilcox all-pairs test for a two-way balanced complete block design data: pomme, traitement and temps A B C D B 0.96 C 0.80 0.99 -

Non-parametric Alternatives to Linear Models

261

D 1.00 0.96 0.80 E 0.38 0.10 0.03 0.38 P value adjustment method: single-step

This reports the P-values for all treatments taken 2 by 2. Here, only treatment D is different from E. For a graphical representation (Figure 8.3): >xyplot (pomme∼traitement ,groups = temps ,pch=c(16,16,16), cex=1.5 ,col=c("black","blue","green","red"),xlab="Increasing dose of insecticide",ylab="Number of insects caugth per hectare")

Figure 8.3. Number of insects caught per hectare according to an increasing rate of insecticide treatment of an apple field: colors show the different weeks of data collection from 1 to 4. The superimposed letters report the results of the differences between treatments from the Nemenyi post hoc test. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

Thus, the highest dose is the only effective one. Variability between treatment weeks prevents a difference for the first three doses (Figure 8.3). It seems that after 4 weeks of treatment (red dots), it starts to take effect for doses B and C: the number of insects is notably lower.

262

Statistics in Environmental Sciences

8.3. Non-parametric ANOVAs based on permutation tests (PERMANOVA) 8.3.1. Principle It is used to test the response of one (or more) variable(s) to one or more qualitative factors (Anderson 2001). It is a non-parametric alternative to the ANOVA, in the sense that it is not based on the assumption that the data follow a known distribution law like comparable conventional or generalized linear ANOVAs (Chapter 7). In this regard, it is more resistant to the normality and homogeneity of residual or observation variances (Chapman 2001). It is also flexible because it is based on association matrices and the association coefficient can be chosen appropriately between objectives and data. However, this does not necessarily imply that there are no application conditions. For PERMANOVAs, observations should be independent and the homogeneity of dispersions between groups must be defined by the conditions of the qualitative factor(s). It is applicable to a wide range of complex experimental or complex sampling designs, as well as variance analyses. But, unlike other nonparametric approaches, it allows the effect of interactions between factors to be tested and the variance partition to be approached. The null hypothesis is that there is no difference in the position and/or dispersion of the groups that are defined by the conditions of qualitative factors in a multidimensional space. Its principle consists of constructing an association matrix between the elements, in other words, a matrix showing all the distances between each pair of elements. The rest of the test is very similar to an ANOVA process, given that the sum of squares of the distances between and within groups is calculated, the mean squares are obtained from the degrees of freedom and then the test statistics are calculated. It resembles the F of an ANOVA and is called Pseudo-F: Pseudo-F = CMinter/CMintra However, it is not the Fisher–Snedecor probability law that is used, but a distribution that is obtained by permutation: the data are randomly

Non-parametric Alternatives to Linear Models

263

redistributed a large number of times and then the F is calculated for each of them (that is to say, 9999.99). The distribution of the data from these random redistributions corresponds to the distribution under the null hypothesis. If the statistics obtained by the samples are outside the 95% range of this distribution, H0 is rejected and the alternative hypothesis is accepted (Figure 8.4). This alternative hypothesis shows that at least one of the groups is different from the others. Beyond two conditions, it will therefore be necessary to use the equivalent of a post hoc test in order to determine where these differences lie.

Figure 8.4. Principle of a PERMANOVA. For a color version of this figure, see www.iste.co.uk/david/statistics.zip

In addition, the association coefficient for constructing the association matrix depends on the objective and data used, especially if the data to be explained are binary or quantitative, and whether or not the double zero is important.

264

Statistics in Environmental Sciences

When assessing whether or not two elements are associated, it may happen that the values are zero for both elements. For physico-chemical parameters, zero has a real value on the descriptor scale. For example, a temperature of 0°C makes as much sense as 20°C or –12°C. Both objects having a double 0 therefore adds a similarity between the two objects, based on this parameter. It must therefore be considered when estimating the similarity between objects. When it is a variable resulting from plant or animal surveys, the zero induces an absence of the species in question. While the copresence of a species may indicate similar conditions in terms of ecological niches and the presence/absence may oppose two niches, the double absence (double 0) may be due to different reasons (rare species not collected, sampling outside the ecological niche for both species, etc.). In the latter case, this double zero is therefore not traditionally considered when estimating the similarity between two elements, particularly in the spirit of biological community characterization. However, this coabsence of species may be crucial for certain objectives, such as the search for indicators, for example. This question of whether or not the double zero is considered in the analyses is a key issue in the PERMANOVA. Here, we have a simplified key to choose the association coefficient and the function to be used in R. It is, of course, not exhaustive: • ‘double 0’ to mesure the similarities -> binary data : Simple concordance of dissimilarities* >library(ade4) ; dist.binary(y, method=2) -> Quantitative data : Euclidean distances >dist(y,method=”Euclidean”) • ‘double 0’ not considered to measure the similarity -> Binary data : Jaccard coefficients >library(ade4) ; dist.binary(y, method=1) -> Quantitative data -> importance of the profile : Chi2 distances >library(vegan) ; dist(decostand(y,”chi.square”),method=”Euclidean) -> Importance of the profile and the abundances : Bray-Curtis dissimilarities >library(vegan) ; vegdist(y,method=”bray”)

8.3.2. Example of application in R Zooplankton are a group of microscopic animal species present in the aquatic environment and an important food resource for fish. In order to determine the large zooplankton communities present at the estuary scale

Non-parametric Alternatives to Linear Models

265

and their spatial-seasonal distribution, researchers sampled these organisms monthly over a full annual cycle (temporal variation) and at five stations corresponding to fixed salinity levels (spatial variation) in the estuary. The underlying question of the study is: does the specific wealth of zooplankton depend on salinity and/or seasons? The database is available in the charente.csv file: > str(data) ‘data.frame’: 58 obs. of 48 variables: $ Code : Factor w/ 58 levels "m1_0.5S","m1_0S",..: 2 1 5 3 4 21 20 24 22 23 ... $ Season : Factor w/ 4 levels "Fall","Summer ",..: 3 3 3 3 3 3 3 3 3 3 ... $ Salinity : num 0 0.5 5 15 25 0 0.5 5 15 25 ... $ Temperature : num 8 8 8 8 8 11 11 11 11 11 ... $ Turbidity : num 0.0155 0.6077 0.4077 0.2184 0.1085 ... $ Chla : num 0.473 2.744 1.704 1.298 1.133 ... $ ACARspp : num 0 0 0 0 0 ... $ ALONspp : num 4.188 7.654 0.406 0 0... $ AMPHspp : num 0 0.0459 0 0 0... $ ANNEspp.larve.: num 5.03 0 0 0 0... $ APHImin : num 0 0 0 0 0 0 0 0 0 0... $ BIVAspp.larve.: num 0.471 0 0.165 0.587 1942.487... …

The file includes 48 columns for 58 rows with a Season column, identified as a qualitative factor, and a salinity column, identified as a numerical variable. We will first isolate these two explanatory variables by considering salinity as a qualitative factor: >SeasonSalBIO library(vegan)

266

Statistics in Environmental Sciences

> specnumber(BIO)->RS; RS [1] 9 6 9 10 17 12 9 11 15 15 8 7 11 14 14 4 9 9 12 [29] 8 14 9 11 9 17 15 6 10 10 10 15 10 8 10 11 12 10 7 7 9

9 16 8

9

6

3

5

6 10 14

5 17 11 9

5

9 10 13

8.3.2.1. Determining the PERMANOVA plan The PERMANOVA to be carried out here consists of analyzing the effect of the salinity, the season and the salinity-season interaction on specific wealth. To check the type of plan in place: > table(Season,Sal) Sal Season 0 0.5 5 15 25 Fall 3 3 3 3 3 Summer 3 3 3 3 3 Winter 2 3 3 3 3 Spring 2 3 3 3 3

This is an unbalanced crossover plan. Specific wealth (in other words, the variable to be explained) is a quantitative variable for which the double zero is important. We will therefore use a Euclidean distance to build the association matrix necessary for the PERMANOVA. 8.3.2.2. Application condition of the PERMANOVA The prerequisite condition is the homogeneity test of the betadisp dispersions between groups, which will be verified by the betadisper() function of the vegan library on two qualitative factors (section 5.2.9). It is based on association matrices, which is constructed from Euclidean distances here. It is necessary to specify the matrix, the factor tested, then the method; the most used is the centroid method. It is then followed by an anova() on the results of the betadisper() function. Let us check that the homogeneity of the dispersions is well-respected for both factors. For this test, H0: homogeneous dispersions: >library(vegan) > betadisper(dist(RS,”Euclidean”), Sal, type = >CA;anova(CA) Analysis of Variance Table Response: Distances Df Sum Sq Mean Sq F value Pr(>F) Groups 4 23.613 5.9032 2.3082 0.06995.

"centroid")-

Non-parametric Alternatives to Linear Models

267

Residuals 53 135.546 2.5575 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1 > betadisper(dist(RS), Season, type = "centroid")->CA;anova(CA) Analysis of Variance Table Response: Distances Df Sum Sq Mean Sq F value Pr(>F) Groups 3 31.054 10.3515 2.68 0.05596. Residuals 54 208.573 3.8625 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

For both factors, the P-value is greater than 0.05, H0 is retained and the dispersions are homogeneous. The condition is respected. 8.3.2.3. Implementation of the PERMANOVA It is carried out with the adonis() function of the vegan library by specifying the variable to be explained and the factors to be tested, such as for a linear model, using * if we want to look at the effects of the factors alone and their interaction or + if we only want the effects of the factors alone. It is also necessary to specify the desired association coefficient in the method argument. Different options are available: “manhattan”, “euclidean”, “canberra”, “bray”, “kulczynski”, “jaccard”, “gower”, “altGower”, “morisita”, “horn”, “mountford”, “raup”, “binomial”, “chao”, “cao” or “mahalanobis”. Here, “euclidean” is used since the coefficient used is the Euclidean distance. Finally, it is also necessary to specify the number of permutations to be performed in order to obtain the distribution under H0, with which the result will be compared. It is set to 999 by default, but if you perform the test several times and the results differ, you will need to increase the number of permutations to stabilize the distribution under H0: > adonis(RS ~ Sal*Season,method = "euclidean", permutations=999) Call: adonis(formula = RS ~ Sal * Saison, permutations = 999, method = "euclidean") Permutation: free Number of permutations: 999 Terms added sequentially (first to last) Df SumsOfSqs MeanSqs F.Model R2 Pr(>F)

268

Statistics in Environmental Sciences

Sal 4 217.39 54.348 0.33356 0.003 ** Season 3 9.18 3.060 0.01409 0.782 Sal:Season 12 82.15 6.846 0.12606 0.685 Residuals 38 343.00 9.026 Total 57 651.72 –Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

6.0210 0.3390 0.7585 0.52630 1.00000

Here, only the effect of salinity is significant on the specific wealth of zooplankton. Neither the effect of the season nor the effect of the interaction is. In addition, it gives an assimilated R2 that reflects the importance of the distances explained by the factor. It can be said that 33% of the distances between groups here are explained by salinity. As with a linear model, the significant effect means that at least one of the salinities is different from the others. It is therefore important to know where these differences lie. A multiple comparison test can be performed in order to determine where the differences are per pair of salinity. 8.3.2.4. Multiple comparison test for the PERMANOVA For this purpose, a function found on a forum has been adapted and is available here. This function, called pairwise.adonis(), allows us to test the differences between pairs, here for the only significant factor (salinity): > table(Sal) Sal 0 0.5 5 15 10 12 12 12

25 12

If the interaction had been significant, we could have replaced the factor with the term interaction (Sal, Season). The interaction factor between two qualitative variables can be obtained using the interaction() function: > Season_Salpairwise.adonis