<p>There are two topics here to be discussed. Gene Set Enrichment Analysis and a kernel Density plot.</p>
<h2>GSEA</h2>
<p>The Wikipedia page about <a href="https://en.wikipedia.org/wiki/Gene_set_enrichment_analysis" rel="nofollow noreferrer">Gene Set Enrichment Analysis</a> covers well the principle and the tools that can be used to calculate it. To cite it:</p>
<blockquote>
<p>The general steps are summarized below:</p>
<p>Calculate the enrichment score (ES) that represents the amount to which the genes in the set are over-represented at either the top or bottom of the list. This score is a Kolmogorov–Smirnov-like statistic [a ranking based statistic].</p>
<p>Estimate the statistical significance of the ES. This calculation is done by a phenotypic-based permutation test in order to produce a null distribution for the ES. The P value is determined by comparison to the null distribution.</p>
<p>Calculating significance this way tests for the dependence of the gene set on the diagnostic/phenotypic labels</p>
</blockquote>
<p>Basically, you want to test if the ranking is random or not. Therefore you make a random dataset and test whether your set is like that. One assumption underlying this is that the output of the gene whatever it may be is <em>not</em> normally distributed (parametric, follows a bell curve), hence the use of ranking, which removes this (you lose signal but you lose biases). I am assuming the non-parametric nature of your ouput values was tested or is a known non-parametric value, such as RPKM or log2 fold change (by virtue of being a ratio). In other words, the magnitude difference between your samples is lost, but their order is not.</p>
<p>There are several tools, some online, that do this enrichment testing for you depending on the task, some even work with gene ontologies.</p>
<h2>KDE</h2>
<p>A kernel density plot is a like a histogram, but smoothed, albeit not in a moving average way. In a histogram you divide your x-axis into bins: it is discreet and you get an integer count per bin. In a kernel density plot the data is fitted to &quot;probability density function&quot;, an equation which given x will give y, where the integral of the curve is one, hence why y is density not counts. The x axis will be your bins-like value, in this case rank. The binning window metric for a KS function is called bandwidth.</p>
<p>See <a href="https://uk.mathworks.com/help/stats/kernel-distribution.html" rel="nofollow noreferrer">Matlab documentation</a> for a fairly non-opaque explanation.</p>
<p>However, if you want to compare histograms, I strongly suggest violin plots, not because they are strongly in vogue, but because you can compare histograms without worrying about overlaying them.</p>

CC BY-SA 4.0
2019-11-26T03:30:26.467Z
Thank you for these resources.
Angus Campbell
CC BY-SA 4.0
2019-11-25T18:15:31.457Z
2019-11-25T18:15:31.457Z
2020-06-18T08:30:41.743Z
Matteo Ferla
<p>I need help interpreting a kernal density plot and a histogram of this data. I have provided some context below let me know if you need more information</p>

<p>A co-worker that I am collaborating with has a list of genes ranked by expression fold change.  He says most of the genes in one dataset are noise with respect to his analysis so he is trying to filter this out.  He has sent me a document of his methods for generating a new list of genes which he think are relevant.  he has run a permutation test on the ranked genes I have provided a quote here:</p>

<p>"To determine which genes contributed more strongly in real data, as compared by permuted data, the rankings achieved for each gene in real data were compared to the shuffled rankings. For each gene, and each permutation type, a p-value was calculated by counting how many times that gene was ranked higher in shuffled data than in real data, and dividing it by the number of comparisons (16 * 256 = 4096). To achieve p&lt;0.05 a gene’s real ranking would have to be higher than 95% of its shuffled rankings."</p>

<p>Following this he provides kernel density plots of the genes under different rankings.</p>

<p>"Below are kernel density estimation (kde) plots, overlaid on histograms, of the rankings of two genes randomly selected from the few genes with p&lt;0.05 vs all three permutations."</p>

<p><a href="https://i.sstatic.net/011cw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/011cw.png" alt="enter image description here"></a></p>

<p>"Below are kde plots of the rankings of two genes randomly selected from the many genes with p>0.05 vs all three permutations."</p>

<p><a href="https://i.sstatic.net/LBAEa.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LBAEa.png" alt="enter image description here"></a></p>

<p>What am I looking at?  What does this tell me about the genes ranking statistics.</p>

CC BY-SA 4.0
2019-11-23T22:52:16.990Z
Are you asking what is a kernel density plot? [The Matlab how-to is the most informative](https://www.mathworks.com/help/stats/kernel-distribution.html).
Or are you asking why this enrichment analysis was favoured over others?
Matteo Ferla
CC BY-SA 4.0
2019-11-24T05:37:20.383Z
Both I guess.  I don't understand what the x and y axes are in either of the graphs.  Is the second chart showing probability density and the histogram the actual distribution?
Angus Campbell
CC BY-SA 4.0
2019-11-24T10:11:26.700Z
A kernel density plot is a like a histogram, but smoothed. Your coworker has given you rough data, _e.g._ unlabelled axes and little explanation. The x-axis is number of genes and the y-axis is the "density", which isn't "number of counts in a bin", but a number so that the area under the curve is one (it's continuous not discreet). So for now, I strongly suggest not worrying about that and concentrating on the gene enrichment analysis. The permutation test is to get statistical support of the enrichment set. Therefore, _what you need is the enriched gene list_ not the metrics of support.
Matteo Ferla
CC BY-SA 4.0
2019-11-24T15:54:40.657Z
If x axis is the number of genes, how come there are negative values in the second plot?
haci
CC BY-SA 4.0
2019-11-24T20:33:58.843Z
I have the gene list, I am just trying to understand the supporting statistics so I could in the future generate my own list based on this method if I wanted to.
Angus Campbell
CC BY-SA 4.0
2019-11-24T20:35:57.960Z
I believe the negative values may be due to the fact that the original dataset reported the fold changes as the beta of the log2, ie the exponent.  So a gene at 0.5 expression would be given -1.
Angus Campbell
CC BY-SA 4.0
2019-11-25T14:32:03.193Z
No. The negative values are due to the kernel density function _extrapolating_ beyond the 0 x-value in order to justify the high sample at zero. It is a common problem with KDE, or smoothing in general. Normally bounds are set beforehand to avoid this or, sloppily, the window set to exclude these.
Matteo Ferla
CC BY-SA 4.0
2019-11-25T14:34:43.093Z
If understanding is what you seek please read [wiki:Gene set enrichment analysis](https://en.wikipedia.org/wiki/Gene_set_enrichment_analysis) as opposed to reverse engineering these results as the "smoothed histogram like plot" (KDE) causes more confusion that it is worth (for info on that see Matlab help above).
Matteo Ferla
<r><gene-expression><gene-ontology>
Interpreting a kernel density function plot of gene expression
# Interpreting a kernel density function plot of gene expression

(Asked by: username_0 on 2019-11-23)

I need help interpreting a kernal density plot and a histogram of this data. I have provided some context below let me know if you need more information

A co-worker that I am collaborating with has a list of genes ranked by expression fold change. He says most of the genes in one dataset are noise with respect to his analysis so he is trying to filter this out. He has sent me a document of his methods for generating a new list of genes which he think are relevant. he has run a permutation test on the ranked genes I have provided a quote here:

"To determine which genes contributed more strongly in real data, as compared by permuted data, the rankings achieved for each gene in real data were compared to the shuffled rankings. For each gene, and each permutation type, a p-value was calculated by counting how many times that gene was ranked higher in shuffled data than in real data, and dividing it by the number of comparisons (16 \* 256 = 4096). To achieve p<0.05 a gene’s real ranking would have to be higher than 95% of its shuffled rankings."

Following this he provides kernel density plots of the genes under different rankings.

"Below are kernel density estimation (kde) plots, overlaid on histograms, of the rankings of two genes randomly selected from the few genes with p<0.05 vs all three permutations."

[![enter image description here](https://i.sstatic.net/011cw.png)](https://i.sstatic.net/011cw.png)

"Below are kde plots of the rankings of two genes randomly selected from the many genes with p>0.05 vs all three permutations."

[![enter image description here](https://i.sstatic.net/LBAEa.png)](https://i.sstatic.net/LBAEa.png)

What am I looking at? What does this tell me about the genes ranking statistics.

**Tags:** r, gene-expression, gene-ontology
> **Comments:**
> **username_1**: Are you asking what is a kernel density plot? [The Matlab how-to is the most informative](https://www.mathworks.com/help/stats/kernel-distribution.html). Or are you asking why this enrichment analysis was favoured over others?  
> **username_0**: Both I guess.  I don't understand what the x and y axes are in either of the graphs.  Is the second chart showing probability density and the histogram the actual distribution?  
> **username_1**: A kernel density plot is a like a histogram, but smoothed. Your coworker has given you rough data, _e.g._ unlabelled axes and little explanation. The x-axis is number of genes and the y-axis is the "density", which isn't "number of counts in a bin", but a number so that the area under the curve is one (it's continuous not discreet). So for now, I strongly suggest not worrying about that and concentrating on the gene enrichment analysis. The permutation test is to get statistical support of the enrichment set. Therefore, _what you need is the enriched gene list_ not the metrics of support.  
> **username_2**: If x axis is the number of genes, how come there are negative values in the second plot?  
> **username_0**: I have the gene list, I am just trying to understand the supporting statistics so I could in the future generate my own list based on this method if I wanted to.  
> **username_0**: I believe the negative values may be due to the fact that the original dataset reported the fold changes as the beta of the log2, ie the exponent.  So a gene at 0.5 expression would be given -1.  
> **username_1**: No. The negative values are due to the kernel density function _extrapolating_ beyond the 0 x-value in order to justify the high sample at zero. It is a common problem with KDE, or smoothing in general. Normally bounds are set beforehand to avoid this or, sloppily, the window set to exclude these.  
> **username_1**: If understanding is what you seek please read [wiki:Gene set enrichment analysis](https://en.wikipedia.org/wiki/Gene_set_enrichment_analysis) as opposed to reverse engineering these results as the "smoothed histogram like plot" (KDE) causes more confusion that it is worth (for info on that see Matlab help above).  

---

## 1 answers

### Answer by username_1 on 2019-11-25. Score: 2 | Accepted answer

There are two topics here to be discussed. Gene Set Enrichment Analysis and a kernel Density plot.

## GSEA

The Wikipedia page about [Gene Set Enrichment Analysis](https://en.wikipedia.org/wiki/Gene_set_enrichment_analysis) covers well the principle and the tools that can be used to calculate it. To cite it:

> The general steps are summarized below:
>
> Calculate the enrichment score (ES) that represents the amount to which the genes in the set are over-represented at either the top or bottom of the list. This score is a Kolmogorov–Smirnov-like statistic [a ranking based statistic].
>
> Estimate the statistical significance of the ES. This calculation is done by a phenotypic-based permutation test in order to produce a null distribution for the ES. The P value is determined by comparison to the null distribution.
>
> Calculating significance this way tests for the dependence of the gene set on the diagnostic/phenotypic labels

Basically, you want to test if the ranking is random or not. Therefore you make a random dataset and test whether your set is like that. One assumption underlying this is that the output of the gene whatever it may be is *not* normally distributed (parametric, follows a bell curve), hence the use of ranking, which removes this (you lose signal but you lose biases). I am assuming the non-parametric nature of your ouput values was tested or is a known non-parametric value, such as RPKM or log2 fold change (by virtue of being a ratio). In other words, the magnitude difference between your samples is lost, but their order is not.

There are several tools, some online, that do this enrichment testing for you depending on the task, some even work with gene ontologies.

## KDE

A kernel density plot is a like a histogram, but smoothed, albeit not in a moving average way. In a histogram you divide your x-axis into bins: it is discreet and you get an integer count per bin. In a kernel density plot the data is fitted to "probability density function", an equation which given x will give y, where the integral of the curve is one, hence why y is density not counts. The x axis will be your bins-like value, in this case rank. The binning window metric for a KS function is called bandwidth.

See [Matlab documentation](https://uk.mathworks.com/help/stats/kernel-distribution.html) for a fairly non-opaque explanation.

However, if you want to compare histograms, I strongly suggest violin plots, not because they are strongly in vogue, but because you can compare histograms without worrying about overlaying them.

> **Comments:**
> **username_0**: Thank you for these resources.  

---

