Skip to content

Instantly share code, notes, and snippets.

@gavinsimpson
Last active February 3, 2021 18:57
Show Gist options
  • Save gavinsimpson/bf11b821be44fc24e30ff8546ad8ca9d to your computer and use it in GitHub Desktop.
Save gavinsimpson/bf11b821be44fc24e30ff8546ad8ca9d to your computer and use it in GitHub Desktop.
Analyse transect data from Shultz et al (2016) PeerJ
## Analyse full transect data of J.A. Shultz et al (2016) PeerJ 4:e1980; DOI 10.7717/peerj.1980
## https://doi.org/10.7717/peerj.1980
## packages
library('readr')
library('dplyr')
library('ggplot2')
library('tidyr')
library('lme4')
library("ggstance")
## ggplot2 theme
theme_set(theme_bw())
## read in the data
transects <- read_csv("transectcounts.csv")
## transects in long form
lt <- gather(transects, species, count, - ssws, - site, - transect)
lt
## convert to factors for modelling
lt <- mutate(lt,
ssws = factor(ssws, levels = c('before', 'after')),
site = factor(site),
transect = factor(transect))
## subset down to the two species of interest
spp <- filter(lt, species %in% c('sunflower.star', 'green.urchin'))
## update the species names
spp <- mutate(spp, species = case_when(species == 'sunflower.star' ~ 'Sunflower Star',
species == 'green.urchin' ~ 'Green Urchin'),
species = factor(species, levels = c('Sunflower Star', 'Green Urchin')))
## Model fitting - sunflower
m.sun <- glmer(count ~ ssws + (1 | site / transect), data = spp, family = poisson,
subset = species == 'Sunflower Star')
m0.sun <- update(m.sun, . ~ . - ssws)
## clear support for an effect
anova(m.sun, m0.sun)
## model fitting - green urchin
m.gur <- glmer(count ~ ssws + (1 | site / transect), data = spp, family = poisson,
subset = species == 'Green Urchin')
m0.gur <- update(m.gur, . ~ . - ssws)
## clear support for an effect
anova(m.gur, m0.gur)
## Let's assume these are good models -
## should really do a lot of diagnostics...
## Extract data from the model for plotting
ilink <- family(m.sun)$linkinv
effs.s <- tibble(species = 'Sunflower Star', ssws = c('before', 'after'), fitted = fixef(m.sun),
se = diag(vcov(m.sun)))
effs.g <- tibble(species = 'Green Urchin', ssws = c('before', 'after'), fitted = fixef(m.gur),
se = diag(vcov(m.gur)))
effs <- bind_rows(effs.s, effs.g)
effs <- mutate(effs, est = ilink(fitted), upper = ilink(fitted + (2*se)),
lower = ilink(fitted - (2*se)),
species = factor(species, levels = c('Sunflower Star','Green Urchin')),
ssws = factor(ssws, levels = c('before', 'after')))
## base plot
base.plt <- ggplot() +
facet_wrap(~ species, scales = 'free_y') +
labs(x = 'Sea star mortality event')
## plot of effects
eff.plt <- base.plt +
geom_pointrange(aes(y = est, x = ssws, ymax = upper, ymin = lower), data = effs) +
labs(y = 'Expected count')
eff.plt
ggsave('model-effects.png', eff.plt, width = 7, height = 3.5)
## plot of effects on data
data.plt <- base.plt +
geom_point(aes(y = count, x = ssws), data = spp, alpha = 0.2,
position = position_jitter(width = 0.2)) +
geom_pointrange(aes(y = est, x = ssws, ymax = upper, ymin = lower, colour = ssws),
data = effs) +
theme(legend.position = 'none') +
labs(y = 'Number of individuals')
data.plt
ggsave('data-plus-effects-original-scale.png', data.plt, width = 7, height = 3.5)
## Data on sqrt scale
data.plt + scale_y_sqrt(minor_breaks = NULL) +
expand_limits(y = 0)
ggsave('data-plus-effects-sqrt-scale.png', width = 7, height = 3.5)
ssws site transect california.cucumber dungeness.crab green.urchin grunt.sculpin longfin.sculpin misc.crab octopus orange.cup.coral red.rock.crab red.urchin sailfin.sculpin scalyhead.sculpin sculpin shrimp spot.prawn squat.lobster sunflower.star white.urchin
after BI4 BI4-A 1 0 0 0 1 0 0 7 0 0 0 1 0 15 0 0 0 1
after BI4 BI4-B 6 0 0 1 5 2 0 11 0 0 0 0 0 6 0 0 0 0
after BI4 BI4-C 2 0 0 0 2 0 0 5 2 0 0 0 0 3 0 0 0 0
after BI4 BI4-D 2 0 0 0 9 4 0 15 0 0 0 0 0 5 0 2 0 0
before BI4 BI4-A 1 0 0 0 0 1 0 1 0 0 0 0 0 77 0 0 0 0
before BI4 BI4-B 2 0 2 0 0 1 0 0 0 0 0 2 0 16 0 0 0 0
before BI4 BI4-C 5 0 0 0 0 2 0 0 0 0 1 1 0 11 0 0 0 0
before BI4 BI4-D 1 0 1 1 0 0 0 0 0 0 0 3 0 33 0 1 0 0
after BI6 BI6-A 6 0 0 0 1 1 0 12 0 0 0 0 0 8 0 0 0 1
after BI6 BI6-B 3 0 2 0 1 4 0 16 0 0 0 0 0 1 0 0 0 0
after BI6 BI6-C 0 0 83 0 0 1 0 7 0 0 0 0 0 0 0 0 0 0
after BI6 BI6-D 0 0 575 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0
before BI6 BI6-A 1 0 17 0 0 0 0 2 0 1 0 0 2 0 0 0 0 0
before BI6 BI6-B 0 0 84 0 0 2 0 0 0 0 0 0 6 2 0 0 0 0
before BI6 BI6-C 0 0 11 0 0 0 0 0 0 2 0 0 0 1 0 0 0 0
before BI6 BI6-D 3 0 61 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
after BICN1 BICN1-A 0 0 0 0 0 0 0 5 0 0 0 1 0 3 0 1 0 0
after BICN1 BICN1-B 4 0 0 0 2 2 0 8 0 0 0 0 0 12 0 0 0 0
after BICN1 BICN1-C 2 0 0 0 0 2 0 3 0 0 0 0 0 5 0 0 0 0
after BICN1 BICN1-D 5 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
before BICN1 BICN1-A 2 0 0 0 0 6 0 15 0 0 0 0 0 13 0 4 1 1
before BICN1 BICN1-B 2 0 0 0 0 3 0 0 0 0 0 0 2 6 0 0 1 0
before BICN1 BICN1-C 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 0
before BICN1 BICN1-D 0 0 0 0 0 5 0 0 0 0 0 0 1 14 0 0 2 0
after BICN3 BICN3-A 2 0 0 0 0 2 0 14 0 0 0 1 2 4 0 0 0 0
after BICN3 BICN3-B 5 0 0 0 0 1 0 18 0 0 0 0 0 9 0 0 0 0
after BICN3 BICN3-C 2 0 0 0 1 0 0 2 0 0 0 0 2 1 0 0 0 0
after BICN3 BICN3-D 2 0 0 0 0 0 0 0 2 0 0 0 0 7 0 0 0 0
before BICN3 BICN3-A 1 0 0 0 0 0 0 12 0 0 0 0 0 3 0 1 0 0
before BICN3 BICN3-B 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
before BICN3 BICN3-C 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0
before BICN3 BICN3-D 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 4
after LB2 LB2-A 2 0 0 0 0 2 0 0 0 0 0 0 0 12 0 0 0 0
after LB2 LB2-B 3 0 0 0 0 0 0 0 0 0 0 1 0 4 0 0 0 0
after LB2 LB2-C 4 0 13 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0
after LB2 LB2-D 4 0 0 0 1 0 0 0 0 0 0 0 0 4 0 0 0 0
before LB2 LB2-A 3 0 0 0 0 1 0 0 0 0 0 0 0 64 1 13 0 0
before LB2 LB2-B 7 0 1 0 0 1 0 0 0 0 0 4 3 10 0 0 3 0
before LB2 LB2-C 1 0 6 0 0 2 0 0 0 0 0 0 2 12 1 3 0 0
before LB2 LB2-D 2 0 2 0 0 9 1 0 0 0 0 0 4 12 0 0 4 0
after LB5 LB5-A 16 0 6 0 0 3 0 7 0 0 0 0 0 2 0 0 0 0
after LB5 LB5-B 6 0 3 0 2 2 0 1 0 0 0 0 0 2 0 0 0 0
after LB5 LB5-C 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
after LB5 LB5-D 3 0 2 0 4 6 0 13 0 0 0 0 0 2 0 0 1 0
before LB5 LB5-A 4 0 0 0 0 2 0 4 0 3 0 0 0 1 0 0 0 0
before LB5 LB5-B 2 0 7 0 0 1 0 0 0 0 0 0 1 2 0 0 0 0
before LB5 LB5-C 0 0 1 0 1 0 0 43 0 0 0 0 0 1 0 0 1 5
before LB5 LB5-D 1 0 4 0 0 2 0 23 0 0 0 1 0 1 0 0 0 1
after LBCN2 LBCN2-A 0 0 1 0 0 0 0 0 0 0 0 1 0 4 0 0 0 1
after LBCN2 LBCN2-B 1 0 2 0 0 0 0 5 0 0 0 0 0 3 0 0 0 0
after LBCN2 LBCN2-C 2 0 46 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
after LBCN2 LBCN2-D 14 0 48 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
before LBCN2 LBCN2-A 12 0 0 0 0 1 0 13 0 0 0 0 0 20 0 0 0 0
before LBCN2 LBCN2-B 3 1 8 0 0 13 0 0 0 0 0 0 1 0 0 0 1 0
before LBCN2 LBCN2-C 19 0 0 0 0 1 0 2 0 0 0 1 0 9 1 1 0 0
before LBCN2 LBCN2-D 6 0 4 0 0 0 0 0 0 0 0 0 1 0 0 35 4 0
after LBCN3 LBCN3-A 5 0 25 0 5 1 0 24 0 0 0 1 0 1 0 0 0 1
after LBCN3 LBCN3-B 0 0 47 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
after LBCN3 LBCN3-C 8 0 1 0 3 2 0 0 0 0 0 0 0 6 0 0 0 0
after LBCN3 LBCN3-D 6 0 1 0 0 1 0 2 0 0 0 0 0 1 0 0 0 0
before LBCN3 LBCN3-A 4 0 4 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0
before LBCN3 LBCN3-B 8 0 4 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0
before LBCN3 LBCN3-C 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 14 0 0
before LBCN3 LBCN3-D 1 0 4 0 0 0 0 0 0 0 0 0 0 7 4 1 0 0
after PASS1 PASS1-A 0 0 14 0 1 3 0 12 0 0 0 0 0 0 0 0 0 0
after PASS1 PASS1-B 0 0 101 0 0 55 0 0 0 0 0 0 1 0 0 0 0 0
after PASS1 PASS1-C 2 0 5 0 0 1 0 43 0 0 0 0 0 4 0 0 0 0
after PASS1 PASS1-D 6 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
before PASS1 PASS1-A 1 0 17 0 0 6 0 0 0 0 0 0 0 5 0 0 1 0
before PASS1 PASS1-B 1 0 10 0 0 31 0 0 0 0 0 0 10 6 0 0 1 0
before PASS1 PASS1-C 0 0 15 0 0 5 0 0 0 0 0 0 2 2 0 0 0 0
before PASS1 PASS1-D 0 0 34 0 0 8 0 0 0 0 0 0 1 3 0 0 0 0
after PASS3 PASS3-A 1 0 9 0 0 25 0 6 0 0 0 1 2 10 0 0 0 0
after PASS3 PASS3-B 7 0 67 0 0 8 0 0 0 0 0 0 0 1 0 0 0 0
after PASS3 PASS3-C 6 0 1 0 0 9 0 0 0 0 0 1 0 1 0 0 0 0
after PASS3 PASS3-D 11 0 18 0 0 15 0 0 0 0 0 4 0 0 0 0 0 0
before PASS3 PASS3-A 0 0 0 0 0 5 0 0 0 0 0 0 1 5 0 0 0 0
before PASS3 PASS3-B 0 0 2 0 0 18 0 0 0 0 0 0 2 3 0 0 3 3
before PASS3 PASS3-C 0 0 0 0 0 5 0 0 0 0 0 0 3 14 0 0 2 0
before PASS3 PASS3-D 0 0 0 0 0 14 0 0 0 0 0 0 8 11 0 0 1 2
after PASSCN22 PASSCN22-A 0 0 0 0 0 10 0 11 0 0 0 0 0 5 0 0 0 0
after PASSCN22 PASSCN22-B 0 0 25 0 0 25 0 0 0 2 0 6 0 0 0 0 0 0
after PASSCN22 PASSCN22-C 2 0 1 0 0 13 0 0 0 0 0 1 0 2 0 0 0 0
after PASSCN22 PASSCN22-D 5 0 26 0 0 39 0 0 1 0 0 1 0 1 0 0 0 0
before PASSCN22 PASSCN22-A 0 0 0 0 0 8 0 0 0 1 0 0 0 7 0 0 1 0
before PASSCN22 PASSCN22-B 1 0 1 0 0 10 0 0 0 0 0 0 3 4 0 0 10 0
before PASSCN22 PASSCN22-C 0 0 1 0 0 30 0 0 0 0 0 0 4 14 0 0 0 0
before PASSCN22 PASSCN22-D 0 0 3 0 0 25 0 0 0 0 0 0 6 21 0 0 0 0
after PASSCN24 PASSCN24-A 0 0 0 0 0 5 0 1 1 1 0 0 0 23 0 0 0 0
after PASSCN24 PASSCN24-B 3 0 1 0 4 7 0 0 0 0 0 0 0 0 0 0 0 0
after PASSCN24 PASSCN24-C 0 0 1 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0
after PASSCN24 PASSCN24-D 2 0 13 0 0 8 0 0 0 0 0 0 0 2 0 0 0 0
before PASSCN24 PASSCN24-A 0 0 0 0 0 29 0 0 0 0 0 0 1 1 0 0 0 0
before PASSCN24 PASSCN24-B 1 0 0 0 0 41 0 0 0 0 0 0 1 7 0 0 0 0
before PASSCN24 PASSCN24-C 2 0 0 0 0 65 0 0 0 0 0 0 1 27 0 0 1 0
before PASSCN24 PASSCN24-D 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 2 0
after PR1 PR1-A 0 0 0 0 0 2 0 7 0 0 0 0 0 3 0 0 0 0
after PR1 PR1-B 6 0 0 0 1 4 0 13 0 0 0 0 0 1 0 0 0 0
after PR1 PR1-C 3 0 0 0 0 0 0 15 0 0 0 0 0 0 0 1 0 0
after PR1 PR1-D 2 0 0 0 1 2 0 3 0 0 0 0 0 4 0 0 0 0
before PR1 PR1-A 1 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0
before PR1 PR1-B 0 0 0 0 0 0 0 2 0 0 0 0 0 0 1 2 50 0
before PR1 PR1-C 7 0 1 0 0 2 0 2 0 0 0 2 0 17 0 0 0 0
before PR1 PR1-D 3 0 0 0 0 2 0 0 0 0 0 0 7 18 0 0 0 4
after PR3 PR3-A 0 0 1 0 0 2 0 0 0 0 0 0 2 3 0 1 0 0
after PR3 PR3-B 1 0 8 0 2 1 0 3 0 1 0 2 0 23 0 0 0 0
after PR3 PR3-C 2 0 4 0 0 2 0 10 0 0 0 2 0 5 0 0 0 0
after PR3 PR3-D 0 0 1 0 0 0 0 0 0 0 0 1 0 4 2 0 0 0
before PR3 PR3-A 3 0 47 0 0 2 0 9 0 0 0 1 0 0 0 0 0 0
before PR3 PR3-B 0 0 0 0 0 0 0 0 0 0 0 0 0 24 0 0 0 0
before PR3 PR3-C 0 0 0 0 0 1 0 0 0 0 0 0 0 14 0 2 0 0
before PR3 PR3-D 1 0 0 0 1 0 0 0 0 0 0 0 0 12 17 0 1 0
after PRCN5 PRCN5-A 1 0 2 0 0 4 0 34 0 0 0 0 2 12 0 0 0 0
after PRCN5 PRCN5-B 2 0 1 0 0 1 0 17 0 0 0 0 0 5 0 0 0 0
after PRCN5 PRCN5-C 2 0 10 0 2 1 0 4 0 0 0 1 0 3 0 0 1 0
after PRCN5 PRCN5-D 1 0 10 0 1 0 0 12 0 0 0 0 1 4 0 0 0 0
before PRCN5 PRCN5-A 0 0 0 0 0 1 0 0 0 0 0 0 0 4 0 0 2 0
before PRCN5 PRCN5-B 0 0 0 0 0 1 0 0 0 0 0 0 2 35 6 0 1 0
before PRCN5 PRCN5-C 0 0 1 0 0 2 0 2 0 0 0 0 0 18 137 0 0 0
before PRCN5 PRCN5-D 0 0 0 0 0 0 0 0 1 0 0 0 4 4 257 0 3 0
after PRCN6 PRCN6-A 2 0 2 0 0 1 0 1 0 1 0 0 1 9 0 0 0 0
after PRCN6 PRCN6-B 5 0 0 0 0 5 0 0 0 0 0 2 0 8 0 0 0 0
after PRCN6 PRCN6-C 2 0 0 0 0 1 0 0 0 0 0 1 0 7 3 0 0 0
after PRCN6 PRCN6-D 0 0 1 0 0 0 0 10 0 0 0 0 0 20 0 0 0 0
before PRCN6 PRCN6-A 0 0 0 0 0 0 0 0 0 0 0 0 0 57 0 0 0 0
before PRCN6 PRCN6-B 2 0 0 0 0 7 0 0 0 0 0 1 0 42 17 0 0 1
before PRCN6 PRCN6-C 0 0 0 0 0 3 0 3 0 0 0 0 2 2 0 0 1 0
before PRCN6 PRCN6-D 1 0 0 0 0 3 0 0 0 0 0 0 1 6 0 0 0 0
after TWB1 TWB1-A 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
after TWB1 TWB1-B 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
after TWB1 TWB1-C 0 0 0 0 0 2 0 2 1 0 0 0 0 4 0 0 0 0
after TWB1 TWB1-D 4 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
before TWB1 TWB1-A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
before TWB1 TWB1-B 0 0 0 0 0 0 0 0 0 0 0 0 4 1 0 0 2 0
before TWB1 TWB1-C 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0
before TWB1 TWB1-D 1 0 0 0 0 0 0 0 0 0 0 0 1 3 0 0 1 0
after TWB2 TWB2-A 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
after TWB2 TWB2-B 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
after TWB2 TWB2-C 3 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
after TWB2 TWB2-D 5 0 0 0 1 0 0 0 0 0 0 0 0 5 0 0 0 0
before TWB2 TWB2-A 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0
before TWB2 TWB2-B 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 0
before TWB2 TWB2-C 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0
before TWB2 TWB2-D 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
after TWBCN2 TWBCN2-A 13 0 0 0 0 10 0 14 0 0 0 0 0 10 0 0 0 0
after TWBCN2 TWBCN2-B 11 0 294 0 0 4 0 0 0 0 0 0 0 0 0 0 14 0
after TWBCN2 TWBCN2-C 9 0 11 0 0 1 0 7 0 0 0 2 0 1 0 0 0 0
after TWBCN2 TWBCN2-D 8 0 25 0 0 1 0 3 0 0 0 1 0 0 0 0 1 0
before TWBCN2 TWBCN2-A 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 1 0
before TWBCN2 TWBCN2-B 0 0 0 0 0 2 0 0 0 0 0 0 6 0 0 0 18 0
before TWBCN2 TWBCN2-C 3 0 3 0 0 7 0 0 0 0 0 0 2 5 0 0 2 0
before TWBCN2 TWBCN2-D 0 0 1 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0
after TWBCN3 TWBCN3-A 0 0 16 0 0 2 0 8 0 0 0 0 0 0 0 0 0 0
after TWBCN3 TWBCN3-B 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
after TWBCN3 TWBCN3-C 3 0 8 0 0 6 0 30 0 0 0 0 0 13 0 0 0 0
after TWBCN3 TWBCN3-D 6 0 1 0 0 1 0 0 0 0 0 3 0 2 0 0 0 0
before TWBCN3 TWBCN3-A 0 0 4 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0
before TWBCN3 TWBCN3-B 0 0 1 0 0 6 0 0 0 0 0 0 4 0 0 0 0 0
before TWBCN3 TWBCN3-C 0 0 2 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0
before TWBCN3 TWBCN3-D 0 0 0 0 0 12 0 0 0 0 0 0 3 0 0 0 1 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment