Last updated: 2020-12-03
Checks: 6 1
Knit directory: exp_evol_respiration/
This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish
to commit the R Markdown file and build the HTML.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20190703)
was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version c8feb2d. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish
or wflow_git_commit
). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: output/.DS_Store
Untracked files:
Untracked: data/1.eclosion_times.csv
Untracked: data/1.wing_length.csv
Untracked: data/3.DesRes.csv
Untracked: data/3.StarvRes.csv
Untracked: output/coxmod.rds
Untracked: output/coxmod_dropINT.rds
Untracked: output/coxmod_dropSEX.rds
Untracked: output/coxmod_dropTRT.rds
Untracked: output/wing_brms.rds
Unstaged changes:
Modified: analysis/juvenile_development.Rmd
Modified: analysis/resistance.Rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were made to the R Markdown (analysis/juvenile_development.Rmd
) and HTML (docs/juvenile_development.html
) files. If you’ve configured a remote Git repository (see ?wflow_git_remote
), click on the hyperlinks in the table below to view the files as they were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 3fdbcb2 | lukeholman | 2020-11-30 | Tweaks Nov 2020 |
library(tidyverse)
library(ggridges)
library(coxme)
library(lme4)
library(nlme)
library(brms)
library(tidybayes)
library(kableExtra)
library(knitrhooks) # install with devtools::install_github("nathaneastwood/knitrhooks")
output_max_height() # a knitrhook option
options(stringsAsFactors = FALSE)
# load eclosion data
eclosion.dat <- read.csv("data/1.eclosion_times.csv")
# remove vials seeded with more than 100 larvae
unique(eclosion.dat[which(eclosion.dat$eclosing < 0), "ID"]) # 4 vials overseeded
eclosion.dat.trim <- eclosion.dat %>%
filter(ID %in% eclosion.dat[which(eclosion.dat$eclosing < 0), "ID"] == FALSE)
# expand data frame so each row is a single fly
ecl.dat <- reshape::untable(eclosion.dat.trim[ ,c(1:7, 9, 10)],
num = eclosion.dat.trim[, 8])
# load wing length data
wing_length <- read.csv("data/1.wing_length.csv") %>%
# scale wing vein length to make effect size comparisons with other data sets?
mutate(Length = as.numeric(scale(Length)))
# add replicate
wing_length$LINE <- paste0(wing_length$Treatment, substr(wing_length$Rep, 2, 2))
eplot <- ecl.dat %>%
filter(EVENT == 1) %>%
ggplot(aes(x = DAY, y = SEX, fill = TRT)) +
geom_boxplot() +
scale_fill_brewer(palette = 'Set1', direction = -1, name = "") +
theme_bw() +
theme(legend.position = 'non') +
NULL
lplot <- wing_length %>%
ggplot(aes(x = Sex, y = Length)) +
geom_violin(aes(colour = Treatment)) +
geom_boxplot(aes(fill = Treatment), width = .1, position = position_dodge(width = .9)) +
scale_colour_brewer(palette = 'Set1', direction = -1, name = "") +
scale_fill_brewer(palette = 'Set1', direction = -1, name = "") +
theme_bw() +
theme() +
NULL
gridExtra::grid.arrange(eplot, lplot, ncol = 2)
Figure 1: Time to eclosion in days for flies in each treatment split by sex. Wing vein IV length has been scaled (subtracted the mean and divided by the standard deviation). We see one Monogamy male with an unusually small value for wing length - possible error in data entry or measurement error?
coxme
We will model time to eclosion (in days) using survival analysis. We measured the time in days from 1st instar until eclosion (EVENT
= 1) upon which flies were stored in ethanol before counting. Of the initially seeded 100 flies per vial, the remaining flies not emerging after two consecutive days of no observed eclosions were right censored (EVENT
= 0) on the last observation day. In total 14400 larvae were seeded (100 larvae x 2 Treatment
x 4 LINE
x 6 VIAL
x 3 SEED
). For P1 only three vials were seeded on day B so we seeded 3 additional vials on day C. Four vials were seeded with too many larvae and excluded from analysis. In total 10448 flies successfully emerged during the observation period leaving 3552 individuals to be right censored on day 9. Cencsored flies were assigned sex based on the observed sex ratio of eclosees. We calculated the number of flies of each sex that emerged from each vial, and assigned the remaining censored individuals (of unknown sex) sex based on the proportion of individuals of each sex that did emerge, so that overall an equal sex ratio of females:males was assigned to each vial.
First we plot Kaplan-Meier survival curves and the median time to eclosion without considering our full experimental design.
survminer::ggsurvplot(survfit(Surv(DAY, EVENT) ~ TRT + SEX, data = ecl.dat),
conf.int = TRUE,
risk.table = FALSE,
linetype = "SEX",
palette = c("#377EB8", "#377EB8", "#E41A1C", "#E41A1C"),
fun = "event",
xlim = c(12, 21),
xlab = "Days",
ylab = "Cumulative proportion eclosed",
legend = c(0.2, 0.7),
legend.title = "",
legend.labs = c("M \u2640","M \u2642",'P \u2640','P \u2642'),
break.time.by = 2,
ggtheme = theme_bw())
# median eclosion times
survfit(Surv(DAY, EVENT) ~ TRT + SEX, data = ecl.dat)
Call: survfit(formula = Surv(DAY, EVENT) ~ TRT + SEX, data = ecl.dat) n events median 0.95LCL 0.95UCL TRT=M, SEX=f 3637 3059 16 16 16 TRT=M, SEX=m 3363 2697 16 16 16 TRT=P, SEX=f 3568 2492 18 18 18 TRT=P, SEX=m 3432 2200 18 18 18
Next we need to check that the ‘proportional hazards’ assumption is not violated before fitting the full model.
# assess proportional hazards assumption
par(mar = c(2, 2, 2, 2))
plot(survfit(Surv(DAY, EVENT) ~ TRT + SEX, data = ecl.dat),
lty = 1:2,
col = c("#377EB8", "#377EB8", "#E41A1C", "#E41A1C"),
fun = "cloglog")
Here we plot the ‘cloglog’ of the observed hazards where crossing lines indicate hazards are not proportional. Looks good so we continue to fit the model.
coxme
modelWe fit a mixed effects Cox Proportional hazards model using the coxme
package, with time (days) to event (eclosion) as the response and TRT
(Monogamy or Polyandry), SEX
(female or male) and their interaction as predictors. We also need to account for the experimental design by including the random effects of LINE
, SEED
day and vial ID
. We calculated p-values using likelihood ratio tests comparing the full model to a model where the fixed effect of interest was removed.
# coxmod <- coxme(Surv(DAY, EVENT) ~ TRT * SEX + (1|LINE) + (1|SEED) + (1|ID),
# data = ecl.dat)
# load in the model instead of running
coxmod <- readRDS('output/coxmod.rds')
# null models to generate P-values using likelihood ratio tests
# coxmod_dropTRT <- coxme(Surv(DAY, EVENT) ~ 1 + SEX + (1|LINE) + (1|SEED) + (1|ID), data = ecl.dat)
# coxmod_dropSEX <- coxme(Surv(DAY, EVENT) ~ TRT + 1 + (1|LINE) + (1|SEED) + (1|ID), data = ecl.dat)
# coxmod_dropINT <- coxme(Surv(DAY, EVENT) ~ TRT + SEX + (1|LINE) + (1|SEED) + (1|ID), data = ecl.dat)
coxmod_dropTRT <- readRDS('output/coxmod_dropTRT.rds')
coxmod_dropSEX <- readRDS('output/coxmod_dropSEX.rds')
coxmod_dropINT <- readRDS('output/coxmod_dropINT.rds')
# make a table of the results
bind_rows(
broom::tidy(anova(coxmod, coxmod_dropTRT)),
broom::tidy(anova(coxmod, coxmod_dropSEX)),
broom::tidy(anova(coxmod, coxmod_dropINT))) %>%
cbind(Parameter = rep(c('Treatment', 'Sex', 'Treatment x Sex'), each = 2)) %>%
mutate(across(1:4, round, 3)) %>%
na.omit() %>% as.tibble %>%
relocate(Parameter, logLik, statistic, df, p.value) %>%
kable() %>%
kable_styling() %>%
kable_styling(full_width = FALSE)
Parameter | logLik | statistic | df | p.value |
---|---|---|---|---|
Treatment | -92191.30 | 7.765 | 2 | 0.021 |
Sex | -92235.26 | 95.679 | 2 | 0.000 |
Treatment x Sex | -92187.43 | 0.011 | 1 | 0.916 |
We see there is an effect of treatment and sex but not their interaction. We can calculate estimates and confidence intervals for the hazard ratio by taking the exponent of the coeffients ± 1.96 * sqrt(vcov(???)). Polyandrous flies have a reduced hazard compared to Monogamy flies, i.e. take longer to eclose (hazard ratio = 0.377, CI = 0.227, 0.627). Males have a reduced hazard compared to females (hazard ratio = 0.823, CI = 0.782, 0.867).
We measured the length of wing vein IV for a subset of flies as a correlate of body size, which may influence development time.
# wing_brms <- brm(Length ~ Treatment * Sex + (1|LINE) + (1|Seed),
# data = wing_length,
# iter = 5000, chains = 4, cores = 4,
# prior = c(set_prior("normal(0,1)", class = "b"),
# set_prior("cauchy(0,1)", class = "sd")),
# control = list(max_treedepth = 20,
# adapt_delta = 0.999)
# )
#saveRDS(wing_brms, file = 'output/wing_brms.rds')
wing_brms <- readRDS('output/wing_brms.rds')
pp_check(wing_brms)
Plot posteriors.
# get posterior predictions
post_dat <- posterior_samples(wing_brms) %>%
as_tibble() %>%
select(contains("b_"), -contains("Intercept")) %>%
mutate(draw = 1:n()) %>%
pivot_longer(-draw) %>%
mutate(key = str_remove_all(name, "b_"))
post_dat %>%
ggplot(aes(value, key, fill = key)) +
geom_vline(xintercept = 0, linetype = 2) +
stat_halfeye(alpha = .8) +
scale_fill_brewer(palette = "Spectral") +
ylab("Model parameter") +
xlab("Effect on Wing length") +
theme_ridges() +
theme(legend.position = "none") +
NULL
data.frame(Parameter = rownames(fixef(wing_brms)),
fixef(wing_brms)) %>%
mutate(star = if_else(sign(Q2.5) == sign(Q97.5), '*', '')) %>%
mutate(Parameter = c('Intercept', 'Polandry', 'Male', 'Polandry x Male')) %>%
kable() %>%
kable_styling() %>%
kable_styling(full_width = FALSE)
Parameter | Estimate | Est.Error | Q2.5 | Q97.5 | star |
---|---|---|---|---|---|
Intercept | 0.8421434 | 0.2489305 | 0.3469517 | 1.3424227 |
|
Polandry | 0.1253864 | 0.2891693 | -0.4567406 | 0.7006169 | |
Male | -1.7642953 | 0.0422023 | -1.8471618 | -1.6813626 |
|
Polandry x Male | 0.0400377 | 0.0604904 | -0.0773042 | 0.1592781 |
fitbrms <- wing_length %>%
modelr::data_grid(Treatment, Sex, LINE, Seed) %>%
add_fitted_draws(wing_brms) %>%
sample_frac(size = .5)
fitbrms %>%
mutate(Treatment = ifelse(Treatment == "M", "Monogamy", "Polyandry"),
Sex = ifelse(Sex == "F", "Female", "Male")) %>%
ggplot(aes(x = Sex, y = .value, colour = Treatment)) +
geom_hline(yintercept = 0, linetype = 2) +
stat_pointinterval(position = position_dodge(0.4),
fill = NA, .width = c(0.5, 0.95), alpha = 0.7) +
scale_colour_brewer(palette = 'Set1', direction = -1, name = "") +
labs(y = 'Wing vein IV') +
theme_bw() +
theme(legend.position = 'top',
axis.title.x = element_blank()) +
NULL
Males are smaller than females and there is no effect of selection treatment on wing length.
sessionInfo()
R version 4.0.3 (2020-10-10) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Mojave 10.14.6 Matrix products: default BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] knitrhooks_0.0.4 knitr_1.30 kableExtra_1.3.1 tidybayes_2.3.1 [5] brms_2.14.4 Rcpp_1.0.5 nlme_3.1-149 lme4_1.1-23 [9] Matrix_1.2-18 coxme_2.2-16 bdsmatrix_1.3-4 survival_3.2-7 [13] ggridges_0.5.2 forcats_0.5.0 stringr_1.4.0 dplyr_1.0.2 [17] purrr_0.3.4 readr_1.4.0 tidyr_1.1.2 tibble_3.0.4 [21] ggplot2_3.3.2 tidyverse_1.3.0 loaded via a namespace (and not attached): [1] readxl_1.3.1 backports_1.1.10 workflowr_1.6.2 [4] plyr_1.8.6 igraph_1.2.6 splines_4.0.3 [7] svUnit_1.0.3 crosstalk_1.1.0.1 rstantools_2.1.1 [10] inline_0.3.16 digest_0.6.25 htmltools_0.5.0 [13] rsconnect_0.8.16 fansi_0.4.1 magrittr_2.0.1 [16] openxlsx_4.2.2 modelr_0.1.8 RcppParallel_5.0.2 [19] matrixStats_0.57.0 xts_0.12.1 prettyunits_1.1.1 [22] colorspace_1.4-1 blob_1.2.1 rvest_0.3.6 [25] ggdist_2.3.0 haven_2.3.1 xfun_0.19 [28] callr_3.5.1 crayon_1.3.4 jsonlite_1.7.1 [31] zoo_1.8-8 glue_1.4.2 survminer_0.4.8 [34] gtable_0.3.0 webshot_0.5.2 V8_3.4.0 [37] distributional_0.2.1 car_3.0-10 pkgbuild_1.1.0 [40] rstan_2.21.2 abind_1.4-5 scales_1.1.1 [43] mvtnorm_1.1-1 DBI_1.1.0 rstatix_0.6.0 [46] miniUI_0.1.1.1 viridisLite_0.3.0 xtable_1.8-4 [49] foreign_0.8-80 km.ci_0.5-2 stats4_4.0.3 [52] StanHeaders_2.21.0-6 DT_0.16 htmlwidgets_1.5.2 [55] httr_1.4.2 threejs_0.3.3 RColorBrewer_1.1-2 [58] arrayhelpers_1.1-0 ellipsis_0.3.1 reshape_0.8.8 [61] pkgconfig_2.0.3 loo_2.3.1 farver_2.0.3 [64] dbplyr_1.4.4 labeling_0.3 tidyselect_1.1.0 [67] rlang_0.4.8 reshape2_1.4.4 later_1.1.0.1 [70] munsell_0.5.0 cellranger_1.1.0 tools_4.0.3 [73] cli_2.1.0 generics_0.0.2 broom_0.7.1 [76] evaluate_0.14 fastmap_1.0.1 yaml_2.2.1 [79] processx_3.4.4 fs_1.5.0 zip_2.1.1 [82] survMisc_0.5.5 whisker_0.4 mime_0.9 [85] projpred_2.0.2 xml2_1.3.2 compiler_4.0.3 [88] bayesplot_1.7.2 shinythemes_1.1.2 rstudioapi_0.11 [91] gamm4_0.2-6 curl_4.3 ggsignif_0.6.0 [94] reprex_0.3.0 statmod_1.4.34 stringi_1.5.3 [97] highr_0.8 ps_1.4.0 Brobdingnag_1.2-6 [100] lattice_0.20-41 nloptr_1.2.2.2 markdown_1.1 [103] KMsurv_0.1-5 shinyjs_2.0.0 vctrs_0.3.4 [106] pillar_1.4.6 lifecycle_0.2.0 bridgesampling_1.0-0 [109] data.table_1.13.0 httpuv_1.5.4 R6_2.4.1 [112] promises_1.1.1 rio_0.5.16 gridExtra_2.3 [115] codetools_0.2-16 boot_1.3-25 colourpicker_1.1.0 [118] MASS_7.3-53 gtools_3.8.2 assertthat_0.2.1 [121] rprojroot_1.3-2 withr_2.3.0 shinystan_2.5.0 [124] mgcv_1.8-33 parallel_4.0.3 hms_0.5.3 [127] grid_4.0.3 coda_0.19-4 minqa_1.2.4 [130] rmarkdown_2.4 carData_3.0-4 ggpubr_0.4.0 [133] git2r_0.27.1 shiny_1.5.0 lubridate_1.7.9 [136] base64enc_0.1-3 dygraphs_1.1.1.6