Performing Linear Discriminant Analysis (LDA) with Morphological Data
Introduction
Morphological data, such as geometric landmarks or shapes, can be used to perform various analyses in fields like biology, medicine, and engineering. However, when dealing with morphological data, we often encounter challenges related to the non-linear relationships between variables. In this article, we’ll explore how to perform Linear Discriminant Analysis (LDA) on morphological data using a combination of existing packages and custom modifications.
Overview of Morphological Data
Morphological data typically consists of multiple measurements or landmarks, which can be used as input features for various analyses. These data can come in the form of matrices, where each row represents an individual, and each column represents a landmark. In this case, we have a 3D array 3D_Array_With_All_Individuals containing 259 matrices with x and y coordinates of 20 landmarks.
Example Data
The provided data is a matrix with x and y coordinates of 20 landmarks for each individual:
[,1] [,2]
[1,] -0.2820496783 -0.003257662
[2,] -0.2501083238 0.020960228
[3,] -0.2250506399 0.049100368
[4,] -0.2015860633 0.024673340
[5,] -0.2246826220 0.001103431
[6,] -0.0750488845 0.133714363
[7,] 0.0006292017 0.133626261
[8,] 0.0679419574 0.117149206
[9,] 0.1239612670 0.097834963
[10,] 0.2008532330 0.063564419
[11,] 0.2763894852 0.026529132
[12,] 0.3606700636 0.038228394
[13,] 0.3665535317 -0.065490660
[14,] 0.2728682794 -0.086504965
[15,] 0.1113276708 -0.147876722
[16,] -0.0742222455 -0.158015857
[17,] -0.0746248649 -0.117267412
[18,] -0.0775485339 -0.055400903
[19,] -0.1122735300 0.041616289
[20,] -0.1839993037 -0.114286212
Semi-Landmarks and the gpagen Function
We have a matrix semi$sliders containing information about semi-landmarks:
[,1] [,2] [,3]
[1,] 6 7 8
[2,] 7 8 9
[3,] 8 9 10
[4,] 9 10 11
The gpagen function is used to visualize the data:
> gpagen(3D_Array_With_All_Individuals, curves = semi$sliders, ShowPlot = T)
Linear Discriminant Analysis (LDA) with Morphological Data
To perform LDA on morphological data, we need a way to extract meaningful features from the data. One approach is to use existing packages like geomorph, which provides tools for Principal Component Analysis (PCA). However, PCA might not be suitable for LDA.
We can modify the plotTangentSpace function in geomorph to extract meaningful features from the morphological data. The idea is to use the semi-landmarks as input features and train a linear model on them.
Modifying the plotTangentSpace Function
To modify the plotTangentSpace function, we need to create a new function that extracts features from the morphological data using the semi-landmarks:
# Create a new function to extract features from the morphological data
extract_features <- function(data, curves) {
# Extract x and y coordinates of landmarks
x_coords <- apply(data, 2, function(row) row[, 1])
y_coords <- apply(data, 2, function(row) row[, 2])
# Create a matrix with semi-landmark indices as rows
semi_landmarks <- matrix(curves, nrow = length(curves))
# Compute the mean of x and y coordinates for each semi-landmark
means_x <- apply(x_coords, 1, function(row) row[semi_landmarks == 1])
means_y <- apply(y_coords, 1, function(row) row[semi_landmarks == 1])
# Create a matrix with mean x and y coordinates as rows
features <- cbind(means_x, means_y)
return(features)
}
Using the extract_features Function
We can use the extract_features function to extract features from our morphological data:
# Extract features from the morphological data
features <- extract_features(3D_Array_With_All_Individuals, semi$sliders)
# Print the extracted features
print(features)
Linear Discriminant Analysis with LDA Package
We can use the lda package in R to perform linear discriminant analysis on our morphological data. First, we need to extract the features from the morphological data using the extract_features function:
# Extract features from the morphological data
features <- extract_features(3D_Array_With_All_Individuals, semi$sliders)
# Compute the mean of each feature across all individuals
means_x <- apply(features, 2, function(x) mean(x))
means_y <- apply(features, 2, function(y) mean(y))
# Create a matrix with means as rows
X <- cbind(means_x, means_y)
Then, we can use the lda package to perform linear discriminant analysis:
# Perform linear discriminant analysis using LDA package
model <- lda(~1, data = X)
# Print the results of the LDA model
print(model)
Conclusion
In this article, we explored how to perform Linear Discriminant Analysis (LDA) on morphological data. We used a combination of existing packages and custom modifications to extract meaningful features from the data using semi-landmarks. The lda package in R was used to perform linear discriminant analysis on the extracted features.
Note that this is just an example, and you may need to modify the code to suit your specific needs. Additionally, there are many other approaches to performing LDA on morphological data, and the choice of method will depend on the specifics of your problem.
Last modified on 2024-08-05