ESCI 340 computer lab: Friday 29 January 2016 Paired-sample t-test, Wilcoxon test, Single-factor Analysis of Variance > # Nonparametric paired-sample test (Wilcoxon) > dbh.edge <- c(87.5, 95.1, 75, 88.3, 83, 92.7, 88.5, 96, 118.9, 62.5) > dbh.int <- c(115.2, 80, 91.6, 63, 112, 107, 52.1, 102.4, 99, 68.0) > m.up <- c(70, 100, 95, 70, 90, 50, 60, 90, 90, 1) > m.down <- c(0, 1, 2, 15, 0, 3, 20, 0, 5, 0) > m.up <- sort(m.up) > m.down <- sort(m.down) > m.up [1] 1 50 60 70 70 90 90 90 95 100 > m.down [1] 0 0 0 0 1 2 3 5 15 20 > m.up <- c(70, 100, 95, 70, 90, 50, 60, 90, 90, 1) > m.down <- c(0, 1, 2, 15, 0, 3, 20, 0, 5, 0) > m.diff <- m.up - m.down > m.diff [1] 70 99 93 55 90 47 40 90 85 1 > m.diff2 <- sort(m.diff) > m.diff2 [1] 1 40 47 55 70 85 90 90 93 99 > # Example with ties among + and - differences > m.diff3 <- m.diff2 > m.diff3[6] <- -90 > m.diff3 [1] 1 40 47 55 70 -90 90 90 93 99 > m.diff4 <- m.diff3 > m.up2 <- c(70, 100, 95, 70, 90, 50, 60, 90, 90, 1) > m.down2 <- c(0, 1, 2, 70, 0, 3, 20, 0, 5, 0) > m.diff5 <- m.up2 - m.down2 > m.diff5 [1] 70 99 93 0 90 47 40 90 85 1 > m.diff6 <- m.diff5 > m.diff6[5] <- -90 > m.diff6 [1] 70 99 93 0 -90 47 40 90 85 1 > sort(abs(m.diff6)) [1] 0 1 40 47 70 85 90 90 93 99 > m.diff7 <- sort(abs(m.diff6)) > m.diff7[7] <- -90 > m.diff7 [1] 0 1 40 47 70 85 -90 90 93 99 > hist(m.diff) # ******************************************************************** > # Single factor Analysis of Variance > # Use avian scavenger count data, collected Wed 26 Jan. 2016 > # Location data: > # Two groups reported data from 4 "food" sites: > # 0,0,3,0 and 0,1,5,0 > # Use average values: 0, 0.5, 4, 0 > food <- c(0,0.5,4,0) > open <- c(0,0,0,0) > nofood <- c(6,2,3,0) > # Calculate means for each sample > m.food <- mean(food) > m.open <- mean(open) > m.nf <- mean(nofood) > m.food [1] 1.125 > m.open [1] 0 > m.nf [1] 2.75 > # Combine all samples; calculate grand mean > all <- c(food, open, nofood) > m.a <- mean(all) > m.a [1] 1.291667 > # Single Factor ANOVA; calculate Sums of Squares > # Total SS > sst <- 11*var(all) > sst [1] 45.22917 > # Groups SS = Sum( n_i * (mean_i - mean_grand)^2 ) > # Equivalently, = (k-1) * n * var(group means) > m3 <- c(m.food,m.open, m.nf) > m3 [1] 1.125 0.000 2.750 > ssg <- 2*4*var(m3) > ssg [1] 15.29167 > # error SS (within groups) > sse <- sst - ssg > sse [1] 29.9375 > # Calculate Mean Square > # Groups MS > msg <- ssg / 2 > msg [1] 7.645833 > # Error MS > mse <- sse / 9 > mse [1] 3.326389 > # F_calc = Groups MS / Error MS > fcalc <- msg / mse > fcalc [1] 2.298539 > # Compare with F_alpha(1),2,9 > # 0.25 > P > 0.10 > # *************************************************** > # Repeat, using R's Analysis of Variance function > site <- rep(c("food", "open", "nofood"), c(4,4,4)) > site <- factor(site) > bird.aov <- aov(all ~ site) > anova(bird.aov) Analysis of Variance Table Response: all Df Sum Sq Mean Sq F value Pr(>F) site 2 15.292 7.6458 2.2985 0.1562 Residuals 9 29.938 3.3264 > # Note: same values for MS, F; precise P-value # ********************************************************* # Tukey Multiple Comparisons test summary(bird.aov) TukeyHSD(bird.aov) plot(TukeyHSD(bird.aov)) # ********************************************************* # R commands w/ results > # Tukey Multiple Comparisons test > summary(bird.aov) Df Sum Sq Mean Sq F value Pr(>F) site 2 15.29 7.646 2.299 0.156 Residuals 9 29.94 3.326 > TukeyHSD(bird.aov) Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = all ~ site) $site diff lwr upr p adj nofood-food 1.625 -1.975707 5.2257071 0.4505040 open-food -1.125 -4.725707 2.4757071 0.6700270 open-nofood -2.750 -6.350707 0.8507071 0.1379151 > plot(TukeyHSD(bird.aov))