In correlation — or association analysis more generally — we can evaluate the relationships between numeric variables. These variables can be taxonomic features or patient metadata. For instance, we might be interested in which taxonomic features are present simultaneously with others, or whether body weight is associated with the abundance of certain features. In this chapter, we will demonstrate how to perform correlation analysis with mia’s getCrossAssociation() method.
17.1 Features vs. features
Here we demonstrate how to analyze which bacteria co-exist in the dataset (Potbhare et al. 2022).
Skin microbiota diversity among genetically unrelated individuals of Indian origin. Potbhare et al. (2022). PeerJ 10:e13075. doi:10.7717/peerj.13075.
A new measure of rank correlation. Kendall (1938). Biometrika 30:81–93. doi:10.1093/biomet/30.1-2.81.
Unifying the analysis of high-throughput sequencing datasets: Characterizing RNA-seq, 16S rRNA gene sequencing and selective growth experiments by compositional data analysis. Fernandes et al. (2014). Microbiome 2:15. doi:10.1186/2049-2618-2-15.
Controlling the false discovery rate: A practical and powerful approach to multiple testing. Benjamini et al. (1995). J R Stat Soc Series B 57:289–300. doi:10.1111/J.2517-6161.1995.TB02031.X.
qgraph: Network visualizations of relationships in psychometric data. Epskamp et al. (2012). J Stat Softw 48:1–18. doi:10.18637/jss.v048.i04.
We can visualize the result with a heatmap as we do later in this chapter, or we can visualize the results with a correlation network plot (Epskamp et al. 2012) as done below (Figure 17.1).
Figure 17.1: Correlation network of the classes in the peerj13075 data, based on correlations between CLR-transformed abundances. Nodes are classes, and edge color (blue: positive, red: negative) and width show the sign and strength of the correlation.
Complex heatmaps reveal patterns and correlations in multidimensional genomic data. Gu et al. (2016). Bioinformatics 32:2847–2849. doi:10.1093/bioinformatics/btw313.
library(ComplexHeatmap)library(shadowtext)# Function for marking significant correlations with "X"add_signif<-function(j, i, x, y, width, height, fill){# If the p-value is under thresholdif(!is.na(res$p_adj[i, j])&res$p_adj[i, j]<0.05){# Print "X"grid.shadowtext(sprintf("%s", "X"), x, y, gp =gpar(fontsize =8, col ="#f5f5f5"))}}# Create a heatmapp<-Heatmap(res$cor,# Mark significant correlations with X cell_fun =add_signif, heatmap_legend_param =list( title ="correlation", legend_height =unit(5, "cm")), column_names_rot =45)p
Figure 17.2: Heatmap of correlations between CLR-transformed abundances of classes (rows) and alpha diversity indices (columns) in the peerj13075 data. Significant correlations (adjusted p-value < 0.05) are marked with X.
17.3 Metadata vs. metadata
Finally, we demonstrate how to calculate correlation between sample metadata variables. Here we estimate correlation between alpha diversity measures.
Compared to the solution in Section 12.2.2.1, getCrossAssociation() allows us to calculate correlations in bulk easily, without the need for looping. The correlations are shown in Figure 17.3.
# Get correlation resultsres<-getCrossAssociation(tse, tse, col.var1 =index, col.var2 =index, test.signif =TRUE, mode ="matrix")# Create a heatmap and store itp<-Heatmap(res$cor,# Mark significant correlations with X cell_fun =add_signif, heatmap_legend_param =list( title ="correlation", legend_height =unit(5, "cm")), column_names_rot =45)p
Figure 17.3: Heatmap of correlations between alpha diversity indices in the peerj13075 data. Significant correlations (adjusted p-value < 0.05) are marked with X.
TipCross-association
See Chapter 20 for further information on correlation and association analyses.
Solve the exercises in order, as each exercise continues from the previous one. Try to solve each exercise yourself before opening the solution.
TipExercise 1: Correlation between taxonomic features
Goal: Calculate correlations between taxonomic features and find the results.
Load the Tengeler2020 dataset from the mia package and store it in a variable called tse.
Agglomerate the data to the Family rank. Then add the prevalence, and combine the families with a prevalence of 0.5 or less into “Other”. How many features are left?
Apply the clr transformation to the counts assay with a pseudocount of 1.
Calculate the correlations between all pairs of families from the clr assay with getCrossAssociation(). Give tse as both the first and the second object, test the significance of the correlations (test.signif = TRUE) and return the results as matrices (mode = "matrix"). Store the result in res.
Check the names of the elements in res. Show the correlations rounded to two decimals.
Count how many pairs of families have an adjusted p-value below 0.05. The matrix is symmetric and its diagonal compares each family with itself, so count only the values above the diagonal.
What do the values on the diagonal of the correlation matrix show? What does a correlation close to 1, close to -1 and close to 0 mean? Why do we use adjusted p-values instead of raw p-values?
upper.tri() selects the values above the diagonal, so each pair of families is counted once and the comparisons of a family with itself are left out.
The diagonal shows the correlation of each family with itself, which is always 1. These values should be ignored, and so should the duplicates, as the matrix is symmetric. A correlation close to 1 means that the two families tend to be abundant in the same samples. A correlation close to -1 means that when one family is abundant, the other tends to be rare. A correlation close to 0 means that there is no clear association. When many correlations are tested at once, some of them are significant just by chance. The adjusted p-values (p_adj) correct for this multiple testing.
TipExercise 2: Correlation network
Goal: Visualize the correlations between taxonomic features as a network.
Visualize the correlation matrix res$cor from Exercise 1 as a network with qgraph(). Use the spring layout (layout = "spring"), label the nodes with the column names of the matrix and use theme = "colorblind".
How do you read the network? What do the nodes, the color of the edges and the width of the edges show?
Each node is a family, and each edge shows the correlation between two families. The color of an edge shows the sign of the correlation (positive or negative), and the width shows its strength. In the spring layout, strongly connected families are placed close to each other. A group of families connected with strong positive edges tends to occur together in the samples.
TipExercise 3: Correlation between taxonomic features and alpha diversity
Goal: Calculate correlations between taxonomic features and sample metadata.
Calculate the alpha diversity indices "shannon", "observed", "gini" and "inverse_simpson" from the counts assay with addAlpha(). Store the names of the indices in a vector called index.
Calculate the correlations between the families in the clr assay and the alpha diversity indices with getCrossAssociation(). Give the indices with the col.var2 argument, test the significance and return the results as matrices. To hide the warnings about ties, set show.warnings = FALSE.
Visualize the correlations with a heatmap.
Show the adjusted p-values rounded to three decimals. Which pairs of a family and an index have an adjusted p-value below 0.05?
How do you read the heatmap? What would a positive correlation between a family and Shannon diversity mean?
Each row is a family and each column an alpha diversity index, and the color of a cell shows the correlation. The rows and columns are ordered with hierarchical clustering, so that families with similar correlation patterns are next to each other. A positive correlation with Shannon diversity means that samples with a higher abundance of the family tend to have higher diversity. A correlation does not tell whether the family causes the difference in diversity. Note that Gini is an inequality index: high values mean low evenness, so its correlations tend to have the opposite sign compared to the diversity indices.
TipExercise 4: Correlation between sample metadata variables
Goal: Calculate correlations between sample metadata variables.
Calculate the correlations between the alpha diversity indices in index with getCrossAssociation(). Give the indices with both col.var1 and col.var2, test the significance, return the results as matrices and set show.warnings = FALSE.
Visualize the correlations with a heatmap.
Make a scatter plot with the Shannon diversity on the x-axis and the inverse Simpson diversity on the y-axis. Add a linear regression line to the plot.
How do you read the scatter plot? How does it relate to the correlation between the two indices in the heatmap?
library(scater)# 1. Correlations between alpha diversity indicesres<-getCrossAssociation(tse, tse, col.var1 =index, col.var2 =index, test.signif =TRUE, mode ="matrix", show.warnings =FALSE)# 2. Heatmap of the correlationsHeatmap(res$cor, name ="correlation")
# 3. Scatter plot with a linear regression lineplotColData(tse, x ="shannon", y ="inverse_simpson", point_alpha =1)+geom_smooth(method ="lm")+labs(x ="Shannon diversity", y ="Inverse Simpson diversity")
In the scatter plot, each point is a sample. The scatter plot shows the same relationship that is summarized by a single correlation value in the heatmap. By default, getCrossAssociation() calculates Kendall’s rank correlation, which measures whether one index tends to increase (or decrease) when the other increases, even if the relationship is not linear. If the points form a clear increasing or decreasing trend, the two indices are strongly correlated and carry largely the same information. If the points are widely scattered, the correlation is weak and the indices describe different aspects of the communities. The scatter plot also shows whether the relationship is driven by a few outlying samples, which a single correlation value cannot show.