Additional Final Exam Hints

Problem 2

The basic feature that defines these data is that the observations are paired. That makes this problem just like Problem 1 in that we have repeated measures data. The fact that there are only two measurement periods gives us some additional analysis options that we don't have in Problem 1.

I outline five different approaches to analyzing these data: paired t-test, Wilcoxon signed rank test, randomization test, linear model (randomized block design), and random intercepts model. Two of them (paired t-test and randomized block design) are the same method viewed from different perspectives. There are other possibilities beyond these five. As I've said in a previous hint, I don't care which one of these methods you decide to use even though some are clearly better than others.

Preliminaries

To illustrate the methodology I pull out the first species alphabetically from the data set, ACMI (Achillea millefolium), and analyze the change in its cover values.

> pat<-read.table('http://www.unc.edu/courses/2007spring/enst/562/001/assignments/final/coverclass.txt', header=T)
> acmi<-pat[pat$CHISSpCode=='ACMI',]

Method 1: Paired t-test

# paired t-test on raw data
> out1<-t.test(acmi[,4],acmi[,3],paired=TRUE,conf.level=.95)
> out1

Paired t-test

data: acmi[, 4] and acmi[, 3]
t = 1.3933, df = 11, p-value = 0.1911
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1449341 0.6449341
sample estimates:
mean of the differences
0.25

# one-sample t-test on differences
> out2<-t.test(acmi[,4]-acmi[,3],conf.level=.95)
> out2

One Sample t-test

data: acmi[, 4] - acmi[, 3]
t = 1.3933, df = 11, p-value = 0.1911
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.1449341 0.6449341
sample estimates:
mean of x
0.25

> table(acmi[,4]-acmi[,3])

-1 0 1
 1 7 4

Method 2: Wilcoxon signed rank test

> out3<-wilcox.test(acmi[,4], acmi[,3], paired=TRUE, conf.int=TRUE, conf.level=.95)
> out3

Wilcoxon signed rank test with continuity correction

data: acmi[, 4] and acmi[, 3]
V = 12, p-value = 0.2330
alternative hypothesis: true mu is not equal to 0
90 percent confidence interval:
-3.297723e-05 1.000000e+00
sample estimates:
(pseudo)median
0.9999193

> library(exactRankTests)
> out4<-wilcox.exact(acmi[,4], acmi[,3], paired=TRUE, conf.int=TRUE, conf.level=.95)
> out4

Exact Wilcoxon signed rank test

data: acmi[, 4] and acmi[, 3]
V = 12, p-value = 0.375
alternative hypothesis: true mu is not equal to 0
95 percent confidence interval:
-1 1
sample estimates:
(pseudo)median
0.5

[Additional information added 5/6/07]

#select COGI
> cover<-read.table('http://www.unc.edu/courses/2007spring/enst/562/001/assignments/final/coverclass.txt', header=T)
> cover[cover$CHISSpCode=="COGI",]->cogi
> cogi.diff<-cogi[,4]-cogi[,3]
#Walsh averages
> Wmat<-outer(cogi.diff,cogi.diff,'+')/2
> Walsh<-c(Wmat[lower.tri(Wmat)],diag(Wmat))
> cogi.median<-median(Walsh)

> Walsh.sort<-sort(Walsh)
# number of transects
> n.cogi<-dim(cogi)[1]
# obtain the positions
> q1<-n.cogi*(n.cogi+1)/2+1-qsignrank(.975,n.cogi)
> q2<-qsignrank(.975,n.cogi)
> cogi.lower95<-Walsh.sort[q1]
> cogi.upper95<-Walsh.sort[q2]
> c(cogi.median,cogi.lower95,cogi.upper95)
[1] 0.5 0.0 1.0

Method 3: Randomization test

> actdiff<-acmi[,4]-acmi[,3]
> perm.func<-function() {
#randomly assign signs
multiplier<-ifelse(runif(length(actdiff))<.5,-1,1)
mean(actdiff*multiplier) }

> out.perm<-replicate(9999,perm.func())
> alldiffs<-c(out.perm,mean(actdiff))
> 2*min(sum(alldiffs<=mean(actdiff)), sum(alldiffs>=mean(actdiff)))/length(alldiffs)

[1] 0.377

> perm.test(acmi[,3],acmi[,4],paired=TRUE)

1-sample Permutation Test

data: acmi[, 3] and acmi[, 4]
T = 1, p-value = 0.375
alternative hypothesis: true mu is not equal to 0

Fig. 1 Permutation distribution of paired differences

and

Then it follows that

and

Putting these two inequalities together yields the desired confidence interval.

> hist(alldiffs,col='grey90')
> points(mean(actdiff),0,pch=16,col=2)
> table(round(alldiffs,4))/length(alldiffs)

-0.4167  -0.25 -0.0833 0.0833   0.25 0.4167
 0.0316 0.1499  0.3162 0.3138 0.1591 0.0294

# define mean function for boot function
> mean.func<-function(x,indices) mean(x[indices])
> library(boot)
> boot(actdiff,mean.func,9999)->myboot
> myboot

ORDINARY NONPARAMETRIC BOOTSTRAP

Call:
boot(data = actdiff, statistic = mean.func, R = 9999)

Bootstrap Statistics :
original bias std. error
t1* 0.25 -0.001450145 0.1703088

> boot.ci(myboot)
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 9999 bootstrap replicates

CALL :
boot.ci(boot.out = myboot)

Intervals :
Level     Normal                Basic
95%   (-0.0823, 0.5852 ) (-0.0833, 0.5833 )

Level    Percentile              BCa
95%   (-0.0833, 0.5833 ) (-0.1667, 0.5000 )
Calculations and Intervals on Original Scale

Method 4: Linear model—randomized block design

> new.acmi<-data.frame(rep(acmi$PlotCode,2), c(acmi$Coverclass.1983, acmi$Coverclass.2002), rep(c(1983,2002), c(dim(acmi)[1], dim(acmi)[1])))
> colnames(new.acmi)<-c('Plot', 'Cover', 'Year')

> lm(Cover~factor(Year)+factor(Plot), data=new.acmi)->out4
> summary(out4)

Call:
lm(formula = Cover ~ factor(Year) + factor(Plot), data = new.acmi)

Residuals:
       Min         1Q     Median        3Q       Max
-6.250e-01 -1.250e-01 -3.469e-17 1.250e-01 6.250e-01

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)
(Intercept)      8.750e-01  3.235e-01  2.705    0.0205 *
factor(Year)2002 2.500e-01  1.794e-01  1.393    0.1911
factor(Plot)602  4.204e-17  4.395e-01  9.57e-17 1.0000
factor(Plot)610 -4.080e-18  4.395e-01 -9.28e-18 1.0000
factor(Plot)619  4.247e-17  4.395e-01  9.66e-17 1.0000
factor(Plot)625 -2.498e-16  4.395e-01 -5.68e-16 1.0000
factor(Plot)723 -5.000e-01  4.395e-01 -1.138    0.2795
factor(Plot)724 -5.000e-01  4.395e-01 -1.138    0.2795
factor(Plot)727  4.730e-16  4.395e-01  1.08e-15 1.0000
factor(Plot)731 -5.000e-01  4.395e-01 -1.138    0.2795
factor(Plot)735 -5.000e-01  4.395e-01 -1.138    0.2795
factor(Plot)742  9.070e-17  4.395e-01  2.06e-16 1.0000
factor(Plot)750 -5.000e-01  4.395e-01 -1.138    0.2795
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.4395 on 11 degrees of freedom
Multiple R-Squared: 0.4632, Adjusted R-squared: -0.1225
F-statistic: 0.7908 on 12 and 11 DF, p-value: 0.6546

> coef(out4)[2]- qt(.975, out4$df.residual)*sqrt(vcov(out4)[2,2])
factor(Year)2002
-0.1449341

> coef(out4)[2]+ qt(.975, out4$df.residual)*sqrt(vcov(out4)[2,2])
factor(Year)2002
0.6449341

Method 5: Random intercepts model

> library(nlme)
> lme(Cover~factor(Year),random=~1|Plot, data=new.acmi,method='ML')->out5
> summary(out5)

Linear mixed-effects model fit by maximum likelihood
Data: new.acmi
    AIC      BIC   logLik
30.4668 35.17902 -11.2334

Random effects:
Formula: ~1 | Plot
        (Intercept)  Residual
StdDev: 9.31861e-06 0.3864008

Fixed effects: Cover ~ factor(Year)
                     Value Std.Error DF t-value  p-value
(Intercept)      0.6666667 0.1165042 11 5.722254  0.0001
factor(Year)2002 0.2500000 0.1647618 11 1.517342  0.1574
Correlation:
                 (Intr)
factor(Year)2002 -0.707

Standardized Within-Group Residuals:
       Min        Q1       Med        Q3       Max
-2.3723210 0.2156655 0.2156655 0.8626622 0.8626622

Number of Observations: 24
Number of Groups: 12

> fixef(out5)[2]- qt(.975, out5$fixDF$X[2])*sqrt(vcov(out5)[2,2])
factor(Year)2002
-0.0971998

> fixef(out5)[2]+ qt(.975, out5$fixDF$X[2])*sqrt(vcov(out5)[2,2])
factor(Year)2002
0.5971998

Summary

Method
p-value for test
95% confidence interval for mean difference
paired t-test
0.191
(–0.145, 0.645)
wilcoxon signed rank
0.233
 
wilcoxon signed rank (exact)
0.375
 
permutation test
0.375
(–0.167, 0.667)
bootstrap (basic, percentile)
(–0.083, 0.583)
bootstrap (BCa)
(–0.167, 0.500)
fixed effect randomized block model (RBD)
0.191
(–0.145, 0.645)
random intercepts model
0.157
(–0.097, 0.597)

Course Home Page


Jack Weiss
Phone: (919) 962-5930
E-Mail: jack_weiss@unc.edu
Address: Curriculum in Ecology, Box 3275, University of North Carolina, Chapel Hill, 27516
Copyright © 2007
Last Revised--May 4, 2007
URL: http://www.unc.edu/courses/2007spring/enst/562/001/docs/assignments/final.htm