Using the delta method, estimate the log-fold change from a state given by a vector contrast0 and the state(s) given by contrast1.
logFC(zlmfit, contrast0, contrast1)
getLogFC(zlmfit, contrast0, contrast1)
ZlmFit output
vector of coefficients giving baseline contrast, or a Hypothesis
. If missing, then the '(Intercept)' is used as baseline.
matrix of coefficients giving comparison contrasts, or a Hypothesis
. If missing, then all non-(Intercept) coefficients are compared.
list of matrices `logFC` and `varLogFC`, giving the log-fold-changes for each contrast (columns) and genes (rows) and the estimated sampling variance thereof
The log-fold change is defined as follows. For each gene, let \(u(x)\) be the expected value of the continuous component, given a covariate x and the estimated coefficients coefC
, ie, \(u(x)=\) crossprod(x, coefC)
.
Likewise, Let \(v(x)=\) 1/(1+exp(-crossprod(coefD, x)))
be the expected value of the discrete component.
The log fold change from contrast0 to contrast1 is defined as
$$u(contrast1)v(contrast1)-u(contrast0)v(contrast0).$$
Note that for this to be a log-fold change, then the regression for u must have been fit on the log scale. This is returned in the matrix logFC
.
An approximation of the variance of logFC
(applying the delta method to formula defined above) is provided in varLogFC
.
getLogFC
: Return results as a perhaps friendlier data.table
1. When method='bayesglm'
(the default), it's no longer necessarily true that the log fold change from condition A to B will be the inverse of the log fold change from B to A if the models are fit separately.
This is due to the shrinkage in bayesglm
.
2. The log fold change can be small, but the Hurdle p-value small and significant when the sign of the discrete and continuous model components are discordant so that the marginal log fold change cancels out. The large sample sizes present in many single cell experiments also means that there is substantial power to detect even small changes.
3. When there is no expression in a gene for a coefficient that is non-zero in either condition0
or condition1
we return NA
because there is not any information
to estimate the continuous component. Technically we might return plus or minus infinity,
but there is not a straightforward way to estimate a confidence interval in any case.
See https://support.bioconductor.org/p/99244/ for details
data(vbetaFA)
zz <- zlm( ~ Stim.Condition+Population, vbetaFA[1:5,])
#>
#> Done!
##log-fold changes in terms of intercept (which is Stim(SEB) and CD154+VbetaResponsive)
lfcStim <- logFC(zz)
##If we want to compare against unstim, we can try the following
coefnames <- colnames(coef(zz, 'D'))
contrast0 <- setNames(rep(0, length(coefnames)), coefnames)
contrast0[c('(Intercept)', 'Stim.ConditionUnstim')] <- 1
contrast1 <- diag(length(coefnames))
rownames(contrast1)<-colnames(contrast1)<-coefnames
contrast1['(Intercept)',]<-1
lfcUnstim <- logFC(zz, contrast0, contrast1)
##log-fold change with itself is 0
stopifnot(all(lfcUnstim$logFC[,2]==0))
##inverse of log-fold change with Stim as reference
stopifnot(all(lfcStim$logFC[,1]==(-lfcUnstim$logFC[,1])))
##As a data.table:
getLogFC(zz)
#> primerid contrast logFC varLogFC
#> 1: B3GAT1 Stim.ConditionUnstim -0.05717195 0.6324474
#> 2: B3GAT1 PopulationCD154+VbetaUnresponsive -0.28467795 0.4957791
#> 3: B3GAT1 PopulationCD154-VbetaResponsive NaN NaN
#> 4: B3GAT1 PopulationCD154-VbetaUnresponsive -0.73597249 0.2502919
#> 5: B3GAT1 PopulationVbetaResponsive -0.73713842 0.2645474
#> 6: B3GAT1 PopulationVbetaUnresponsive -0.37754594 0.3765092
#> 7: BAX Stim.ConditionUnstim -0.43275760 1.6497560
#> 8: BAX PopulationCD154+VbetaUnresponsive -1.29197981 2.6550423
#> 9: BAX PopulationCD154-VbetaResponsive -0.61352080 2.7503564
#> 10: BAX PopulationCD154-VbetaUnresponsive -1.95097043 1.7282713
#> 11: BAX PopulationVbetaResponsive 1.08078307 2.2660863
#> 12: BAX PopulationVbetaUnresponsive 1.33097535 2.3109374
#> 13: BCL2 Stim.ConditionUnstim -1.64819989 0.2828739
#> 14: BCL2 PopulationCD154+VbetaUnresponsive 0.60475231 1.4007298
#> 15: BCL2 PopulationCD154-VbetaResponsive NaN NaN
#> 16: BCL2 PopulationCD154-VbetaUnresponsive -0.19121849 0.7824061
#> 17: BCL2 PopulationVbetaResponsive 1.50241843 1.3773279
#> 18: BCL2 PopulationVbetaUnresponsive 1.53341248 1.4119269
#> 19: CCL2 Stim.ConditionUnstim 1.19557602 3.4427019
#> 20: CCL2 PopulationCD154+VbetaUnresponsive 0.05906539 0.9221676
#> z
#> 1: -0.07189038
#> 2: -0.40430559
#> 3: NaN
#> 4: -1.47108647
#> 5: -1.43316857
#> 6: -0.61529305
#> 7: -0.33692625
#> 8: -0.79290290
#> 9: -0.36994299
#> 10: -1.48403698
#> 11: 0.71796010
#> 12: 0.87553985
#> 13: -3.09894170
#> 14: 0.51097582
#> 15: NaN
#> 16: -0.21617914
#> 17: 1.28018349
#> 18: 1.29048477
#> 19: 0.64435839
#> 20: 0.06150751
#> [ reached getOption("max.print") -- omitted 11 rows ]