-
Notifications
You must be signed in to change notification settings - Fork 0
/
plant-growth-no-insects.Rmd
157 lines (134 loc) · 5.82 KB
/
plant-growth-no-insects.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: "Plant growth in the absence of insects"
author: "Matthew A. Barbour"
date: "`r Sys.Date()`"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE)
# load required libraries
library(tidyverse)
library(kableExtra)
library(clubSandwich)
library(cowplot)
# set ggplot theme
theme_set(theme_cowplot())
```
# Setup
```{r}
# load data
ChamberNoInsectsDF <- read_csv("data/PreExperimentNoInsectsPlantBiomass.csv") %>%
mutate(Cage = as.character(Cage),
Pot = as.character(Pot))
# conduct analyses at cage level
CageLevelBiomass <- ChamberNoInsectsDF %>%
# sum biomass across both pots
group_by(Cage, Temperature, Richness, Composition, Col, gsm1, AOP2, AOP2.gsoh) %>%
summarise_at(vars(Biomass_g), list(sum)) %>%
# tidy data
ungroup() %>%
select(cage = Cage, temp = Temperature, rich = Richness, com = Composition, Col, gsm1, AOP2, AOP2.gsoh, Biomass_g) %>%
# adjust temp and rich so effect of +1 C is comparable to +1 genotype
mutate(temp = ifelse(temp == "20 C", 0, 3),
rich = rich - 1,
# define orthogonal constrasts to test for above-average allele effects.
# aop2_vs_AOP2 must be included first
aop2_vs_AOP2 = Col + gsm1 - AOP2 - AOP2.gsoh,
mam1_vs_MAM1 = gsm1 - Col, # aop2_vs_AOP2 must be included in model
gsoh_vs_GSOH = AOP2.gsoh - AOP2)
# source in ANOVA GLM for adjusted F-tests
source('code/glm-ftest.R')
```
# Show equivalence of Analysis of deviance and ANOVA
An Analysis of deviance on a GLM with a gaussian error distribution is equivalent to ANOVA. However, unadjusted *F*-tests are inappropriate because all terms are tested against residual variation rather than the intended error level (e.g. `com` for `rich`). The analysis below is just to prove this equivalence. I'm doing this so I can use the same function `glm.ftest.v2` for the ANOVA in the following section.
```{r}
glm.ftest.v2(
model = glm(data = CageLevelBiomass,
family = gaussian(link = "identity"),
# logging improves residual distribution
formula = log(Biomass_g) ~ temp + rich + com + temp:rich + temp:com),
test.formula = list(c("temp","temp:com"),
c("rich","com"),
c("temp:rich","temp:com")))[[1]]
anova(aov(log(Biomass_g) ~ temp + rich + com + temp:rich + temp:com, CageLevelBiomass))
```
# Reproduce Table S5
```{r}
# fit ANOVA
biomass_noinsects_glmf <- glm.ftest.v2(
model = glm(data = CageLevelBiomass,
family = gaussian(link = "identity"),
# logging improves residual distribution
formula = log(Biomass_g) ~ temp + rich + aop2_vs_AOP2 + mam1_vs_MAM1 + gsoh_vs_GSOH + com + temp:(rich + aop2_vs_AOP2 + mam1_vs_MAM1 + gsoh_vs_GSOH) + temp:com),
test.formula = list(c("temp","temp:com"),
c("rich","com"),
c("aop2_vs_AOP2","com"),
c("mam1_vs_MAM1","com"),
c("gsoh_vs_GSOH","com"),
c("temp:rich","temp:com"),
c("temp:aop2_vs_AOP2","temp:com"),
c("temp:mam1_vs_MAM1","temp:com"),
c("temp:gsoh_vs_GSOH","temp:com")))[[3]] %>%
# tidy table
select(Source = treatment,
`df (Source)` = num_df,
`df (Error)` = den_df,
Deviance = deviance,
`Mean Deviance` = mean_deviance,
F = F, P = P, Error = error)
# reproduce table S5 in Supplementary Materials
biomass_noinsects_glmf %>%
kable(., caption = "Analysis of variance for plant biomass (log transformed) in the absence of insects.", booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position"))
```
# Reproduce Fig. S6
```{r}
# calculate 95% confidence intervals with `com` as the cluster level
aop2_CI <- conf_int(
glm(data = CageLevelBiomass,
family = gaussian(link = "identity"),
formula = log(Biomass_g) ~ -1 + temp + I(AOP2 + AOP2.gsoh) + I(Col + gsm1)),
vcov = "CR2",
test = "naive-t",
coefs = c("I(AOP2 + AOP2.gsoh)","I(Col + gsm1)"),
cluster = CageLevelBiomass$com) %>%
data.frame() %>%
rownames_to_column(var = "term") %>%
mutate(allele = c("AOP2","aop2"))
# note that I back transform to original scale for plotting
exp(aop2_CI$beta[2])
# get the effect of each genotype
mean_geno <- conf_int(
glm(data = CageLevelBiomass,
family = gaussian(link = "identity"),
formula = log(Biomass_g) ~ -1 + temp + AOP2 + AOP2.gsoh + Col + gsm1),
vcov = "CR2",
test = "naive-t",
cluster = CageLevelBiomass$com,
coefs = c("AOP2","AOP2.gsoh","Col","gsm1")
) %>%
data.frame() %>%
rownames_to_column(var = "term") %>%
mutate(allele = c("AOP2","AOP2","aop2","aop2"),
term = factor(term, levels = c("Col","gsm1","AOP2","AOP2.gsoh"), labels = c("Col","gsm1","AOP2","AOP2/gsoh")))
# plot on original scale
# adding a genotype with an aop2 allele to the population doubles the likelihood of species persistence
ggplot(aop2_CI, aes(x = allele, y = exp(beta))) +
geom_point(size = 5) +
geom_point(data = mean_geno, aes(color = term), size = 5, position = position_dodge(width = 0.3)) +
geom_linerange(aes(ymax = exp(beta + SE), ymin = exp(beta - SE)), size = 1.5) +
geom_linerange(aes(ymax = exp(CI_U), ymin = exp(CI_L))) +
scale_x_discrete(labels = c("AOP2\u2013","AOP2+")) +
scale_y_continuous("Plant biomass (g)") +
xlab("Allele") +
scale_color_manual(values = c("darkgreen","steelblue","darkorange","firebrick1"), name = "") +
theme_cowplot(font_size = 18, line_size = 1)
# ggsave(filename = "figures/AOP2-growth-no-insects.pdf", height = 6, width = 8, device=cairo_pdf)
```
# Save analysis
Write out an `.RData` file to use for creating the Supplementary Material Results.
```{r}
# save.image(file = "output/plant-growth-no-insects.RData")
```