library(tidyverse)
library(dplyr)
library(ggplot2)
library(tidyr)
library(readr)
monolith_unmanipulated <-
readr::read_csv("Elder_Monolith_21June2018_Part2.csv",
skip = 2,
col_names = c("objects_unmanipulated", "events", "timecode", "time_sec", "X", "Y", "Z",
"PLD_error", "re_projection_error", "nearest_camera_distance"))
monolith_unmanipulated
final_data_monolith_unmanipulated <- monolith_unmanipulated %>%
arrange(objects_unmanipulated, time_sec) %>%
filter(!grepl("^Length.*", events)) %>%
mutate(distance_travelled_X_cm = X - lag(X, default = first(X))) %>%
mutate(distance_travelled_Y_cm = Y - lag(Y, default = first(Y))) %>%
mutate(distance_travelled_Z_cm = Z - lag(Z, default = first(Z))) %>%
mutate(fish_distance_travelled_cm = sqrt((distance_travelled_X_cm)^2
+ (distance_travelled_Y_cm)^2
+ (distance_travelled_Z_cm)^2)) %>%
group_by(objects_unmanipulated) %>%
mutate(distance_cm_per_sec = fish_distance_travelled_cm /(time_sec - lag(time_sec, default = first(time_sec)))) %>%
filter(!distance_cm_per_sec == Inf) %>%
select(objects_unmanipulated, events, time_sec, X, Y, Z, distance_travelled_X_cm, distance_travelled_Y_cm, distance_travelled_Z_cm, distance_cm_per_sec)
final_data_monolith_unmanipulated
ggplot(final_data_monolith_unmanipulated, aes(time_sec, distance_cm_per_sec, group = events, color = objects_unmanipulated)) + geom_point() + facet_wrap(~ objects_unmanipulated)
ggplot(final_data_monolith_unmanipulated, aes(time_sec, distance_cm_per_sec)) + geom_boxplot(aes(time_sec, color = objects_unmanipulated)) +
labs(x = "Time in Video (sec)", y = "Distance Travelled per Unit Time (cm/sec)") +
ggtitle("Unmanipulated: Distance Travelled per Time (cm/sec) Boxplot")
monolith_unmanipulated_summary <- final_data_monolith_unmanipulated %>%
group_by(objects_unmanipulated) %>%
summarize(unmanipulated_mean_fish_distance_travelled_cm_per_sec = mean(distance_cm_per_sec), unmanipulated_median_fish_distance_travelled_cm_per_sec = median(distance_cm_per_sec))
monolith_unmanipulated_summary
mean(monolith_unmanipulated_summary$unmanipulated_mean_fish_distance_travelled_cm_per_sec)
## [1] 8.439308
mean(monolith_unmanipulated_summary$unmanipulated_median_fish_distance_travelled_cm_per_sec)
## [1] 6.579131
ggplot(monolith_unmanipulated_summary, aes(objects_unmanipulated, color = objects_unmanipulated)) +
geom_point(aes(y = unmanipulated_mean_fish_distance_travelled_cm_per_sec), shape = 13) +
theme(axis.text.x = element_blank()) + xlab("Objects") + ylab("Mean Fish Distance Travelled (cm/sec)") + ggtitle("Unmanipulated: Average Fish Movement (cm/sec)")
# Tallying Foraging Types Dataset: Unmanipulated
unmanipulated_behavior_tally <- final_data_monolith_unmanipulated %>%
select(events) %>%
ungroup() %>%
mutate(benthic_forage = grepl("^Benthic_Forage.*", events)) %>%
mutate(drift_forage = grepl("^Drift_Forage.*", events)) %>%
mutate(search_forage = grepl("Search_Forage.*", events)) %>%
mutate(benthic_forage_int = if_else(benthic_forage == TRUE, 1, 0)) %>%
mutate(drift_forage_int = if_else(drift_forage == TRUE, 1, 0)) %>%
mutate(search_forage_int = if_else(search_forage == TRUE, 1, 0)) %>%
mutate(benthic_fraction = cumsum(benthic_forage_int)) %>%
mutate(drift_fraction = cumsum(drift_forage_int)) %>%
mutate(search_fraction = cumsum(search_forage_int))
## Adding missing grouping variables: `objects_unmanipulated`
unmanipulated_behavior_tally
slices_manipulated <- c(3, 73, 33, 3)
slices_names <- c("Benthic Forage (3%)", "Drift Forage (65%)", "Search Forage (29%)", "Other (3%)")
pie(slices_manipulated, labels = slices_names, col = rainbow(length(slices_names)))
## Nearest Neighbor Distance: Unmanipulated
nnd_dataset_unmanipulated <- final_data_monolith_unmanipulated %>%
ungroup() %>%
select(objects_unmanipulated, time_sec, X, Y, Z) %>%
arrange(time_sec) %>%
group_by(time_sec) %>%
mutate(distance_travelled_X_cm = X - lag(X, default = first(X))) %>%
mutate(distance_travelled_Y_cm = Y - lag(Y, default = first(Y))) %>%
mutate(distance_travelled_Z_cm = Z - lag(Z, default = first(Z))) %>%
mutate(nnd = sqrt((distance_travelled_X_cm)^2
+ (distance_travelled_Y_cm)^2
+ (distance_travelled_Z_cm)^2)) %>%
group_by(time_sec) %>%
filter(!nnd == 0) %>%
filter(nnd == min(nnd))
nnd_dataset_unmanipulated
mean(nnd_dataset_unmanipulated$nnd)
## [1] 77.86774
monolith_manipulated <-
readr::read_csv("Elder_Monolith_22June2018_Part2.csv",
skip = 2,
col_names = c("objects_manipulated", "events", "timecode", "time_sec", "X", "Y", "Z", "PLD_error", "re_projection_error", "nearest_camera_distance"))
monolith_manipulated
final_data_monolith_manipulated <- monolith_manipulated %>%
arrange(objects_manipulated, time_sec) %>%
filter(!grepl("^Length.*", events)) %>%
mutate(distance_travelled_X_cm = X - lag(X, default = first(X))) %>%
mutate(distance_travelled_Y_cm = Y - lag(Y, default = first(Y))) %>%
mutate(distance_travelled_Z_cm = Z - lag(Z, default = first(Z))) %>%
mutate(fish_distance_travelled_cm = sqrt((distance_travelled_X_cm)^2
+ (distance_travelled_Y_cm)^2
+ (distance_travelled_Z_cm)^2)) %>%
group_by(objects_manipulated) %>%
mutate(distance_cm_per_sec = fish_distance_travelled_cm /(time_sec - lag(time_sec, default = first(time_sec)))) %>%
filter(!distance_cm_per_sec == Inf) %>%
select(objects_manipulated, events, time_sec, X, Y, Z, distance_cm_per_sec)
final_data_monolith_manipulated
ggplot(final_data_monolith_manipulated, aes(time_sec, distance_cm_per_sec, group = events, color = objects_manipulated)) + geom_point() + facet_wrap(~ objects_manipulated)
## Plotting boxplot manipulated data
ggplot(final_data_monolith_manipulated, aes(time_sec, distance_cm_per_sec)) + geom_boxplot(aes(time_sec, color = objects_manipulated)) +
labs(x = "Time in Video (sec)", y = "Distance Travelled per Unit Time (cm/sec)") +
ggtitle("Manipulated: Distance Travelled per Time (cm/sec) Boxplot")
monolith_manipulated_summary <- final_data_monolith_manipulated %>%
group_by(objects_manipulated) %>%
summarize(manipulated_mean_fish_distance_travelled_cm_per_sec = mean(distance_cm_per_sec), manipulated_median_fish_distance_travelled_cm_per_sec = median(distance_cm_per_sec))
monolith_manipulated_summary
mean(monolith_manipulated_summary$manipulated_mean_fish_distance_travelled_cm_per_sec)
## [1] 6.581297
mean(monolith_manipulated_summary$manipulated_median_fish_distance_travelled_cm_per_sec)
## [1] 5.22252
ggplot(monolith_manipulated_summary, aes(objects_manipulated, color = objects_manipulated)) +
geom_point(aes(y = manipulated_mean_fish_distance_travelled_cm_per_sec), shape = 13) +
theme(axis.text.x = element_blank()) + xlab("Objects") + ylab("Mean Fish Distance Travelled (cm/sec)") + ggtitle("Manipulated: Average Fish Movement (cm/sec)")
## Tallying Foraging Types Dataset: Manipulated
manipulated_behavior_tally <- final_data_monolith_manipulated %>%
select(events) %>%
ungroup() %>%
mutate(benthic_forage = grepl("^Benthic_Forage.*", events)) %>%
mutate(drift_forage = grepl("^Drift_Forage.*", events)) %>%
mutate(search_forage = grepl("Search_Forage.*", events)) %>%
mutate(benthic_forage_int = if_else(benthic_forage == TRUE, 1, 0)) %>%
mutate(drift_forage_int = if_else(drift_forage == TRUE, 1, 0)) %>%
mutate(search_forage_int = if_else(search_forage == TRUE, 1, 0)) %>%
mutate(benthic_fraction = cumsum(benthic_forage_int)) %>%
mutate(drift_fraction = cumsum(drift_forage_int)) %>%
mutate(search_fraction = cumsum(search_forage_int))
## Adding missing grouping variables: `objects_manipulated`
manipulated_behavior_tally
slices_manipulated <- c(8, 115, 36, 3)
slices_names <- c("Benthic Forage (5%)", "Drift Forage (70%)", "Search Forage (22%)", "Other (3%)")
pie(slices_manipulated, labels = slices_names, col = rainbow(length(slices_names)))
nnd_dataset_manipulated <- final_data_monolith_manipulated %>%
ungroup() %>%
select(objects_manipulated, time_sec, X, Y, Z) %>%
arrange(time_sec) %>%
group_by(time_sec) %>%
mutate(distance_travelled_X_cm = X - lag(X, default = first(X))) %>%
mutate(distance_travelled_Y_cm = Y - lag(Y, default = first(Y))) %>%
mutate(distance_travelled_Z_cm = Z - lag(Z, default = first(Z))) %>%
mutate(nnd = sqrt((distance_travelled_X_cm)^2
+ (distance_travelled_Y_cm)^2
+ (distance_travelled_Z_cm)^2)) %>%
group_by(time_sec) %>%
filter(!nnd == 0) %>%
filter(nnd == min(nnd))
nnd_dataset_manipulated
mean(nnd_dataset_manipulated$nnd)
## [1] 75.62127