library(tidyverse) #data wrangle
library(ggplot2) # plotting
library(lubridate) # change the date to a standard "Julian Data"
library(emmeans) # pairwise comparisons
library(car) #stats
library(multcomp)
library(bbmle)
library (dlookr) # creating plots to visualize normality
library(ggpubr) # plotting
library(broom) # make tidy tibbles out of summary stat operations
library(lme4) # creating GLMMs
library(redres) # plotting residuals
library(jtools)
library(Matrix)
library(interactions)
library(ggeffects)
library(DHARMa)CACO GH Data
Castilleja Greenhouse experiment
Setting up the workspace
Load necessary packages
Set working directory and read in data from CSV as a data frame called “gh”
#set working directory
setwd(("/Users/emmafetterly/Library/Mobile Documents/com~apple~CloudDocs/Documents/Grad School/Thesis/GH_data/Data_csv"))
#read in the spreadsheet, your file name might be different
gh <- read.csv("GH_data6.csv", header=TRUE)First, let’s check our data types
str(gh)'data.frame': 1440 obs. of 11 variables:
$ Plant_ID : chr "R1_49" "Y1_37" "R1_1" "Y2_13" ...
$ Block : int 1 1 1 1 1 1 1 1 1 1 ...
$ Position : chr "A1" "A2" "A3" "A4" ...
$ Date : chr "7/6/23" "7/6/23" "7/6/23" "7/6/23" ...
$ Survivval : chr "Y" "Y" "Y" "Y" ...
$ Rosette.Diameter..cm.: chr "4.01" "5.92" "6.82" "8.64" ...
$ Number.of.leaves..cm.: chr "10" "12" "15" "11" ...
$ LL..cm. : chr "2.76" "30" "4.06" "4.73" ...
$ Soil.Moisture.... : num NA NA NA NA NA NA NA NA NA NA ...
$ Week : int 1 1 1 1 1 1 1 1 1 1 ...
$ Notes : chr "Very worn" "" "" "" ...
Data wrangle process:
Make new columns for “population” “plant_number_pop” “color” and “block” “tub” and “treatment” based on existing columns
# Split the "plant_ID" column into "population" and "plant_number_pop" columns gh <- separate(gh, Plant_ID, into = c("population", "plant_number_pop"), sep = "_", remove = FALSE) #make a column that just has "red" and "yellow" labels gh <- gh %>% mutate(color = case_when( substr(population, 1, 1) == "R" ~ "red", substr(population, 1, 1) == "Y" ~ "yellow", TRUE ~ NA_character_ )) #combine "small blocks" into experimental block units gh <- gh %>% mutate(Combined_Block = ifelse(Block %in% c(1, 2), 1, ifelse(Block %in% c(3, 4), 2, ifelse(Block %in% c(5, 6), 3, ifelse(Block %in% c(7, 8), 4, ifelse(Block %in% c(9, 10), 5, ifelse(Block %in% c(11, 12), 6, NA))))))) # rename the existing Block column to tub and rename the newly created Combined_Block column to Block gh <- gh %>% rename(tub = Block, Block = Combined_Block) # Create a new column "treatment" based on even/odd status of the "block" column gh <- gh %>% mutate(treatment = ifelse(Block %% 2 == 0, "D", "C"))Clean up variable names
gh <- gh %>% rename(alive = Survivval, size = Rosette.Diameter..cm., leaves = Number.of.leaves..cm., LL = LL..cm., VWC = Soil.Moisture....)Ensure variable types are correct
gh$Plant_ID<- as.factor(gh$Plant_ID) gh$population <- as.factor(gh$population) gh$plant_number_pop <- as.integer(gh$plant_number_pop) gh$size <- as.numeric(gh$size) gh$leaves <- as.numeric(gh$leaves) gh$LL <- as.numeric(gh$LL) gh$alive <- factor(gh$alive, levels = c("N", "Y")) gh$Date<- as.Date(gh$Date, "%m/%d/%y") #it would be good to create a column with a standard measure of days using the yday function from lubridate gh$julian <- yday(gh$Date)Remove “dummy plants” R31-60
gh <- subset(gh, !(population == 'R1' & plant_number_pop > 30))Make appropriate subsets of data for analysis
#data frame where there are no dead plants gh.alive <- subset(gh, alive != "N") # for the "end of experiment results" we'll want a week 5 data frame to see the most extreme effect of the drought # we will do this by creating a subset of the data called wk5 wk5 <- gh.alive[gh.alive$Week == 5, ] # make subset data frames for easy plotting/sum stats r1_subset_week5 <- gh.alive %>% filter(Week == 5)%>% filter(population == "R1") r2_subset_week5 <- gh.alive %>% filter(Week == 5)%>% filter(population == "R2") y1_subset_week5 <- gh.alive %>% filter(Week == 5)%>% filter(population == "Y1") y2_subset_week5 <- gh.alive %>% filter(Week == 5)%>% filter(population == "Y2")
Exploratory Data Analysis
Descriptive statistics
Start by looking at descriptive stats of our response variables (size, LL, leaves) by each week
sum_stat_allwks <- gh.alive %>%
group_by(Week, treatment, population) %>%
describe(size, LL, leaves)
sum_stat_allwks# A tibble: 144 × 29
described_variables Week treatment population n na mean sd
<chr> <int> <chr> <fct> <int> <int> <dbl> <dbl>
1 LL 1 C R1 16 0 4.03 0.873
2 LL 1 C R2 30 0 4.07 0.817
3 LL 1 C Y1 30 0 5.67 4.80
4 LL 1 C Y2 30 0 6.34 1.17
5 LL 1 D R1 13 0 4.37 0.803
6 LL 1 D R2 30 0 3.95 0.711
7 LL 1 D Y1 30 0 4.76 1.09
8 LL 1 D Y2 30 0 5.93 1.41
9 LL 2 C R1 16 0 4.35 0.714
10 LL 2 C R2 30 0 4.22 0.785
# ℹ 134 more rows
# ℹ 21 more variables: se_mean <dbl>, IQR <dbl>, skewness <dbl>,
# kurtosis <dbl>, p00 <dbl>, p01 <dbl>, p05 <dbl>, p10 <dbl>, p20 <dbl>,
# p25 <dbl>, p30 <dbl>, p40 <dbl>, p50 <dbl>, p60 <dbl>, p70 <dbl>,
# p75 <dbl>, p80 <dbl>, p90 <dbl>, p95 <dbl>, p99 <dbl>, p100 <dbl>
That’s a lot of information, what about just the last week (to look at the major effects of the drought)
sum_stat_wk5 <- wk5 %>%
group_by(Week, treatment, population) %>%
describe(size, LL, leaves)
sum_stat_wk5# A tibble: 24 × 29
described_variables Week treatment population n na mean sd
<chr> <int> <chr> <fct> <int> <int> <dbl> <dbl>
1 LL 5 C R1 16 0 4.80 1.20
2 LL 5 C R2 22 0 4.85 1.10
3 LL 5 C Y1 27 0 5.38 1.23
4 LL 5 C Y2 27 0 6.54 1.36
5 LL 5 D R1 13 1 5.01 1.10
6 LL 5 D R2 25 0 4.80 0.957
7 LL 5 D Y1 25 0 5.46 1.17
8 LL 5 D Y2 26 0 6.74 1.53
9 leaves 5 C R1 16 0 21.2 8.08
10 leaves 5 C R2 22 0 19.8 7.35
# ℹ 14 more rows
# ℹ 21 more variables: se_mean <dbl>, IQR <dbl>, skewness <dbl>,
# kurtosis <dbl>, p00 <dbl>, p01 <dbl>, p05 <dbl>, p10 <dbl>, p20 <dbl>,
# p25 <dbl>, p30 <dbl>, p40 <dbl>, p50 <dbl>, p60 <dbl>, p70 <dbl>,
# p75 <dbl>, p80 <dbl>, p90 <dbl>, p95 <dbl>, p99 <dbl>, p100 <dbl>
Response variables graphed over time
By population:
By color:
Assumption checking for parametric statistical tests
- Normality - Visual checks
Rosette size
R1_normplots_size <- wk5 %>%
filter(population == "R1") %>%
group_by(treatment) %>%
plot_normality(size)R2_normplots_size <- wk5 %>%
filter(population == "R2") %>%
group_by(treatment) %>%
plot_normality(size)Y1_normplots_size <- wk5 %>%
filter(population == "Y2") %>%
group_by(treatment) %>%
plot_normality(size)Y2_normplots_size <- wk5 %>%
filter(population == "Y2") %>%
group_by(treatment) %>%
plot_normality(size)Longest leaf length
R1_normplots_LL <- wk5 %>%
filter(population == "R1") %>%
group_by(treatment) %>%
plot_normality(LL)R2_normplots_LL <- wk5 %>%
filter(population == "R2") %>%
group_by(treatment) %>%
plot_normality(LL)Y1_normplots_LL <- wk5 %>%
filter(population == "Y1") %>%
group_by(treatment) %>%
plot_normality(LL)Y2_normplots_LL <- wk5 %>%
filter(population == "Y2") %>%
group_by(treatment) %>%
plot_normality(LL)Number of leaves
R1_normplots_leaves <- wk5 %>%
filter(population == "R1") %>%
group_by(treatment) %>%
plot_normality(leaves)R2_normplots_leaves <- wk5 %>%
filter(population == "R2") %>%
group_by(treatment) %>%
plot_normality(leaves)Y1_normplots_leaves <- wk5 %>%
filter(population == "Y2") %>%
group_by(treatment) %>%
plot_normality(leaves)Y2_normplots_leaves <- wk5 %>%
filter(population == "Y2") %>%
group_by(treatment) %>%
plot_normality(leaves)Shapiro-Wilks normality test
p-value above 0.05 indicates normality
Based on the Shapiro Wilks test, almost all traits have normal distributions except for Y2 D for longest leaf length and R1 D for number of leaves - (is this due to an extreme outlier? R1_12 had 52 leaves, average for group was 21 leaves with SD of 10 leaves)
# size
wk5 %>%
group_by(population, treatment) %>%
do(tidy(shapiro.test(.$size)))# A tibble: 8 × 5
# Groups: population, treatment [8]
population treatment statistic p.value method
<fct> <chr> <dbl> <dbl> <chr>
1 R1 C 0.906 0.0992 Shapiro-Wilk normality test
2 R1 D 0.958 0.715 Shapiro-Wilk normality test
3 R2 C 0.970 0.701 Shapiro-Wilk normality test
4 R2 D 0.960 0.405 Shapiro-Wilk normality test
5 Y1 C 0.978 0.804 Shapiro-Wilk normality test
6 Y1 D 0.948 0.223 Shapiro-Wilk normality test
7 Y2 C 0.967 0.513 Shapiro-Wilk normality test
8 Y2 D 0.926 0.0632 Shapiro-Wilk normality test
#number of leaves
wk5 %>%
group_by(population, treatment) %>%
do(tidy(shapiro.test(.$leaves)))# A tibble: 8 × 5
# Groups: population, treatment [8]
population treatment statistic p.value method
<fct> <chr> <dbl> <dbl> <chr>
1 R1 C 0.987 0.996 Shapiro-Wilk normality test
2 R1 D 0.756 0.00212 Shapiro-Wilk normality test
3 R2 C 0.965 0.586 Shapiro-Wilk normality test
4 R2 D 0.982 0.918 Shapiro-Wilk normality test
5 Y1 C 0.969 0.581 Shapiro-Wilk normality test
6 Y1 D 0.973 0.709 Shapiro-Wilk normality test
7 Y2 C 0.957 0.312 Shapiro-Wilk normality test
8 Y2 D 0.939 0.127 Shapiro-Wilk normality test
#longest leaf length
wk5 %>%
group_by(population, treatment) %>%
do(tidy(shapiro.test(.$LL)))# A tibble: 8 × 5
# Groups: population, treatment [8]
population treatment statistic p.value method
<fct> <chr> <dbl> <dbl> <chr>
1 R1 C 0.965 0.753 Shapiro-Wilk normality test
2 R1 D 0.953 0.649 Shapiro-Wilk normality test
3 R2 C 0.951 0.334 Shapiro-Wilk normality test
4 R2 D 0.986 0.976 Shapiro-Wilk normality test
5 Y1 C 0.950 0.211 Shapiro-Wilk normality test
6 Y1 D 0.940 0.146 Shapiro-Wilk normality test
7 Y2 C 0.917 0.0338 Shapiro-Wilk normality test
8 Y2 D 0.969 0.591 Shapiro-Wilk normality test
Equality of variances
p-value above 0.05 indicates equal variance
Data look good for equality of variances
#size
leveneTest(data = wk5, size ~ population*treatment)Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 7 1.3321 0.2378
173
# data look good for equality of variances#number of leaves
leveneTest(data = wk5, leaves ~ population*treatment)Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 7 0.9001 0.5077
173
#LL
leveneTest(data = wk5, LL ~ population*treatment)Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 7 0.9144 0.4967
173
Box plots of traits by population
theme_emma <- function () {
theme_bw() %+replace%
theme(panel.grid.major = element_blank(), #remove grids
panel.grid.minor = element_blank(),
strip.background = element_blank(),
axis.text.x= element_text(face = "bold", size = "12"),
axis.text.y= element_text(face = "bold", size = "12"),
axis.title = element_text(face = "bold", size = "15"),
axis.ticks.x=element_blank())}
size.plot.box <- ggplot(wk5, aes(x = population, y = size, fill = treatment)) +
geom_boxplot() +
scale_fill_manual(values=c("azure3", "azure4")) +
theme_emma() +
theme(
panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent', color=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill='transparent'),
legend.box.background = element_rect(fill='transparent')
)+
labs(y = 'Rosette size (cm)', x = 'Population', fill = 'Treatment')
size.plot.boxWarning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
LL.plot.box <- ggplot(wk5, aes(x = population, y = LL, fill = treatment)) +
geom_boxplot() +
scale_fill_manual(values=c("azure3", "azure4")) +
theme_emma() +
theme(
panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent', color=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill='transparent'),
legend.box.background = element_rect(fill='transparent')
)+
labs(y = 'Longest leaf length (cm)', x = 'Population', fill = 'Treatment')
LL.plot.boxWarning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
leaves.plot.box <- ggplot(wk5, aes(x = population, y = leaves, fill = treatment)) +
geom_boxplot() +
scale_fill_manual(values=c("azure3", "azure4")) +
theme_emma() +
theme(
panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent', color=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill='transparent'),
legend.box.background = element_rect(fill='transparent')
)+
labs(y = 'Number of leaves', x = 'Population', fill = 'Treatment')
leaves.plot.boxWarning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
Box plots of traits by color
size.plot.box.color <- ggplot(wk5, aes(x = color, y = size, fill = treatment)) +
geom_boxplot() +
scale_fill_manual(values=c("azure3", "azure4")) +
theme_emma() +
theme(
panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent', color=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill='transparent'),
legend.box.background = element_rect(fill='transparent')
)+
labs(y = 'Rosette size (cm)', x = 'Flower color', fill = 'Treatment')
size.plot.box.colorWarning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
LL.plot.box.color <- ggplot(wk5, aes(x = color, y = LL, fill = treatment)) +
geom_boxplot() +
scale_fill_manual(values=c("azure3", "azure4")) +
theme_emma() +
theme(
panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent', color=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill='transparent'),
legend.box.background = element_rect(fill='transparent')
)+
labs(y = 'Longest leaf length (cm)', x = 'Flower color', fill = 'Treatment')
LL.plot.box.colorWarning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
leaves.plot.box.color <- ggplot(wk5, aes(x = color, y = leaves, fill = treatment)) +
geom_boxplot() +
scale_fill_manual(values=c("azure3", "azure4")) +
theme_emma() +
theme(
panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent', color=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill='transparent'),
legend.box.background = element_rect(fill='transparent')
)+
labs(y = 'Number of leaves', x = 'Flower color', fill = 'Treatment')
leaves.plot.box.colorWarning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
Modeling
- Response variables
-
trait (size, LL, number of leaves) - continuous
- Predictor variables
-
- treatment - categorical control (C) and drought (D)
- color - categorical red and yellow
- population - (nested within color?) - R1, R2, Y1, Y2
block (random effect) - 1-6
Need to nest population within color? Repeat measures for time series data?
Trait and population GLM without accounting for block
#model with population + treatment interaction
mod.size.pop.norandom <- lm(size ~ population*treatment, data = wk5)
summ(mod.size.pop.norandom)| Observations | 181 (1 missing obs. deleted) |
| Dependent variable | size |
| Type | OLS linear regression |
| F(7,173) | 9.06 |
| R² | 0.27 |
| Adj. R² | 0.24 |
| Est. | S.E. | t val. | p | |
|---|---|---|---|---|
| (Intercept) | 8.95 | 0.52 | 17.32 | 0.00 |
| populationR2 | 0.04 | 0.68 | 0.05 | 0.96 |
| populationY1 | 0.81 | 0.65 | 1.24 | 0.22 |
| populationY2 | 2.59 | 0.65 | 3.97 | 0.00 |
| treatmentD | -0.23 | 0.77 | -0.30 | 0.77 |
| populationR2:treatmentD | 0.12 | 0.98 | 0.12 | 0.90 |
| populationY1:treatmentD | 0.36 | 0.96 | 0.38 | 0.71 |
| populationY2:treatmentD | 0.77 | 0.96 | 0.80 | 0.43 |
| Standard errors: OLS |
pop_size_intplot_norandom <- cat_plot(mod.size.pop.norandom, pred = population , modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
anova_size_pop_norandom <- Anova(mod.size.pop.norandom)
anova_size_pop_norandomAnova Table (Type II tests)
Response: size
Sum Sq Df F value Pr(>F)
population 266.58 3 20.7927 1.494e-11 ***
treatment 0.79 1 0.1837 0.6688
population:treatment 3.77 3 0.2942 0.8295
Residuals 739.33 173
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pop_size_intplot_norandom# checking residuals
resid_ouput_size_pop.norandom <- simulateResiduals(fittedModel = mod.size.pop.norandom, plot = F)
plot(resid_ouput_size_pop.norandom)#model with color + treatment interaction
mod.LL.pop.norandom <- lm(LL ~ population*treatment, data = wk5)
summ(mod.LL.pop.norandom)| Observations | 181 (1 missing obs. deleted) |
| Dependent variable | LL |
| Type | OLS linear regression |
| F(7,173) | 9.60 |
| R² | 0.28 |
| Adj. R² | 0.25 |
| Est. | S.E. | t val. | p | |
|---|---|---|---|---|
| (Intercept) | 4.80 | 0.31 | 15.57 | 0.00 |
| populationR2 | 0.05 | 0.40 | 0.13 | 0.90 |
| populationY1 | 0.58 | 0.39 | 1.50 | 0.13 |
| populationY2 | 1.74 | 0.39 | 4.48 | 0.00 |
| treatmentD | 0.21 | 0.46 | 0.47 | 0.64 |
| populationR2:treatmentD | -0.26 | 0.58 | -0.45 | 0.66 |
| populationY1:treatmentD | -0.14 | 0.57 | -0.24 | 0.81 |
| populationY2:treatmentD | -0.01 | 0.57 | -0.01 | 0.99 |
| Standard errors: OLS |
anova_LL_pop_norandom <- Anova(mod.LL.pop.norandom)
anova_LL_pop_norandomAnova Table (Type II tests)
Response: LL
Sum Sq Df F value Pr(>F)
population 101.104 3 22.2044 3.27e-12 ***
treatment 0.499 1 0.3285 0.5673
population:treatment 0.502 3 0.1103 0.9540
Residuals 262.575 173
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pop_LL_intplot_norandom <- cat_plot(mod.LL.pop.norandom, pred = population , modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
pop_LL_intplot_norandom#checking residuals
resid_ouput_LL_pop.norandom <- simulateResiduals(fittedModel = mod.LL.pop.norandom, plot = F)
plot(resid_ouput_LL_pop.norandom)#model with color + treatment interaction
mod.leaves.pop.norandom <- glm(leaves ~ population*treatment, data = wk5, family = poisson)
summ(mod.leaves.pop.norandom)| Observations | 181 (1 missing obs. deleted) |
| Dependent variable | leaves |
| Type | Generalized linear model |
| Family | poisson |
| Link | log |
| 𝛘²(7) | 39.63 |
| Pseudo-R² (Cragg-Uhler) | 0.20 |
| Pseudo-R² (McFadden) | 0.03 |
| AIC | 1391.08 |
| BIC | 1416.67 |
| Est. | S.E. | z val. | p | |
|---|---|---|---|---|
| (Intercept) | 3.06 | 0.05 | 56.36 | 0.00 |
| populationR2 | -0.07 | 0.07 | -0.96 | 0.33 |
| populationY1 | 0.03 | 0.07 | 0.51 | 0.61 |
| populationY2 | 0.10 | 0.07 | 1.44 | 0.15 |
| treatmentD | 0.00 | 0.08 | 0.03 | 0.97 |
| populationR2:treatmentD | -0.10 | 0.11 | -0.94 | 0.35 |
| populationY1:treatmentD | -0.09 | 0.10 | -0.93 | 0.35 |
| populationY2:treatmentD | 0.06 | 0.10 | 0.66 | 0.51 |
| Standard errors: MLE |
anova_leaves_pop_norandom <- Anova(mod.leaves.pop.norandom)
anova_leaves_pop_norandomAnalysis of Deviance Table (Type II tests)
Response: leaves
LR Chisq Df Pr(>Chisq)
population 33.597 3 2.41e-07 ***
treatment 0.605 1 0.4368
population:treatment 5.174 3 0.1595
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pop_leaves_intplot_norandom <- cat_plot(mod.leaves.pop.norandom, pred = population , modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
pop_leaves_intplot_norandom# checking residuals
resid_ouput_leaves_pop.norandom <- simulateResiduals(fittedModel = mod.leaves.pop.norandom, plot = F)
plot(resid_ouput_leaves_pop.norandom)Mixed model with random effect of block
#model with color by treatment interaction and random effect of block
mod.size.pop <- lmer(size ~ population*treatment + (1|Block), data = wk5)boundary (singular) fit: see help('isSingular')
summ(mod.size.pop)| Observations | 181 |
| Dependent variable | size |
| Type | Mixed effects linear regression |
| AIC | 786.94 |
| BIC | 818.93 |
| Pseudo-R² (fixed effects) | 0.26 |
| Pseudo-R² (total) | 0.26 |
| Est. | S.E. | t val. | d.f. | p | |
|---|---|---|---|---|---|
| (Intercept) | 8.95 | 0.52 | 17.27 | 81.21 | 0.00 |
| populationR2 | 0.03 | 0.68 | 0.05 | 169.09 | 0.96 |
| populationY1 | 0.81 | 0.65 | 1.24 | 170.11 | 0.22 |
| populationY2 | 2.59 | 0.65 | 3.97 | 169.44 | 0.00 |
| treatmentD | -0.23 | 0.77 | -0.30 | 93.04 | 0.77 |
| populationR2:treatmentD | 0.12 | 0.98 | 0.12 | 169.25 | 0.90 |
| populationY1:treatmentD | 0.36 | 0.96 | 0.38 | 169.87 | 0.71 |
| populationY2:treatmentD | 0.77 | 0.96 | 0.80 | 169.36 | 0.43 |
| p values calculated using Kenward-Roger standard errors and d.f. |
| Group | Parameter | Std. Dev. |
|---|---|---|
| Block | (Intercept) | 0.00 |
| Residual | 2.07 |
| Group | # groups | ICC |
|---|---|---|
| Block | 6 | 0.00 |
anova_size_pop <- Anova(mod.size.pop, 3)
anova_size_popAnalysis of Deviance Table (Type III Wald chisquare tests)
Response: size
Chisq Df Pr(>Chisq)
(Intercept) 299.8978 1 < 2.2e-16 ***
population 24.7363 3 1.753e-05 ***
treatment 0.0882 1 0.7665
population:treatment 0.8826 3 0.8296
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
population_size_intplot <- cat_plot(mod.size.pop, pred = population, modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
population_size_intplot# checking residuals
resid_ouput_size_pop <- simulateResiduals(fittedModel = mod.size.pop, plot = F)
plot(resid_ouput_size_pop)#model with color by treatment interaction and random effect of block
mod.LL.pop <- lmer(LL ~ population*treatment + (1|Block), data = wk5)
summ(mod.LL.pop)| Observations | 181 |
| Dependent variable | LL |
| Type | Mixed effects linear regression |
| AIC | 607.63 |
| BIC | 639.61 |
| Pseudo-R² (fixed effects) | 0.27 |
| Pseudo-R² (total) | 0.28 |
| Est. | S.E. | t val. | d.f. | p | |
|---|---|---|---|---|---|
| (Intercept) | 4.81 | 0.32 | 15.13 | 58.08 | 0.00 |
| populationR2 | 0.05 | 0.40 | 0.12 | 169.07 | 0.91 |
| populationY1 | 0.57 | 0.39 | 1.47 | 169.83 | 0.14 |
| populationY2 | 1.73 | 0.39 | 4.48 | 169.32 | 0.00 |
| treatmentD | 0.21 | 0.47 | 0.44 | 67.64 | 0.66 |
| populationR2:treatmentD | -0.26 | 0.58 | -0.44 | 169.18 | 0.66 |
| populationY1:treatmentD | -0.13 | 0.57 | -0.23 | 169.64 | 0.82 |
| populationY2:treatmentD | -0.00 | 0.57 | -0.01 | 169.26 | 0.99 |
| p values calculated using Kenward-Roger standard errors and d.f. |
| Group | Parameter | Std. Dev. |
|---|---|---|
| Block | (Intercept) | 0.14 |
| Residual | 1.23 |
| Group | # groups | ICC |
|---|---|---|
| Block | 6 | 0.01 |
anova_LL_pop <- Anova(mod.LL.pop, 3)
anova_LL_popAnalysis of Deviance Table (Type III Wald chisquare tests)
Response: LL
Chisq Df Pr(>Chisq)
(Intercept) 229.7450 1 < 2.2e-16 ***
population 30.8950 3 8.945e-07 ***
treatment 0.1899 1 0.663
population:treatment 0.3313 3 0.954
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
population_LL_intplot <- cat_plot(mod.LL.pop, pred = population , modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
population_LL_intplot# checking residuals
resid_ouput_LL_pop <- simulateResiduals(fittedModel = mod.LL.pop, plot = F)
residuals(resid_ouput_LL_pop) [1] 0.168 0.712 0.260 0.828 0.380 0.760 0.540 0.500 0.048 0.128 0.092 0.788
[13] 0.400 0.864 0.768 0.860 0.812 0.048 0.708 0.268 0.552 0.636 0.216 0.352
[25] 0.960 0.924 0.112 0.512 0.128 0.040 0.844 0.476 0.504 0.596 0.076 0.520
[37] 0.856 0.156 0.016 0.996 0.980 0.896 0.532 0.232 0.220 0.548 0.748 0.740
[49] 0.020 0.240 0.332 0.244 0.344 0.000 0.276 0.444 0.492 0.480 0.784 0.328
[61] 0.944 0.168 0.360 0.640 0.512 0.928 0.696 0.756 0.096 0.464 0.804 0.920
[73] 0.628 0.060 0.828 0.668 0.424 0.472 0.372 0.228 0.016 0.200 0.560 0.040
[85] 0.712 0.312 0.672 0.300 0.392 0.112 0.008 0.572 0.460 0.652 0.040 0.416
[97] 0.720 0.456 0.452 0.392 0.632 0.840 0.176 0.512 0.916 0.884 0.900 0.816
[109] 0.540 0.708 0.712 0.108 0.628 0.248 0.940 0.576 0.300 0.064 0.644 0.008
[121] 0.740 0.140 0.772 0.692 0.516 0.604 0.572 0.320 0.536 0.784 0.768 0.080
[133] 0.712 0.624 0.100 0.396 0.268 0.968 1.000 0.708 0.404 0.812 0.940 0.924
[145] 0.732 0.436 0.408 0.964 0.284 0.540 0.704 0.876 0.844 0.704 0.592 0.936
[157] 0.792 0.396 0.692 0.512 0.304 0.432 0.584 0.060 0.520 0.008 0.364 0.672
[169] 0.908 0.632 0.696 0.688 0.372 0.608 0.668 0.472 0.216 0.572 0.648 0.328
[181] 0.048
plot(resid_ouput_LL_pop)#model with color by treatment interaction random effect of block
mod.leaves.pop <- lmer(leaves ~ population*treatment + (1|Block), data = wk5)boundary (singular) fit: see help('isSingular')
summ(mod.size.pop)| Observations | 181 |
| Dependent variable | size |
| Type | Mixed effects linear regression |
| AIC | 786.94 |
| BIC | 818.93 |
| Pseudo-R² (fixed effects) | 0.26 |
| Pseudo-R² (total) | 0.26 |
| Est. | S.E. | t val. | d.f. | p | |
|---|---|---|---|---|---|
| (Intercept) | 8.95 | 0.52 | 17.27 | 81.21 | 0.00 |
| populationR2 | 0.03 | 0.68 | 0.05 | 169.09 | 0.96 |
| populationY1 | 0.81 | 0.65 | 1.24 | 170.11 | 0.22 |
| populationY2 | 2.59 | 0.65 | 3.97 | 169.44 | 0.00 |
| treatmentD | -0.23 | 0.77 | -0.30 | 93.04 | 0.77 |
| populationR2:treatmentD | 0.12 | 0.98 | 0.12 | 169.25 | 0.90 |
| populationY1:treatmentD | 0.36 | 0.96 | 0.38 | 169.87 | 0.71 |
| populationY2:treatmentD | 0.77 | 0.96 | 0.80 | 169.36 | 0.43 |
| p values calculated using Kenward-Roger standard errors and d.f. |
| Group | Parameter | Std. Dev. |
|---|---|---|
| Block | (Intercept) | 0.00 |
| Residual | 2.07 |
| Group | # groups | ICC |
|---|---|---|
| Block | 6 | 0.00 |
anova_leaves_pop <- Anova(mod.leaves.pop, 3)
anova_leaves_popAnalysis of Deviance Table (Type III Wald chisquare tests)
Response: leaves
Chisq Df Pr(>Chisq)
(Intercept) 115.7942 1 <2e-16 ***
population 2.5955 3 0.4583
treatment 0.0004 1 0.9844
population:treatment 1.7442 3 0.6271
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pop_leaves_intplot <- cat_plot(mod.leaves.pop, pred = population , modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
pop_leaves_intplot#residuals
resid_ouput_leaves_pop <- simulateResiduals(fittedModel = mod.leaves.pop, plot = F)
residuals(resid_ouput_leaves_pop) [1] 0.200 0.868 0.580 0.904 0.276 0.528 0.436 0.572 0.088 0.888 0.592 0.900
[13] 0.632 0.860 0.768 0.868 0.928 0.068 0.992 0.392 0.892 0.232 0.044 0.624
[25] 0.444 0.424 0.552 0.628 0.296 0.040 0.396 0.832 0.720 0.860 0.420 0.308
[37] 0.736 0.928 0.092 0.304 0.788 0.704 0.640 0.352 0.064 0.732 0.600 0.596
[49] 0.064 0.192 0.560 0.096 0.348 0.076 0.768 0.520 0.600 0.496 0.536 0.440
[61] 0.440 0.096 0.416 0.828 0.972 0.936 0.496 0.916 0.344 0.228 0.876 0.960
[73] 0.372 0.192 0.560 0.780 0.320 0.404 0.808 0.192 0.072 0.080 0.576 0.036
[85] 0.732 0.244 0.656 0.280 0.372 0.076 0.048 0.232 0.096 0.116 0.360 0.184
[97] 0.708 0.592 0.300 0.496 0.912 0.448 0.828 0.236 0.716 0.896 0.828 0.880
[109] 0.876 0.708 0.508 0.156 0.500 0.300 0.852 0.108 0.236 0.044 0.284 0.140
[121] 0.604 0.124 0.408 0.660 0.776 0.360 0.152 0.624 0.964 0.692 0.296 0.228
[133] 0.620 0.840 0.192 0.412 0.176 1.000 0.928 0.892 0.472 0.248 0.764 0.760
[145] 0.320 0.216 0.064 0.516 0.152 0.084 0.648 0.560 0.908 0.088 0.308 0.864
[157] 0.996 0.516 0.668 0.956 0.296 0.344 0.428 0.136 0.548 0.040 0.516 1.000
[169] 0.732 0.628 0.256 0.392 0.456 0.332 0.624 0.408 0.172 0.472 0.296 0.460
[181] 0.408
plot(resid_ouput_leaves_pop)Mortality data
# Convert 'alive' column to binary (0 for 'N', 1 for 'Y')
gh$alive_binary <- as.integer(gh$alive == 'Y')
# Subset the data for a specific week
# ( Week 6 - date of final mortality survey of experiment)
subset_week6.all <- gh %>%
filter(Week == 6)
# Calculate percent mortality and count by population
population_summary <- subset_week6.all %>%
group_by(population, treatment) %>%
summarise(count = n(),
percent_mortality = mean(alive_binary)*100) # Percent mortality`summarise()` has grouped output by 'population'. You can override using the
`.groups` argument.
% Survival by population
% Survival by color
`summarise()` has grouped output by 'color'. You can override using the
`.groups` argument.
logit.reg_color <- glm(alive_binary ~ VWC*color,
data = subset_week6.all,
family = binomial(link = "logit"))
summ(logit.reg_color)| Observations | 210 |
| Dependent variable | alive_binary |
| Type | Generalized linear model |
| Family | binomial |
| Link | logit |
| 𝛘²(3) | 12.67 |
| Pseudo-R² (Cragg-Uhler) | 0.08 |
| Pseudo-R² (McFadden) | 0.05 |
| AIC | 258.31 |
| BIC | 271.70 |
| Est. | S.E. | z val. | p | |
|---|---|---|---|---|
| (Intercept) | 0.04 | 0.47 | 0.09 | 0.93 |
| VWC | 0.03 | 0.02 | 1.53 | 0.13 |
| coloryellow | -0.30 | 0.61 | -0.49 | 0.62 |
| VWC:coloryellow | 0.02 | 0.02 | 0.87 | 0.39 |
| Standard errors: MLE |
GLM_survival_facet_pop <- ggplot(subset_week6.all, aes(x = VWC, y = alive_binary, color = population)) +
geom_point() +
stat_smooth(method = "glm", method.args = list(family=binomial), se = TRUE) +
xlab("VWC") +
ylab("Probability of survival") +
ggtitle("Probability of survival against the soil water content (VWC)") +
theme_classic() +
facet_wrap(~ population) +
scale_color_manual(values = c("R1" = "red3", "R2" = "red3", "Y1" = "gold", "Y2" = "gold"))
GLM_survival_facet_pop`geom_smooth()` using formula = 'y ~ x'
GLM_survival_facet_color <- ggplot(subset_week6.all, aes(x = VWC, y = alive_binary, color = color)) +
geom_point() +
stat_smooth(method = "glm", method.args = list(family=binomial), se = TRUE) +
xlab("VWC") +
ylab("Probability of survival") +
ggtitle("Probability of survival against the soil water content (VWC)") +
theme_classic() +
facet_wrap(~ color) +
scale_color_manual(values = c("red" = "red3", "yellow" = "gold"))
GLM_survival_facet_color`geom_smooth()` using formula = 'y ~ x'
Relative Growth Rate
#### RGR ####
# Week 1 - Week 5
# make week 1 and week 5 data frames
week1_data <- gh %>% filter(Week == 1)
week5_data <- gh %>% filter(Week == 5)
# Merge the data based on Plant_ID
merged_data <- merge(week1_data, week5_data, by = "Plant_ID", suffixes = c("_week1", "_week5"))
# Calculate relative growth rate (RGR)
merged_data <- merged_data %>%
mutate(
RGR = (log(size_week5) - log(size_week1)) / (5 - 1) # Assuming measurements are weekly
)
#select certain columns to retain plant ID, popualtion, plant number, color, treatment and block info
selected_columns <- merged_data[, c("Plant_ID", "population_week1","plant_number_pop_week1","color_week1", "RGR", "treatment_week1", "Block_week1", "alive_week5", "alive_binary_week5")]
#get rid of negative RGR and dead plants
RGR_clean <- selected_columns %>%
filter(RGR >= 0) %>%
filter(alive_binary_week5 >= 0) %>%
rename(treatment = treatment_week1,
block = Block_week1,
alive = alive_week5,
alive_binary = alive_binary_week5,
color = color_week1,
plant_number_pop = plant_number_pop_week1,
population = population_week1)
str(RGR_clean)'data.frame': 131 obs. of 9 variables:
$ Plant_ID : Factor w/ 240 levels "R1_1","R1_10",..: 1 2 3 4 5 6 7 8 9 12 ...
$ population : Factor w/ 4 levels "R1","R2","Y1",..: 1 1 1 1 1 1 1 1 1 1 ...
$ plant_number_pop: int 1 10 11 12 13 14 15 16 17 2 ...
$ color : chr "red" "red" "red" "red" ...
$ RGR : num 0.1156 0.038 0.0849 0.0825 0.0279 ...
$ treatment : chr "C" "C" "D" "D" ...
$ block : num 1 5 6 6 1 1 2 2 3 1 ...
$ alive : Factor w/ 2 levels "N","Y": 2 2 2 2 2 2 2 2 2 2 ...
$ alive_binary : int 1 1 1 1 1 1 1 1 1 1 ...
#### Examining RGR with model - color and treatment ####
mod.RGR.color.norandom <- lm(RGR ~ color*treatment, data = RGR_clean)
summ(mod.RGR.color.norandom)| Observations | 131 |
| Dependent variable | RGR |
| Type | OLS linear regression |
| F(3,127) | 1.15 |
| R² | 0.03 |
| Adj. R² | 0.00 |
| Est. | S.E. | t val. | p | |
|---|---|---|---|---|
| (Intercept) | 0.05 | 0.00 | 9.76 | 0.00 |
| coloryellow | -0.01 | 0.01 | -1.02 | 0.31 |
| treatmentD | 0.00 | 0.01 | 0.44 | 0.66 |
| coloryellow:treatmentD | -0.00 | 0.01 | -0.42 | 0.68 |
| Standard errors: OLS |
color_RGR_intplot_norandom <- cat_plot(mod.RGR.color.norandom, pred = color, modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
color_RGR_intplot_norandom # ANOVA of model
anova_RGR_color_norandom <- Anova(mod.RGR.color.norandom)
anova_RGR_color_norandomAnova Table (Type II tests)
Response: RGR
Sum Sq Df F value Pr(>F)
color 0.002218 1 3.2073 0.07569 .
treatment 0.000025 1 0.0368 0.84825
color:treatment 0.000121 1 0.1757 0.67581
Residuals 0.087809 127
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Significant effect of color and treatment, but not a significant effect of color : treatment#### Examining RGR with model - population and treatment ####
mod.RGR.pop.norandom <- lm(RGR ~ population*treatment, data = RGR_clean)
summ(mod.RGR.pop.norandom)| Observations | 131 |
| Dependent variable | RGR |
| Type | OLS linear regression |
| F(7,123) | 1.55 |
| R² | 0.08 |
| Adj. R² | 0.03 |
| Est. | S.E. | t val. | p | |
|---|---|---|---|---|
| (Intercept) | 0.05 | 0.01 | 7.05 | 0.00 |
| populationR2 | -0.01 | 0.01 | -0.97 | 0.34 |
| populationY1 | -0.01 | 0.01 | -0.79 | 0.43 |
| populationY2 | -0.02 | 0.01 | -1.88 | 0.06 |
| treatmentD | -0.00 | 0.01 | -0.01 | 0.99 |
| populationR2:treatmentD | 0.01 | 0.01 | 0.41 | 0.68 |
| populationY1:treatmentD | 0.00 | 0.01 | 0.29 | 0.77 |
| populationY2:treatmentD | -0.00 | 0.01 | -0.32 | 0.75 |
| Standard errors: OLS |
pop_RGR_intplot_norandom <- cat_plot(mod.RGR.pop.norandom, pred = population, modx = treatment, plot.points = TRUE, point.shape = TRUE,
vary.lty = TRUE, colors = "Dark2")
pop_RGR_intplot_norandom # ANOVA of model
anova_RGR_pop_norandom <- Anova(mod.RGR.pop.norandom)
anova_RGR_pop_norandomAnova Table (Type II tests)
Response: RGR
Sum Sq Df F value Pr(>F)
population 0.006695 3 3.3114 0.02237 *
treatment 0.000104 1 0.1542 0.69525
population:treatment 0.000565 3 0.2794 0.84019
Residuals 0.082889 123
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Significant effect of population and treatment, but not a significant effect of population : treatment