Euclidean distance and CLR transformation #63
-
Hello, thank you for developing this amazing tool and making it available! My Professor and I are interested in using Euclidean distances to calculate the beta diversity, and using central log ratio to transform the data. Is there a way we can do this for the generate_beta_trend_test_long function? I’ve attached the code where we are using the generate_beta_trend_test_long function, with Bray-Curtis distance instead of Euclidean distance. Thank you so much! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello Miles, Thank you for your interest in MicrobiomeStat and for bringing up this important question about using Euclidean distance with CLR transformation in the While Euclidean distance isn't directly available as a default option in
coral_data.obj <- mStat_convert_phyloseq_to_data_obj(phy.all)
library(vegan) # Used for calculating distance
clr_transform_and_calculate_distance <- function(data_obj) {
# Extract feature table
feature_tab <- data_obj$feature.tab
# Manually implement CLR conversion
clr_transform <- function(x) {
# Add a small value to avoid log(0)
x <- x + 1e-6
# Calculate the geometric mean of each sample
g_mean <- exp(rowMeans(log(x)))
# CLR conversion
log(x / g_mean)
}
# Apply CLR transformation
clr_data <- t(clr_transform(t(feature_tab)))
# Calculate Euclidean distance
dist_matrix <- vegdist(t(clr_data), method = "euclidean")
# Create dist_obj
dist_obj <- list(
CLR_Euclidean = as.matrix(dist_matrix)
)
return(dist_obj)
}
# Use the function
dist_obj <- clr_transform_and_calculate_distance(coral_data.obj)
all_times_analysis <- generate_beta_trend_test_long(
data.obj = coral_data.obj,
dist.obj = dist_obj,
subject.var = "Colony.ID",
time.var = "Collection.time.num",
group.var = "Coral.species",
adj.vars = c("Depth"),
dist.name = "CLR_Euclidean"
)
# View the results
all_times_analysis$CLR_Euclidean This approach allows you to use Euclidean distance on CLR-transformed data within the MicrobiomeStat framework. The I hope this helps you and others in the community who might be interested in using Euclidean distance with CLR transformation. If you have any further questions or run into any issues implementing this solution, please don't hesitate to ask. Thank you for contributing to the MicrobiomeStat community! Best regards, |
Beta Was this translation helpful? Give feedback.
Hello Miles,
Thank you for your interest in MicrobiomeStat and for bringing up this important question about using Euclidean distance with CLR transformation in the
generate_beta_trend_test_long
function. I appreciate you sharing this with the community, as it will likely benefit other users as well.While Euclidean distance isn't directly available as a default option in
generate_beta_trend_test_long
, we can implement it using a custom approach. Here's a solution that allows you to use Euclidean distance on CLR-transformed data within the MicrobiomeStat framework: