Skip to content

Commit

Permalink
correct model contrasts vignette with correct code
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeDMorgan committed Jun 3, 2024
1 parent 492a163 commit d60e51f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 2.0.1 (2023-11-09)
# 2.0.X (2024-10-30)
+ Warning on GLMM if glmm.solver not set
+ Bug fix in model contrasts vignette with multiple contrasts

# 2.0.1 (2024-04-30)
+ Introduce NB-GLMM into Milo 2.0 for random effect variables and modelling dependencies between observations
+ Diagnostic function for checking model separation for experimental variables, i.e. splitting zero from non-zero counts perfectly
+ Vignette describing basic usage of GLMM functions in `testNhoods`
Expand Down
7 changes: 4 additions & 3 deletions R/testNhoods.R
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,12 @@ testNhoods <- function(x, design, design.df, kinship=NULL,
} else {
# need to use legacy=TRUE to maintain original edgeR behaviour
fit <- glmQLFit(dge, x.model, robust=robust, legacy=TRUE)
message("Running with model contrasts")
if(!is.null(model.contrasts)){
mod.constrast <- makeContrasts(contrasts=model.contrasts, levels=x.model)
res <- as.data.frame(topTags(glmQLFTest(fit, contrast=mod.constrast),
sort.by='none', n=Inf))
pre.res <- topTags(glmQLFTest(fit, contrast=mod.constrast),
sort.by='none', n=Inf)
res <- as.data.frame(pre.res)
} else{
n.coef <- ncol(x.model)
res <- as.data.frame(topTags(glmQLFTest(fit, coef=n.coef), sort.by='none', n=Inf))
Expand All @@ -554,7 +556,6 @@ testNhoods <- function(x, design, design.df, kinship=NULL,

res$Nhood <- as.numeric(rownames(res))
message("Performing spatial FDR correction with ", fdr.weighting[1], " weighting")
# res1 <- na.omit(res)
mod.spatialfdr <- graphSpatialFDR(x.nhoods=nhoods(x),
graph=graph(x),
weighting=fdr.weighting,
Expand Down
22 changes: 17 additions & 5 deletions vignettes/milo_contrasts.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ how to use contrasts to compare multiple groups.

```{r}
contrast.all <- c("Age1wk - Age4wk", "Age4wk - Age16wk", "Age16wk - Age32wk", "Age32wk - Age52wk")
# contrast.all <- c("Age1wk - Age4wk", "Age16wk - Age32wk")
# this is the edgeR code called by `testNhoods`
model <- model.matrix(~ 0 + Age, data=thy.design)
Expand All @@ -128,22 +129,33 @@ This shows the contrast matrix. If we want to test each of these comparisons the
multiple testing correction to the spatial FDR values.

```{r}
contrast1.res <- testNhoods(thy.milo, design=~ Age, design.df=thy.design, fdr.weighting="graph-overlap", model.contrasts = mod.constrast)
contrast1.res <- testNhoods(thy.milo, design=~0+ Age, design.df=thy.design, fdr.weighting="graph-overlap", model.contrasts = contrast.all)
head(contrast1.res)
```

This matrix of contrasts will perform a quasi-likelihood F-test over all contrasts, hence a single p-value and spatial FDR are returned. Log fold changes are returned for
all contrasts on each level of the `Age` variable, which gives 20 log-fold change columns for each - this is the default behaviour of `glmQLFTest` in the `edgeR` package
This matrix of contrasts will perform a quasi-likelihood F-test over all 5 contrasts, hence a single p-value and spatial FDR are returned. Log fold changes are returned for
each contrast of the `Age` variable, which gives 1 log-fold change column for each - this is the default behaviour of `glmQLFTest` in the `edgeR` package
which is what Milo uses for hypothesis testing. In general, and to avoid confusion, we recommend testing each pair of contrasts separately if these are the comparisons
of interest, as shown below.

```{r}
# compare weeks 4 and 16, with week 4 as the reference.
cont.4vs16.res <- testNhoods(thy.milo, design=~ Age, design.df=thy.design, fdr.weighting="graph-overlap", model.contrasts = mod.constrast[c("Age4wk"), c("Age4wk - Age16wk")])
cont.4vs16.res <- testNhoods(thy.milo, design=~0+ Age, design.df=thy.design, fdr.weighting="graph-overlap", model.contrasts = c("Age4wk - Age16wk"))
head(cont.4vs16.res)
```

Now we have a single logFC which compares nhood abundance between week 4 and week 16.
Now we have a single logFC which compares nhood abundance between week 4 and week 16 - as we can see the LFC estimates should be the same, but the SpatialFDR will be different.

```{r, fig.height=4, fig.width=7.5}
par(mfrow=c(1, 2))
plot(contrast1.res$logFC.Age4wk...Age16wk, cont.4vs16.res$logFC,
xlab="4wk vs. 16wk LFC\nsingle contrast", ylab="4wk vs. 16wk LFC\nmultiple contrast")
plot(contrast1.res$SpatialFDR, cont.4vs16.res$SpatialFDR,
xlab="Spatial FDR\nsingle contrast", ylab="Spatial FDR\nmultiple contrast")
```


Contrasts are not limited to these simple pair-wise comparisons, we can also group levels together for comparisons. For instance, imagine we want to know
what the effect of the cell counts in the week 1 mice is _compared to all other time points_.
Expand Down

0 comments on commit d60e51f

Please sign in to comment.