Exercise_4B

Author

Robin Merz

#Setup
rm(list=ls())
library(tidyverse)
library(SimilarityMeasures)
#Import dataset
pedestrian <- read_csv("pedestrian.csv")

#visualize trajectories
pedestrian |> 
  ggplot(aes(x=E, y =N)) +
  geom_point(colour = pedestrian$TrajID) +
  geom_line(colour = pedestrian$TrajID) +
  facet_wrap(~TrajID)+
  theme_minimal()

# Erstelle eine Liste von Matrizen für jeden Track
# Erstelle eine Liste von Datensätzen und Matrizen für jeden Track
track_data_matrices <- pedestrian |> 
  group_by(TrajID) |> 
  summarise(
    data_set = list(cur_data()),  # Erstelle ein DataFrame für jeden Track
    matrix_data = list(as.matrix(select(cur_data(), E, N)))  # Erstelle eine Matrix für E und B
  ) |> 
  ungroup()
#create matrices for each trajectory
matrices <- split(pedestrian[, c("E", "N")], pedestrian$TrajID)

#check number of matrices
num_matrices <- length(matrices)

#Extra: compare all trajectories with all trajectories
dtw_results <- matrix(NA, nrow = num_matrices, ncol = num_matrices) #create matrix for storage
rownames(dtw_results) <- paste("TrajID", 1:num_matrices) #rownames
colnames(dtw_results) <- paste("TrajID", 1:num_matrices) #colnames

#calculate distances for all trajectory-pairs
for (i in 1:num_matrices) {
  for (j in i:num_matrices) { 
    if (i != j) {  #only calculate difference, if matrices differ
      dtw_distance <- DTW(as.matrix(matrices[[i]]), as.matrix(matrices[[j]]))
      dtw_results[i, j] <- dtw_distance  #storage of distance
      dtw_results[j, i] <- dtw_distance  #symmetrical distance
    } else {
      dtw_results[i, j] <- 0  #if matrices identical, distance = 0
    }
  }
}

#print resutls
print(dtw_results)
          TrajID 1  TrajID 2 TrajID 3  TrajID 4  TrajID 5  TrajID 6
TrajID 1     0.000  3650.025 50785.51  5906.787  2178.411  1152.718
TrajID 2  3650.025     0.000 57604.43 14779.970  4372.635  3577.662
TrajID 3 50785.511 57604.432     0.00 45128.931 51250.309 50482.827
TrajID 4  5906.787 14779.970 45128.93     0.000  5401.993  5455.425
TrajID 5  2178.411  4372.635 51250.31  5401.993     0.000  1924.707
TrajID 6  1152.718  3577.662 50482.83  5455.425  1924.707     0.000
#Preliminaries: choose reference matrix 
reference_index <- 1
reference_matrix <- as.matrix(matrices[[reference_index]])

#calculate distances between reference and other trajecories
dtw_results <- numeric(length(matrices) - 1)  #storage space

for (i in 2:length(matrices)) {  #start at 2nd position (no reference-reference pair)
  dtw_distance <- DTW(reference_matrix, as.matrix(matrices[[i]]))
  dtw_results[i - 1] <- dtw_distance  #store distance
}

#save results as a data frame --> Preliminary for visualization
dtw_df <- data.frame(TrajID = paste(2:length(matrices)), Distance = dtw_results)

ggplot(dtw_df, aes(x = factor(TrajID), y = Distance, fill = TrajID)) +  # fill = colors
  geom_bar(stat = "identity") +   
  scale_fill_brewer(palette = "Set2") +  # choose color palette
  labs(x = "Trajectory", y = "Distance", title = "DTW") +  
  theme_minimal()

#storage spaces other calculations
#same as above
edit_results <- numeric(num_matrices - 1)
frechet_results <- numeric(num_matrices - 1)
lcss_results <- numeric(num_matrices - 1)

#Chose reference trajectory
reference_index <- 1
reference_matrix <- as.matrix(matrices[[reference_index]])

#calculate distances between reference and all other trajectories
for (i in 2:num_matrices) {  #start at 2nd position
  current_matrix <- as.matrix(matrices[[i]])
  
  #calcluate Edit Distance
  edit_results[i - 1] <- EditDist(reference_matrix, current_matrix)
  
  #calculate Fréchet-Distance
  frechet_results[i - 1] <- Frechet(reference_matrix, current_matrix)

}

# Adjust parameters for LCSS
lcssThreshold <- 1  # Distance threshold for LCSS
errorMarg <- 0.5    # Allowable error margin for LCSS

# # Calculate LCSS
# lcss_results <- matrix(NA, nrow = num_matrices, ncol = num_matrices)
# for (i in 1:num_matrices) {
#   for (j in i:num_matrices) {
#     if (i != j) {
#       # Calculate LCSS with the specified threshold and error margin
#       lcss_distance <- LCSS(as.matrix(matrices[[i]]), as.matrix(matrices[[j]]),
#                             pointDistance = lcssThreshold, errorMarg = errorMarg)
#       lcss_results[i, j] <- lcss_distance
#       lcss_results[j, i] <- lcss_distance
#     } else {
#       lcss_results[i, j] <- 0  # Distance to itself is 0
#     }
#   }
# }


#save results in data frame
edit_df <- data.frame(TrajID = paste(2:num_matrices), Distance = edit_results)
frechet_df <- data.frame(TrajID = paste(2:num_matrices), Distance = frechet_results)
lcss_df <- data.frame(TrajID = paste(2:num_matrices), Distance = lcss_results)

#EditDist-plot
ggplot(edit_df, aes(x = factor(TrajID), y = Distance, fill = TrajID)) +
  geom_bar(stat = "identity") +   
  scale_fill_brewer(palette = "Set2") +  
  labs(x = "TrajID", y = "Edit-Distanz", title = "Edit-Distance") +  
  theme_minimal()

# Plot für Fréchet-Distanz
ggplot(frechet_df, aes(x = factor(TrajID), y = Distance, fill = TrajID)) +
  geom_bar(stat = "identity") +   
  scale_fill_brewer(palette = "Set2") +  
  labs(x = "TrajID", y = "Fréchet-Distanz", title = "Fréchet-Distance") +  
  theme_minimal()

# # Plot für LCSS-Distanz
# ggplot(lcss_df, aes(x = factor(TrajID), y = Distance, fill = factor(TrajID))) +
#   geom_bar(stat = "identity") +   
#   scale_fill_brewer(palette = "Set2") +  
#   labs(x = "TrajID", y = "LCSS-Distanz", title = "LCSS-Distance") +  
#   theme_minimal()

Informationen: