Jump to main navigation


Tutorial 13.3 - Transformations and standardizations

12 Mar 2015

View R code for preliminaries.
> library(vegan)
> library(ggplot2)
> library(grid)
> #define my common ggplot options
> murray_opts <- opts(panel.grid.major=theme_blank(),
+                            panel.grid.minor=theme_blank(),
+                            panel.border = theme_blank(),
+                            panel.background = theme_blank(),
+                            axis.title.y=theme_text(size=15, vjust=0,angle=90),
+                            axis.text.y=theme_text(size=12),
+                            axis.title.x=theme_text(size=15, vjust=-1),
+                            axis.text.x=theme_text(size=12),
+                            axis.line = theme_segment(),
+                            plot.margin=unit(c(0.5,0.5,1,2),"lines")
+ )
Error: Use 'theme' instead. (Defunct;
last used in version 0.9.1)
> coenocline <- function(x,A0,m,r,a,g, int=T, noise=T) {
+ #x is the environmental range
+ #A0 is the maximum abundance of the species at the optimum environmental conditions
+ #m is the value of the environmental gradient that represents the optimum conditions for the species
+ #r the species range over the environmental gradient (niche width)
+ #a and g are shape parameters representing the skewness and kurtosis
+ # when a=g, the distribution is symmetrical
+ # when a>g - negative skew (large left tail)
+ # when a<g - positive skew (large right tail)
+ #int - indicates whether the responses should be rounded to integers (=T)
+ #noise - indicates whether or not random noise should be added (reflecting random sampling)  
+ #NOTE.  negative numbers converted to 0
+          b <- a/(a+g)
+          d <- (b^a)*(1-b)^g
+          cc <- (A0/d)*((((x-m)/r)+b)^a)*((1-(((x-m)/r)+b))^g)
+          if (noise) {n <- A0/10; n[n<0]<-0; cc<-cc+rnorm(length(cc),0,n)}
+          cc[cc<0] <- 0
+          cc[is.na(cc)]<-0
+          if (int) cc<-round(cc,0)
+          cc
+ }
> #plot(coenocline(0:100,40,40,20,1,1, int=T, noise=T), ylim=c(0,100))

Tutorial 13.1 discussed the idea of amalgamating variables together so as to create new, condensed insights into the composition of communities (objects). However, the success and appropriateness of the amalgamations is very much dependent on the characteristics of the original variables (e.g. species).

Some of the techniques are parametric and assume that the variables are:

  • normally distributed
  • linearly related
  • measured on the same scale

Furthermore, the amalgams of many of the techniques can be overly biased towards the patterns of highly abundant species or variables whose scales yield values of large magnitude (such as units of force or pressure). Rare or variables with values small in magnitude tend to have almost no influence at all.

Ecological/biological multivariate data typically comprises of the following:
  • species abundances (counts). Whilst species abundances are usually expressed in the same units and on the same scale, they tend to be positively skewed (since they are truncated at zero).
  • environmental data (measurements). Environmental data tend to be measured on disparate scales (pH, degrees C, mm, kg, etc) and thus can present issues of non-normality and non-linearity
  • morphometric data (counts, measurements). Morphometric data as used in taxonomic studies often represent a mixture of measurements (lengths, masses) in addition to binary (feature present/absent) and counts and thus normality and linearity can also pose issues.

Hence, the first step of most multivariate analyses is to transform the data so that the data used to create the amalgamations (and thus the amalgamations themselves) best represents the broad characteristics of the communities.

Again we will use a simulated data set introduced in the previous Tutorial. This multivariate dataset comprises the abundances of 10 species from each of 10 sites located throughout a landscape.

> set.seed(1)
> x <- seq(0,50,l=10)
> n <- 10
> sp1<-coenocline(x=x,A0=5,m=0,r=2,a=1,g=1,int=T, noise=T)
> sp2<-coenocline(x=x,A0=70,m=7,r=30,a=1,g=1,int=T, noise=T)
> sp3<-coenocline(x=x,A0=50,m=15,r=30,a=1,g=1,int=T, noise=T)
> sp4<-coenocline(x=x,A0=7,m=25,r=20,a=0.4,g=0.1,int=T, noise=T)
> sp5<-coenocline(x=x,A0=40,m=30,r=30,a=0.6,g=0.5,int=T, noise=T)
> sp6<-coenocline(x=x,A0=15,m=35,r=15,a=0.2,g=0.3,int=T, noise=T)
> sp7<-coenocline(x=x,A0=20,m=45,r=25,a=0.5,g=0.9,int=T, noise=T)
> sp8<-coenocline(x=x,A0=5,m=45,r=5,a=1,g=1,int=T, noise=T)
> sp9<-coenocline(x=x,A0=20,m=45,r=15,a=1,g=1,int=T, noise=T)
> sp10<-coenocline(x=x,A0=30,m=50,r=5,a=1,g=1,int=T, noise=T)
> X <- cbind(sp1, sp10,sp9,sp2,sp3,sp8,sp4,sp5,sp7,sp6)
> #X<-X[c(1,10,9,2,3,8,4,5,7,6),] 
> colnames(X) <- paste("Sp",1:10,sep="")
> rownames(X) <- paste("Site", c(1,10,9,2,3,8,4,5,7,6), sep="")
> X <- X[c(1,4,5,7,8,10,9,6,3,2),]
> data <- data.frame(Sites=factor(rownames(X),levels=rownames(X)), X)
Sites Sp1 Sp2 Sp3 Sp4 Sp5 Sp6 Sp7 Sp8 Sp9 Sp10
Site1 5 0 0 65 5 0 0 0 0 0
Site2 0 0 0 25 39 0 6 23 0 0
Site3 0 0 0 6 42 0 6 31 0 0
Site4 0 0 0 0 0 0 0 40 0 14
Site5 0 0 6 0 0 0 0 34 18 12
Site6 0 29 12 0 0 0 0 0 22 0
Site7 0 0 21 0 0 5 0 0 20 0
Site8 0 0 0 0 13 0 6 37 0 0
Site9 0 0 0 60 47 0 4 0 0 0
Site10 0 0 0 72 34 0 0 0 0 0

I will also introduce another simulated data set that comprises five biophysical measurements made from the 10 sites. These biophysical environmental data include pH (log scale), Pressure (Pa), Altitude (m), Slope (degrees) and Substrate (categorical: Quartz or Shale).

> set.seed(1)
> Site <- gl(10,1,10,lab=paste('Site',1:10, sep=""))
> Y <- matrix(c(
+ 6.1,4.2,101325,2,
+ 6.7,9.2,101352,510,
+ 6.8,8.6,101356,546,
+ 7.0,7.4,101372,758,
+ 7.2,5.8,101384,813,
+ 7.5,8.4,101395,856,
+ 7.5,0.5,101396,854,
+ 7.0,11.8,101370,734,
+ 8.4,8.2,101347,360,
+ 6.2,1.5,101345,356
+   ),10,4, byrow=TRUE)
> colnames(Y) <- c('pH','Slope', 'Pressure', 'Altitude')
> Substrate <- factor(c('Quartz','Shale','Shale','Shale','Shale','Quartz','Quartz','Shale','Quartz','Quartz'))
> enviro <- data.frame(Site,Y,Substrate)
Site pH Slope Pressure Altitude Substrate
Site1 6.1 4.2 101325 2 Quartz
Site2 6.7 9.2 101352 510 Shale
Site3 6.8 8.6 101356 546 Shale
Site4 7.0 7.4 101372 758 Shale
Site5 7.2 5.8 101384 813 Shale
Site6 7.5 8.4 101395 856 Quartz
Site7 7.5 0.5 101396 854 Quartz
Site8 7.0 11.8 101370 734 Shale
Site9 8.4 8.2 101347 360 Quartz
Site10 6.2 1.5 101345 356 Quartz

Simple transformations

Simple transformations such as those outlined in Tutorial 4.1 can be used to help address issues of normality and non-linearity. Indeed species abundance data are routinely forth-root ($\sqrt[4]{}$) or logx+1 transformed prior to multivariate analysis.

In addition, a rather drastic transformation is that which transforms count data in to binary (presence/absence) data. When performed on the entire data frame, such a transformation removes the distinction between dominant and rare species. It can be useful when the applied to a single variable whose values are predominantly 0's and 1's.

Many of the common transformation/standardization routines used in ecology are supported in R by the decostand function within the vegan package.

> library(vegan)
> decostand(data[,-1], method="pa")
       Sp1 Sp2 Sp3 Sp4 Sp5 Sp6 Sp7 Sp8
Site1    1   0   0   1   1   0   0   0
Site2    0   0   0   1   1   0   1   1
Site3    0   0   0   1   1   0   1   1
Site4    0   0   0   0   0   0   0   1
Site5    0   0   1   0   0   0   0   1
Site6    0   1   1   0   0   0   0   0
Site7    0   0   1   0   0   1   0   0
Site8    0   0   0   0   1   0   1   1
Site9    0   0   0   1   1   0   1   0
Site10   0   0   0   1   1   0   0   0
       Sp9 Sp10
Site1    0    0
Site2    0    0
Site3    0    0
Site4    0    1
Site5    1    1
Site6    1    0
Site7    1    0
Site8    0    0
Site9    0    0
Site10   0    0

Standardizations

Close inspection of the above species abundance data reveals that whilst some of the species are relatively abundant (Species 1, 3, 4 and 8), other species (such as Species 2, 6 and especially 9) are relatively rare. As previously stated, most numerical methods of amalgamating will be more heavily influenced by the more abundant species. Sometimes this is a desired outcome - you might want your measures of the community to reflect this dominance. That is, you may want a numerical description of what is visible obvious in the field. Yet for other purposes, you may want the patterns to be more representative of the subtleties and complexities of the communities.

The environmental data highlight a different set of common issues:

  • some variables (particularly Pressure) have values of much greater magnitude than others
  • despite having values of very high magnitude, the Pressure measurements have relatively little spread (variation).
  • the altitude measurements have relatively high levels of spread

Rather than simply transforming the variables in isolation, standardizations transform the values relative to other variables, objects or both. There are numerous ways that multivariate data can be standardized in an attempt to alter the balance of weightings and inter-relationships. Most work by adjusting the values such that some property such as means, maximums, totals and or spread are equivalent for each species.

  • Total per row. Each value in a row is divided by the total for the row. This is a simple standardization that can dampen objects (sites) that have very high abundances.
    > library(vegan)
    > decostand(data[,-1], method="total")
    
               Sp1    Sp2     Sp3     Sp4
    Site1  0.06667 0.0000 0.00000 0.86667
    Site2  0.00000 0.0000 0.00000 0.26882
    Site3  0.00000 0.0000 0.00000 0.07059
    Site4  0.00000 0.0000 0.00000 0.00000
    Site5  0.00000 0.0000 0.08571 0.00000
    Site6  0.00000 0.4603 0.19048 0.00000
    Site7  0.00000 0.0000 0.45652 0.00000
    Site8  0.00000 0.0000 0.00000 0.00000
    Site9  0.00000 0.0000 0.00000 0.54054
    Site10 0.00000 0.0000 0.00000 0.67925
               Sp5    Sp6     Sp7    Sp8
    Site1  0.06667 0.0000 0.00000 0.0000
    Site2  0.41935 0.0000 0.06452 0.2473
    Site3  0.49412 0.0000 0.07059 0.3647
    Site4  0.00000 0.0000 0.00000 0.7407
    Site5  0.00000 0.0000 0.00000 0.4857
    Site6  0.00000 0.0000 0.00000 0.0000
    Site7  0.00000 0.1087 0.00000 0.0000
    Site8  0.23214 0.0000 0.10714 0.6607
    Site9  0.42342 0.0000 0.03604 0.0000
    Site10 0.32075 0.0000 0.00000 0.0000
              Sp9   Sp10
    Site1  0.0000 0.0000
    Site2  0.0000 0.0000
    Site3  0.0000 0.0000
    Site4  0.0000 0.2593
    Site5  0.2571 0.1714
    Site6  0.3492 0.0000
    Site7  0.4348 0.0000
    Site8  0.0000 0.0000
    Site9  0.0000 0.0000
    Site10 0.0000 0.0000
    
  • Maximum per column. Each value in a column is divided by the maximum of the column. This is a simple standardization that can even up the influence of each of the columns. This is effective for variables that are measured on the same scale and that have similar spread (variance). Standardized values will range from 0 to 1. Note this standardization will not address changes in the spread or variability of variables measured on different scales.
    > library(vegan)
    > decostand(data[,-1], method="max")
    
           Sp1 Sp2    Sp3     Sp4    Sp5
    Site1    1   0 0.0000 0.90278 0.1064
    Site2    0   0 0.0000 0.34722 0.8298
    Site3    0   0 0.0000 0.08333 0.8936
    Site4    0   0 0.0000 0.00000 0.0000
    Site5    0   0 0.2857 0.00000 0.0000
    Site6    0   1 0.5714 0.00000 0.0000
    Site7    0   0 1.0000 0.00000 0.0000
    Site8    0   0 0.0000 0.00000 0.2766
    Site9    0   0 0.0000 0.83333 1.0000
    Site10   0   0 0.0000 1.00000 0.7234
           Sp6    Sp7   Sp8    Sp9   Sp10
    Site1    0 0.0000 0.000 0.0000 0.0000
    Site2    0 1.0000 0.575 0.0000 0.0000
    Site3    0 1.0000 0.775 0.0000 0.0000
    Site4    0 0.0000 1.000 0.0000 1.0000
    Site5    0 0.0000 0.850 0.8182 0.8571
    Site6    0 0.0000 0.000 1.0000 0.0000
    Site7    1 0.0000 0.000 0.9091 0.0000
    Site8    0 1.0000 0.925 0.0000 0.0000
    Site9    0 0.6667 0.000 0.0000 0.0000
    Site10   0 0.0000 0.000 0.0000 0.0000
    
  • Wisconsin double standardization. Each value is first standardized by the column maximum before being standardized by the row total. This standardization tends to enhance the patterns in the data and therefore is a popular choice.
    > library(vegan)
    > wisconsin(data[,-1])
    
              Sp1    Sp2    Sp3     Sp4
    Site1  0.4977 0.0000 0.0000 0.44933
    Site2  0.0000 0.0000 0.0000 0.12617
    Site3  0.0000 0.0000 0.0000 0.03028
    Site4  0.0000 0.0000 0.0000 0.00000
    Site5  0.0000 0.0000 0.1016 0.00000
    Site6  0.0000 0.3889 0.2222 0.00000
    Site7  0.0000 0.0000 0.3438 0.00000
    Site8  0.0000 0.0000 0.0000 0.00000
    Site9  0.0000 0.0000 0.0000 0.33333
    Site10 0.0000 0.0000 0.0000 0.58025
               Sp5    Sp6    Sp7    Sp8
    Site1  0.05295 0.0000 0.0000 0.0000
    Site2  0.30152 0.0000 0.3634 0.2089
    Site3  0.32472 0.0000 0.3634 0.2816
    Site4  0.00000 0.0000 0.0000 0.5000
    Site5  0.00000 0.0000 0.0000 0.3024
    Site6  0.00000 0.0000 0.0000 0.0000
    Site7  0.00000 0.3438 0.0000 0.0000
    Site8  0.12563 0.0000 0.4542 0.4201
    Site9  0.40000 0.0000 0.2667 0.0000
    Site10 0.41975 0.0000 0.0000 0.0000
              Sp9   Sp10
    Site1  0.0000 0.0000
    Site2  0.0000 0.0000
    Site3  0.0000 0.0000
    Site4  0.0000 0.5000
    Site5  0.2911 0.3049
    Site6  0.3889 0.0000
    Site7  0.3125 0.0000
    Site8  0.0000 0.0000
    Site9  0.0000 0.0000
    Site10 0.0000 0.0000
    
  • Range. Each value in a column is standardized into a range of 0 to 1. This is one way to adjust for differences in the spread of values. For example, it could be used to even up the spread of measurements in the simulated environmental data.
    > library(vegan)
    > #we need to first convert the categorical variable (Substrate) into a numeric
    > enviro1 <- within(enviro, Substrate <- as.numeric(Substrate))
    > decostand(enviro1[,-1], method="range")
    
            pH  Slope Pressure Altitude
    1  0.00000 0.3274   0.0000   0.0000
    2  0.26087 0.7699   0.3803   0.5948
    3  0.30435 0.7168   0.4366   0.6370
    4  0.39130 0.6106   0.6620   0.8852
    5  0.47826 0.4690   0.8310   0.9496
    6  0.60870 0.6991   0.9859   1.0000
    7  0.60870 0.0000   1.0000   0.9977
    8  0.39130 1.0000   0.6338   0.8571
    9  1.00000 0.6814   0.3099   0.4192
    10 0.04348 0.0885   0.2817   0.4145
       Substrate
    1          0
    2          1
    3          1
    4          1
    5          1
    6          0
    7          0
    8          1
    9          0
    10         0
    
  • Centre. Each value in a column is standardized to have a mean of 0 by subtracting the column mean from each of the values in the column. This is useful when the variables all have similar absolute spreads of values yet vastly different magnitudes. values when
    > #we need to first convert the categorical variable (Substrate) into a numeric
    > enviro1 <- within(enviro, Substrate <- as.numeric(Substrate))
    > apply(enviro1[,-1],2,scale, scale=FALSE)
    
             pH Slope Pressure Altitude
     [1,] -0.94 -2.36    -39.2   -576.9
     [2,] -0.34  2.64    -12.2    -68.9
     [3,] -0.24  2.04     -8.2    -32.9
     [4,] -0.04  0.84      7.8    179.1
     [5,]  0.16 -0.76     19.8    234.1
     [6,]  0.46  1.84     30.8    277.1
     [7,]  0.46 -6.06     31.8    275.1
     [8,] -0.04  5.24      5.8    155.1
     [9,]  1.36  1.64    -17.2   -218.9
    [10,] -0.84 -5.06    -19.2   -222.9
          Substrate
     [1,]      -0.5
     [2,]       0.5
     [3,]       0.5
     [4,]       0.5
     [5,]       0.5
     [6,]      -0.5
     [7,]      -0.5
     [8,]       0.5
     [9,]      -0.5
    [10,]      -0.5
    
  • Standardize. Each value in a column is standardized to a mean of 0 and standard deviation of 1. That is, each of the variables are normalized. This is another way to adjust for differences in the spread of values.
    > #we need to first convert the categorical variable (Substrate) into a numeric
    > enviro1 <- within(enviro, Substrate <- as.numeric(Substrate))
    > apply(enviro1[,-1],2,scale)
    
    > #OR
    > library(vegan)
    > decostand(enviro1[,-1], method="standardize")
    
             pH   Slope Pressure Altitude
    1  -1.39885 -0.6636  -1.6863  -2.0691
    2  -0.50597  0.7423  -0.5248  -0.2471
    3  -0.35715  0.5736  -0.3527  -0.1180
    4  -0.05953  0.2362   0.3355   0.6424
    5   0.23810 -0.2137   0.8517   0.8396
    6   0.68455  0.5173   1.3249   0.9938
    7   0.68455 -1.7039   1.3679   0.9867
    8  -0.05953  1.4733   0.2495   0.5563
    9   2.02387  0.4611  -0.7399  -0.7851
    10 -1.25004 -1.4227  -0.8259  -0.7995
       Substrate
    1    -0.9487
    2     0.9487
    3     0.9487
    4     0.9487
    5     0.9487
    6    -0.9487
    7    -0.9487
    8     0.9487
    9    -0.9487
    10   -0.9487
    

As with simple transformations in statistical models, it is usually advised that multivariate analyses be repeated with a range of transformation and standardization options so as to gain an appreciation of the influence of dominant/rare species, populous and sparse sites, large or varied measurements. If the various standardizations ultimately yield similar patterns amongst communities, then it suggests that the patterns are stable within the scale of your observations and that any one of the outcomes can be used to describe the patterns. If the patterns are substantially different, then it is likely that the different standardizations are drawing out different scales of community patterns.




Worked Examples

Basic statistics references
  • Legendre and Legendre
  • Quinn & Keough (2002) - Chpt 17

Standardizations

The following community data represent the abundances of three species of gastropods in five quadrats (ranging from high shore marsh - Quadrat 1, to low shore marsk - Quadrat 5) in a saltmarsh.

Download gastropod data set
Format of the gastropod
Salinator Ophicardelus Marinula
4 0 1
9 3 0
9 4 1
6 2 0
0 1 1

SalinatorNumber of Salinator gastropods - variable
OphicardelusNumber of Ophicardelus gastropods - variable
MarinulaNumber of Marinula gastropods - variable
Q1-Q5Quadrats - these are the objects
Leaves

Open the gastropod data set.
Show code
> gastropod <- read.csv('../downloads/data/gastropod.csv')
> gastropod
  Salinator Ophicardelus Marinula
1         4            0        1
2         9            3        0
3         9            4        1
4         6            2        0
5         0            1        1
  1. Before proceeding with any multivariate analyses, it is a good idea to get a 'feel' for your data. The gastropod data set is intentionally very small so that we can help relate various calculated properties to what we can see by simply inspecting the counts.

    To build up a picture of these data, generate the following exploratory properties:

    1. Scale of each of the species (column maximums)
      Show code
      > apply(gastropod,2,max)
      
         Salinator Ophicardelus     Marinula 
                 9            4            1 
      
    2. Scale of each of the species (column means)
      Show code
      > apply(gastropod,2,mean)
      
         Salinator Ophicardelus     Marinula 
               5.6          2.0          0.6 
      
    3. Variability of each of the species (column variance)
      Show code
      > apply(gastropod,2,var)
      
         Salinator Ophicardelus     Marinula 
              14.3          2.5          0.3 
      
    4. Abundances in each quadrat (row totals)
      Show code
      > apply(gastropod,1,sum)
      
      [1]  5 12 14  8  2
      
    5. Correlations between species
      Show code
      > cor(gastropod)
      
                   Salinator Ophicardelus
      Salinator       1.0000       0.7944
      Ophicardelus    0.7944       1.0000
      Marinula       -0.4587      -0.2887
                   Marinula
      Salinator     -0.4587
      Ophicardelus  -0.2887
      Marinula       1.0000
      

  2. We intend to use these data in some sort of multivariate analysis. Typically, before doing so, we standardize the data in order to ensure that certain features are honored in the analysis. Standardize the gastropod data to achieve the following:
    1. ensure that the rare and abundant species alike have similar weighting and are constrained to the range of 0-1
      Show code
      > library(vegan)
      > gast1 <- decostand(gastropod,"max")
      > gast1
      
        Salinator Ophicardelus Marinula
      1    0.4444         0.00        1
      2    1.0000         0.75        0
      3    1.0000         1.00        1
      4    0.6667         0.50        0
      5    0.0000         0.25        1
      
      > apply(gast1,2,max)
      
         Salinator Ophicardelus     Marinula 
                 1            1            1 
      
      > apply(gast1,2,range)
      
           Salinator Ophicardelus Marinula
      [1,]         0            0        0
      [2,]         1            1        1
      
    2. ensure that the all species have similar weighting yet maintain their variability. This could be important if you want multivariate patterns to reflect heterogeneity (many analyses are drawn towards higher variability).
      Show code
      > #center the data
      > gast2<-apply(gastropod,2,scale,scale=FALSE)
      > gast2
      
           Salinator Ophicardelus Marinula
      [1,]      -1.6           -2      0.4
      [2,]       3.4            1     -0.6
      [3,]       3.4            2      0.4
      [4,]       0.4            0     -0.6
      [5,]      -5.6           -1      0.4
      
      > apply(gast2,2,mean)
      
         Salinator Ophicardelus     Marinula 
         3.554e-16    0.000e+00    2.220e-17 
      
      > apply(gast2,2,var)
      
         Salinator Ophicardelus     Marinula 
              14.3          2.5          0.3 
      
    3. ensure that the all species have similar weighting. The influences of highly abundant and/or variable species are suppressed and those of rare species are enhanced so that all have similar influence.
      Show code
      > #scale data to mean=0 and variance of 1
      > gast3<-apply(gastropod,2,scale)
      > #OR
      > library(vegan)
      > gast3<-decostand(gastropod,method="standardize")
      > gast3
      
        Salinator Ophicardelus Marinula
      1   -0.4231      -1.2649   0.7303
      2    0.8991       0.6325  -1.0954
      3    0.8991       1.2649   0.7303
      4    0.1058       0.0000  -1.0954
      5   -1.4809      -0.6325   0.7303
      
      > apply(gast3,2,mean)
      
         Salinator Ophicardelus     Marinula 
         1.193e-16    0.000e+00    0.000e+00 
      
      > apply(gast3,2,var)
      
         Salinator Ophicardelus     Marinula 
                 1            1            1 
      
    4. ensure that all sites have similar weightings and are constrained to a range of 0-1.
      Show code
      > library(vegan)
      > gast4 <- decostand(gastropod,"total")
      > gast4
      
        Salinator Ophicardelus Marinula
      1    0.8000       0.0000  0.20000
      2    0.7500       0.2500  0.00000
      3    0.6429       0.2857  0.07143
      4    0.7500       0.2500  0.00000
      5    0.0000       0.5000  0.50000
      
      > apply(gast4,1,sum)
      
      [1] 1 1 1 1 1
      
      > cor(gast4)
      
                   Salinator Ophicardelus
      Salinator       1.0000      -0.8353
      Ophicardelus   -0.8353       1.0000
      Marinula       -0.8852       0.4836
                   Marinula
      Salinator     -0.8852
      Ophicardelus   0.4836
      Marinula       1.0000
      
    5. ensure that all species and sites have similar weightings and yet enhances any underlying patterns (increases species correlations for example). This can improve the success of any resulting multivariate analyses.
      Show code
      > library(vegan)
      > # Wisconsin double standardization
      > gast5 <- wisconsin(gastropod)
      > gast5
      
        Salinator Ophicardelus Marinula
      1    0.3077       0.0000   0.6923
      2    0.5714       0.4286   0.0000
      3    0.3333       0.3333   0.3333
      4    0.5714       0.4286   0.0000
      5    0.0000       0.2000   0.8000
      
      > cor(gast5)
      
                   Salinator Ophicardelus
      Salinator       1.0000       0.6123
      Ophicardelus    0.6123       1.0000
      Marinula       -0.9241      -0.8680
                   Marinula
      Salinator     -0.9241
      Ophicardelus  -0.8680
      Marinula       1.0000
      

Exponential family of distributions

The exponential distributions are a class of continuous distribution which can be characterized by two parameters. One of these parameters (the location parameter) is a function of the mean and the other (the dispersion parameter) is a function of the variance of the distribution. Note that recent developments have further extended generalized linear models to accommodate other non-exponential residual distributions.

End of instructions

  Quitting R

There are two ways to quit R elegantly
  1. Goto the RGui File menu and select Quit
  2. In the R Console, type
    > q()
    
Either way you will be prompted to save the workspace, generally this is not necessary or even desirable.

End of instructions

  The R working directory

The R working directory is the location that R looks in to read/write files. When you load R, it initially sets the working directory to a location within the R subdirectory of your computer (Windows and MacOSX) or the location from which you executed R (Linux). However, we can alter the working directory so that it points to another location. This not only prevents having to search for files, it also negates the need to specify a path as well as a filename in various operations.
The working directory can be altered by specifying a path (full or relative) as an argument to the setwd() function. Note that R uses Unix path slashes ("/") irrespective of what operating system.
> setwd("/Work/Analyses/Project1") #change the working directory

Alternatively, on Windows:
  1. Goto the RGui File menu
  2. Select the Change dir submenu
  3. Locate the directory that contains your data and/or scripts
  4. Click OK

End of instructions

  Read in (source) an R script

There are two ways to read in (source) a R script
  1. Goto the RGui File menu and select Source R code
  2. In the R Console, type

    > source('filename')

    where 'filename' is the name of the R script file.
All commands in the file will be executed sequentially starting at the first line. Note that R will ignore everything that is on a line following the '#' character. This provides a way of inserting comments into script files (a practice that is highly encouraged as it provides a way of reminding yourself what each line of syntax does).

Note that there is an alternative way of using scripts

  1. Goto the RGui File menu and select the Open script submenu
  2. This will display the R script file in the R Editor window
  3. Syntax can then be directly cut and paste into the R Console. Again,each line will be executed sequentially and comments are ignored.
When working with multi-step analyses, it is recommended that you have the R Editor open and that syntax is entered directly into the editor and then pasted into the R Console. Then provided the R script is saved regularly, your data and analyses are safe from computer disruptions.

End of instructions

  R workspace

The R workspace stores all the objects that are created during an R session. To view a list of objects occupying the current R workshpace

> ls()

The workspace is cabable of storing huge numbers of objects, and thus it can become cluttered (with objects that you have created, but forgotten about, or many similar objects that contain similar contents) very rapidly. Whilst this does not affect the performance of R, it can lead to chronic confusion. It is advisable that you remove all unwanted objects when you have finished with them. To remove and object;

> rm(object name)

where object name is the name of the object to be removed.

When you exit R, you will be prompted for whether you want to save the workspace. You can also save the current workspace at any time by; A saved workspace is not in easily readable text format. Saving a workspace enables you to retrieve an entire sessions work instantly, which can be useful if you are forced to perform you analyses over multiple sittings. Furthermore, by providing different names, it is possible to maintain different workspaces for different projects.

End of instructions

  Removing objects

To remove an object

> rm(object name)

where object name is the name of the object to be removed.

End of instructions

  R indexing

A vector is simply a array (list) of entries of the same type. For example, a numeric vector is just a list of numbers. A vector has only a single dimension - length - and thus the first entry in the vector has an index of 1 (is in position 1), the second has an index of 2 (position 2), etc. The index of an entry provides a convenient way to access entries. If a vector contained the numbers 10, 5, 8, 3.1, then the entry with index 1 is 10, at index 4 is the entry 3.1. Consider the following examples
> var <- c(4,8,2,6,9,2)
> var
[1] 4 8 2 6 9 2
> var[5]
[1] 9
> var[3:5]
[1] 2 6 9

A data frame on the other hand is not a single vector, but rather a collection of vectors. It is a matrix, consisting of columns and rows and therefore has both length and width. Consequently, each entry has a row index and a column index. Consider the following examples
> dv <- c(4,8,2,6,9,2)
> iv <- c('a','a','a','b','b','b')
> data <- data.frame(iv,dv)
> data
  iv dv
1  a  4
2  a  8
3  a  2
4  b  6
5  b  9
6  b  2
> #The first column (preserve frame)
> data[1]
  iv
1  a
2  a
3  a
4  b
5  b
6  b
> #The second column (preserve frame)
> data[2]
  dv
1  4
2  8
3  2
4  6
5  9
6  2
> #The first column (as vector)
> data[,1]
[1] a a a b b b
Levels: a b
> #The first row (as vector)
> data[1,]
  iv dv
1  a  4
> #list the entry in row 3, column 2
> data[3,2]
[1] 2

End of instructions

  R 'fix()' function

The 'fix()' function provides a very rudimentary spreadsheet for data editing and entry.

> fix(data frame name)

The spreadsheet (Data Editor) will appear. A new variable can be created by clicking on a column heading and then providing a name fo r the variable as well as an indication of whether the contents are expected to be numeric (numbers) or character (contain some letters). Data are added by clicking on a cell and providing an entry. Closing the Data Editor will cause the changes to come into affect.

End of instructions

  R transformations

The following table illustrates the use of common data transmformation functions on a single numeric vector (old_var). In each case a new vector is created (new_var)

TransformationSyntax
loge

> new_var <- log(old_var)

log10

> new_var <- log10(old_var)

square root

> new_var <- sqrt(old_var)

arcsin

> new_var <- asin(sqrt(old_var))

scale (mean=0, unit variance)

> new_var <- scale(old_var)

where old_var is the name of the original variable that requires transformation and new_var is the name of the new variable (vector) that is to contain the transformed data. Note that the original vector (variable) is not altered.

End of instructions

  Data transformations


Essentially transforming data is the process of converting the scale in which the observations were measured into another scale.

I will demonstrate the principles of data transformation with two simple examples.
Firstly, to illustrate the legality and frequency of data transformations, imagine you had measured water temperature in a large number of streams. Naturally, you would have probably measured the temperature in ° C. Supposing you later wanted to publish your results in an American journal and the editor requested that the results be in ° F. You would not need to re-measure the stream temperature. Rather, each of the temperatures could be converted from one scale (° C) to the other (° F). Such transformations are very common.

To further illustrate the process of transformation, imagine a botanist wanted to examine the size of a particular species leaves for some reason. The botanist decides to measure the length of a random selection of leaves using a standard linear, metric ruler and the distribution of sample observations as represented by a histogram and boxplot are as follows.


The growth rate of leaves might be expected to be greatest in small leaves and decelerate with increasing leaf size. That is, the growth rate of leaves might be expected to be logarithmic rather than linear. As a result, the distribution of leaf sizes using a linear scale might also be expected to be non-normal (log-normal). If, instead of using a linear scale, the botanist had used a logarithmic ruler, the distribution of leaf sizes may have been more like the following.


If the distribution of observations is determined by the scale used to measure of the observations, and the choice of scale (in this case the ruler) is somewhat arbitrary (a linear scale is commonly used because we find it easier to understand), then it is justifiable to convert the data from one scale to another after the data has been collected and explored. It is not necessary to re-measure the data in a different scale. Therefore to normalize the data, the botanist can simply convert the data to logarithms.

The important points in the process of transformations are;
  1. The order of the data has not been altered (a large leaf measured on a linear scale is still a large leaf on a logarithmic scale), only the spacing of the data
  2. Since the spacing of the data is purely dependent on the scale of the measuring device, there is no reason why one scale is more correct than any other scale
  3. For the purpose of normalization, data can be converted from one scale to another following exploration

End of instructions

  R writing data

To export data into R, we read the empty the contents a data frame into a file. The general format of the command for writing data in from a data frame into a file is

> write.data(data frame name, 'filename.csv', quote=F, sep=',')

where data frame name is a name of the data frame to be saved and filename.csv is the name of the csv file that is to be created (or overwritten). The argument quote=F indicates that quotation marks should not be placed around entries in the file. The argument sep=',' indicates that entries in the file are separated by a comma (hence a comma delimited file).

As an example

> write.data(LEAVES, 'leaves.csv', quote=F, sep=',')

End of instructions

  Exporting from Excel

End of instructions

  Reading data into R

Ensure that the working directory is pointing to the path containing the file to be imported before proceeding.
To import data into R, we read the contents of a file into a data frame. The general format of the command for reading data into a data frame is

> name <- read.table('filename.csv', header=T, sep=',', row.names=column, strip.white=T)

where name is a name you wish the data frame to be referred to as, filename.csv is the name of the csv file that you created in excel and column is the number of the column that had row labels (if there were any). The argument header=T indicates that the variable (vector) names will be created from the names supplied in the first row of the file. The argument sep=',' indicates that entries in the file are separated by a comma (hence a comma delimited file). If the data file does not contain row labels, or you are not sure whether it does, it is best to omit the row.names=column argument. The strip.white=T arguement ensures that no leading or trailing spaces are left in character names (these can be particularly nasty in categorical variables!).

As an example
> phasmid <- read.data('phasmid.csv', header=T, sep=',', row.names=1, strip.white=T)

End of instructions

  Cutting and pasting data into R

To import data into R via the clipboard, copy the data in Excel (including a row containing column headings - variable names) to the clipboard (CNTRL-C). Then in R use a modification of the read.table function:

> name <- read.table('clipboard', header=T, sep='\t', row.names=column, strip.white=T)

where name is a name you wish the data frame to be referred to as, clipboard is used to indicate that the data are on the clipboard, and column is the number of the column that had row labels (if there were any). The argument header=T indicates that the variable (vector) names will be created from the names supplied in the first row of the file. The argument sep='\t' indicates that entries in the clipboard are separated by a TAB. If the data file does not contain row labels, or you are not sure whether it does, it is best to omit the row.names=column argument. The strip.white=T arguement ensures that no leading or trailing spaces are left in character names (these can be particularly nasty in categorical variables!).

As an example
> phasmid <- read.data('clipboard', header=T, sep='\t', row.names=1, strip.white=T)

End of instructions

  Writing a copy of the data to an R script file

> phasmid <- dump('data', "")

where 'data' is the name of the object that you wish to be stored.
Cut and paste the output of this command directly into the top of your R script. In future, when the script file is read, it will include a copy of your data set, preserved exactly.

End of instructions

  Frequency histogram

> hist(variable)

where variable is the name of the numeric vector (variable) for which the histogram is to be constructed.

End of instructions

  Summary Statistics

# mean
> mean(variable)
# variance
> var(variable)
# standard deviation
> sd(variable)
# variable length
> length(variable)
# median
> median(variable)
# maximum
> max(variable)
# minimum
> min(variable)
# standard error of mean
> sd(variable)/sqrt(length(variable))
# interquartile range
> quantile(variable, c(.25,.75))
# 95% confidence interval
> qnorm(0.975,0,1)*(sd(variable)/sqrt(length(variable)))

where variable is the name of the numeric vector (variable).

End of instructions

  Normal probabilities

> pnorm(c(value), mean=mean, sd=sd, lower.tail=FALSE)

this will calculate the area under a normal distribution (beyond the value of value) with mean of mean and standard deviation of sd. The argument lower.tail=FALSE indicates that the area is calculated for values greater than value. For example

> pnorm(c(2.9), mean=2.025882, sd=0.4836265, lower.tail=FALSE)

End of instructions

  Boxplots

A boxplot is a graphical representation of the distribution of observations based on the 5-number summary that includes the median (50%), quartiles (25% and 75%) and data range (0% - smallest observation and 100% - largest observation). The box demonstrates where the middle 50% of the data fall and the whiskers represent the minimum and maximum observations. Outliers (extreme observations) are also represented when present.
The figure below demonstrates the relationship between the distribution of observations and a boxplot of the same observations. Normally distributed data results in symmetrical boxplots, whereas skewed distributions result in asymmetrical boxplots with each segment getting progressively larger (or smaller).


End of instructions

  Boxplots

> boxplot(variable)

where variable is the name of the numeric vector (variable)

End of instructions

  Observations, variables & populations

Observations are the sampling units (e.g quadrats) or experimental units (e.g. individual organisms, aquaria) that make up the sample.

Variables are the actual properties measured by the individual observations (e.g. length, number of individuals, rate, pH, etc). Random variables (those variables whose values are not known for sure before the onset of sampling) can be either continuous (can theoretically be any value within a range, e.g. length, weight, pH, etc) or categorical (can only take on certain discrete values, such as counts - number of organisms etc).

Populations are defined as all the possible observations that we are interested in.

A sample represents a collected subset of the population's observations and is used to represent the entire population. Sample statistics are the characteristics of the sample (e.g. sample mean) and are used to estimate population parameters.


End of instructions

  Population & sample

A population refers to all possible observations. Therefore, population parameters refer to the characteristics of the whole population. For example the population mean.

A sample represents a collected subset of the population's observations and is used to represent the entire population. Sample statistics are the characteristics of the sample (e.g. sample mean) and are used to estimate population parameters.


End of instructions

  Standard error and precision

A good indication of how good a single estimate is likely to be is how precise the measure is. Precision is a measure of how repeatable an outcome is. If we could repeat the sampling regime multiple times and each time calculate the sample mean, then we could examine how similar each of the sample means are. So a measure of precision is the degree of variability between the individual sample means from repeated sampling events.

Sample number Sample mean
112.1
212.7
312.5
Mean of sample means12.433
> SD of sample means0.306

The table above lists three sample means and also illustrates a number of important points;
  1. Each sample yields a different sample mean
  2. The mean of the sample means should be the best estimate of the true population mean
  3. The more similar (consistent) the sample means are, the more precise any single estimate of the population mean is likely to be
The standard deviation of the sample means from repeated sampling is called the Standard error of the mean.

It is impractical to repeat the sampling effort multiple times, however, it is possible to estimate the standard error (and therefore the precision of any individual sample mean) using the standard deviation (SD) of a single sample and the size (n) of this single sample.

The smaller the standard error (SE) of the mean, the more precise (and possibly more reliable) the estimate of the mean is likely to be.

End of instructions

  Confidence intervals


A 95% confidence interval is an interval that we are 95% confident will contain the true population mean. It is the interval that there is a less than 5% chance that this interval will not contain the true population mean, and therefore it is very unlikely that this interval will not contain the true mean. The frequentist approach to statistics dictates that if multiple samples are collected from a population and the interval is calculated for each sample, 95% of these intervals will contain the true population mean and 5% will not. Therefore there is a 95% probability that any single sample interval will contain the population mean.

The interval is expressed as the mean ± half the interval. The confidence interval is obviously affected by the degree of confidence. In order to have a higher degree of confidence that an interval is likely to contain the population mean, the interval would need to be larger.

End of instructions

  Successful transformations


Since the objective of a transformation is primarily to normalize the data (although variance homogeneity and linearization may also be beneficial side-effects) the success of a transformation is measured by whether or not it has improved the normality of the data. It is not measured by whether the outcome of a statistical analysis is more favorable!

End of instructions

  Selecting subsets of data

# select the first 5 entries in the variable
> variable[1:5]
# select all but the first entry in the variable
> variable[-1]
# select all cases of variable that are less than num
> variable[variablenum]
# select all cases of variable that are equal to 'Big'
> variable1[variablelabel]

where variable is the name of a numeric vector (variable) and num is a number. variable1 is the name of a character vector and label is a character string (word). 5. In the Subset expression box enter a logical statement that indicates how the data is to be subsetted. For example, exclude all values of DOC that are greater than 170 (to exclude the outlier) and therefore only include those values that are less that 170, enter DOC < 170. Alternatively, you could chose to exclude the outlier using its STREAM name. To exclude this point enter STREAM. != 'Santa Cruz'.

End of instructions

  Summary Statistics by groups

> tapply(dv,factor,func)

the tapply() function applies the function func to the numeric vector dv for each level of the factor factor. For example

# calculate the mean separately for each group
> tapply(dv,factor,mean)
# calculate the mean separately for each group
> tapply(dv,factor,var)

End of instructions

  EDA - Normal distribution

Parametric statistical hypothesis tests assume that the population measurements follow a specific distribution. Most commonly, statistical hypothesis tests assume that the population measurements are normally distributed (Question 4 highlights the specific reasoning for this).

While it is usually not possible to directly examine the distribution of measurements in the whole population to determine whether or not this requirement is satisfied or not (for the same reasons that it is not possible to directly measure population parameters such as population mean), if the sampling is adequate (unbiased and sufficiently replicated), the distribution of measurements in a sample should reflect the population distribution. That is a sample of measurements taken from a normally distributed population should be normally distributed.

Tests on data that do not meet the distributional requirements may not be reliable, and thus, it is essential that the distribution of data be examined routinely prior to any formal statistical analysis.


End of instructions

  Factorial boxplots

> boxplot(dv~factor,data=data)

where dv is the name of the numeric vector (dependent variable), factor is the name of the factor (categorical or factor variable) and data is the name of the data frame (data set). The '~' is used in formulas to represent that the left hand side is proportional to the right hand side.

End of instructions

  EDA - Linearity

The most common methods for analysing the relationship or association between variables.assume that the variables are linearly related (or more correctly, that they do not covary according to some function other than linear). For example, to examine for the presence and strength of the relationship between two variables (such as those depicted below), it is a requirement of the more basic statistical tests that the variables not be related by any function other than a linear (straight line) function if any function at all.

There is no evidence that the data represented in Figure (a) are related (covary) according to any function other than linear. Likewise, there is no evidence that the data represented in Figure (b) are related (covary) according to any function other than linear (if at all). However, there is strong evidence that the data represented in Figure (c) are not related linearly. Hence Figure (c) depicts evidence for a violation in the statistical necessity of linearity.


End of instructions

  EDA - Scatterplots

Scatterplots are constructed to present the relationships, associations and trends between continuous variables. By convention, dependent variables are arranged on the Y-axis and independent variables on the X-axis. The variable measurements are used as coordinates and each replicate is represented by a single point.

Figure (c) above displays a linear smoother (linear `line-of-best-fit') through the data. Different smoothers help identify different trends in data.


End of instructions

  Scatterplots

# load the 'car' library
> library(car)
# generate a scatterplot
> scatterplot(dv~iv,data=data)

where dv is the dependent variable, iv is the independent variable and data is the data frame (data set).

End of instructions

  Plotting y against x


The statement 'plot variable1 against variable2' is interpreted as 'plot y against x'. That is variable1 is considered to be the dependent variable and is plotted on the y-axis and variable2 is the independent variable and thus is plotted on the x-axis.

End of instructions

  Scatterplot matrix (SPLOM)

# make sure the car package is loaded > library(car)
> scatterplot.matrix(~var1 + var2 + var3, data=data, diagonal='boxplot')
#OR > pairs(~var1 + var2 + var3, data=data)

where var1, var2 etc are the names of numeric vectors in the data data frame (data set)

End of instructions

  EDA - Homogeneity of variance

Many statistical hypothesis tests assume that the populations from which the sample measurements were collected are equally varied with respect to all possible measurements. For example, are the growth rates of one population of plants (the population treated with a fertilizer) more or less varied than the growth rates of another population (the control population that is purely treated with water). If they are, then the results of many statistical hypothesis tests that compare means may be unreliable. If the populations are equally varied, then the tests are more likely to be reliable.
Obviously, it is not possible to determine the variability of entire populations. However, if sampling is unbiased and sufficiently replicated, then the variability of samples should be directly proportional to the variability of their respective populations. Consequently, samples that have substantially different degrees of variability provide strong evidence that the populations are likely to be unequally varied.
There are a number of options available to determine the likelihood that populations are equally varied from samples.
1. Calculate the variance or standard deviation of the populations. If one sample is more than 2 times greater or less than the other sample(s), then there is some evidence for unequal population variability (non-homogeneity of variance)
2. Construct boxplots of the measurements for each population, and compare the lengths of the boxplots. The length of a symmetrical (and thus normally distributed) boxplot is a robust measure of the spread of values, and thus an indication of the spread of population values. Therefore if any of the boxplots are more than 2 times longer or shorter than the other boxplot(s), then there is again, some evidence for unequal population variability (non-homogeneity of variance)


End of instructions

  t test

The frequentist approach to hypothesis testing is based on estimating the probability of collecting the observed sample(s) when the null hypothesis is true. That is, how rare would it be to collect samples that displayed the observed degree of difference if the samples had been collected from identical populations. The degree of difference between two collected samples is objectively represented by a t statistic.

Where y1 and y2 are the sample means of group 1 and 2 respectively and √s²/n1 + s²/n2 represents the degree of precision in the difference between means by taking into account the degree of variability of each sample.
If the null hypothesis is true (that is the mean of population 1 and population 2 are equal), the degree of difference (t value) between an unbiased sample collected from population 1 and an unbiased sample collected from population 2 should be close to zero (0). It is unlikely that an unbiased sample collected from population 1 will have a mean substantially higher or lower than an unbiased sample from population 2. That is, it is unlikely that such samples could result in a very large (positive or negative) t value if the null hypothesis (of no difference between populations) was true. The question is, how large (either positive or negative) does the t value need to be, before we conclude that the null hypothesis is unlikely to be true?.

What is needed is a method by which we can determine the likelihood of any conceivable t value when null hypothesis is true. This can be done via simulation. We can simulate the collection of random samples from two identical populations and calculate the proportion of all possible t values.

Lets say a vivoculturalist was interested in comparing the size of Eucalyptus regnans seedlings grown under shade and full sun conditions. In this case we have two populations. One population represents all the possible E. regnans seedlings grown in shade conditions, and the other population represents all the possible E. regnans seedlings grown in full sun conditions. If we had grown 200 seedlings under shade conditions and 200 seedlings under full sun conditions, then these samples can be used to assess the null hypothesis that the mean size of an infinite number (population) of E. regnans seedlings grown under shade conditions is the same as the mean size of an infinite number (population) of E. regnans seedlings grown under full sun conditions. That is that the population means are equal.

We can firstly simulate the sizes of 200 seedlings grown under shade conditions and another 200 seedlings grown under full sun conditions that could arise naturally when shading has no effect on seedling growth. That is, we can simulate one possible outcome when the null hypothesis is true and shading has no effect on seedling growth
Now we can calculate the degree of difference between the mean sizes of seedlings grown under the two different conditions taking into account natural variation (that is, we can calculate a t value using the formula from above). From this simulation we may have found that the mean size of seedling grown in shade and full sun was 31.5cm and 33.7cm respectively and the degree of difference (t value) was 0.25. This represents only one possible outcome (t value). We now repeat this simulation process a large number of times (1000) and generate a histogram (or more correctly, a distribution) of the t value outcomes that are possible when the null hypothesis is true.

It should be obvious that when the null hypothesis is true (and the two populations are the same), the majority of t values calculated from samples containing 200 seedlings will be close to zero (0) - indicating only small degrees of difference between the samples. However, it is also important to notice that it is possible (albeit less likely) to have samples that are quit different from one another (large positive or negative t values) just by pure chance (for example t values greater than 2).

It turns out that it is not necessary to perform these simulations each time you test a null hypothesis. There is a mathematical formulae to estimate the t distribution appropriate for any given sample size (or more correctly, degrees of freedom) when the null hypothesis is true. In this case, the t distribution is for (200-1)+(200-1)=398 degrees of freedom.

At this stage we would calculate the t value from our actual observed samples (the 200 real seedlings grown under each of the conditions). We then compare this t value to our distribution of all possible t values (t distribution) to determine how likely our t value is when the null hypothesis is true. The simulated t distribution suggests that very large (positive or negative) t values are unlikely. The t distribution allows us to calculate the probability of obtaining a value greater than a specific t value. For example, we could calculate the probability of obtaining a t value of 2 or greater when the null hypothesis is true, by determining the area under the t distribution beyond a value of 2.

If the calculated t value was 2, then the probability of obtaining this t value (or one greater) when the null hypothesis is true is 0.012 (or 1.2%). Since the probability of obtaining our t value or one greater (and therefore the probability of having a sample of 200 seedlings grown in the shade being so much different in size than a sample of 200 seedlings grown in full sun) is so low, we would conclude that the null hypothesis of no effect of shading on seedling growth is likely to be false. Thus, we have provided some strong evidence that shading conditions do effect seedling growth!


End of instructions

  t-test degrees of freedom

Degrees of freedom is the number of observations that are free to vary when estimating a parameter. For a t-test, df is calculated as
df = (n1-1)+(n2-1)
where n1 is population 1 sample size and n2 is the sample size of population 2.

End of instructions

  One-tailed critical t-value

One-tail critical t-values are just calculated from a t distribution (of given df). The area under the t distribution curve above (or below) this value is 0.05 (or some other specified probability). Essentially we calculate a quantile of a specified probability from a t distribution of df degrees of freedom

# calculate critical t value (&alpha =0.05) for upper tail (e.g. A larger than B)
> qt(0.05,df=df,lower.tail=F)
# calculate critical t value (&alpha =0.05) for lower tail (e.g. B larger than A)
> qt(0.05,df=df,lower.tail=T)

where 0.05 is the specified &alpha value and df is the specified degrees of freedom. Note that it is not necessary to have collected the data before calculating the critical t-value, you only need to know sample sizes (to get df).

It actually doesn't matter whether you select Lower tail is TRUE or FALSE. The t-distribution is a symmetrical distribution, centered around 0, therefore, the critical t-value is the same for both Lower tail (e.g. population 2 greater than population 1) and Upper tail (e.g. population 1 greater than population 2), except that the Lower tail is always negative. As it is often less confusing to work with positive values, it is recommended that you use Upper tail values. An example of a t-distribution with Upper tail for a one-tailed test is depicted below. Note that this is not part of the t quantiles output!


End of instructions

  Two-tailed critical t-value

Two-tail critical t-values are just calculated from a t distribution (of given df). The area under the t distribution curve above and below the positive and negative of this value respectively is 0.05 (or some other specified probability). Essentially we calculate a quantile of a specified probability from a t distribution of df degrees of freedom. In a two-tailed test, half of the probability is associated with the area above the positive critical t-value and the other half is associated with the area below the negative critical t-value. Therefore when we use the quantile to calculate this critical t-value, we specify the probability as &alpha/2 - since &alpha/2 applies to each tail.

# calculate critical t value (&alpha =0.05) for upper tail (e.g. A different to B)
> qt(0.05/2,df=df, lower.tail=T)

where 0.05 is the specified &alpha value and df is the specified degrees of freedom. Note that it is not necessary to have collected the data before calculating the critical t-value, you only need to know sample sizes (to get df).

Again, it actually doesn't matter whether you select Lower tail as either TRUE or FALSE. For a symmetrical distribution, centered around 0, the critical t-value is the same for both Lower tail (e.g. population 2 greater than population 1) and Upper tail (e.g. population 1 greater than population 2), except that the Lower tail is always negative. As it is often less confusing to work with positive values, it is recommended that you use Upper tail values. An example of a t-distribution with Upper tail for a two-tailed test is depicted below. Note that this is not part of the t quantiles output!

End of instructions

  Basic steps of Hypothesis testing


Step 1 - Clearly establish the statistical null hypothesis. Therefore, start off by considering the situation where the null hypothesis is true - e.g. when the two population means are equal

Step 2 - Establish a critical statistical criteria (e.g. alpha = 0.05)

Step 3 - Collect samples consisting of independent, unbiased samples

Step 4 - Assess the assumptions relevant to the statistical hypothesis test. For a t test:
  1.  Normality
  2.  Homogeneity of variance

Step 5 - Calculate test statistic appropriate for null hypothesis (e.g. a t value)


Step 6 - Compare observed test statistic to a probability distribution for that test statistic when the null hypothesis is true for the appropriate degrees of freedom (e.g. compare the observed t value to a t distribution).

Step 7 - If the observed test statistic is greater (in magnitude) than the critical value for that test statistic (based on the predefined critical criteria), we conclude that it is unlikely that the observed samples could have come from populations that fulfill the null hypothesis and therefore the null hypothesis is rejected, otherwise we conclude that there is insufficient evidence to reject the null hypothesis. Alternatively, we calculate the probability of obtaining the observed test statistic (or one of greater magnitude) when the null hypothesis is true. If this probability is less than our predefined critical criteria (e.g. 0.05), we conclude that it is unlikely that the observed samples could have come from populations that fulfill the null hypothesis and therefore the null hypothesis is rejected, otherwise we conclude that there is insufficient evidence to reject the null hypothesis.

End of instructions

  Pooled variance t-test

> t.test(dv~factor,data=data,var.equal=T)

where dv is the name of the dependent variable, factor is the name of the categorical/factorial variable and data is the name of the data frame (data set). The argument var.equal=T indicates a pooled variances t-test

End of instructions

  Separate variance t-test

> t.test(dv~factor,data=data,var.equal=F)

where dv is the name of the dependent variable, factor is the name of the categorical/factorial variable and data is the name of the data frame (data set). The argument var.equal=F indicates a separate variances t-test

End of instructions

  Output of t-test


The following output are based on a simulated data sets;
1.  Pooled variance t-test for populations with equal (or nearly so) variances

2.  Separate variance t-test for population with unequal variances

End of instructions

  Paired t-test

> t.test(cat1, cat2, data=data, paired=T)

where cat1 and cat2 are two numeric vectors (variables) in the data data frame (data set). The argument paired=T indicates a paired t-test)

End of instructions

  Non-parametric tests

Non-parametric tests do not place any distributional limitations on data sets and are therefore useful when the assumption of normality is violated. There are a number of alternatives to parametric tests, the most common are;

1. Randomization tests - rather than assume that a test statistic calculated from the data follows a specific mathematical distribution (such as a normal distribution), these tests generate their own test statistic distribution by repeatedly re-sampling or re-shuffling the original data and recalculating the test statistic each time. A p value is subsequently calculated as the proportion of random test statistics that are greater than or equal to the test statistic based on the original (un-shuffled) data.

2. Rank-based tests - these tests operate not on the original data, but rather data that has first been ranked (each observation is assigned a ranking, such that the largest observation is given the value of 1, the next highest is 2 and so on). It turns out that the probability distribution of any rank based test statistic for a is identical.

End of instructions

  Wilcoxon test

> wilcox.test(dv ~ factor, data=data)

where dv is the dependent variable and factor is the categorical variable from the data data frame (data set).

End of instructions

  Randomization (permutation) tests

The basis of hypothesis testing (when comparing two populations) is to determine how often we would expect to collect a specific sample (or more one more unusual) if the populations were identical. That is, would our sample be considered unusual under normal circumstances? To determine this, we need to estimate what sort of samples we would expect under normal circumstances. The theoretical t-distribution mimics what it would be like to randomly resample repeatedly (with a given sample size) from such identical populations.

A single sample from the (proposed) population(s) provides a range of observations that are possible. Completely shuffling those data (or labels), should mimic the situation when there is no effect (null hypothesis true situation), since the data are then effectively assigned randomly with respect to the effect. In the case of a t-test comparing the means of two groups, randomly shuffling the data is likely to result similar group means. This provides one possible outcome (t-value) that might be expected when the null hypothesis true. If this shuffling process is repeated multiple times, a distribution of all of the outcomes (t-values) that are possible when the null hypothesis is true can be generated. We can now determine how unusual our real (unshuffled) t-value is by comparing it to the built up t-distribution. If the real t-value is expected to occur less than 5% of the time, we reject the null hypothesis.

Randomization tests - rather than assume that a test statistic calculated from the data follows a specific mathematical distribution (such as a normal distribution), these tests generate their own test statistic distribution by repeatedly re-sampling or re-shuffling the original data and recalculating the test statistic each time. A p value is subsequently calculated as the proportion of random test statistics that are greater than or equal to the test statistic based on the original (un-shuffled) data.

End of instructions

  Defining the randomization statistic

> stat <- function(data, indices){
+ t <- t.test(data[,2]~data[,1])$stat
+ t
+ }
The above function takes a data set (data) and calculates a test statistic (in this case a t-statistic). The illustrated function uses the t.test() function to perform a t-test on column 2 ([,2]) against column 1 ([,1]). The value of the t-statistic is stored in an object called 't'. The function returns the value of this object. The indentations are not important, they are purely used to improve human readability.

End of instructions

  Defining the data shuffling procedure

> rand.gen <- function(data, mle){
+ out <- data
+ out[,1] <- sample(out[,1], replace=F)
+ out
+ }
The above function defines how the data should be shuffled. The first line creates a temporary object (out) that stores the original data set (data) so that any alterations do not effect the original data set. The second line uses the sample() function to shuffle the first column (the group labels). The third line of the function just returns the shuffled data set.

End of instructions

  Perform randomization test (bootstrapping)

> library(boot)
> coots.boot <- boot(coots, stat, R=100, sim="parametric", ran.gen=rand.gen)
Error: object 'coots' not found
The sequence begins by ensuring that the boot package has been loaded into memory. Then the boot() function is used to repeatedly (R=100: repeat 100 times) perform the defined statistical procedure (stat). The ran.gen=rand.gen parameter defines how the data set should be altered prior to performing the statistical procedure, and the sim="parametric" parameter indicates that all randomizations are possible (as opposed to a permutation where each configuration can only occur once). The outcome of the randomization test (bootstrap) is stored in an object called coots.boot

End of instructions

  Calculating p-value from randomization test

> p.length <- length(coots.boot$t[coots.boot$t >= coots.boot$t0])+1
Error: object 'coots.boot' not found
> print(p.length/(coots.boot$R + 1))
Error: object 'p.length' not found
The coots.boot object contains a list of all of the t-values calculated from the resampling (shuffling and recalculating) procedure (coots.boot$t)as well as the actual t-value from the actual data set (coots.boot$t0). It also stores the number of randomizations that it performed (coots.boot$R).

The first line in the above sequence determines how many of the possible t values (including the actual t-value) are greater or equal to the actual t-value. It does this by specifying a list of coots.boot$t values that are greater or equal to the coots.boot$t0 value. (coots.boot$t[coots.boot$t >= coots.boot$t0]) and calculating the length of this list using the length() function. One (1) is added to this length, since the actual t-value must be included as a possible t-value.
The second line expresses the number of randomization t-values greater or equal to the actual t-value as a fraction of the total number of randomizations (again adding 1 to account for the actual situation). This is interpreted as any other p-value - the probability of obtaining our sample statistic (and thus our observations) when the null hypothesis is true.

End of instructions

  Calculating p-value from randomization test

In a t-test, the effect size is the absolute difference between the expected population means. Therefore if the expected population means of population A and B are 10 and 16 respectively, then the effect size is 6.

Typically, effect size is estimated by considering the magnitude of an effect that is either likely or else what magnitude of effect would be regarded biologically significant. For example, if the overall population mean was estimated to be 12 and the researches regarded a 15% increase to be biologically significant, then the effect size is the difference between 12 and a mean 15% greater than 12 (12+(12*.15)=13.8). Therefore the effect size is (13.8-12=1.8).

End of instructions