#Setup
rm(list=ls())
library(tidyverse)
library(SimilarityMeasures)
Exercise_4B
#Import dataset
<- read_csv("pedestrian.csv")
pedestrian
#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
<- pedestrian |>
track_data_matrices 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
<- split(pedestrian[, c("E", "N")], pedestrian$TrajID)
matrices
#check number of matrices
<- length(matrices)
num_matrices
#Extra: compare all trajectories with all trajectories
<- matrix(NA, nrow = num_matrices, ncol = num_matrices) #create matrix for storage
dtw_results 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(as.matrix(matrices[[i]]), as.matrix(matrices[[j]]))
dtw_distance <- dtw_distance #storage of distance
dtw_results[i, j] <- dtw_distance #symmetrical distance
dtw_results[j, i] else {
} <- 0 #if matrices identical, distance = 0
dtw_results[i, j]
}
}
}
#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
<- 1
reference_index <- as.matrix(matrices[[reference_index]])
reference_matrix
#calculate distances between reference and other trajecories
<- numeric(length(matrices) - 1) #storage space
dtw_results
for (i in 2:length(matrices)) { #start at 2nd position (no reference-reference pair)
<- DTW(reference_matrix, as.matrix(matrices[[i]]))
dtw_distance - 1] <- dtw_distance #store distance
dtw_results[i
}
#save results as a data frame --> Preliminary for visualization
<- data.frame(TrajID = paste(2:length(matrices)), Distance = dtw_results)
dtw_df
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
<- numeric(num_matrices - 1)
edit_results <- numeric(num_matrices - 1)
frechet_results <- numeric(num_matrices - 1)
lcss_results
#Chose reference trajectory
<- 1
reference_index <- as.matrix(matrices[[reference_index]])
reference_matrix
#calculate distances between reference and all other trajectories
for (i in 2:num_matrices) { #start at 2nd position
<- as.matrix(matrices[[i]])
current_matrix
#calcluate Edit Distance
- 1] <- EditDist(reference_matrix, current_matrix)
edit_results[i
#calculate Fréchet-Distance
- 1] <- Frechet(reference_matrix, current_matrix)
frechet_results[i
}
# Adjust parameters for LCSS
<- 1 # Distance threshold for LCSS
lcssThreshold <- 0.5 # Allowable error margin for LCSS
errorMarg
# # 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
<- data.frame(TrajID = paste(2:num_matrices), Distance = edit_results)
edit_df <- data.frame(TrajID = paste(2:num_matrices), Distance = frechet_results)
frechet_df <- data.frame(TrajID = paste(2:num_matrices), Distance = lcss_results)
lcss_df
#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:
LCSS Analyse nach 6h abgebrochen, was ist falsch gelaufen?
Warum stimmen Distanzen nicht mit Vorgabe überein?