Showing posts with label Eco-Stats Labs. Show all posts
Showing posts with label Eco-Stats Labs. Show all posts

Friday, 12 June 2015

It's boral time!

Details for the next R-lab are now available:
Day: Friday 19th June 2pm (it's been pushed a week early for conference reasons)
Location: Bioscience Level 6
Topic: boral -- A R package for bayesian analysis of multivariate abundance data in ecology
Links: 
1) Presentation slides Go here
2) R script: Go here

Notes: Please be aware that we will be using the MCMC package JAGS, as well as the R package boral and mvabund. If you are using your own laptop, then you could save some time by installing those prior to coming. Thanks!

FH

Wednesday, 27 May 2015

Missing Data Analysis

The May Eco-Stats Lab (Friday 29th, 2pm, Bioscience level 6) will be on the missing data analysis, using the method of Multiple Imputation.

One often encounters missing data in almost all types of studies. Ecological data is also commonly subject to missing data. However, most of the statistical analysis methods are designed for complete datasets. A common way to handle missing data is to remove cases with missing values in order to obtain a complete dataset, which reduces the sample size and thus the statistical power. This approach can result in biased estimates for descriptive statistics and regression coefficients as well. An alternative approach is to impute (fill-in) the missing data by plausible values multiple times, analyse each imputed dataset separately, and then combine the results together. This method is called Multiple Imputation (MI) and was proposed by Rubin (1987).

In this lab we will explore the method of MI implemented in the mice package (van Buuren & Groothuis-Oudshoorn, 2011), which stands for multivariate imputation by chained equations. For more details see
http://www.jstatsoft.org/v45/i03/

Click here

Monday, 20 April 2015

Zero inflation in ecology


The April Eco-Stats Lab (Friday 24th, 2pm, Bioscience level 6) will be on zero inflated data in ecology. 

It's very common for ecological data to contain many zeros. To account for this we may need to:

1. Use zero inflated regression models
2. Do absolutely nothing (i.e. fit standard glm's)

In this lab we'll talk about why many zeros may occur in ecology, and the appropriate ways to account for them in your analysis.We will mostly use the pscl package in R.

Monday, 23 March 2015

Species traits in multivariate analysis

The March Eco-Stats Lab (Friday 27th, 2pm, Bioscience level 6) will be on incorporating species traits into multivariate analysis (or equivalently, community level models), using some functions recently added to the mvabund package.

Often ecologists collect data (especially abundance or presence-absence data) simultaneously across many taxa, with the intention of studying what occurs where (and why). This tutorial focusses on the why - methods to help us move towards a functional explanation of community abundances. McGill et al (2006) and Shipley (2010) argue passionately for the need for this.

A common strategy in any field looking at "why" is to look for predictor variables that can explain the response. In the case of studying why some taxa are abundant at a site while others are not, the relevant predictors are species traits. These come in a matrix, different traits in different columns, different taxa in different rows.

We will explore methods for using such a matrix of traits in a multivariate analysis, using the mvabund package (version 3.10.1 or later).  Full details here:
             http://rpubs.com/dwarton/68823
We will also discuss the relationship of these methods with standard analysis of multivariate abundance data, SDMs, and Bill Shipley's CATS models.

*** Note you need mvabund 3.10.1 or later... ***

UPDATE (13/5/15): mvabund 3.10.4 is now available from CRAN.  It has a formula argument for more control of the traitglm model you fit, and composition and col.intercepts arguments to control whether or not you include row/column effects in the model (to focus on relative abundance), and a block resampling option on anova.traitglm (useful for example if you have repeated measures).

Tuesday, 25 November 2014

(Generalised) linear (mixed) models with R2jags: an extremely brief primer...

The purpose of this lab (Friday 28th 2pm) is to provide a very basic primer on running basic lm, glm and glmms in a Bayesian framework using the R2jags package (and of course JAGS). As such, the goal is not to debate the relative merits of Bayesian vs frequentist approaches, but hopefully to demystify the fitting of Bayesian models, and more specifically demonstrate that in a wide variety of (more basic) use cases the parameter estimates obtained from the two approaches are typically very similar.

We will be attempting to reproduce a small element of the analysis from a recently published article in Journal of Ecology (for which all the data is available at datadryad.org).

Kessler, M., Salazar, L., Homeier, J., Kluge, J. (2014), Species richness-productivity relationships of tropical terrestrial ferns at regional and local scales. Journal of Ecology, 102: 1623-1633.

Code (jagstut.Rmd) and data (KessDiv.csv) available from dropbox or github. View the compiled html version here.

See you Friday,
Andrew


Monday, 27 October 2014

R-lab: Inference with Spatially correlated data with nlme

Hi guys,

Problem: You have data that may be spatially correlated, and you worry this might affect your inference (p-values and confidence intervals for fixed effects). 

 Solution : This Friday, 31st October at 2pm, R-lab on inference with Spatially correlated data with the nlme package.

Download R code and data here.

See you there,
Gordana


Thursday, 25 September 2014

September Eco-Stats lab: model-based multivariate analysis in ecology (mvabund package and recent additions)


What does mvabund do?
Analyses multivariate data (especially abundance of presence-absence data) using simultaneous univariate models and design-based inference.

The main functions are manyglm, which fits a GLM to each response variable, and anova/summary, which use row-resampling for valid multivariate inference (i.e. taking into account correlation between variables)

Designed specially for multivariate abundance data in ecology, species-by-site stuff, which has two key properties that need to be dealt with:
(1) strong mean-variance relationship.
(2) correlation between response variables (e.g. due to species interaction)

Why is mvabund better than using PRIMER, PC-ORD, etc?
A few reasons, see this R script for details (and code to work through):


Wednesday, 27 August 2014

R-lab on Mixed models in Ecology

Hi guys,

This Friday, 29th August at 2pm, there will be in an R-lab on mixed models in ecology, a topic which many of you are interested in digging deeper into. I've set up too links to dropbox for the slides as well as a csv file which we will be playing with (dataset courtesy of Sylvia Hay =D)

Thanks.

PDF slides
Example 1: Bird counts
Example 2: Nested design dataset

(Corrected) code for bird analysis

Yours non-significantly,
FH

Friday, 20 June 2014

Eco-Stats Lab June 2014: Phylogenetic model adequacy


In this lab (June 29th) I'll introduce a new package that we're developing called "arbutus" after a funny looking tree that grows in the Pacific Northwest.  The package is designed to test the adequacy of models of trait evolution on phylogenies.  We've started out looking at relatively simple models but it should be possible to test more and more complex models.

A few links:

1) the slides and code for the lab

2) the package repository

3) the pre-print of the paper

As this work is still in progress, any feedback on theory, usability or aesthetics is more than welcome!


Thursday, 29 May 2014

Eco-Stats Lab May 2014: Longitudinal data

In this week’s lab we will learn about analysing longitudinal data, that is, data collected over time for each study unit (e.g. subject or site). 


Standard (generalised) linear models assume all observations are independent, which is typically not reasonable if repeated measures have been taken over time.  Data collected for one site at times close together will be more similar than data for different sites, or for times further apart. This induces correlation in the data, which we must account for to obtain valid inference.

Normal data

#Load data
library(lme4)
library(nlme)
library(MASS)

data(BodyWeight)

#scatterplot by Diet
plot(weight~Time,col=Diet,data=BodyWeight)

#ignore all dependence, bad analysis
Rat.lm0=lm(weight~Time,data=BodyWeight)
Rat.lm=lm(weight~Time+Diet,data=BodyWeight)
summary(Rat.lm)
anova(Rat.lm0,Rat.lm)
plot(Rat.lm,which=1)
plot(Rat.lm$fitted,Rat.lm$residuals,col=BodyWeight$Rat)

Problem:
The effects of ignoring dependence vary with the type of dependence but in many cases you can expect
  • Standard errors are underestimated, giving “false confidence”
  • T-statistics will be overestimated, and regression coefficients that appear significant may not be


Solution 1 – mixed effects model with random intercept and slope

#plot data by Rat
interaction.plot(BodyWeight$Time, BodyWeight$Rat, BodyWeight$weight)


#model with random effects for intercept and slope
Rat.mixed0<- lmer(weight ~ Time + (1 + Time | Rat) , data = BodyWeight)
Rat.mixed<- lmer(weight ~Time +Diet+ (1 + Time | Rat) , data = BodyWeight)
anova(Rat.mixed0,Rat.mixed)

#There seems to be an effect of diet.

summary(Rat.mixed)
plot(Rat.mixed)
plot(fitted(Rat.mixed),residuals(Rat.mixed),col=BodyWeight$Rat)


This analysis assumes that after fitting a line for each subject, the observations for each subject have the same correlation, regardless of how far away they are in time.  This is not generally realistic; observations closer to each other in time might be more correlated than those further apart.

We can’t use lme4 for analysis which incorporates more flexible correlation structure, we will need to use library(nlme). The autocorrelation structure most commonly used for data correlated in time is autoregressive AR(1). It assumes data points close in time are more strongly correlated than those further apart in time.

Solution 2 – mixed effects model with flexible correlation structures

Rat.lme0 <-lme(weight ~  Time + Diet, random = ~ Time  | Rat, data = BodyWeight)
Rat.lme1 <-lme(weight ~  Time + Diet, random = ~ Time  | Rat, corr=corAR1(, form= ~ Time| Rat), data = BodyWeight)

AIC(Rat.lme0,Rat.lme1 )



#We can see that the AR1 correlation structure seems to work better as the AIC is smaller for this model.


Non Normal Data

What about non normal data? Well we can use the same set up as before in lme4, but now using the glmer function. 

data(epil)
interaction.plot(epil$period, epil$subject, epil$y)
interaction.plot(epil$period, epil$subject, log(epil$y+1),col=epil$subject)

#model with random effects for intercept and slope
Epil.mixed0<- glmer(y ~1 + period + (1 + period | subject) , data = epil,family=poisson)
Epil.mixed<- glmer(y ~1 + period +age+ (1 + period | subject) , data = epil,family=poisson)

# whoops, warning about convergence, let's not worry about it, it's not a problem in this case
# read http://stackoverflow.com/a/21370041 and http://stats.stackexchange.com/a/99719 if you have a similar problem


anova(Epil.mixed0,Epil.mixed)
                                                                                 
#There seems to be no effect of age. 

What if we want an AR(1) correlation structure instead? Well, it is more complicated, some options are glmmPQL in the MASS package, and geeglm in the geepack package. glmmPQL uses the lme function above, and has very similar syntax. geeglm does not fit quite the same model as lme4, but can also be used if you would like to fit an AR(1)  structure, or other more flexible correlation structures. 





Tuesday, 29 April 2014

Eco-Stats Lab April 2014: Block Bootstrap

In this weeks lab we learn about the block bootstrap. A non parametric way to deal with spatial auto correlation in your data and still make valid inferences.

Bootstrap Recap

Bootstrapping allows us to find the unknown distribution of a statistic by resampling the original data (with replacement) and recalculating the statistic many times.
Hence we can calculate p-values and standard errors of things we don’t know the distribution of.
Assumptions: observations are independent and identically distributed ("iid")


 But you can't use an iid bootstrap when data are spatially correlated

Wednesday, 26 March 2014

Eco-Stats Lab, March 2014 - Measurement Error modeling using SIMEX

Measurement Error modeling using SIMEX

Date: 28th March 2-3pm
Venue: Computer Lab Room 640

Slides:


Measurement Error Modeling

  • Measurement error or error-in-variables arises whenever we have imprecise measurements on our predictor variables (or covariates).
  • If X is the true covariate and U is the measurement error, then what we observe is
    W = X + U:
  • In a simple regression, we usually assume that our covariates are measured precisely or they represent the true covariate values quite well. But what happens to our estimates if our covariates have measurement error?

Monday, 24 February 2014

Eco-Stats Lab, Feb 2014 - SMATR

The SMATR package (Standardised Major Axis estimation and Testing Routines) is designed for when you are fitting lines and:
- you are primarily interested in the slope (rather than significance or strength of association)
- the problem is symmetric, i.e. you could happily swap which variable is on which axis without changing the meaning of what you are doing.  Or put another way, rather than predicting Y from X (regression), you have a pair of Y variables (Y1 and Y2) and you want to see how they are related to each other.

This situation commonly arises in allometry (the study of how one size variable scales against another), this is the main place these methods are useful in ecology.

Saturday, 25 January 2014

Ecostats Workshop - Mixture Models

To all those coming to the UNSW Ecostats workshop on Mixture models,

Date: 31st January 2-3pm
Venue: Computer Lab Room 640...somewhere in Sydney =D
Topic: A very very short introduction to mixture models with a very very short taste of how to implement them in R
MC: Francis Hui (PhD student; UNSW School of Maths and Stats)

Please note that this blog is NOT to be used for indicating that you want to attend. That should have been done via the email sent out by Richard Kingsford earlier.

Unfortunately, blogger does not allow one to attach thing that aren't videos or images, so I've instead provided links to the material I shall be using.

Slides: https://www.dropbox.com/s/0ibha7nk2u8k22u/minilecturev1.pdf?dl=0
R script: https://www.dropbox.com/s/kg3bcyt65ec6nhl/scripts_cutdown.R?dl=0



Thank you.

Yours non-significantly,
FH