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

Monday, 28 April 2014

David Warton wins Young Investigator Award from American Statistical Association


We're all very proud that David has won another major award this year, this one the Young Investigator Award from the American Statistical Association Section on Statistics and the Environment. He's been recognized internationally for outstanding contributions to the development of methods, issues, concepts, applications, and initiatives in environmental statistics by a young statistician. And we quite agree.

Well done David!

More on the school website:
https://www.maths.unsw.edu.au/news/2014-04/david-warton-young-investigator-award

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.

MAXENT equivalence paper rated "Exceptional" on Faculty of 1000

The first paper from Ian Renner's PhD thesis, "Equivalence of MAXENT and Poisson point process models for species distribution modeling in ecology", has been rated by the Faculty of 1000.  F1000 is a post-publication peer review website that highlights noteworthy articles from the scientific literature, especially biology and medicine articles.  Renner & Warton (2013) received the top rating (three stars, "Exceptional"), which we are pretty chuffed about.  For details, see the review at http://f1000.com/prime/718270492

Sunday, 2 February 2014

Eco-Stats Paper of the Year, 2013

We just had our second annual paper of the year competition, highlighting papers that made an impression to UNSW Eco-Stats researchers over the previous year.  Papers were supposed to be in print in 2013, but this was interpreted generously. And the nominees are...