Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
bbolker committed Mar 21, 2024
1 parent aea5f76 commit 54b1059
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 80 deletions.
87 changes: 65 additions & 22 deletions docs/lectures/Contrasts.notes.html

Large diffs are not rendered by default.

93 changes: 51 additions & 42 deletions docs/lectures/Contrasts.slides.html

Large diffs are not rendered by default.

40 changes: 24 additions & 16 deletions lectures/Contrasts.rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: "Introduction to contrasts, and using emmeans in R"
bibliography: "../qmee.bib"
author: "Ian Dworkin"
date: "`r format(Sys.time(),'%d %b %Y')`"
output:
Expand Down Expand Up @@ -480,7 +481,7 @@ head(size_dat)

### A quick visual summary

```{r}
```{r beeswarm, fig.width = 14, out.width="100%"}
ggplot(size_dat,
aes( y = length, x = selection:sex, col = sex, shape = replicate)) +
geom_quasirandom(alpha = 0.8, size = 1.4) +
Expand Down Expand Up @@ -537,30 +538,34 @@ This is where contrasts become SO useful!

#### Estimated marginal means
First, just the estimated means, for each sex, by selective regime
```{r}
```{r marg-means-plot, fig.width = 14}
thorax_emm <- emmeans(mod1_thorax, specs = ~ sex | selection)
thorax_emm
## rotate strip labels:
## https://stackoverflow.com/questions/40484090/rotate-switched-facet-labels-in-ggplot2-facet-grid
rot_strips <- theme_bw() +
theme(text = element_text(size = 16),
strip.text.y.right = element_text(angle = 0))
plot(thorax_emm,
xlab = "model estimates, thorax length, log2 microM") +
theme_bw() +
theme(text = element_text(size = 16))
xlab = "model estimates, thorax length, log2 µm") +
rot_strips
```


#### side note: Back-transforming in emmeans
Like I mentioned emmeans can recognize the log2 transformation, so if you prefer the measures or plots in $\mu m$ response scale.

```{r}
```{r fig.width = 14}
thorax_emm_response <- emmeans(mod1_thorax, specs = ~ sex | selection, type = "response")
thorax_emm_response
plot(thorax_emm_response,
xlab = "model estimates, thorax length, microM") +
theme_bw() +
theme(text = element_text(size = 16))
xlab = "model estimates, thorax length, µm") +
rot_strips
```


Expand All @@ -583,16 +588,15 @@ confint(SSD_contrasts_treatment)
```{r}
plot(SSD_contrasts_treatment) +
geom_vline(xintercept = 0, lty = 2, alpha = 0.5) +
labs(x = "sexual size dimorphism") +
theme_bw() +
theme(text = element_text(size = 16))
labs(x = "sexual size dimorphism") +
rot_strips
```


### The interaction contrast
But what we really want is to see how dimorphism changes in the selected treatments, relative to the controls. This is the interaction contrast (contrast of contrasts)

```{r}
```{r fig.width = 14}
thorax_ssd <- emmeans(mod1_thorax, pairwise ~ sex*selection) # warning is letting you know these are not of general use. We only do this as we are forming an interaction contrast.
thorax_ssd_contrasts <- contrast(thorax_ssd[[1]],
Expand All @@ -606,9 +610,9 @@ confint(thorax_ssd_contrasts)
plot(thorax_ssd_contrasts) +
geom_vline(xintercept = 0, lty = 2, alpha = 0.5) +
labs(x = "change in SSD relative to control lineages", y = "comparison") +
theme_bw() +
theme(text = element_text(size = 16))
labs(x = "change in SSD relative to control lineages", y = "comparison") +
theme_bw() + theme(text = element_text(size = 16))
```


Expand All @@ -628,4 +632,8 @@ Chapter 5 of the book by Irizarry and Love, Data Analysis for the Life Sciences
overview of contrast codings
https://rpubs.com/timflutre/tuto_contrasts

@schadHow2018

https://bbolker.github.io/mixedmodels-misc/notes/contrasts.pdf

## references
16 changes: 16 additions & 0 deletions qmee.bib
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,19 @@ @article{vanhoveCollinearity2021
keywords = {multiple regression, interpreting regression models, regression assumptions},
file = {Full Text PDF:/home/bolker/Documents/zotero_new/storage/TKJW5RM6/Vanhove - 2021 - Collinearity isn't a disease that needs curing.pdf:application/pdf},
}

@article{schadHow2018,
title = {How to Capitalize on a Priori Contrasts in Linear (Mixed) Models: {{A}} Tutorial},
shorttitle = {How to Capitalize on a Priori Contrasts in Linear (Mixed) Models},
author = {Schad, Daniel J. and Hohenstein, Sven and Vasishth, Shravan and Kliegl, Reinhold},
year = {2018},
month = jul,
journal = {arXiv:1807.10451 [stat]},
eprint = {1807.10451},
primaryclass = {stat},
urldate = {2018-09-10},
abstract = {Factorial experiments in research on memory, language, and in other areas are often analyzed using analysis of variance (ANOVA). However, for experimental factors with more than two levels, the ANOVA omnibus F-test is not informative about the source of a main effect or interaction. This is unfortunate as researchers typically have specific hypotheses about which condition means differ from each other. A priori contrasts (i.e., comparisons planned before the sample means are known) between specific conditions or combinations of conditions are the appropriate way to represent such hypotheses in the statistical model. Many researchers have pointed out that contrasts should be "tested instead of, rather than as a supplement to, the ordinary `omnibus' F test" (Hayes, 1973, p. 601). In this tutorial, we explain the mathematics underlying different kinds of contrasts (i.e., treatment, sum, repeated, Helmert, and polynomial contrasts), discuss their properties, and demonstrate how they are applied in the R System for Statistical Computing (R Core Team, 2018). In this context, we explain the generalized inverse which is needed to compute the weight coefficients for contrasts that test hypotheses that are not covered by the default set of contrasts. A detailed understanding of contrast coding is crucial for successful and correct specification in linear models (including linear mixed models). Contrasts defined a priori yield far more precise confirmatory tests of experimental hypotheses than standard omnibus F-test.},
archiveprefix = {arxiv},
keywords = {Statistics - Methodology},
file = {/home/bolker/Zotero/storage/WU4VJNID/Schad et al. - 2018 - How to capitalize on a priori contrasts in linear .pdf;/home/bolker/Zotero/storage/YPA8WMCU/1807.html}
}

0 comments on commit 54b1059

Please sign in to comment.