MARADONER

Hidden covariates

A covariate can be passed to the model only if it was written down. Often it was not: the age of a donor is unknown, the batches went unrecorded, the samples differ in the share of some cell type. The --activity-lowrank option of fit is made for such data. With it, MARADONER estimates hidden properties of samples along which the activities of many motifs change together. Below they are called factors. We have frequently used the option to reconstruct covariates missing from the annotation of samples.

What is a factor?

A factor is a covariate with unknown values. An activity is decomposed the same way as with a covariate:

Activity motif, sample = Mean per motif + Effect random × Factor per sample + Deviation motif, group

There are 2 differences. First, the values of a factor (the highlighted box, a number per sample) are estimated from the data. Second, the effects of a factor on motifs are random. No effect is estimated for a single motif. The effects are assumed to have the variance $τ$ of the motif, so a motif that varies much between groups is allowed to follow a factor as much.

In the basic model, the deviations of activities in different samples are independent, and their covariance matrix between samples is the diagonal matrix $G$ of the multipliers $ν$. With $k$ factors the matrix becomes $$G + V V^T.$$ Here, $V$ has a row per sample and a column per factor. The rank of $V V^T$ is $k$, a low one next to the number of samples, hence the name of the option.

How does a hidden covariate show itself?

Let's take the dataset of the previous section, where age has an effect on 400 motifs, and fit it without the table of covariates. The activities/activity_raw.tsv table of the exported folder holds the deviations predicted for single samples. For each pair of samples we compute the correlation of their deviations over the motifs:

2 heatmaps of correlations between samples: with a red and blue pattern when age is ignored, and blank off the diagonal when age is a covariate2 heatmaps of correlations between samples: with a red and blue pattern when age is ignored, and blank off the diagonal when age is a covariate
Correlations between samples, with the samples ordered by age. On the left age is ignored. On the right it is a covariate.

On the left, samples of a close age have similar deviations, and the youngest samples are opposite to the oldest. The basic model has no parameter for that: the deviations of different samples are independent in it. On the right the same data are fitted with age as a covariate, and nothing is left off the diagonal.

With real data there is no known age to order the samples by. The factors are a way to find the order.

Example

First, we create a project from the same data with no covariates, and fit it with 1 factor:

> maradoner create Hidden demo/expression.tsv demo/loadings.tsv --sample-groups demo/groups.json --loading-transform none
> maradoner fit Hidden --activity-lowrank 1

Here, the value of the option is the number of factors. Among the usual lines, the output of fit has these:

[activity-lowrank k=1] Sigma/G REML deviance -284531.204 -> -297730.330 (gain +13199.127)
  activity covariance: diagonal G + rank-1 update (REML deviance -297730.33).

The first line compares the model with the factor to the model without it. The lower the deviance the better, and the gain is the improvement brought by the factor. The mean activities and their standard errors are estimated with the factor in the model.

The factors are written by export:

> maradoner predict Hidden
> maradoner export Hidden hidden_results

The hidden_results/params/factors.tsv table has a row per sample and a column per factor:

sample	factor_1
col_1	0.20619442773034688
col_2	0.46976280119087915

Now the factor can be compared to the age kept away from the model:

The factor of each sample against the age of the sample: the dots lie on a descending lineThe factor of each sample against the age of the sample: the dots lie on a descending line
A dot is a sample. The correlation is -0.999.

The age is reconstructed. The minus is no error: $V V^T$ is the same for $V$ and $-V$, so the sign of a factor is arbitrary. Neither is a factor measured in years. It orders the samples and gives the distances between them on a scale with no units.

The motif variances gain as well. Their rank correlation with the true ones is 0.63 in the model without a factor and 0.97 with it. The latter is the same number as with the real age as a covariate.

From a factor to a covariate

A factor tells which samples are alike and tells nothing about single motifs. For the effects on motifs with tests, it can be passed back to the model as a covariate. The factors.tsv table is a valid table of covariates:

> maradoner create MyProject demo/expression.tsv demo/loadings.tsv --sample-groups demo/groups.json --loading-transform none --covariates hidden_results/params/factors.tsv
> maradoner fit MyProject
> maradoner predict MyProject
> maradoner export MyProject results

The first rows of results/params/covariates/factor_1.tsv are

	effect	std	z	pvalue	fdr
motif_1	-0.04214135416082032	0.04435864816188356	-0.9500143919406336	0.342104939861239	0.44544914044432166
motif_2	0.48314283445826667	0.01695392505188151	28.497402989560136	1.261530997922801e-178	1.5769137474035013e-176
inactive_motif_3	-0.017758757118260464	0.01693309123269336	-1.0487604935342794	0.2942883644108639	0.39448842414324925

With the real age, the effect on motif_2 was -0.48. Here it is 0.48, as the factor runs against the age. At an FDR of 0.05 the factor has an effect on 302 motifs, and 301 of them are among the 303 found with the real age.

How many factors?

A factor lowers the deviance even when there is nothing to find, as it brings a parameter per sample to the model. Hence the question is whether the gain is larger than that. The select-rank command measures both. It fits the models with 1, 2 and more factors one after another. For each new factor it also draws several datasets from the model without that factor, and adds the factor to each of them. The gains on the simulated datasets tell what a factor gets when there is nothing to find:

> maradoner select-rank Hidden
┏━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┓
┃         ┃               ┃           ┃ Gain on simulated data      ┃         ┃          ┃
┃ Factors ┃ REML deviance ┃ Gain      ┃ mean (min .. max)           ┃ p-value ┃ Accepted ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━┩
│ 0       │ -284531.204   │           │                             │         │          │
│ 1       │ -297730.529   │ 13199.325 │ 101.041 (86.793 .. 114.716) │ 1.7e-12 │ yes      │
│ 2       │ -297821.281   │ 90.752    │ 93.529 (81.801 .. 99.063)   │ 0.63    │ no       │
└─────────┴───────────────┴───────────┴─────────────────────────────┴─────────┴──────────┘
Number of factors supported by the data: 1 (fit it with maradoner fit Hidden --activity-lowrank 1)

Here, the p-value is the probability of a gain that large on one more simulated dataset, with the simulated gains taken for normally distributed. A factor is accepted if the p-value is below 0.05. The first factor gains over a hundred times more than a factor does on simulated data. The second one gains what a factor gets from data with nothing to find, so the search stops, and 1 factor is the answer. The command needs a fitted project, and any fit will do. It changes nothing in the project: the chosen number is then passed to fit.

For the dataset of the Quickstart, with nothing hidden in it, the answer is 0:

┏━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┓
┃         ┃               ┃        ┃ Gain on simulated data     ┃         ┃          ┃
┃ Factors ┃ REML deviance ┃ Gain   ┃ mean (min .. max)          ┃ p-value ┃ Accepted ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━┩
│ 0       │ -118060.612   │        │                            │         │          │
│ 1       │ -118157.459   │ 96.847 │ 93.847 (85.904 .. 102.661) │ 0.38    │ no       │
└─────────┴───────────────┴────────┴────────────────────────────┴─────────┴──────────┘
Number of factors supported by the data: 0

The same holds for the project of the previous section with age as a covariate: once age is in the model, a factor gains 88 against 85 to 112 on simulated data.

Name Description Default value
--max-rank <int> The largest number of factors to try 5
--null <int> The number of simulated datasets per factor, at least 3. With 0 the command only reports the gains, for all numbers of factors up to --max-rank 5
--alpha <float> A factor is accepted if its p-value is below this level 0.05
--full All numbers of factors up to --max-rank are tried, also after a factor is not accepted Disabled
--seed <int> The seed of the simulated datasets 0
--output <path> The table is also saved to a tab-separated file

The p-value rests on a handful of simulations and on a normal approximation, so it is a rough one. It leaves no doubt in the examples above. When a gain is close to the simulated ones, more simulations (--null) make it steadier.

A few more facts about the number of factors:

What does it cost?

What about --error-lowrank?

It is the same idea applied to the noise: the covariance matrix of errors between samples turns from the diagonal $D$ into $D + W W^T$. The 2 options differ in where a hidden property of samples acts. If it changes the expression through motifs, it belongs to the activities. If it changes the expression of promoters regardless of their motifs, it belongs to the noise.

On this page